为了演示跨集群访问,在一个Kubernetes集群中部署sleep应用服务,在第二个集群中部署httpbin应用服务,然后验证sleep应用是否可以调用远程集群的httpbin服务。
1)部署sleep服务到第一个集群cluster1中,执行如下命令:
kubectl create namespace app1
kubectl label namespace app1 istio-injection=enabled
kubectl apply -n app1 -f samples/sleep/sleep.yaml
export SLEEP_POD=$(kubectl get -n app1 pod -l app=sleep -o jsonpath={.items..metadata.name})
2)部署httpbin服务到第二个集群cluster2中,执行如下命令:
kubectl create namespace app2 kubectl label namespace app2 istio-injection=enabled kubectl apply -n app2 -f samples/httpbin/httpbin.yaml
3)获取集群cluster2的入口网关地址,如下所示:
export CLUSTER2_GW_ADDR=$(kubectl get svc --selector=app=istio-ingressgateway \
-n istio-system -o jsonpath="{.items[0].status.loadBalancer.ingress[0].ip}")
4)为了让在集群cluster1中的服务sleep能够访问集群cluster2中的服务httpbin,我们需要在集群cluster1中为服务httpbin创建一个服务条目ServiceEntry资源。服务条目ServiceEntry的主机名应该是<name>.<namespace>.globalname,其中name和namespace分别对应于集群cluster2中的远程服务的名称和命名空间。
对于*.global域下服务的DNS解析,需要为这些服务分配一个IP地址,并且保证.globalDNS域中的每个服务在集群中必须具有唯一的IP地址。这些IP地址在pod之外是不可路由的。在这个例子中,我们将使用网段127.255.0.0/16来避免与其他的IP冲突。这些IP的应用流量将由Sidecar代理捕获并路由到适当的其他远程服务。
在集群cluster1中创建该httpbin服务对应的ServiceEntry,执行如下命令:
kubectl apply -n app1 -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: httpbin-app2
spec:
hosts:
# must be of form name.namespace.global
- httpbin.app2.global
# Treat remote cluster services as part of the service mesh
# as all clusters in the service mesh share the same root of trust.
location: MESH_INTERNAL
ports:
- name: http1
number: 8000
protocol: http
resolution: DNS
addresses:
# the IP address to which httpbin.bar.global will resolve to
# must be unique for each remote service, within a given cluster.
# This address need not be routable. Traffic for this IP will be captured
# by the sidecar and routed appropriately.
- 127.255.0.2
endpoints:
# This is the routable address of the ingress gateway in cluster2 that
# sits in front of sleep.bar service. Traffic from the sidecar will be
# routed to this address.
- address: ${CLUSTER2_GW_ADDR}
ports:
http1: 15443 # Do not change this port value
EOF
上面的配置将会使集群cluster1中访问httpbin.app2.global的所有流量,包括访问它的任何端口的流量,都会被路由到启用了双向TLS连接的端点<IPofCluster2IngressGateway>:15443上。
端口15443的网关是一个特殊的SNI感知的Envoy代理,它是在前面开始部分中作为多集群Istio安装步骤的一部分预先配置和安装的。进入端口15443的流量将在目标集群的适当内部服务的pod中进行负载均衡。
在集群cluster1下执行如下命令查看容器istiocoredns,可以看到上述ServiceEntry的域名映射关系已经被加载:
export ISTIO_COREDNS=$(kubectl get -n istio-system po -l app=istiocoredns -o jsonpath={.items..metadata.name})
kubectl logs --tail 2 -n istio-system ${ISTIO_COREDNS} -c istio-coredns-plugin
执行结果如下所示:

5)验证在集群cluster1中的sleep服务是否可以正常调用位于集群cluster2中的httpbin服务,在集群cluster1执行如下命令:
kubectl exec $SLEEP_POD -n app1 -c sleep -- curl httpbin.app2.global:8000/headers
执行结果如下所示:

至此,集群cluster1与cluster2在多控制平面配置下完成了连通。