非容器应用的网格化需要前一篇讲述的WorkloadEntry配置,同时需要本篇讲述的sidecar配置。前者让kubernetes容器内的POD可以找到VM中的应用,后者让VM中的应用可以找到POD。(注:本系列的重点是流量转移,但VM网格化后不止具备流量管理能力。)
1 搭建实验环境
示例实验需要一个ack集群和3个ecs节点。在ack集群中,包含hello1和hello3版本为en
的POD,3个ecs节点各启动一个hello2 app,每个app对应一个版本(en
/fr
/es
)的hello容器。请求从hello1 POD到VM中的hello2 app,再到hello3 POD。在此基础上,我们希望路由到hello2 en
/fr
/es
的流量比例为:30%:60%:10%。
示例(http_reciprocal_demo)包含如下元素:
- hello1 deployment(镜像http_springboot_v1)
- hello3 deployment(镜像http_springboot_v1)
- hello2 docker containers(镜像http_springboot_v1/http_springboot_v2/http_springboot_v3)
- 入口网关:istio-ingressgateway
- 入口流量配置:gateway/virtualservice
- hello1流量配置:hello1 service(/hello1 virtualservice/hello1 destinationrule)
- hello2流量配置:hello2 service/hello2 virtualservice/hello2 destinationrule
- hello3流量配置:hello3 service(/hello3 virtualservice/hello3 destinationrule)
- hello2 serviceentry/hello2 workloadentry
启动hello2应用
sh vm/ssh1.sh
docker run \
--rm \
--network host \
--name http_v1 \
-e HTTP_HELLO_BACKEND=hello3-svc.http-reciprocal-hello.svc.cluster.local \
registry.cn-beijing.aliyuncs.com/asm_repo/http_springboot_v1:1.0.1
sh vm/ssh2.sh
docker run \
--rm \
--network host \
--name http_v2 \
-e HTTP_HELLO_BACKEND=hello3-svc.http-reciprocal-hello.svc.cluster.local \
registry.cn-beijing.aliyuncs.com/asm_repo/http_springboot_v2:1.0.1
sh vm/ssh3.sh
docker run \
--rm \
--network host \
--name http_v3 \
-e HTTP_HELLO_BACKEND=hello3-svc.http-reciprocal-hello.svc.cluster.local \
registry.cn-beijing.aliyuncs.com/asm_repo/http_springboot_v3:1.0.1
启动hello1/hello3 POD
k apply -f data_plane/http-reciprocal-hello-all.yaml
k -n http-reciprocal-hello get pod,svc
NAME READY STATUS RESTARTS AGE
pod/hello1-deploy-6c857bc499-f658r 2/2 Running 0 2m7s
pod/hello3-deploy-f57bb9db7-lzvg8 2/2 Running 0 2m7s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello1-svc ClusterIP 172.19.8.178 <none> 8004/TCP 2m7s
service/hello3-svc ClusterIP 172.19.3.17 <none> 8001/TCP 6s
配置WorkloadEntry等设施
asm.deploy.sh/asm_traffic_shift.sh
aliyun servicemesh AddVmAppToMesh \
--ServiceMeshId "$MESH_ID" \
--Namespace external-hello \
--ServiceName hello2-svc \
--Ips "$VM_PRI_1","$VM_PRI_2","$VM_PRI_3" \
--Ports http:8001 \
--Labels app=hello-workload
m apply -f control_plane/http-reciprocal-hello-all.yaml
m -n http-reciprocal-hello get serviceentry,workloadentry
NAME AGE
serviceentry.networking.istio.io/mesh-expansion-hello2-svc 73s
NAME AGE
workloadentry.networking.istio.io/mesh-expansion-hello2-svc-1 73s
workloadentry.networking.istio.io/mesh-expansion-hello2-svc-2 73s
workloadentry.networking.istio.io/mesh-expansion-hello2-svc-3 73s
m -n http-reciprocal-hello get gateway,virtualservice,destinationrule
NAME AGE
gateway.networking.istio.io/hello-gateway 2m25s
NAME AGE
virtualservice.networking.istio.io/gateway-vs 2m25s
virtualservice.networking.istio.io/hello2-vs 2m25s
NAME AGE
destinationrule.networking.istio.io/hello2-dr 2m25s
为WorkloadEntry添加version标签
分别为3个WorkloadEntry添加version:v1
/version:v2
/version:v3
标签,示意如下。
spec:
address: 192.168.0.170
labels:
app: hello-workload
version: v1
为VM添加hello3 DNS
为虚拟机节点添加hello3 service的clusterIp
hello3_svc_ip=$(k get svc hello3-svc -n hybrid-hello -o jsonpath='{.spec.clusterIP}')
echo "$hello3_svc_ip hello3-svc.http-reciprocal-hello.svc.cluster.local" > dns_record
VMS=("$VM_PUB_1" "$VM_PUB_2" "$VM_PUB_3")
for vm in "${VMS[@]}"; do
ssh root@"$vm" "cat >> /etc/hosts" < dns_record
done
rm -rf dns_record
验证虚拟机对hello3 POD和hello3 service的访问
for vm in "${VMS[@]}"; do
hello3_pod_ip=$(k get pod -l app=hello3-deploy -n http-reciprocal-hello -o jsonpath={.items[*].status.podIP})
echo "Test access to hello3 pod on $vm"
ssh root@"$vm" "curl -s $hello3_pod_ip:8001/hello/pod_testing_msg"
echo
echo "Test access to hello3 service on $vm"
ssh root@"$vm" "curl -s hello3-svc.trace-hello.svc.cluster.local:8001/hello/svc_testing_msg"
echo
done
2 KUBE验证-POD和VM互访
完成实验环境搭建后,我们首先验证POD和VM互访。
首先,我们从hello1 POD中分别直接向3个VM中的应用发起请求,验证hello2 app是否可以访问到hello3 POD
hello1_pod=$(k get pod -l app=hello1-deploy -n http-reciprocal-hello -o jsonpath={.items..metadata.name})
echo "Test access vm ip directly"
VMS=("$VM_PRI_1" "$VM_PRI_2" "$VM_PRI_3")
for vm in "${VMS[@]}"; do
k exec "$hello1_pod" -c hello-v1-deploy -n http-reciprocal-hello -- curl -s "$vm":8001/hello/eric
echo
done
验证结果如下,我们期待3个hello2 app均能返回下游hello3的信息,证明VM已经完全网格化。hello2 app通过sidecar找到了hello3 svc对应的POD ip,并完成请求。
Test access vm ip directly
Hello eric(192.168.0.170)<-Hello eric(172.18.1.36)
Bonjour eric(192.168.0.171)<-Hello eric(172.18.1.36)
Hola eric(192.168.0.172)<-Hello eric(172.18.1.36)
直接访问hello2 app所在ecs节点的ip验证通过后,我们再从hello1 POD中向hello2 svc发起请求,验证hello1 POD到hello2 VM是否生效。这其实是在回归前一篇介绍的WorkloadEntry是否生效。
echo "Test access hello2-svc"
k exec "$hello1_pod" -c hello-v1-deploy -n http-reciprocal-hello \
-- curl -s hello2-svc.http-reciprocal-hello.svc.cluster.local:8001/hello/eric
验证结果如下,我们期待hello1 POD通过ServiceEntry和WorkloadEntry找到相应的VM。
Test access hello2-svc
Hello eric(192.168.0.170)<-Hello eric(172.18.1.36)
最后我们在hello1 POD中验证全链路:
echo "Test access hello1-svc"
k exec "$hello1_pod" -c hello-v1-deploy -n http-reciprocal-hello \
-- curl -s hello1-svc.http-reciprocal-hello.svc.cluster.local:8004/hello/eric
Test access hello1-svc
Hello eric(172.18.0.230)<-Bonjour eric(192.168.0.171)<-Hello eric(172.18.1.36)
3 MESH验证-全链路流量转移
物理链路验证完毕,我们再来验证逻辑链路,即流量转移的配比是否生效。
这个验证我们从本地向服务网格入口网关发起。首先确认本示例对外暴露的8004
端口已经在IstioGateway在配置好,然后获取istio-ingressgateway的ip,并通过ip发起请求。脚本示意如下:
m get IstioGateway -n istio-system -o jsonpath='{.items[0].spec.ports[?(@.name=="http-reciprocal")]}'
map[name:http-reciprocal port:8004 targetPort:8004]
IP=$(k -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
for i in {1..100}; do
resp=$(curl -s "$IP":8004/hello/eric)
echo "$resp" >>test_traffic_shift_result
done
echo "expected 30%(Hello eric)-60%(Bonjour eric)-10%(Hola eric):"
sort test_traffic_shift_result | grep -v "^[[:space:]]*$"| uniq -c | sort -nrk1
验证结果如下:
Test route n a loop
expected 30%(Hello eric)-60%(Bonjour eric)-10%(Hola eric):
61 Hello eric(172.18.0.230)<-Bonjour eric(192.168.0.171)<-Hello eric(172.18.1.36)
28 Hello eric(172.18.0.230)<-Hello eric(192.168.0.170)<-Hello eric(172.18.1.36)
11 Hello eric(172.18.0.230)<-Hola eric(192.168.0.172)<-Hello eric(172.18.1.36)
到此,POD和VM互访的http协议流量管理验证完毕。
本篇示例完整演示了非容器应用网格化过程中的一个比较经典的场景。覆盖到从istio-ingressgateway到deployment、workloadentry等各种CRD的配置,较完整地展示了POD和VM互访中遇到的各技术点的配置。在有了http协议的基础后,下一篇我们使用grpc代替http再次验证,并着重关注grpc的长链接特性。