本系列文章目錄
- (一)基礎k8s yaml腳本釋出
- (二)helm+shell腳本優化大量備援配置釋出
- (三)jenkins使用者稽核的流水化方式部署
- (四)service mesh(istio)服務網格化釋出
- (五)istio對項目進行金絲雀部署
其他相關文章
- spring-cloud-kubernetes之開發環境搭建
- spring cloud項目改造為spring-cloud-kubernetes項目
- 使用istio對spring cloud kubernetes項目進行金絲雀釋出
前言
上一篇文章《spring cloud項目改造為spring-cloud-kubernetes項目》以開源的spring-boot-cloud項目為例分享了如何将一個傳統的spring cloud項目換成spring cloud kubernetes項目,同時還示範了改造後中的Hystrix熔斷、本地服務調用本地服務、fabric8插件一鍵部署等功能。
這一篇為是對之前的文章《采用rancher2+kubernetes+skywalking部署springcloud項目(五[istio藍綠部署]-錯誤示範)》的一個後續,上次因為項目全采用的spring cloud原生元件來實作的微服務,但将項目部署到istio時因為原來spring cloud元件與istio中部分元件的沖突導緻沒能将藍綠部署示範成功。但目前原來的spring-boot-cloud項目中的元件已經用spring cloud kubernetes替換掉了,是以這裡決定再次進行嘗試用istio來示範如何進行金絲雀/灰階釋出
對于istio的環境的搭建直接參考之前的文章即可
部署改造後的spring-boot-cloud項目
這次僅部署config、gateway、svca-service、svcb-service,通路gateway項目會調用svca-service,svca-service又會調用svcb-service
其部署檔案如下:
#-------------config-----------------
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: config
spec:
replicas: 1
selector:
matchLabels:
app: config
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: config
spec:
containers:
- image: ccr.ccs.tencentyun.com/spring-boot-cloud/config:v3
imagePullPolicy: Always
name: config
ports:
- containerPort: 30876
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: config
spec:
ports:
- name: http
port: 30876
protocol: TCP
targetPort: 30876
selector:
app: config
#-------------svca-service-----------------
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: svca-service
spec:
replicas: 1
selector:
matchLabels:
app: svca-service
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: svca-service
spec:
containers:
- image: ccr.ccs.tencentyun.com/spring-boot-cloud/svca-service:v3
imagePullPolicy: Always
name: svca-service
ports:
- containerPort: 8080
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: svca-service
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: svca-service
#-------------svcb-service-----------------
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: svcb-service
spec:
replicas: 1
selector:
matchLabels:
app: svcb-service
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: svcb-service
spec:
containers:
- image: ccr.ccs.tencentyun.com/spring-boot-cloud/svcb-service:v3
imagePullPolicy: Always
name: svcb-service
ports:
- containerPort: 8070
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: svcb-service
spec:
ports:
- name: http
port: 8070
protocol: TCP
targetPort: 8070
selector:
app: svcb-service
#-------------gateway-----------------
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: gateway
spec:
replicas: 1
selector:
matchLabels:
app: gateway
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: gateway
spec:
containers:
- image: ccr.ccs.tencentyun.com/spring-boot-cloud/gateway:v3
imagePullPolicy: Always
name: gateway
ports:
- containerPort: 8060
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: gateway
spec:
ports:
- name: http
port: 8060
protocol: TCP
targetPort: 8060
selector:
app: gateway
---
如果啟動時釋出有權限不足的話,記得給serviceaccount權重限,要不然可能會報下面這樣的錯誤
具體的操作方式參考上一篇文章中k8s rbac部分
部署好了項目之後,接着添加virtualservice和gateway,以通路gateway項目
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: gateway-gateway
namespace: istio-system
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "gateway.springcloud.com"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: gateway
spec:
hosts:
- "gateway.springcloud.com"
gateways:
- istio-system/gateway-gateway #can omit the namespace if gateway is in same namespace as virtual service
http:
- route:
- destination:
host: gateway
port:
number: 8060
---
然後再通路測試一下:
由于上面gateway中配置的是gateway.springcloud.com這上域名,是以需要在通路的機器上配一下host
使用istio釋出注意點
在使用istio部署spring cloud kubernetes項目前記得将kubernetes的ribbon模式修改為service模式,預設是pod模式。
如果是pod模式的話,istio中對于virtualservice的配置是不會生效的
spring:
cloud:
kubernetes:
ribbon:
mode: service
svcb-service藍綠部署
為了示範友善,這裡對svcb-service進行藍綠部署,準備好2個版本分别為v3和v5,不同的版本會輸出不同的内容
其svcb-service的deployment配置檔案如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: svcb-service-v3
spec:
replicas: 1
selector:
matchLabels:
app: svcb-service
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: svcb-service
version: v3
spec:
containers:
- image: ccr.ccs.tencentyun.com/spring-boot-cloud/svcb-service:v3
imagePullPolicy: Always
name: svca-service
ports:
- containerPort: 8070
protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: svcb-service-v5
spec:
replicas: 1
selector:
matchLabels:
app: svcb-service
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: svcb-service
version: v5
spec:
containers:
- image: ccr.ccs.tencentyun.com/spring-boot-cloud/svcb-service:v5
imagePullPolicy: Always
name: svcb-service
ports:
- containerPort: 8070
protocol: TCP
執行後測試一下,會出現一會兒輸出V3一會兒輸出V5的情況
通路截圖如下:
通過觀察請求結果我們可以看到,目前因為有兩個版本的svcb-service,是以通路時也會輪詢v3和v5兩個版本的服務。其kiali監控圖如下:
接下來加virtualservice,來讓所有對于svcb-service的請求都發到v5版本上去
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: svcb-service
spec:
hosts:
- svcb-service
http:
- route:
- destination:
host: svcb-service
subset: v5
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: svcb-service
spec:
host: svcb-service
subsets:
- name: v3
labels:
version: v3
- name: v5
labels:
version: v5
上面的操作添加了一個VirtualService并綁定了一個DestinationRule,其VirtualService中讓所有的svcb-service請求都轉發到了labels為v5的pod中
此時再來測試一下效果
從圖檔可以看出,所有的請求全發到了v5版本的svcb-service中了
金絲雀/灰階釋出示範
感覺所謂的金絲雀部署就是按照比例來進行部署,有了藍綠部署金絲雀部署就簡單了,直接來個配置檔案修改virtualservice的配置就行了:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: svcb-service
spec:
hosts:
- svcb-service
http:
- route:
- destination:
host: svcb-service
subset: v3
weight: 90
- destination:
host: svcb-service
subset: v5
weight: 10
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: svcb-service
spec:
host: svcb-service
subsets:
- name: v3
labels:
version: v3
- name: v5
labels:
version: v5
示範一下:
從結果中可以看出,在加上金絲雀釋出後,根據請求的比例差不多10%的請求被轉發到了V5版本中,其餘的請求轉發到了V3版本中
最後想說
在體驗了spring cloud kubernetes+istio之後最後想說,service mesh果然名不虛傳