备考ICA----Istio实验20---跨网络Primary-Remote主从架构部署

发布于:2024-04-19 ⋅ 阅读:(21) ⋅ 点赞:(0)

备考ICA----Istio实验20—跨网络Primary-Remote主从架构部署

按照本实验在 cluster1(主集群)上安装 Istio 控制平面,并将 cluster2(远程集群)配置为使用 cluster1 中的控制平面。群集 cluster1 在 network1 网络上,而 cluster2 在 network2 网络上。这意味着 Pod 之间没有跨越群集边界的直接连接。
在这里插入图片描述
在此配置中,cluster1 中的 Istiod 将监视两个集群中的 APIServer 的端点。通过这种方式,控制平面将能够为两个集群中的工作负载提供服务发现。跨集群边界的服务工作负载通过用于东西向流量的专用网关进行间接通信。每个群集中的网关必须可以从另一个群集中访问。
cluster2 中的服务将通过相同的 east-west gateway 到达集 cluster1 的控制平面。

1. 环境准备

确认2个集群存在
在这里插入图片描述
如果不存在,可以使用上个实验一样的命令进行创建

kind create cluster --name cluster1
kind create cluster --name cluster2

在这里插入图片描述

上下文,api,metallb和istioctl安装创建见上一个实验
具体见前一个实验的1-3节内容

2. 为集群创建Secret

在每个集群中,创建一个名为 cacerts Secret 包含所有输入文件 ca-cert.pem、ca-key.pem、root-cert.pem 和 cert-chain.pe

2.1 cluster1

切换上下文在cluster1中执行

cd certs/
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

2.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

在这里插入图片描述

3. 为 cluster1配置primary控制平面

3.1 为cluster1设置默认网络

kubectl --context="${CTX_CLUSTER1}" \
  label namespace istio-system \
  topology.istio.io/network=network1

确认配置正确

kubectl --context $CTX_CLUSTER1 get ns istio-system --show-labels

在这里插入图片描述

3.2 istioctl安装文件

cluster1.yaml

apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
  values:
    global:
      meshID: mesh1
      multiCluster:
        clusterName: cluster1
      network: network1

3.3 安装istiod控制平面

cd
istioctl install --context $CTX_CLUSTER1 -f cluster1.yaml -y

在这里插入图片描述

3.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

在这里插入图片描述

3.5 放开cluster1中的服务

kubectl --context="${CTX_CLUSTER1}" apply -n istio-system \
   -f istio/samples/multicluster/expose-services.yaml

4. cluster2配置控制平面集群

4.1 为cluster2的命名空间打标签

为 istio-system 命名空间添加注解来识别应管理 cluster2 的外部控制平面集群

kubectl --context="${CTX_CLUSTER2}" annotate namespace istio-system \
topology.istio.io/controlPlaneClusters=cluster1

4.2 为cluster1设置默认网络

kubectl --context="${CTX_CLUSTER2}" \
  label namespace istio-system \
  topology.istio.io/network=network2

确认label正确

kubectl get ns istio-system --show-labels --context ${CTX_CLUSTER2}

在这里插入图片描述

4.3 将cluster2设为从集群

确认cluster1的istio-eastwestgateway的external-ip

kubectl get svc -n istio-system --context ${CTX_CLUSTER1} istio-eastwestgateway 

在这里插入图片描述
配置cluster2安装文件
cluster2.yaml

apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
  profile: remote
  values:
    istiodRemote:
      injectionPath: /inject/cluster/cluster2/net/network2
    global:
      remotePilotAddress: '172.18.0.221'

安装生效

istioctl install --context="${CTX_CLUSTER2}" -f cluster2.yaml -y

在这里插入图片描述
为了提供对 cluster2 的 API 服务器访问,我们生成一个远程机密并将其应用于 cluster1

istioctl create-remote-secret --context="${CTX_CLUSTER2}" --name=cluster2 | \
  kubectl apply -f - --context="${CTX_CLUSTER1}"

验证是否生成完成

kubectl get secrets --context ${CTX_CLUSTER1} -n istio-system

在这里插入图片描述

5. 配置cluster2网关

5.1 在cluster2上安装东西网关

cd istio
samples/multicluster/gen-eastwest-gateway.sh --mesh mesh1 \
--cluster cluster2 --network network2 | \
istioctl --context="${CTX_CLUSTER2}" install -y -f -

在这里插入图片描述

5.2 开放 cluster2 中的服务

kubectl --context="${CTX_CLUSTER1}" apply -n istio-system \
-f samples/multicluster/expose-services.yaml

6. 部署测试环境

环境部署脚本
test.sh

#!/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

部署

chmod +x test.sh 
./test.sh 

在这里插入图片描述

7. 测试

和上个实验一样
通过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

在这里插入图片描述
上一个实验是在2个集群上都安装了istiod,而本实验只在cluster1上搭建了istiod通过打通东西向gateway实现集群的互通
至此备考ICA----Istio实验20—跨网络Primary-Remote主从架构部署完成