ArgoCD Rollouts - 结合Istio进行Canary流量迁移【五】
实战案例2:结合Istio进行Canary流量迁移
将名称控件打标签 启用istio-injection注入功能
[root@xianchaomaster1 ~]# kubectl get ns --show-labels
NAME STATUS AGE LABELS
argo-rollouts Active 165m kubernetes.io/metadata.name=argo-rollouts
argocd Active 27h kubernetes.io/metadata.name=argocd
default Active 4d23h kubernetes.io/metadata.name=default
dev Active 21h kubernetes.io/metadata.name=dev
event-demo Active 4d14h kubernetes.io/metadata.name=event-demo
gitlab Active 4d14h kubernetes.io/metadata.name=gitlab
guestbook Active 23h kubernetes.io/metadata.name=guestbook
guestbooking Active 26h kubernetes.io/metadata.name=guestbooking
helloworld Active 22h kubernetes.io/metadata.name=helloworld
istio-system Active 4d16h kubernetes.io/metadata.name=istio-system
knative-eventing Active 4d14h app.kubernetes.io/name=knative-eventing,app.kubernetes.io/version=1.7.1,eventing.knative.dev/release=v1.7.1,kubernetes.io/metadata.name=knative-eventing
knative-serving Active 4d16h app.kubernetes.io/name=knative-serving,app.kubernetes.io/version=1.7.1,kubernetes.io/metadata.name=knative-serving
knative-sources Active 4d14h contrib.eventing.knative.dev/release=v1.7.0,kubernetes.io/metadata.name=knative-sources
kube-node-lease Active 4d23h kubernetes.io/metadata.name=kube-node-lease
kube-public Active 4d23h kubernetes.io/metadata.name=kube-public
kube-system Active 4d23h kubernetes.io/metadata.name=kube-system
nfs Active 4d12h kubernetes.io/metadata.name=nfs
prod Active 21h kubernetes.io/metadata.name=prod
staging Active 21h kubernetes.io/metadata.name=staging
tekton-pipelines Active 4d13h app.kubernetes.io/instance=default,app.kubernetes.io/part-of=tekton-pipelines,kubernetes.io/metadata.name=tekton-pipelines,pod-security.kubernetes.io/enforce=restricted
tekton-pipelines-resolvers Active 4d13h app.kubernetes.io/component=resolvers,app.kubernetes.io/instance=default,app.kubernetes.io/part-of=tekton-pipelines,kubernetes.io/metadata.name=tekton-pipelines-resolvers,pod-security.kubernetes.io/enforce=restricted
[root@xianchaomaster1 ~]# kubectl create ns istio-injection
namespace/istio-injection created
[root@xianchaomaster1 ~]# kubectl label namespace istio-injection istio-injection=true
namespace/istio-injection labeled
[root@xianchaomaster1 ~]# kubectl get ns --show-labels | grep istio-injection
istio-injection Active 48s istio-injection=true,kubernetes.io/metadata.name=istio-injection
配置Yaml文件
查看代码
#下载镜像
crictl pull ikubernetes/spring-boot-helloworld:v0.9.1
#编写yaml 创建6个pod
[root@xianchaomaster1 08-argo-rollouts]# cat 02-argo-rollouts-with-istio-traffic-shifting.yaml
# CopyRight: MageEdu <mage@magedu.com>
---
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
name: rollouts-helloworld-with-traffic-shifting
spec:
replicas: 6
strategy:
canary:
trafficRouting:
istio:
virtualService:
name: helloworld-rollout-vsvc # required
routes:
- primary # optional if there is a single route in VirtualService, required otherwise
destinationRule:
name: helloworld-rollout-destrule # required
canarySubsetName: canary # required
stableSubsetName: stable # required
steps:
- setCanaryScale:
matchTrafficWeight: true
- setWeight: 5
- pause: {duration: 20}
- setWeight: 10
- pause: {duration: 20}
- setWeight: 20
- pause: {duration: 20}
- setWeight: 40
- pause: {duration: 20}
- setWeight: 60
- pause: {duration: 20}
- setWeight: 80
- pause: {duration: 20}
revisionHistoryLimit: 5
selector:
matchLabels:
app: spring-boot-helloworld
template:
metadata:
labels:
app: spring-boot-helloworld
spec:
containers:
- name: spring-boot-helloworld
image: ikubernetes/spring-boot-helloworld:v0.9.1
ports:
- name: http
containerPort: 80
protocol: TCP
resources:
requests:
memory: 32Mi
cpu: 50m
livenessProbe:
httpGet:
path: '/'
port: 80
scheme: HTTP
initialDelaySeconds: 3
readinessProbe:
httpGet:
path: '/'
port: 80
scheme: HTTP
initialDelaySeconds: 5
---
apiVersion: v1
kind: Service
metadata:
name: spring-boot-helloworld
spec:
ports:
- port: 80
targetPort: http
protocol: TCP
name: http
selector:
app: spring-boot-helloworld
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: helloworld-rollout-vsvc
spec:
#gateways:
#- istio-rollout-gateway
hosts:
- spring-boot-helloworld
http:
- name: primary # referenced in canary.trafficRouting.istio.virtualService.routes
route:
- destination:
host: spring-boot-helloworld
subset: stable # referenced in canary.trafficRouting.istio.destinationRule.stableSubsetName
weight: 100
- destination:
host: spring-boot-helloworld
subset: canary # referenced in canary.trafficRouting.istio.destinationRule.canarySubsetName
weight: 0
---
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: helloworld-rollout-destrule
spec:
host: spring-boot-helloworld
subsets:
- name: canary # referenced in canary.trafficRouting.istio.destinationRule.canarySubsetName
labels: # labels will be injected with canary rollouts-pod-template-hash value
app: spring-boot-helloworld
- name: stable # referenced in canary.trafficRouting.istio.destinationRule.stableSubsetName
labels: # labels will be injected with stable rollouts-pod-template-hash value
app: spring-boot-helloworld
---
#先删除 之前的配置 01
[root@xianchaomaster1 08-argo-rollouts]# kubectl delete -f 01-argo-rollouts-demo.yaml
rollout.argoproj.io "rollouts-spring-boot-helloworld" deleted
#将yaml应用到 istio-injection名称空间下
#这里实验中没有注入 sidecar 实验有点问题 但不影响 应该是REAdy 2/2
[root@xianchaomaster1 08-argo-rollouts]# kubectl apply -f 02-argo-rollouts-with-istio-traffic-shifting.yaml -n istio-injection
rollout.argoproj.io/rollouts-helloworld-with-traffic-shifting created
service/spring-boot-helloworld created
virtualservice.networking.istio.io/helloworld-rollout-vsvc created
destinationrule.networking.istio.io/helloworld-rollout-destrule created
[root@xianchaomaster1 08-argo-rollouts]# kubectl get pods -n istio-injection
NAME READY STATUS RESTARTS AGE
rollouts-helloworld-with-traffic-shifting-5dc94d6f9d-76c57 1/1 Running 0 70s
rollouts-helloworld-with-traffic-shifting-5dc94d6f9d-8n5hz 1/1 Running 0 70s
rollouts-helloworld-with-traffic-shifting-5dc94d6f9d-ffbg7 1/1 Running 0 70s
rollouts-helloworld-with-traffic-shifting-5dc94d6f9d-hdhwz 1/1 Running 0 70s
rollouts-helloworld-with-traffic-shifting-5dc94d6f9d-td2xh 1/1 Running 0 70s
rollouts-helloworld-with-traffic-shifting-5dc94d6f9d-zphdh 1/1 Running 0 70s
#此时的vs 流量比例为 100% => v1
[root@xianchaomaster1 08-argo-rollouts]# kubectl get vs helloworld-rollout-vsvc -o yaml -n istio-injection -w
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"networking.istio.io/v1beta1","kind":"VirtualService","metadata":{"annotations":{},"name":"helloworld-rollout-vsvc","namespace":"istio-injection"},"spec":{"hosts":["spring-boot-helloworld"],"http":[{"name":"primary","route":[{"destination":{"host":"spring-boot-helloworld","subset":"stable"},"weight":100},{"destination":{"host":"spring-boot-helloworld","subset":"canary"},"weight":0}]}]}}
creationTimestamp: "2023-07-18T06:31:11Z"
generation: 1
name: helloworld-rollout-vsvc
namespace: istio-injection
resourceVersion: "1502516"
uid: aa69d2db-daf3-49d1-96d5-577b70fe44ef
spec:
hosts:
- spring-boot-helloworld
http:
- name: primary
route:
- destination:
host: spring-boot-helloworld
subset: stable
weight: 100
- destination:
host: spring-boot-helloworld
subset: canary
weight: 0
#流量100% 在 v1
[root@xianchaomaster1 08-argo-rollouts]# kubectl run client-$RANDOM --image ikubernetes/admin-box:v1.2 --restart=Never -it --rm --command -- /bin/bash
If you dont see a command prompt, try pressing enter.
#while true; do curl spring-boot-helloworld/version; sleep .5;done
v1
v1
#更新镜像
[root@xianchaomaster1 08-argo-rollouts]# vim 02-argo-rollouts-with-istio-traffic-shifting.yaml
改为: image: ikubernetes/spring-boot-helloworld:v0.9.2
[root@xianchaomaster1 08-argo-rollouts]# kubectl apply -f 02-argo-rollouts-with-istio-traffic-shifting.yaml
rollout.argoproj.io/rollouts-helloworld-with-traffic-shifting created
service/spring-boot-helloworld created
virtualservice.networking.istio.io/helloworld-rollout-vsvc created
destinationrule.networking.istio.io/helloworld-rollout-destrule created
#慢慢的 查看vs比例 已经到了 90%-v1 10%-v2 、80%-v1 20%-v2 、60%-v1 40%-v2 、40%-v1 60%-v2 、20%-v1 80%-v2 、0%-v1 100%-v2
kubectl get vs helloworld-rollout-vsvc -o yaml -n istio-injection -w
[root@xianchaomaster1 08-argo-rollouts]# kubectl get vs helloworld-rollout-vsvc -o yaml -n istio-injection -w
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"networking.istio.io/v1beta1","kind":"VirtualService","metadata":{"annotations":{},"name":"helloworld-rollout-vsvc","namespace":"istio-injection"},"spec":{"hosts":["spring-boot-helloworld"],"http":[{"name":"primary","route":[{"destination":{"host":"spring-boot-helloworld","subset":"stable"},"weight":100},{"destination":{"host":"spring-boot-helloworld","subset":"canary"},"weight":0}]}]}}
creationTimestamp: "2023-07-18T05:55:21Z"
generation: 3
name: helloworld-rollout-vsvc
namespace: istio-injection
resourceVersion: "1488533"
uid: 63ab1182-6b67-4301-b7ea-002e5822c8fd
spec:
hosts:
- spring-boot-helloworld
http:
- name: primary
route:
- destination:
host: spring-boot-helloworld
subset: stable
weight: 90
- destination:
host: spring-boot-helloworld
subset: canary
weight: 10
#此时旧版的 pod不会被删除、等到全部迁移到v2 结束 旧版开始缩容
[root@xianchaomaster1 08-argo-rollouts]# kubectl get pods -n istio-injection
NAME READY STATUS RESTARTS AGE
rollouts-helloworld-with-traffic-shifting-55dd796d9c-5d6cc 1/1 Running 0 9m36s
rollouts-helloworld-with-traffic-shifting-55dd796d9c-9vkxk 1/1 Running 0 9m36s
rollouts-helloworld-with-traffic-shifting-55dd796d9c-fbk6c 1/1 Running 0 10m
rollouts-helloworld-with-traffic-shifting-55dd796d9c-jbvlb 1/1 Running 0 10m
rollouts-helloworld-with-traffic-shifting-55dd796d9c-jdb42 1/1 Running 0 9m6s
rollouts-helloworld-with-traffic-shifting-55dd796d9c-jhl92 1/1 Running 0 10m
rollouts-helloworld-with-traffic-shifting-55dd796d9c-qplng 1/1 Running 0 13m
rollouts-helloworld-with-traffic-shifting-55dd796d9c-r72wg 1/1 Running 0 9m6s
rollouts-helloworld-with-traffic-shifting-5dc94d6f9d-45rkd 1/1 Running 0 26m
rollouts-helloworld-with-traffic-shifting-5dc94d6f9d-4p9lf 1/1 Running 0 26m
rollouts-helloworld-with-traffic-shifting-5dc94d6f9d-4pj8g 1/1 Running 0 26m
#等到流量全部迁移到 v2
[root@xianchaomaster1 08-argo-rollouts]# kubectl get vs helloworld-rollout-vsvc -o yaml -n istio-injection -w
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"networking.istio.io/v1beta1","kind":"VirtualService","metadata":{"annotations":{},"name":"helloworld-rollout-vsvc","namespace":"istio-injection"},"spec":{"hosts":["spring-boot-helloworld"],"http":[{"name":"primary","route":[{"destination":{"host":"spring-boot-helloworld","subset":"stable"},"weight":100},{"destination":{"host":"spring-boot-helloworld","subset":"canary"},"weight":0}]}]}}
creationTimestamp: "2023-07-18T06:31:11Z"
generation: 9
name: helloworld-rollout-vsvc
namespace: istio-injection
resourceVersion: "1506262"
uid: aa69d2db-daf3-49d1-96d5-577b70fe44ef
spec:
hosts:
- spring-boot-helloworld
http:
- name: primary
route:
- destination:
host: spring-boot-helloworld
subset: stable
weight: 100
- destination:
host: spring-boot-helloworld
subset: canary
weight: 0
#查看pod状态、镜像切换到v0.9.2
[root@xianchaomaster1 08-argo-rollouts]# kubectl get pods -n istio-injection
NAME READY STATUS RESTARTS AGE
rollouts-helloworld-with-traffic-shifting-55dd796d9c-7mtdv 1/1 Running 0 2m45s
rollouts-helloworld-with-traffic-shifting-55dd796d9c-9mbqm 1/1 Running 0 3m15s
rollouts-helloworld-with-traffic-shifting-55dd796d9c-jbgdq 1/1 Running 0 3m45s
rollouts-helloworld-with-traffic-shifting-55dd796d9c-kstl6 1/1 Running 0 2m15s
rollouts-helloworld-with-traffic-shifting-55dd796d9c-mb5sd 1/1 Running 0 5m6s
rollouts-helloworld-with-traffic-shifting-55dd796d9c-td2k5 1/1 Running 0 4m15s
kubectl describe pods -n istio-injection
Image: ikubernetes/spring-boot-helloworld:v0.9.2
#等镜像 全部启动成功后 旧版本 会慢慢的 缩减



浙公网安备 33010602011771号