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

K8S之常见部署方案

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

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


一、普通部署

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

创建recreate.yaml

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: recreate
spec:
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: recreate
  replicas: 4
  template:
    metadata:
      labels:
        app: recreate
    spec:
      containers:
      - name: recreate
        image: registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test-docker-image:v1.0
        ports:
        - containerPort: 8080
        livenessProbe:
          tcpSocket:
            port: 8080

命令:

bash 复制代码
kubectl apply -f recreate.yaml
kubectl get pods

修改recreate.yaml文件

bash 复制代码
kubectl apply -f recreate.yaml
kubectl get pods

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

bash 复制代码
NAME READY STATUS RESTARTS AGE
recreate-655d4868d8-5dqcz 0/1 Terminating 0 2m31s
recreate-655d4868d8-sb688 0/1 Terminating 0 2m31s

测试:

bash 复制代码
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数量

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rollingupdate
spec:
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  selector:
    matchLabels:
      app: rollingupdate
  replicas: 4
  template:
    metadata:
      labels:
        app: rollingupdate
    spec:
      containers:
      - name: rollingupdate
        image: registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test-docker-image:v1.0
        ports:
        - containerPort: 8080  
---
apiVersion: v1
kind: Service
metadata:
  name: rollingupdate
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: rollingupdate
  type: ClusterIP

命令:

bash 复制代码
kubectl apply -f rollingupdate.yaml
kubectl get pods
kubectl get svc
curl cluster-ip/dockerfile

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

bash 复制代码
# 在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是会共存的,并且可以访问测试看一下

bash 复制代码
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

yaml 复制代码
#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: blue
spec:
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  selector:
    matchLabels:
      app: bluegreen
  replicas: 4
  template:
    metadata:
      labels:
        app: bluegreen
        version: v1.0
    spec:
      containers:
      - name: bluegreen
        image: registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test-docker-image:v1.0
        ports:
        - containerPort: 8080

命令:

bash 复制代码
kubectl apply -f bluegreen.yaml
kubectl get pods

创建bluegreen-service.yaml

yaml 复制代码
apiVersion: v1
kind: Service
metadata:
  name: bluegreen
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: bluegreen
    version: v1.0
  type: ClusterIP

命令:

bash 复制代码
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

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

修改bluegreen-service.yaml

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

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

修改bluegreen-service.yaml

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

修改后:

yaml 复制代码
apiVersion: v1
kind: Service
metadata:
  name: bluegreen
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: bluegreen
    #version: v1.0
  type: ClusterIP

命令:

bash 复制代码
kubectl apply -f bluegreen-service.yaml

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

相关推荐
Xiaofeng36933 分钟前
GPT-5.6 发布后,我用了一个周末重新规划学习路线图
gpt·学习
努力进修1 小时前
Docker+cpolar 实战:打造灵活可控的远程办公系统
运维·docker·容器
其实防守也摸鱼1 小时前
运维--怎么看接口的请求和返回
运维·学习·网络安全·网络攻击模型·burpsuite·攻防对抗·蓝队
酷讯网络_2408701601 小时前
区块粮仓宠物NFT源码区块狗/抢购转让预约区块投资理财系统
学习·开源·宠物
Generalzy2 小时前
从本地 Demo 到生产级检索:Milvus 学习笔记(3)
笔记·学习·milvus
心中有国也有家3 小时前
AtomGit Flutter 鸿蒙客户端:ModalBottomSheet 实战
android·javascript·学习·flutter·华为·harmonyos
tyqtyq223 小时前
求职信生成:AI 智能求职信撰写系统的鸿蒙实现
人工智能·学习·华为·生活·harmonyos
MartinYeung53 小时前
[论文学习]PrivacyLens:评估语言模型在行动中的隐私规范意识
人工智能·学习·语言模型
jieyucx4 小时前
Docker 入门第一阶段:建立正确认知与初体验
运维·docker·容器
凉、介4 小时前
Virtio 系列(一):框架概览
笔记·学习·嵌入式·虚拟化·virtio