在进行蓝/绿部署时,应用程序的一个新副本(绿)将与现有版本(蓝)一起部署。然后更新应用程序的入口/路由器以切换到新版本(绿)。然后,您需要等待旧(蓝)版本来完成所有发送给它的请求,但是大多数情况下,应用程序的流量将一次更改为新版本;Kubernetes不支持内置的蓝/绿部署。目前最好的方式是创建新的部署,然后更新应用程序的服务(如service)以指向新的部署;蓝绿部署是不停老版本,部署新版本然后进行测试,确认OK后将流量逐步切到新版本。蓝绿部署无需停机,并且风险较小。
demo-blue1.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: demo-blue1 namespace: default labels: app: demo version: v1 spec: replicas: 1 revisionHistoryLimit: 3 strategy: rollingUpdate: maxSurge: 30% maxUnavailable: 30% selector: matchLabels: app: demo version: v1 template: metadata: labels: app: demo version: v1 spec: containers: - name: demo-blue1 image: opcache/devops-go-sample:hello1 resources: limits: cpu: 5m memory: 10Mi requests: cpu: 5m memory: 10Mi livenessProbe: httpGet: path: / port: 8080 scheme: HTTP initialDelaySeconds: 3 timeoutSeconds: 5 periodSeconds: 30 successThreshold: 1 failureThreshold: 5 readinessProbe: httpGet: path: / port: 8080 scheme: HTTP initialDelaySeconds: 3 timeoutSeconds: 5 periodSeconds: 10 successThreshold: 1 failureThreshold: 5 ports: - name: http containerPort: 8080 protocol: TCP
demo-green2.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: demo-green2 namespace: default labels: app: demo version: v2 spec: replicas: 1 revisionHistoryLimit: 3 strategy: rollingUpdate: maxSurge: 30% maxUnavailable: 30% selector: matchLabels: app: demo version: v2 template: metadata: labels: app: demo version: v2 spec: containers: - name: demo-green2 image: opcache/devops-go-sample:hello2 livenessProbe: httpGet: path: / port: 8080 scheme: HTTP initialDelaySeconds: 3 timeoutSeconds: 5 periodSeconds: 30 successThreshold: 1 failureThreshold: 5 readinessProbe: httpGet: path: / port: 8080 scheme: HTTP initialDelaySeconds: 3 timeoutSeconds: 5 periodSeconds: 10 successThreshold: 1 failureThreshold: 5 ports: - name: http containerPort: 8080 protocol: TCP
Service-v1文件
Service-v1.yaml
apiVersion: v1 kind: Service metadata: name: demo namespace: default labels: app: demo spec: ports: - port: 8080 targetPort: http protocol: TCP name: http selector: app: demo version: v1
Service-v2文件
Service-v2.yaml
apiVersion: v1 kind: Service metadata: name: demo namespace: default labels: app: demo spec: ports: - port: 8080 targetPort: http protocol: TCP name: http selector: app: demo version: v2
执行步骤
kubectl apply -f demo-blue1.yaml kubectl apply -f demo-green2.yaml kubectl apply -f Service-v1.yaml #获取SVC IP kubectl get rc,pods,svc,ingress --all-namespaces -o wide | grep demo
验证请求
for i in `seq 1 100000`; do curl 10.106.49.228:8080/?a=$i ;echo " "; done
kubectl apply -f Service-v2.yaml
这个时候请求切换到了demo-green2
一直循环打印 Hello World2