当前位置: 首页 > news >正文

kubernetes(K8S)学习(八):K8S之常见部署方案

K8S之常见部署方案

  • 一、普通部署
  • 二、滚动更新(Rolling update)
  • 三、蓝绿部署(Blue/Green Deployment)
  • 四、灰度发布(金丝雀发布)

常见的部署方案参考博文:常见部署方案:普通部署、滚动部署、蓝绿部署、灰度发布(金丝雀发布)


一、普通部署

特点: 先停止旧的pod,然后再创建新的pod,这个过程服务是会间断的。

创建recreate.yaml

apiVersion: apps/v1
kind: Deployment
metadata:name: recreate
spec:strategy:type: Recreateselector:matchLabels:app: recreatereplicas: 4template:metadata:labels:app: recreatespec:containers:- name: recreateimage: registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test-docker-image:v1.0ports:- containerPort: 8080livenessProbe:tcpSocket:port: 8080

命令:

kubectl apply -f recreate.yaml
kubectl get pods

修改recreate.yaml文件

kubectl apply -f recreate.yaml
kubectl get pods

conclusion :发现pod是先停止,然后再创建新的。

NAME READY STATUS RESTARTS AGE
recreate-655d4868d8-5dqcz 0/1 Terminating 0 2m31s
recreate-655d4868d8-sb688 0/1 Terminating 0 2m31s

测试:

kubectl rollout pause deploy rollingupdate
kubectl rollout resume deploy rollingupdate
kubectl rollout undo deploy rollingupdate # 回到上一个版本

二、滚动更新(Rolling update)

服务不会停止,但是整个pod会有新旧并存的情况。

创建rollingupdate.yaml

maxSurge :滚动升级时先启动的pod数量
maxUnavailable :滚动升级时允许的最大unavailable的pod数量

apiVersion: apps/v1
kind: Deployment
metadata:name: rollingupdate
spec:strategy:rollingUpdate:maxSurge: 25%maxUnavailable: 25%type: RollingUpdateselector:matchLabels:app: rollingupdatereplicas: 4template:metadata:labels:app: rollingupdatespec:containers:- name: rollingupdateimage: registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test-docker-image:v1.0ports:- containerPort: 8080  
---
apiVersion: v1
kind: Service
metadata:name: rollingupdate
spec:ports:- port: 80protocol: TCPtargetPort: 8080selector:app: rollingupdatetype: ClusterIP

命令:

kubectl apply -f rollingupdate.yaml
kubectl get pods
kubectl get svc
curl cluster-ip/dockerfile

修改rollingupdate.yaml文件,将镜像修改成v2.0

# 在w1上,不断地访问观察输出
while sleep 0.2;do curl cluster-ip/dockerfile;echo "";done
# 在w2上,监控pod
kubectl get pods -w
# 使得更改生效
kubectl apply -f rollingupdate.yaml
kubectl get pods

conclusion :发现新旧pod是会共存的,并且可以访问测试看一下

kubectl get pods -w
kubectl get svc

可以发现,新老版本的确会共存。


三、蓝绿部署(Blue/Green Deployment)

无需停机,风险较小

  • (1)部署v1的应用(一开始的状态)
    所有外部请求的流量都打到这个版本上
  • (2)部署版本2的应用
    版本2的代码与版本1不同(新功能、Bug修复等).
  • (3)将流量从版本1切换到版本2。
  • (4)如版本2测试正常,就删除版本1正在使用的资源(例如实例),从此正式用版本2

创建bluegreen.yaml

#deploy
apiVersion: apps/v1
kind: Deployment
metadata:name: blue
spec:strategy:rollingUpdate:maxSurge: 25%maxUnavailable: 25%type: RollingUpdateselector:matchLabels:app: bluegreenreplicas: 4template:metadata:labels:app: bluegreenversion: v1.0spec:containers:- name: bluegreenimage: registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test-docker-image:v1.0ports:- containerPort: 8080

命令:

kubectl apply -f bluegreen.yaml
kubectl get pods

创建bluegreen-service.yaml

apiVersion: v1
kind: Service
metadata:name: bluegreen
spec:ports:- port: 80protocol: TCPtargetPort: 8080selector:app: bluegreenversion: v1.0type: ClusterIP

命令:

kubectl apply -f bluegreen-service.yaml
kubectl get svc
# 在w1上不断访问观察
while sleep 0.3;do curl cluster-ip/dockerfile;echo "";done

修改bluegreen.yaml

01-deployment-name:blue —> green
02-image:v1.0—> v2.0
03-version:v1.0 —> v2.0

kubectl apply -f bluegreen.yaml
kubectl get pods
# 同时观察刚才访问的地址有没有变化
# 可以发现,两个版本就共存了,并且之前访问的地址没有变化

修改bluegreen-service.yaml

# 也就是把流量切到2.0的版本中
selector:app: bluegreenversion: v2.0
kubectl apply -f bluegreen-service.yaml
kubectl get svc
# 同时观察刚才访问的地址有没有变化
# 发现流量已经完全切到了v2.0的版本上

四、灰度发布(金丝雀发布)

修改bluegreen-service.yaml

selector:
app: bluegreen
version: v2.0 # 把version删除掉,只是根据bluegreen进行选择

修改后:

apiVersion: v1
kind: Service
metadata:name: bluegreen
spec:ports:- port: 80protocol: TCPtargetPort: 8080selector:app: bluegreen#version: v1.0type: ClusterIP

命令:

kubectl apply -f bluegreen-service.yaml

同时观察刚才访问的地址有没有变化,发现此时新旧版本能够同时被访问到。

http://www.lryc.cn/news/328313.html

相关文章:

  • 《AIGC重塑金融:AI大模型驱动的金融变革与实践》
  • 【详解】运算放大器工作原理及其在信号处理中的核心作用
  • 银河麒麟V10:sudo: /usr/bin/sudo 必须属于用户 ID 0(的用户)并且设置 setuid 位
  • Android 多层级列表实现
  • 柔数组的介绍
  • 跳槽多次未成功,问题源自何处?
  • Linux 操作系统 022-串口/U盘/共享文件夹
  • java题目9:100匹马驮100担货,大马一匹驮3担,中马一匹驮2担,小马两匹驮1担。计算大中小马的数目(HorsesPackGoods9)
  • 操作系统OS Chapter1
  • UE4_Mouse_Interaction——拖拽物体的实现
  • Tomcat配置https
  • Modelsim手动仿真实例
  • AXI-Stream——草稿版
  • 【编码器应用】第一节-编码器从从原理到应用详解
  • 瑞_23种设计模式_中介者模式
  • sqlite删除数据表
  • Spring Boot简介及案例
  • Learning To Count Everything
  • 大语言模型(LLM)token解读
  • 【Micro 2014】NoC Architectures for Silicon Interposer Systems
  • 《极客时间 - 左耳听风》01 | 程序员如何用技术变现?(上)【文章笔记 + 个人思考】
  • Typora结合PicGo + Github搭建个人图床
  • 【JavaWeb】Day27.Web入门——Tomcat介绍
  • 怎么更新sd-webui AUTOMATIC1111/stable-diffusion-webui ?
  • Apache Iceberg最新最全面试题及详细参考答案(持续更新)
  • 从TCP/IP协议到socket编程详解
  • uniapp开发小程序遇到的问题,持续更新中
  • C++经典面试题目(十一)
  • 设计模式(6):桥接模式
  • Java切面编程