无状态服务Deployment
- 无状态服务Deployment概念
- 1、Deployment概念
- 2、Deployment的更新
- 3、Deploymen的回滚
- 4、Deploymen的扩容和缩容
- 5、Deploymen更新-暂停和恢复
- 6、Deploymen更新注意事项
无状态服务Deployment概念
1、Deployment概念
用于部署无状态的服务,这最常用的控制器。一般用于管理维护企业内部无状态的微服务,比如configserver、
zull、springboot。他可以管理多个副本的pod实现无缝迁移、自动扩容缩容、自动灾难恢复、一键回滚等功能。
- 手动创建一个无状态的nginx
kubectl create deployment nginx --image=nginx:1.21.6
kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 94s
kubectl get deployment nginx -o yaml > nginx-deplo.yaml
- 修改副本数 replicas 改为2 replicas: 2
vim nginx-deplo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
/revision: "1"
creationTimestamp: "2022-05-11T14:26:38Z"
generation: 1
labels:
app: nginx
name: nginx
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.2
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
#重新发布
kubectl replace -f nginx-deplo.yaml
查看po
[root@k8s-master ~]# kubectl get po -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-77fcc495b8-f4l7g 1/1 Running 0 6m15s 192.161.125.5 k8s-node01 <none> <none>
nginx-77fcc495b8-ptvb9 1/1 Running 0 10m 192.169.214.196 k8s-node03 <none> <none>
- 用edit 管理 将replicas 改为 1
kubectl edit deploy nginx
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
/revision: "1"
creationTimestamp: "2022-05-12T03:13:17Z"
generation: 2
labels: # Deployment本身的labels
app: nginx
name: nginx
namespace: default
resourceVersion: "25266"
uid: 0cd92e48-3e13-4cc5-bde3-e625b100621f
spec:
progressDeadlineSeconds: 600
replicas: 1 #副本数
revisionHistoryLimit: 10 # 历史记录保留的个数
selector:
matchLabels:
app: nginx #与下面pod的labels必须保持一致,不然管理不了pod,匹配rs,新版本创建之后不允许修改,修改之后产生新的rs,无法对应旧的label
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template: # pod的参数
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.21.6
imagePullPolicy: IfNotPresent
uid: 0cd92e48-3e13-4cc5-bde3-e625b100621f
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.21.6
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
kubectl get po -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-77fcc495b8-f4l7g 1/1 Running 0 11m 192.161.125.5 k8s-node01 <none> <none>
- 查看deploy的labels
[root@k8s-master ~]# kubectl get deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx 1/1 1 1 26m app=nginx
- 状态解析
[root@k8s-master ~]# kubectl get deploy -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 2/2 2 2 35m nginx nginx:1.21.6 app=nginx
- NAME: Deployment名称
- READY:Pod的状态,已经Ready的个数
- UP-TO-DATE:已经达到期望状态的被更新的副本数
- AVAILABLE:已经可以用的副本数
- AGE:显示应用程序运行的时间
- CONTAINERS:容器名称
- IMAGES:容器的镜像
- SELECTOR:管理的Pod的标签
- 查看pod的标签
[root@k8s-master ~]# kubectl get po --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-77fcc495b8-79g7b 1/1 Running 0 12m app=nginx,pod-template-hash=77fcc495b8
nginx-77fcc495b8-f4l7g 1/1 Running 0 38m app=nginx,pod-template-hash=77fcc495b8
[root@k8s-master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-77fcc495b8 2 2 2 43m
2、Deployment的更新
- 修改spec里面的template才会触发更新
- 查看镜像版本
[root@k8s-master ~]# kubectl get deploy -oyaml | grep image
- image: nginx:1.21.6
imagePullPolicy: IfNotPresent
- 更改deployment的镜像并记录
[root@k8s-master ~]# kubectl set image deploy nginx nginx=nginx:1.20.2 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx image updated
#查看po状态
[root@k8s-master ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-5777589579-pt9x5 0/1 ContainerCreating 0 16s
nginx-5777589579-sr25f 1/1 Running 0 18s
nginx-77fcc495b8-f4l7g 1/1 Running 0 51m
- 查看滚动过程
[root@k8s-master ~]# kubectl rollout status deploy nginx
deployment "nginx" successfully rolled out
- 或使用describe 查看 查看滚动过程
[root@k8s-master ~]# kubectl describe deploy nginx
Name: nginx
Namespace: default
CreationTimestamp: Thu, 12 May 2022 11:13:17 +0800
Labels: app=nginx
Annotations: /revision: 3
kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.20.1 --record=true
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.20.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-9dfb46bc4 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 44m (x2 over 70m) deployment-controller Scaled up replica set nginx-77fcc495b8 to 2
Normal ScalingReplicaSet 19m deployment-controller Scaled up replica set nginx-5777589579 to 1
Normal ScalingReplicaSet 19m (x2 over 59m) deployment-controller Scaled down replica set nginx-77fcc495b8 to 1
Normal ScalingReplicaSet 19m deployment-controller Scaled up replica set nginx-5777589579 to 2
Normal ScalingReplicaSet 18m deployment-controller Scaled down replica set nginx-77fcc495b8 to 0
Normal ScalingReplicaSet 2m51s deployment-controller Scaled up replica set nginx-9dfb46bc4 to 1
Normal ScalingReplicaSet 118s deployment-controller Scaled down replica set nginx-5777589579 to 1
Normal ScalingReplicaSet 118s deployment-controller Scaled up replica set nginx-9dfb46bc4 to 2
Normal ScalingReplicaSet 69s deployment-controller Scaled down replica set nginx-5777589579 to 0
- 查看更新后的配置
[root@k8s-master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-5777589579 2 2 2 5m18s
nginx-77fcc495b8 0 0 0 60m
[root@k8s-master ~]# kubectl get po --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-5777589579-pt9x5 1/1 Running 0 6m8s app=nginx,pod-template-hash=5777589579
nginx-5777589579-sr25f 1/1 Running 0 6m10s app=nginx,pod-template-hash=5777589579
[root@k8s-master ~]# kubectl get po nginx-5777589579-pt9x5 -oyaml | grep image
- image: nginx:1.20.2
imagePullPolicy: IfNotPresent
image: nginx:1.20.2
imageID: docker-pullable://nginx@sha256:dc43c7f22a8310cac31cb27d7a3f63c5db1ee5eb0ac7944bc89a52dbd08b6d3c
查看之前 rs 配置
[root@k8s-master ~]# kubectl get rs nginx-77fcc495b8 -oyaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
annotations:
/desired-replicas: "2"
/max-replicas: "3"
/revision: "1"
creationTimestamp: "2022-05-12T03:13:17Z"
generation: 6
labels:
app: nginx
pod-template-hash: 77fcc495b8
name: nginx-77fcc495b8
namespace: default
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: Deployment
name: nginx
uid: 0cd92e48-3e13-4cc5-bde3-e625b100621f
resourceVersion: "31102"
uid: f3efa00a-1e05-4ccf-b0a4-9745a2eb7cb8
spec:
replicas: 0
selector:
matchLabels:
app: nginx
pod-template-hash: 77fcc495b8
template:
metadata:
creationTimestamp: null
labels:
app: nginx
pod-template-hash: 77fcc495b8
spec:
containers:
- image: nginx:1.21.6
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
observedGeneration: 6
replicas: 0
- 滚动更新的策略是:先启动一个新的rs,将副本数设置为1,再把旧的删掉一个,然后再启动一个新的
- 查看滚动更新策略配置
[root@k8s-master ~]# vim nginx-deploy.yaml
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
3、Deploymen的回滚
更新deploy一个错误的镜像
[root@k8s-master ~]# kubectl set image deploy nginx nginx=nginx:778899 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx image updated
[root@k8s-master ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-67ddfd77d-5hvsp 0/1 ErrImagePull 0 6m2s
nginx-9dfb46bc4-27fwx 1/1 Running 0 53m
nginx-9dfb46bc4-l44s4 1/1 Running 0 54m
#查看历史更新
[root@k8s-master ~]# kubectl rollout history deploy nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deploy nginx nginx=nginx:1.20.2 --record=true
3 kubectl set image deploy nginx nginx=nginx:1.20.1 --record=true
4 kubectl set image deploy nginx nginx=nginx:778899 --record=true
- 回滚到上一个版本
[root@k8s-master ~]# kubectl rollout undo deploy nginx
deployment.apps/nginx rolled back
[root@k8s-master ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-9dfb46bc4-27fwx 1/1 Running 0 60m
nginx-9dfb46bc4-l44s4 1/1 Running 0 61m
#查看当前 deploy nginx images 信息
[root@k8s-master ~]# kubectl get deploy nginx -oyaml | grep image
kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.20.1
- image: nginx:1.20.1
imagePullPolicy: IfNotPresent
- 多次更新
[root@k8s-master ~]# kubectl set image deploy nginx nginx=nginx:787977da --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx image updated
[root@k8s-master ~]# kubectl set image deploy nginx nginx=nginx:787977da1 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx image updated
[root@k8s-master ~]# kubectl set image deploy nginx nginx=nginx:787977da12 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx image updated
[root@k8s-master ~]# kubectl set image deploy nginx nginx=nginx:787977da123 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx image updated
[root@k8s-master ~]# kubectl rollout history deploy nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deploy nginx nginx=nginx:1.20.2 --record=true
4 kubectl set image deploy nginx nginx=nginx:778899 --record=true
5 kubectl set image deploy nginx nginx=nginx:1.20.1 --record=true
6 kubectl set image deploy nginx nginx=nginx:787977da --record=true
7 kubectl set image deploy nginx nginx=nginx:787977da1 --record=true
8 kubectl set image deploy nginx nginx=nginx:787977da12 --record=true
9 kubectl set image deploy nginx nginx=nginx:787977da123 --record=true
- 查看指定版本的详细信息
[root@k8s-master ~]# kubectl rollout history deploy nginx --revision=5
deployment.apps/nginx with revision #5
Pod Template:
Labels: app=nginx
pod-template-hash=9dfb46bc4
Annotations: kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.20.1 --record=true
Containers:
nginx:
Image: nginx:1.20.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
- 回滚到指定版本
[root@k8s-master ~]# kubectl rollout undo deploy nginx --to-revision=5
deployment.apps/nginx rolled back
#查看历史记录信息
[root@k8s-master ~]# kubectl rollout history deploy nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deploy nginx nginx=nginx:1.20.2 --record=true
4 kubectl set image deploy nginx nginx=nginx:778899 --record=true
6 kubectl set image deploy nginx nginx=nginx:787977da --record=true
7 kubectl set image deploy nginx nginx=nginx:787977da1 --record=true
8 kubectl set image deploy nginx nginx=nginx:787977da12 --record=true
9 kubectl set image deploy nginx nginx=nginx:787977da123 --record=true
10 kubectl set image deploy nginx nginx=nginx:1.20.1 --record=true
#查看当前 deploy nginx images 信息
[root@k8s-master ~]# kubectl get deploy nginx -oyaml | grep image
kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.20.1
- image: nginx:1.20.1
imagePullPolicy: IfNotPresent
4、Deploymen的扩容和缩容
- 扩容
[root@k8s-master ~]# kubectl scale --replicas=3 deploy nginx
deployment.apps/nginx scaled
[root@k8s-master ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-9dfb46bc4-27fwx 1/1 Running 0 93m
nginx-9dfb46bc4-8qngb 1/1 Running 0 8s
nginx-9dfb46bc4-l44s4 1/1 Running 0 94m
- 缩容
[root@k8s-master ~]# kubectl scale --replicas=2 deploy nginx
deployment.apps/nginx scaled
[root@k8s-master ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-9dfb46bc4-27fwx 1/1 Running 0 94m
nginx-9dfb46bc4-l44s4 1/1 Running 0 95m
5、Deploymen更新-暂停和恢复
- 暂停和恢复deploy 更新
[root@k8s-master ~]# kubectl rollout pause deployment nginx
deployment.apps/nginx paused
#修改版本
[root@k8s-master ~]# kubectl set image deploy nginx nginx=nginx:1.21.6 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx image updated
#修改CPU 内存 资源参数
[root@k8s-master ~]# kubectl set resources deploy nginx -c nginx --limits=cpu=200m,memory=128Mi --requests=cpu=10m,memory=16Mi
deployment.apps/nginx resource requirements updated
#恢复deploy 更新
[root@k8s-master ~]# kubectl rollout resume deploy nginx
deployment.apps/nginx resumed
#查看rc状态
[root@k8s-master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-5777589579 0 0 0 142m
nginx-5b5c77df9 0 0 0 52m
nginx-67ddfd77d 0 0 0 77m
nginx-777c8f69d4 2 2 1 5s
nginx-77fcc495b8 0 0 0 3h17m
nginx-7bb4f4dfd9 0 0 0 52m
nginx-7d79b96f68 0 0 0 52m
nginx-99bf8f66 0 0 0 52m
nginx-9dfb46bc4 1 1 1 125m
#查看pode 状态
[root@k8s-master ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-777c8f69d4-ccnrw 1/1 Running 0 11s
nginx-777c8f69d4-k2zlq 1/1 Running 0 8s
nginx-9dfb46bc4-l44s4 0/1 Terminating 0 125m
#查看pode yaml 参数
[root@k8s-master ~]# kubectl get po nginx-777c8f69d4-ccnrw -oyaml | grep -e image -e cpu -e memory
- image: nginx:1.21.6
imagePullPolicy: IfNotPresent
cpu: 200m
memory: 128Mi
cpu: 10m
memory: 16Mi
image: nginx:1.21.6
imageID: docker-pullable://nginx@sha256:19da26bd6ef0468ac8ef5c03f01ce1569a4dbfb82d4d7b7ffbd7aed16ad3eb46
6、Deploymen更新注意事项
[root@k8s-master ~]# kubectl get deploy nginx -oyaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
/revision: "12"
kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.21.6
--record=true
creationTimestamp: "2022-05-12T03:13:17Z"
generation: 19
labels:
app: nginx
name: nginx
namespace: default
resourceVersion: "47436"
uid: 0cd92e48-3e13-4cc5-bde3-e625b100621f
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10 #保留RS历史副本数,设置为0的话,不保留历史数据
selector:
matchLabels:
app: nginx
strategy: # 滚动更新的策略
rollingUpdate:
maxSurge: 25% #可以超过期望值的最大Pod数,可选字段,默认为25%,可以设置成数字或百分比,如果该值为0,那么maxUnavailable不能为0
maxUnavailable: 25% #指定在回滚或更新时最大不可用的Pod的数量,可选字段,默认25%,可以设置成数字或百分比,如果该值为0,那么maxSurge就不能0
type: RollingUpdate # 更新deployment的方式,默认是RollingUpdate,滚动更新,可以指定maxSurge和maxUnavailable
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.21.6
imagePullPolicy: IfNotPresent
name: nginx
resources:
limits:
cpu: 200m
memory: 128Mi
requests:
cpu: 10m
memory: 16Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 2
conditions:
- lastTransitionTime: "2022-05-12T05:59:05Z"
lastUpdateTime: "2022-05-12T05:59:05Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
- lastTransitionTime: "2022-05-12T06:30:24Z"
lastUpdateTime: "2022-05-12T06:30:29Z"
message: ReplicaSet "nginx-777c8f69d4" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
observedGeneration: 19
readyReplicas: 2
replicas: 2
updatedReplicas: 2
.spec.minReadySeconds:可选参数,指定新创建的Pod在没有任何容器崩溃的情况下视为Ready最小的秒数,默认为0,即一旦被创建就视为可用。
.spec.strategy.type Recreate:重建,先删除旧的Pod,在创建新的Pod