k8s入门-3

部署扩缩容

kubectl get deploy

复制代码
C:\Users\zhang>kubectl get deploy
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
hello-k8s-deployment   3/3     3            3           5h56m

通过上面的命令,我们先找到一个deploy用来进行测试。现在看他是3个副本,现在我们把他先缩到1个。

kubectl scale deploy hello-k8s-deployment --replicas=1

复制代码
C:\Users\zhang>kubectl scale deploy hello-k8s-deployment --replicas=1
deployment.apps/hello-k8s-deployment scaled

C:\Users\zhang>kubectl get deploy
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
hello-k8s-deployment   1/1     1            1           5h58m

然后再加到5个。

kubectl scale deploy hello-k8s-deployment --replicas=5

复制代码
C:\Users\zhang>kubectl scale deploy hello-k8s-deployment --replicas=5
deployment.apps/hello-k8s-deployment scaled

C:\Users\zhang>kubectl get deploy
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
hello-k8s-deployment   1/5     5            1           6h17m

C:\Users\zhang>kubectl get deploy
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
hello-k8s-deployment   5/5     5            5           6h18m

通过命令可以看出来,它不是马上就建好5个的,而是有一定的时间,逐渐建立起来的。

滚动更新

建一个新版本

docker build -t hello-k8s:1.0 .

复制代码
D:\LEO\project\test3\k8s-test>docker build -t hello-k8s:1.0 .
[+] Building 0.3s (10/10) FINISHED                                                                 docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 204B                                                                               0.0s
 => [internal] load metadata for docker.io/library/python:3.10-slim                                                0.1s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [1/5] FROM docker.io/library/python:3.10-slim@sha256:70f65c721aaddfb22b20ed6ec12606c59d9592493c5fcb6639f3d0e8  0.0s
 => => resolve docker.io/library/python:3.10-slim@sha256:70f65c721aaddfb22b20ed6ec12606c59d9592493c5fcb6639f3d0e8  0.0s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 63B                                                                                   0.0s
 => CACHED [2/5] WORKDIR /app                                                                                      0.0s
 => CACHED [3/5] COPY requirements.txt .                                                                           0.0s
 => CACHED [4/5] RUN pip install --no-cache-dir -r requirements.txt                                                0.0s
 => CACHED [5/5] COPY app.py .                                                                                     0.0s
 => exporting to image                                                                                             0.1s
 => => exporting layers                                                                                            0.0s
 => => exporting manifest sha256:14484ee5ad887573cfd12ac62cbf7eb5547ff03226021a1c4234d5e12b02f89b                  0.0s
 => => exporting config sha256:d69d28bf3d152a2e5b862be56583bac59c13ab81873688aa94be4c2403458c43                    0.0s
 => => exporting attestation manifest sha256:693e5b58682b01e416285b2a47593679eee55578474b79d3e8a6866ea7158774      0.0s
 => => exporting manifest list sha256:f989154c957c2aaba2e37f7abf7ca172b0a54e53f6532dd7ed10ea4d5ac9d43d             0.0s
 => => naming to docker.io/library/hello-k8s:1.0                                                                   0.0s
 => => unpacking to docker.io/library/hello-k8s:1.0                                                                0.0s

更换版本

D:\LEO\project\test3\k8s-test>kubectl set image deploy hello-k8s-deployment hello-k8s=hello-k8s:1.0

deployment.apps/hello-k8s-deployment image updated

D:\LEO\project\test3\k8s-test>kubectl rollout status deploy hello-k8s-deployment

deployment "hello-k8s-deployment" successfully rolled out

查看更换版本的情况可以用下面的方式来确认

复制代码
D:\LEO\project\test3\k8s-test>kubectl rollout history deploy hello-k8s-deployment
deployment.apps/hello-k8s-deployment
REVISION  CHANGE-CAUSE
1         <none>
2         <none>


D:\LEO\project\test3\k8s-test>kubectl rollout history deploy hello-k8s-deployment --revision=2
deployment.apps/hello-k8s-deployment with revision #2
Pod Template:
  Labels:       app=hello-k8s
        pod-template-hash=5dbcc46558
  Containers:
   hello-k8s:
    Image:      hello-k8s:1.0
    Port:       5000/TCP
    Host Port:  0/TCP
    Liveness:   http-get http://:5000/health delay=5s timeout=1s period=10s #success=1 #failure=3
    Readiness:  http-get http://:5000/health delay=5s timeout=1s period=5s #success=1 #failure=3
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>
  Node-Selectors:       <none>
  Tolerations:  <none>


D:\LEO\project\test3\k8s-test>kubectl rollout history deploy hello-k8s-deployment --revision=1
deployment.apps/hello-k8s-deployment with revision #1
Pod Template:
  Labels:       app=hello-k8s
        pod-template-hash=54d8cb895d
  Containers:
   hello-k8s:
    Image:      hello-k8s:latest
    Port:       5000/TCP
    Host Port:  0/TCP
    Liveness:   http-get http://:5000/health delay=5s timeout=1s period=10s #success=1 #failure=3
    Readiness:  http-get http://:5000/health delay=5s timeout=1s period=5s #success=1 #failure=3
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>
  Node-Selectors:       <none>
  Tolerations:  <none>

最后一步,咱们再试一下回退。

kubectl rollout undo deploy hello-k8s-deployment

复制代码
D:\LEO\project\test3\k8s-test>kubectl rollout history deploy hello-k8s-deployment
deployment.apps/hello-k8s-deployment
REVISION  CHANGE-CAUSE
1         <none>
2         <none>


D:\LEO\project\test3\k8s-test>kubectl rollout undo deploy hello-k8s-deployment
deployment.apps/hello-k8s-deployment rolled back

D:\LEO\project\test3\k8s-test>kubectl rollout history deploy hello-k8s-deployment
deployment.apps/hello-k8s-deployment
REVISION  CHANGE-CAUSE
2         <none>
3         <none>

验证成果,还可以通过查看当前的配置文件来完成,这个更好一点。

kubectl get deploy hello-k8s-deployment -o yaml

可以通过输出看到 image: hello-k8s:latest,这就很明显了。说明已经回退到了之前的版本。

完成。下次再研究一下分环境的功能。

相关推荐
实心儿儿38 分钟前
Linux —— 线程控制(1)
linux·运维·服务器
筠筠喵呜喵1 小时前
Linux软件开发性能优化
linux·c++·性能优化
Bruce_kaizy1 小时前
c++ linux环境编程——文件io介绍以及open 、write 、read 三剑客深度详解
linux·服务器·c++·ubuntu·操作系统·文件io
亦良Cool2 小时前
VMware虚拟机ubuntu瘦身,解决虚拟机越用越大
linux·运维·ubuntu
星辰&与海3 小时前
KVM + QEMU虚拟化方案
linux·运维
宋浮檀s3 小时前
应急响应——恶意流量&攻击行为识别
linux·运维·网络·网络安全·应急响应
REDcker3 小时前
Linux OverlayFS详解
java·linux·运维
lwx9148524 小时前
Linux系统中用户锁定后如何解锁
linux·运维·服务器
zhangrelay5 小时前
ROS 2 Lyrical Luth启程-Ubuntu26.04-
linux·笔记·学习·ubuntu
WoY20205 小时前
使用iostat看磁盘IO
linux