apiVersion: apps/v1
kind: Deployment
metadata:
namespace: workspace
name: nginx-deployment # Deployment的名称。
labels:
app: nginx
spec:
replicas: 3 # 创建pod的副本数
selector: # 定义Deployment如何找到要管理的Pod,与template的label(标签)对应。
matchLabels:
app: nginx # Pod的label标签
template:
metadata:
labels:
app: nginx
spec: # 表示Pod运行一个名字为nginx的容器
containers:
- name: nginx
image: nginx:1.7.9 # 运行此pod使用的镜像
ports:
- containerPort: 80 # 容器用于发送和接收流量的端口
[root@master01 yaml]# kubectl create -f dc-nginx.yaml
deployment.apps/nginx-deployment created
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 7m56s
[root@master01 yaml]# kubectl rollout status deployment/nginx-deployment -n workspace
deployment "nginx-deployment" successfully rolled out
[root@master01 yaml]# kubectl get rs -n workspace
NAME DESIRED CURRENT READY AGE
nginx-deployment-5d59d67564 3 3 3 12m
[root@master01 yaml]# kubectl get po -n workspace --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-deployment-5d59d67564-8cqb4 1/1 Running 0 14m app=nginx,pod-template-hash=5d59d67564
nginx-deployment-5d59d67564-gltz8 1/1 Running 0 14m app=nginx,pod-template-hash=5d59d67564
nginx-deployment-5d59d67564-p7trc 1/1 Running 0 14m app=nginx,pod-template-hash=5d59d67564
一般对应用程序升级或者版本迭代时,会通过Deployment对Pod进行滚动更新。
将nginx Pod的image版本更新到1.9.1
[root@master01 yaml]# kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 -n workspace --record
deployment.apps/nginx-deployment image updated
[root@master01 yaml]# kubectl edit deploy -n workspace nginx-deployment
[root@master01 yaml]# kubectl describe deployments.apps -n workspace nginx-deployment
Name: nginx-deployment
Namespace: workspace
CreationTimestamp: Fri, 20 May 2022 08:43:48 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 2
kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
Selector: app=nginx
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.9.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-5bfdf46dc6 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 4d13h deployment-controller Scaled up replica set nginx-deployment-f7ccf9478 to 3
Warning ReplicaSetCreateError 2m10s deployment-controller Failed to create new replica set "nginx-deployment-5bfdf46dc6": Unauthorized
Normal ScalingReplicaSet 2m10s deployment-controller Scaled up replica set nginx-deployment-5bfdf46dc6 to 1
Normal ScalingReplicaSet 108s deployment-controller Scaled down replica set nginx-deployment-f7ccf9478 to 2
Normal ScalingReplicaSet 107s deployment-controller Scaled up replica set nginx-deployment-5bfdf46dc6 to 2
Normal ScalingReplicaSet 61s deployment-controller Scaled down replica set nginx-deployment-f7ccf9478 to 1
Normal ScalingReplicaSet 61s deployment-controller Scaled up replica set nginx-deployment-5bfdf46dc6 to 3
Normal ScalingReplicaSet 37s deployment-controller Scaled down replica set nginx-deployment-f7ccf9478 to 0
[root@master01 ~]# kubectl rollout history -n workspace deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
3 kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
4 kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
5 kubectl set image deployment nginx-deployment nginx=nginx:1.10.3 --namespace=workspace --record=true
[root@master01 ~]# kubectl rollout history -n workspace deployment/nginx-deployment --revision=2
deployment.apps/nginx-deployment with revision #2
Pod Template:
Labels: app=nginx
pod-template-hash=69c44dfb78
Annotations: kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
Containers:
nginx:
Image: nginx:1.9.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
[root@master01 ~]# kubectl rollout undo -n workspace deployment/nginx-deployment --to-revision=2
deployment.apps/nginx-deployment rolled back
[root@master01 ~]# kubectl rollout history -n workspace deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
3 kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
4 kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
5 kubectl set image deployment nginx-deployment nginx=nginx:1.10.3 --namespace=workspace --record=true
6 kubectl set image deployment nginx-deployment nginx=nginx:1.10.4 --namespace=workspace --record=true
7 kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
[root@master01 ~]# kubectl describe po -n workspace nginx-deployment-69c44dfb78-zv9vl | grep Image
Image: nginx:1.9.1
Image ID: docker-pullable://nginx@sha256:2f68b99bc0d6d25d0c56876b924ec20418544ff28e1fb89a4c27679a40da811b
[root@master01 ~]# kubectl scale -n workspace deployment/nginx-deployment --replicas=5
deployment.apps/nginx-deployment scaled
[root@master01 ~]# kubectl get po -n workspace
NAME READY STATUS RESTARTS AGE
nginx-deployment-69c44dfb78-pjkwb 1/1 Running 0 6m3s
nginx-deployment-69c44dfb78-shw9d 1/1 Running 0 33s
nginx-deployment-69c44dfb78-tgsmn 1/1 Running 0 33s
nginx-deployment-69c44dfb78-v4mxp 1/1 Running 0 6m5s
nginx-deployment-69c44dfb78-zv9vl 1/1 Running 0 6m1s
- 使用kubectl rollout pause暂停Deployment更新:
[root@master01 ~]# kubectl rollout pause deployment/nginx-deployment -n workspace
deployment.apps/nginx-deployment paused
- 然后对Deployment进行相关更新操作,比如更新镜像,然后对其资源进行限制:
[root@master01 ~]# kubectl set image -n workspace deployment/nginx-deployment nginx=nginx:1.11.1
deployment.apps/nginx-deployment image updated
[root@master01 ~]# kubectl set resources -n workspace deployment/nginx-deployment -c=nginx --limits=cpu=500m,memory=1Gi --requests=cpu=100m,memory=64Mi
deployment.apps/nginx-deployment resource requirements updated
- 通过rollout history可以看到没有新的更新:
[root@master01 ~]# kubectl rollout history deployment/nginx-deployment -n workspace
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
3 kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
4 kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
5 kubectl set image deployment nginx-deployment nginx=nginx:1.10.3 --namespace=workspace --record=true
6 kubectl set image deployment nginx-deployment nginx=nginx:1.10.4 --namespace=workspace --record=true
7 kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
- 使用kubectl rollout resume恢复Deployment更新:
[root@master01 ~]# kubectl rollout resume deployment/nginx-deployment -n workspace
deployment.apps/nginx-deployment resumed
- 可以查看到恢复更新的Deployment创建了一个新的RS(复制集):
[root@master01 ~]# kubectl get rs -n workspace
NAME DESIRED CURRENT READY AGE
nginx-deployment-55dcddcb7 0 0 0 16m
nginx-deployment-5c6ccdb5db 0 0 0 24m
nginx-deployment-5d59d67564 0 0 0 8h
nginx-deployment-645d9d5cd7 0 0 0 31m
nginx-deployment-69c44dfb78 0 0 0 8h
nginx-deployment-6f7f5f76dc 5 5 5 65s
nginx-deployment-87f7bfb9 0 0 0 29m
-可以查看Deployment的image(镜像)已经变为nginx:1.11.1
[root@master01 ~]# kubectl describe deploy nginx-deployment -n workspace | grep Image
Image: nginx:1.11.1