本系列文章目錄
(一)基礎k8s yaml腳本發佈
(二)helm+shell腳本優化大量冗餘配置發佈
(三)jenkins用戶審覈的流水化方式部署
(四)service mesh(istio)服務網格化發佈
(五)istio對項目進行金絲雀部署(待完成)
在有了上一篇採用rancher2+kubernetes+skywalking部署springcloud項目(四[istio服務網格化版本])的實戰後,對istio有了一個初步簡單的認識。
這一次我決定來把spring-boot-cloud項目中的svca-service做個藍綠髮布的測試
此部分直接參考的istio官方的BookInfo示例
創建兩個版本的deployment
#-------------svca-service-----------------
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: svca-service-v1
spec:
replicas: 1
selector:
matchLabels:
app: svca-service
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: svca-service
version: v1
spec:
initContainers:
- name: init-skywalking-agent
image: ccr.ccs.tencentyun.com/haiyang/skywalking-agent:7.0.0
command:
- 'sh'
- '-c'
- 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
volumeMounts:
- mountPath: /vmskywalking/agent
name: skywalking-agent
containers:
- image: ccr.ccs.tencentyun.com/spring-boot-cloud/svca-service:latest
imagePullPolicy: Always
name: svca-service
ports:
- containerPort: 8080
protocol: TCP
volumeMounts:
- mountPath: /opt/skywalking/agent
name: skywalking-agent
volumes:
- name: skywalking-agent
emptyDir: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: svca-service-v2
spec:
replicas: 1
selector:
matchLabels:
app: svca-service
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: svca-service
version: v2
spec:
initContainers:
- name: init-skywalking-agent
image: ccr.ccs.tencentyun.com/haiyang/skywalking-agent:7.0.0
command:
- 'sh'
- '-c'
- 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
volumeMounts:
- mountPath: /vmskywalking/agent
name: skywalking-agent
containers:
- image: ccr.ccs.tencentyun.com/spring-boot-cloud/svca-service:v2
imagePullPolicy: Always
name: svca-service
ports:
- containerPort: 8080
protocol: TCP
volumeMounts:
- mountPath: /opt/skywalking/agent
name: skywalking-agent
volumes:
- name: skywalking-agent
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: svca-service
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: svca-service
上面的yaml定義了兩個版本的svca-service
測試正常響應
通過postman測試一下接口:
通過上面的postman請求,我們可以看出:同一個url的請求在多次訪問下會出現不同的結果,因爲svca-service的v1和v2版本中加入了**[][][v2][][]**這個字符串以做版本區分
其訪問的kiali監控面板如下:
從上圖可以看出對於一個發向svca-service的請求會最終發向svca-servcie的兩個版本,也就是上圖中的v1和v2。所以也就會出現postman裏的情況,每次請求同一個url其結果不一樣,因爲有負載均衡服務在輪詢不同的服務
藍綠部署
此演示沒有成功,可忽略
接下來演示藍綠部署,將所有的服務的請求全執行到v2版本
添加一個DestinationRule和VirtualService
DestinationRule介紹:https://istio.io/latest/zh/docs/reference/config/networking/destination-rule/
VirtualService介紹:https://istio.io/latest/zh/docs/reference/config/networking/virtual-service/
然後執行下面的yaml:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: svca-service
spec:
hosts:
- svca-service
http:
- route:
- destination:
host: svca-service
subset: v2
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: svca-service
spec:
host: svca-service
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
執行完畢後,再進行測試
然後………………
在postman裏觀察請求是否全到了v2版本的svca-service,結果………………結果很尷尬!
與預期結果不一樣,請求仍然是輪詢的,這裏就不貼圖了。
分析原因
之所以在添加了VirtualService和DestinationRule的配置後仍然沒有生效是因爲這個項目是基於spring cloud的,訪問服務時是通過spring cloud的ribbon框架來進行負載的,如果想通過istio來實現各種部署最快的辦法是通過spring-cloud-kubernetes來實現