k8s学习(六)Deployment的使用
目录前言一、chang二、使用步骤1.引入库2.读入数据总结前言Deployment 为 Pod 和 ReplicaSet 提供了一个声明式定义。一、chang二、使用步骤1.引入库代码如下(示例):import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsimport warn
前言
Deployment 为 Pod 和 ReplicaSet 提供了一个声明式定义。
一、创建
(1)nginx-deployment.yaml
[root@k8s-master k8s]# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx-deployment-test
template:
metadata:
labels:
app: nginx-deployment-test
spec:
containers:
- name: nginx-deployment-test
image: nginx:1.20
ports:
- containerPort: 80
(2)查询 deployment
[root@k8s-master k8s]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 45s
NAME 列出了集群中 Deployment 的名称。
READY 显示应用程序的可用的 副本 数。显示的模式是“就绪个数/期望个数”。
UP-TO-DATE 显示为了达到期望状态已经更新的副本数。
AVAILABLE 显示应用可供用户使用的副本数。
AGE 显示应用程序运行的时间。
(3)查看状态
[root@k8s-master k8s]# kubectl rollout status deployment/nginx-deployment
deployment "nginx-deployment" successfully rolled out
(4)查询ReplicaSet
[root@k8s-master k8s]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-5bf86c7b68 3 3 3 51s
NAME 列出名字空间中 ReplicaSet 的名称;
DESIRED 显示应用的期望副本个数,即在创建 Deployment 时所定义的值。 此为期望状态;
CURRENT 显示当前运行状态中的副本个数;
READY 显示应用中有多少副本可以为用户提供服务;
AGE 显示应用已经运行的时间长度。
(5)查询pod
[root@k8s-master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5bf86c7b68-hl7rr 1/1 Running 0 56s
nginx-deployment-5bf86c7b68-l8pvb 1/1 Running 0 56s
nginx-deployment-5bf86c7b68-wrnff 1/1 Running 0 56s
二、扩容
(1)scale
[root@k8s-master k8s]# kubectl scale deployment nginx-deployment --replicas 5
deployment.apps/nginx-deployment scaled
[root@k8s-master k8s]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 5/5 5 5 14m
(2)autoscale
[root@k8s-master k8s]# kubectl autoscale deployment nginx-deployment --min=6 --max=10 --cpu-percent=80
horizontalpodautoscaler.autoscaling/nginx-deployment autoscaled
[root@k8s-master k8s]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 5/6 6 5 16m
[root@k8s-master k8s]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 6/6 6 6 16m
三、更新、回退
1、从1.20 更新至 1.20.1
[root@k8s-master k8s]# kubectl set image deployment/nginx-deployment nginx-deployment-test=nginx:1.20.1
deployment.apps/nginx-deployment image updated
创建了新的ReplicaSet
[root@k8s-master k8s]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-5878549b9c 6 6 6 43s
nginx-deployment-5bf86c7b68 0 0 0 22m
2、 kubectl describe deployments 查看deployment详细信息
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 27m deployment-controller Scaled up replica set nginx-deployment-5bf86c7b68 to 3
Normal ScalingReplicaSet 13m deployment-controller Scaled up replica set nginx-deployment-5bf86c7b68 to 5
Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set nginx-deployment-5bf86c7b68 to 6
Normal ScalingReplicaSet 5m27s deployment-controller Scaled up replica set nginx-deployment-5878549b9c to 2
Normal ScalingReplicaSet 5m27s deployment-controller Scaled down replica set nginx-deployment-5bf86c7b68 to 5
Normal ScalingReplicaSet 5m27s deployment-controller Scaled up replica set nginx-deployment-5878549b9c to 3
Normal ScalingReplicaSet 5m6s deployment-controller Scaled down replica set nginx-deployment-5bf86c7b68 to 4
Normal ScalingReplicaSet 5m6s deployment-controller Scaled up replica set nginx-deployment-5878549b9c to 4
Normal ScalingReplicaSet 5m4s deployment-controller Scaled down replica set nginx-deployment-5bf86c7b68 to 3
Normal ScalingReplicaSet 5m4s deployment-controller Scaled up replica set nginx-deployment-5878549b9c to 5
Normal ScalingReplicaSet 5m (x4 over 5m2s) deployment-controller (combined from similar events): Scaled down replica set nginx-deployment-5bf86c7b68 to 0
3、从1.20.1 更新至 1.20.2
[root@k8s-master k8s]# kubectl set image deployment/nginx-deployment nginx-deployment-test=nginx:1.20.2
deployment.apps/nginx-deployment image updated
4、查看升级历史记录
[root@k8s-master k8s]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
5、查看第2个版本
[root@k8s-master k8s]# kubectl rollout history deployment/nginx-deployment --revision=2
deployment.apps/nginx-deployment with revision #2
Pod Template:
Labels: app=nginx-deployment-test
pod-template-hash=5878549b9c
Containers:
nginx-deployment-test:
Image: nginx:1.20.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
6、回退到第一个版本
[root@k8s-master k8s]# kubectl rollout undo deployment/nginx-deployment
deployment.apps/nginx-deployment rolled back
查看当前版本
[root@k8s-master k8s]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
[root@k8s-master k8s]# kubectl rollout history deployment/nginx-deployment --revision=4
deployment.apps/nginx-deployment with revision #4
Pod Template:
Labels: app=nginx-deployment-test
pod-template-hash=5878549b9c
Containers:
nginx-deployment-test:
Image: nginx:1.20.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
7、回退到指定版本
[root@k8s-master k8s]# kubectl rollout undo deployment/nginx-deployment --to-revision=1
deployment.apps/nginx-deployment rolled back
查看当前版本
[root@k8s-master k8s]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
3 <none>
4 <none>
5 <none>
[root@k8s-master k8s]# kubectl rollout history deployment/nginx-deployment --revision=5
deployment.apps/nginx-deployment with revision #5
Pod Template:
Labels: app=nginx-deployment-test
pod-template-hash=5bf86c7b68
Containers:
nginx-deployment-test:
Image: nginx:1.20
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
四、暂停、恢复
1、暂停
[root@k8s-master k8s]# kubectl rollout pause deployment/nginx-deployment
deployment.apps/nginx-deployment paused
2、恢复
kubectl rollout resume deploy nginx-deployment
五、状态
1、progressing
Deployment 正在创建新的 ReplicaSet 过程中;
Deployment 正在扩容一个已有的 ReplicaSet;
Deployment 正在缩容一个已有的 ReplicaSet;
有新的可用的 pod 出现。
2、Complete
Deployment 最小可用。最小可用意味着 Deployment 的可用 replica 个数等于或者 超过 Deployment 策略中的期望个数。
所有与该 Deployment 相关的 replica 都被更新到了你指定版本,也就说更新完成。
该 Deployment 中没有旧的 Pod 存在。
3、Failed
这可能是因为以下几个因素引起的:
无效的引用
不可读的 probe failure
镜像拉取错误
权限不够
范围限制
程序运行时配置错误
更多推荐
所有评论(0)