备考ICA----Istio实验19—跨网络Multi-Primary多主架构部署
按照本实验,在 cluster1 和 cluster2 两个集群上,安装 Istio 控制平面, 且将两者均设置为主集群(primary cluster)。 集群 cluster1 在 network1 网络上,而集群 cluster2 在 network2 网络上。这意味着这些跨集群边界的 Pod 之间,网络不能直接连通。
在此配置中,cluster1 和 cluster2 均监测两个集群 API Server 的服务端点。
跨集群边界的服务负载通过专用的 east-west gateway,以间接的方式通讯。每个集群中的网关在其他集群必须可以访问。
1. 环境准备
1.1 创建集群
使用 kind 创建两个集群 cluster1、cluster2
kind create cluster --name cluster1
kind create cluster --name cluster2
1.2 准备kubeconfig文件
API1_IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' cluster1-control-plane)
API2_IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' cluster2-control-plane)
kubectl config set clusters.kind-cluster1.server https://$API1_IP:6443
kubectl config set clusters.kind-cluster2.server https://$API2_IP:6443
1.3 检索集群上下文
kubectl config get-contexts
export CTX_CLUSTER1=kind-cluster1
export CTX_CLUSTER2=kind-cluster2
1.4 部署metallb
kubectl --context $CTX_CLUSTER1 apply -f ~/metallb/v0.14.3/metallb-native.yaml
kubectl --context $CTX_CLUSTER2 apply -f ~/metallb/v0.14.3/metallb-native.yaml
kubectl --context $CTX_CLUSTER1 get pods -n metallb-system
kubectl --context $CTX_CLUSTER2 get pods -n metallb-system
kubectl --context $CTX_CLUSTER1 apply -f ~/metallb/v0.14.3/specific-L2Advertisement.yaml
kubectl --context $CTX_CLUSTER2 apply -f ~/metallb/v0.14.3/specific-L2Advertisement.yaml
确认pod被正确创建
metallb的地址池
确认2个cluster都拿到了metallb分配的ip
2. 安装istio
wget https://github.com/istio/istio/releases/download/1.20.3/istio-1.20.3-linux-amd64.tar.gz
tar xf istio-1.20.3-linux-amd64.tar.gz
ln -sf istio-1.20.3 istio
export VERSION=istio
echo "PATH=$PATH:$PWD/$VERSION/bin" >> ~/.bash_profile
echo "source <(istioctl completion bash)" >> ~/.bash_profile
source ~/.bash_profile
istioctl x precheck
3. 生成证书
3.1 生成根证书
sudo apt install make tree -y
mkdir -p certs
cd certs/
make -f ~/istio/tools/certs/Makefile.selfsigned.mk root-ca
tree
文件名 | 作用 |
---|---|
root-ca.conf | 生成根证书的 openssl 配置 |
root-cert.csr | 为根证书生成的 CSR |
root-cert.pem | 生成的根证书 |
root-key.pem | 生成的根密钥 |
3.2 生成cluster1的证书
make -f ~/istio/tools/certs/Makefile.selfsigned.mk cluster1-cacerts
3.3 生成cluster2的证书
make -f ~/istio/tools/certs/Makefile.selfsigned.mk cluster2-cacerts
4. 为集群创建Secret
在每个集群中,创建一个名为 cacerts Secret 包含所有输入文件 ca-cert.pem、ca-key.pem、root-cert.pem 和 cert-chain.pem
4.1 cluster1
切换上下文在cluster1中执行
kubectl --context $CTX_CLUSTER1 create namespace istio-system
kubectl --context $CTX_CLUSTER1 create secret generic cacerts -n istio-system \
--from-file=cluster1/ca-cert.pem \
--from-file=cluster1/ca-key.pem \
--from-file=cluster1/root-cert.pem \
--from-file=cluster1/cert-chain.pem
确认secrets被正确创建
kubectl --context $CTX_CLUSTER1 get secrets -n istio-system
4.2 cluster2
切换上下文在cluster2中执行
kubectl --context $CTX_CLUSTER2 create namespace istio-system
kubectl --context $CTX_CLUSTER2 create secret generic cacerts -n istio-system \
--from-file=cluster2/ca-cert.pem \
--from-file=cluster2/ca-key.pem \
--from-file=cluster2/root-cert.pem \
--from-file=cluster2/cert-chain.pem
确认secrets被正确创建
kubectl --context $CTX_CLUSTER2 get secrets -n istio-system
5. 配置 primary 控制平面
5.1 cluster1配置 primary 控制平面
5.1.1 为istio-system打标签
kubectl --context="${CTX_CLUSTER1}" \
label namespace istio-system \
topology.istio.io/network=network1
确认配置正确
kubectl --context $CTX_CLUSTER1 get ns istio-system --show-labels
5.1.2 istioctl安装文件
cluster1.yaml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
values:
global:
meshID: mesh1
multiCluster:
clusterName: cluster1
network: network1
5.13 安装istiod控制平面
cd
istioctl install --context $CTX_CLUSTER1 -f cluster1.yaml -y
5.1.4 cluster1上安装东西向网关
istio/samples/multicluster/gen-eastwest-gateway.sh --mesh mesh1 \
--cluster cluster1 --network network1 | \
istioctl --context="${CTX_CLUSTER1}" install -y -f -
确认服务正确创建
kubectl --context="${CTX_CLUSTER1}" get svc istio-eastwestgateway -n istio-system
5.1.5 放开cluster1中的服务
kubectl --context="${CTX_CLUSTER1}" apply -n istio-system \
-f istio/samples/multicluster/expose-services.yaml
5.2 cluster2配置 primary 控制平面
5.2.1 为istio-system打标签
kubectl --context="${CTX_CLUSTER2}" \
label namespace istio-system \
topology.istio.io/network=network2
确认配置正确
kubectl --context $CTX_CLUSTER2 get ns istio-system --show-labels
5.2.2 istioctl安装文件
cluster2.yaml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
values:
global:
meshID: mesh2
multiCluster:
clusterName: cluster2
network: network2
5.2.3 安装istiod控制平面
cd
istioctl install --context $CTX_CLUSTER2 -f cluster1.yaml -y
5.2.4 cluster2上安装东西向网关
istio/samples/multicluster/gen-eastwest-gateway.sh --mesh mesh2 \
--cluster cluster2 --network network2 | \
istioctl --context="${CTX_CLUSTER2}" install -y -f -
确认服务正确创建
kubectl --context="${CTX_CLUSTER2}" get svc istio-eastwestgateway -n istio-system
5.2.5 放开cluster2中的服务
kubectl --context="${CTX_CLUSTER2}" apply -n istio-system \
-f istio/samples/multicluster/expose-services.yaml
6. 启用端点发现
cluster1中配置cluster2访问的secret
istioctl create-remote-secret --context="${CTX_CLUSTER2}" --name=cluster2 | \
kubectl apply -f - --context="${CTX_CLUSTER1}"
cluster2中配置cluster1访问的secret
istioctl create-remote-secret --context="${CTX_CLUSTER1}" --name=cluster1 | \
kubectl apply -f - --context="${CTX_CLUSTER2}"
7. 部署测试环境
在cluster1上部署一个v1版本的helloworld
在cluster2上部署一个v1版本的helloworld
#!/bin/bash
VERSION=$(ls ~ | grep istio)
export CTX_CLUSTER1=kind-cluster1
export CTX_CLUSTER2=kind-cluster2
############ Create ns sample in cluster1 cluster2 ############
kubectl create --context="${CTX_CLUSTER1}" namespace sample
kubectl create --context="${CTX_CLUSTER2}" namespace sample
############ Label ns sample istio-injection=enabled ############
kubectl label --context="${CTX_CLUSTER1}" namespace sample istio-injection=enabled
kubectl label --context="${CTX_CLUSTER2}" namespace sample istio-injection=enabled
############ deploy helloword in cluster1 cluster2 ############
cd ~/istio
kubectl apply --context="${CTX_CLUSTER1}" -f samples/helloworld/helloworld.yaml -l service=helloworld -n sample
kubectl apply --context="${CTX_CLUSTER2}" -f samples/helloworld/helloworld.yaml -l service=helloworld -n sample
############ deploy helloword V1 in cluster1 ############
kubectl apply --context="${CTX_CLUSTER1}" -f samples/helloworld/helloworld.yaml -l version=v1 -n sample
kubectl get pod --context="${CTX_CLUSTER1}" -n sample -l app=helloworld
############ deploy helloword V2 in cluster2 ############
kubectl apply --context="${CTX_CLUSTER2}" -f samples/helloworld/helloworld.yaml -l version=v2 -n sample
kubectl get pod --context="${CTX_CLUSTER2}" -n sample -l app=helloworld
############ deploy sleep in cluster1 cluster2 ############
kubectl apply --context="${CTX_CLUSTER1}" -f samples/sleep/sleep.yaml -n sample
kubectl apply --context="${CTX_CLUSTER2}" -f samples/sleep/sleep.yaml -n sample
kubectl get pod --context="${CTX_CLUSTER1}" -n sample -l app=sleep
kubectl get pod --context="${CTX_CLUSTER2}" -n sample -l app=sleep
部署服务
bash test.sh
8. 访问测试
通过cluster1的sleep访问sample名称空间中的helloworld,理论上只应该返回v1的版本,但由于打通了cluster2,所以有一部分流量被分发到了cluster2上,由v2进行响应
for x in {1..10};do kubectl exec --context="${CTX_CLUSTER1}" \
-n sample deploy/sleep -- curl -s helloworld.sample:5000/hello;done
同理cluster2上的sleep也一样
for x in {1..10};do kubectl exec --context="${CTX_CLUSTER2}" \
-n sample deploy/sleep -- curl -s helloworld.sample:5000/hello;done
至此备考ICA----Istio实验19—跨网络Multi-Primary多主架构部署实验完成