自学内容网 自学内容网

使用istio实现权重路由

istio概述

**概述:**Istio 是一个开源的 服务网格(Service Mesh)解决方案,主要用于管理、保护和监控微服务架构中的服务通信。它为微服务提供了基础设施层的控制功能,不需要更改应用程序的代码,从而解决服务之间的安全性、流量管理、可观察性等问题

**工作机制:**通过在每个服务的旁边部署一个 Sidecar Proxy(通常是 Envoy)。这个代理会拦截所有进出该服务的流量,并将其发送到 Istio 的控制平面进行管理和监控。应用程序本身不需要进行修改,所有的功能都通过配置管理

K8S版本要求:https://istio.io/latest/zh/docs/releases/supported-releases/#support-status-of-istio-releases

VersionCurrently SupportedRelease DateEnd of LifeSupported Kubernetes VersionsTested, but not supported
masterNo, development only1.29, 1.30, 1.31, 1.321.23, 1.24, 1.25, 1.26, 1.27, 1.28
1.24YesNovember 7, 2024~Aug 2025 (Expected)1.28, 1.29, 1.30, 1.311.23, 1.24, 1.25, 1.26, 1.27
1.23YesAug 14, 2024~May 2025 (Expected)1.27, 1.28, 1.29, 1.301.23, 1.24, 1.25, 1.26
1.22YesMay 13, 2024~Jan 2025 (Expected)1.27, 1.28, 1.29, 1.301.23, 1.24, 1.25, 1.26
1.21YesMar 13, 2024Sept 27, 20241.26, 1.27, 1.28, 1.291.23, 1.24, 1.25
1.20NoNov 14, 2023Jun 25, 20241.25, 1.26, 1.27, 1.28, 1.291.23, 1.24
1.19NoSept 5, 2023Apr 24, 20241.25, 1.26, 1.27, 1.281.21, 1.22, 1.23, 1.24
1.18NoJun 3, 2023Jan 4, 20241.24, 1.25, 1.26, 1.271.20, 1.21, 1.22, 1.23
1.17NoFeb 14, 2023Oct 27, 20231.23, 1.24, 1.25, 1.261.16, 1.17, 1.18, 1.19, 1.20, 1.21, 1.22
1.16NoNov 15, 2022Jul 25, 20231.22, 1.23, 1.24, 1.251.16, 1.17, 1.18, 1.19, 1.20, 1.21
1.15NoAug 31, 2022Apr 4, 20231.22, 1.23, 1.24, 1.251.16, 1.17, 1.18, 1.19, 1.20, 1.21
1.14NoMay 24, 2022Dec 27, 20221.21, 1.22, 1.23, 1.241.16, 1.17, 1.18, 1.19, 1.20
1.13NoFeb 11, 2022Oct 12, 20221.20, 1.21, 1.22, 1.231.16, 1.17, 1.18, 1.19

安装istio

参考链接:https://istio.io/v1.17/zh/docs/setup/getting-started/#download

1.下载指定版本的Istio,以K8S1.23版本为例安装istio1.17.8
[root@master231 06-istio]# curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.17.8 TARGET_ARCH=x86_64 sh -

2.配置Istioctl工具的环境变量
[root@master231 06-istio]# tar xf istio-1.17.8-linux-amd64.tar.gz 
[root@master231 06-istio]# 
[root@master231 06-istio]# ll
total 26504
drwxr-xr-x 3 root root     4096 Aug 14 11:30 ./
drwxr-xr-x 8 root root     4096 Aug 14 11:24 ../
drwxr-x--- 6 root root     4096 Oct 11  2023 istio-1.17.8/
-rw-r--r-- 1 root root 27127663 Jun 21 17:39 istio-1.17.8-linux-amd64.tar.gz

[root@master231 06-istio]# echo 'export PATH="$PATH:`pwd`/istio-1.17.8/bin"' > /etc/profile.d/istio.sh
[root@master231 06-istio]# source /etc/profile.d/istio.sh
[root@master231 06-istio]# istioctl --help

3.安装Istio
[root@master241 ~]# istioctl install --set profile=demo -y  # 安装demo的配置

[root@master241 ~]# istioctl profile dump demo|default|minimal|... # 查看你想要查看的配置即可。


在安装 Istio 时所能够使用的内置配置文件。这些配置文件提供了对Istio控制平面和Istio数据平面Sidecar的定制内容。
可以从Istio内置配置文件的其中一个开始入手,然后根据您的特定需求进一步自定义配置文件。当前提供以下几种内置配置文件:
- default:
根据 IstioOperator API 的默认设置启动组件。 
建议用于生产部署和 Multicluster Mesh 中的 Primary Cluster。
您可以运行 istioctl profile dump 命令来查看默认设置。

- demo:
这一配置具有适度的资源需求,旨在展示 Istio 的功能。 
它适合运行 Bookinfo 应用程序和相关任务。 这是通过快速开始指导安装的配置。
此配置文件启用了高级别的追踪和访问日志,因此不适合进行性能测试。

- minimal:
与默认配置文件相同,但只安装了控制平面组件。 
它允许您使用 Separate Profile 配置控制平面和数据平面组件(例如 Gateway)。

- remote:
配置 Multicluster Mesh 的 Remote Cluster。

- empty:
不部署任何东西。可以作为自定义配置的基本配置文件。

- preview:
预览文件包含的功能都是实验性。这是为了探索 Istio 的新功能。不确保稳定性、安全性和性能(使用风险需自负)。



参考链接:
https://istio.io/v1.17/zh/docs/setup/additional-setup/config-profiles/
https://istio.io/v1.17/zh/docs/setup/getting-started/#download 



温馨提示:
此环节可能下载镜像失败,需要手动解决。




成功的输出如下:
[root@master231 06-istio]# istioctl install --set profile=demo -y
✔ Istio core installed                                                                                                                 
✔ Istiod installed                                                                                                                     
✔ Egress gateways installed                                                                                                            
✔ Ingress gateways installed                                                                                                           
✔ Installation complete                                                                                                                Making this installation the default for injection and validation.

Thank you for installing Istio 1.17.  Please take a few minutes to tell us about your install/upgrade experience!  https://forms.gle/hMHGiwZHPU7UQRWe9
[root@master231 06-istio]# 

6.查看istio的版本号
[root@master231 06-istio]# istioctl version
client version: 1.17.8
control plane version: 1.17.8
data plane version: 1.17.8 (2 proxies)

7.添加自动补全 
[root@master231 06-istio]# source istio-1.17.8/tools/istioctl.bash l 

手动注入pod

在安装完毕istio组件后,创建一些业务pod,然后在注入istiopod

cat > 00-test_istio.yaml <<eof
apiVersion: v1
kind: Namespace
metadata:
  name: wzyluckyboy
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-apps
  namespace: wzyluckyboy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: v1
  template:
    metadata:
      labels:
        app: v1
    spec:
      containers:
      - name: c1
        image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
        ports:
        - containerPort: 80
eof

2.待pod创建完毕后再手动注入

istioctl kube-inject -f 00-test_istio.yaml | kubectl -n wzyluckyboy apply -f -

3.可以查看到带有istio标签的pod

[root@master23101-istio]# kubectl -n wzyluckyboy get pods --show-labels 
NAME                           READY   STATUS    RESTARTS   AGE   LABELS
deploy-apps-858765cd5c-pmbpm   2/2     Running   0          29s   app=v1,pod-template-hash=858765cd5c,security.istio.io/tlsMode=istio,service.istio.io/canonical-name=v1,service.istio.io/canonical-revision=latest
deploy-apps-858765cd5c-w8gxf   2/2     Running   0          54s   app=v1,pod-template-hash=858765cd5c,security.istio.io/tlsMode=istio,service.istio.io/canonical-name=v1,service.istio.io/canonical-revision=latest
deploy-apps-858765cd5c-zkbfk   2/2     Running   0          51s   app=v1,pod-template-hash=858765cd5c,security.istio.io/tlsMode=istio,service.istio.io/canonical-name=v1,service.istio.io/canonical-revision=latest

查看pod的init容器:kubectl -n wzyluckyboy get pods -o yaml

istio实现权重路由

istio实现灰度发布

# 相关配置文件说明
01-deploy-apps.yaml       # 部署了2个pod,对应要灰度发布的应用
02-svc-apps.yaml          # 2个svc,关联到2个pod
03-deploy-client.yaml     # 仅用于业务访问测试
04-vs-apps-svc-all.yaml   # 虚拟服务,影响权重的操作在这个文件

1.流量管理之路由(权重路由模拟灰度发布)

cat > 01-deploy-apps.yaml <<EOF
apiVersion: v1
kind: Namespace
metadata:
  name: wzyluckyboy
---

apiVersion: apps/v1
# 注意,创建pod建议使用deploy资源,不要使用rc资源,否则istioctl可能无法手动注入。
kind: Deployment
metadata:
  name: apps-v1
  namespace: wzyluckyboy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: xiuxian01
      version: v1
      auther: wzyluckyboy
  template:
    metadata:
      labels:
        app: xiuxian01
        version: v1
        auther: wzyluckyboy
    spec:
      containers:
      - name: c1
        ports:
        - containerPort: 80
        image: busybox:1.36.1
        command: ["/bin/sh","-c","echo 'c1' > /var/www/index.html;httpd -f -p 80 -h /var/www"]
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: apps-v2
  namespace: wzyluckyboy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: xiuxian02
      version: v2
      auther: wzyluckyboy
  template:
    metadata:
      labels:
        app: xiuxian02
        version: v2
        auther: wzyluckyboy
    spec:
      containers:
      - name: c2
        ports:
        - containerPort: 80
        image: busybox:1.36.1
        command: ["/bin/sh","-c","echo 'c2' > /var/www/index.html;httpd -f -p 80 -h /var/www"]
EOF

2.创建3个service,分别关联不同的pod,其中一个svc管理之前创建的所有pod

cat > 02-svc-apps.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
  name: apps-svc-v1
  namespace: wzyluckyboy
spec:
  selector:
    version: v1
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    name: http

---

apiVersion: v1
kind: Service
metadata:
  name: apps-svc-v2
  namespace: wzyluckyboy
spec:
  selector:
    version: v2
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    name: http

---

apiVersion: v1
kind: Service
metadata:
  name: apps-svc-all
  namespace: wzyluckyboy
spec:
  selector:
    auther: wzyluckyboy
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    name: http
EOF

3.创建一个客户端用户业务测试

cat > 03-deploy-client.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: apps-client
  namespace: wzyluckyboy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: client-test
  template:
    metadata:
      labels:
        app: client-test
    spec:
      containers:
      - name: c1
        image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1 
        command:
        - tail
        - -f
        - /etc/hosts
EOF

4.注入影响路由权重

cat > 04-vs-apps-svc-all.yaml <<eof
apiVersion: networking.istio.io/v1beta1
# apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: apps-svc-all-vs
  namespace: wzyluckyboy
spec:
  # 指定vs关联的后端svc名称
  hosts:
  - apps-svc-all
  # 配置http配置
  http:
    # 定义路由信息
  - route:
      # 定义目标
    - destination:
        host: apps-svc-v1
      # 指定权重
      weight: 90
    - destination:
        host: apps-svc-v2
      weight: 10
eof
3.手动注入Istio-proxy
1.注入前
[root@master241 yinzhengjie]# kubectl get pods -n wzyluckyboy 
NAME                          READY   STATUS    RESTARTS   AGE
apps-client-f84c89565-kmqkv   1/1     Running   0          31s
apps-v1-9bff7546c-fsnmn       1/1     Running   0          32s
apps-v2-6c957bf64b-lz65z      1/1     Running   0          32s
[root@master241 yinzhengjie]# 
2.开始手动注入
[root@master241 yinzhengjie]# istioctl kube-inject -f 03-deploy-client.yaml | kubectl -n wzyluckyboy apply -f -
deployment.apps/apps-client configured
[root@master241 yinzhengjie]# 
[root@master241 yinzhengjie]# istioctl kube-inject -f 01-deploy-apps.yaml | kubectl -n wzyluckyboy apply -f -
namespace/yinzhengjie unchanged
deployment.apps/apps-v1 configured
deployment.apps/apps-v2 configured
[root@master241 yinzhengjie]# 
3.注入后
[root@master241 yinzhengjie]# kubectl get pods -n wzyluckyboy 
NAME                          READY   STATUS        RESTARTS   AGE
apps-client-5cc67d864-g2r2v   2/2     Running       0          41s
apps-v1-85c976498b-5qp59      2/2     Running       0          30s
apps-v2-5bb84548fc-65r7x      2/2     Running       0          30s
[root@master241 yinzhengjie]# 
5.4.开始测试
[root@master231 ~]# kubectl -n wzyluckyboy exec -it apps-client-5f579696d5-s7nvc   -- sh
/ # while true; do curl http://apps-svc-all;sleep 0.1;done
c1
c1
c1
c1
c1
c1
c1
c1
c1
c1
c1
c1
c1
c1
c2

原文地址:https://blog.csdn.net/qq_73797346/article/details/145430160

免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!