1. Deployment 是什么?
Deployment 是 Kubernetes 最常用的控制器之一,用于声明式地管理一组 Pod 副本(通常是无状态应用),实现自动部署、滚动升级、回滚等功能。
它是生产环境推荐的工作负载类型。
底层自动创建和管理 ReplicaSet,保证你定义的 Pod 副本数始终健康。
支持零停机滚动升级、版本回滚。
可以扩容缩容、自动重建失败 Pod。
2. Deployment 的核心功能
管理副本数(replicas)
自动重建失败的 Pod
支持滚动升级(Rolling Update)
支持回滚(Rollback)
配合 Service 暴露应用
3. Deployment YAML 示例
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploy
spec:replicas: 3selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.25.3ports:- containerPort: 80
4.部署测试
4.1.创建了deployment.yaml
4.2 部署测试
azureuser@master-001:~$ vi deployment1.yaml
# 执行失败,因为matchlabels 要改成 matchLabels
azureuser@master-001:~$ kubectl create -f deployment1.yaml
Error from server (BadRequest): error when creating "deployment1.yaml": Deployment in version "v1" cannot be handled as a Deployment: strict decoding error: unknown field "spec.selector.matchlabels"
azureuser@master-001:~$ vi deployment1.yaml
azureuser@master-001:~$ kubectl apply -f deployment1.yaml
deployment.apps/nginx-deploy created
azureuser@master-001:~$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deploy-789bf7b8fc-pnmbp 1/1 Running 0 6s
nginx-deploy-789bf7b8fc-v5fcw 1/1 Running 0 6s
nginx-deploy-789bf7b8fc-xphxk 1/1 Running 0 6s
azureuser@master-001:~$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42h
nodeport-svc NodePort 10.101.111.134 <none> 80:30443/TCP 41h
web-svc ClusterIP 10.104.249.89 <none> 80/TCP 41h
azureuser@master-001:~$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deploy-789bf7b8fc-pnmbp 1/1 Running 0 17s
pod/nginx-deploy-789bf7b8fc-v5fcw 1/1 Running 0 17s
pod/nginx-deploy-789bf7b8fc-xphxk 1/1 Running 0 17sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42h
service/nodeport-svc NodePort 10.101.111.134 <none> 80:30443/TCP 41h
service/web-svc ClusterIP 10.104.249.89 <none> 80/TCP 41hNAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deploy 3/3 3 3 17sNAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deploy-789bf7b8fc 3 3 3 17s
azureuser@master-001:~$ kubectl scale deployment nginx-deploy --replicas=5
deployment.apps/nginx-deploy scaled
azureuser@master-001:~$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deploy-789bf7b8fc-62qzp 1/1 Running 0 3s
pod/nginx-deploy-789bf7b8fc-8jlgt 1/1 Running 0 3s
pod/nginx-deploy-789bf7b8fc-pnmbp 1/1 Running 0 68s
pod/nginx-deploy-789bf7b8fc-v5fcw 1/1 Running 0 68s
pod/nginx-deploy-789bf7b8fc-xphxk 1/1 Running 0 68sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42h
service/nodeport-svc NodePort 10.101.111.134 <none> 80:30443/TCP 41h
service/web-svc ClusterIP 10.104.249.89 <none> 80/TCP 41hNAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deploy 5/5 5 5 68sNAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deploy-789bf7b8fc 5 5 5 68s
#设置滚动升级
azureuser@master-001:~$ kubectl set image deployment/nginx-deploy nginx=nginx:1.26.0
deployment.apps/nginx-deploy image updated
#查看升级状态
azureuser@master-001:~$ kubectl rollout status deployment/nginx-deploy
Waiting for deployment "nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
#查看状态
azureuser@master-001:~$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deploy-8556777689-2dd92 1/1 Running 0 9s
pod/nginx-deploy-8556777689-9r8w2 1/1 Running 0 9s
pod/nginx-deploy-8556777689-b9hwl 1/1 Running 0 8s
pod/nginx-deploy-8556777689-h5fcm 1/1 Running 0 9s
pod/nginx-deploy-8556777689-sm89m 1/1 Running 0 8sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42h
service/nodeport-svc NodePort 10.101.111.134 <none> 80:30443/TCP 41h
service/web-svc ClusterIP 10.104.249.89 <none> 80/TCP 41hNAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deploy 5/5 5 5 3m47sNAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deploy-789bf7b8fc 0 0 0 3m47s
replicaset.apps/nginx-deploy-8556777689 5 5 5 116s
azureuser@master-001:~$ kubectl describe pod nginx-deploy-8556777689-2dd92
Name: nginx-deploy-8556777689-2dd92
Namespace: default
Priority: 0
Service Account: default
Node: node-002/10.0.0.5
Start Time: Fri, 26 Sep 2025 02:04:18 +0000
Labels: app=nginxpod-template-hash=8556777689
Annotations: <none>
Status: Running
IP: 10.244.2.18
IPs:IP: 10.244.2.18
Controlled By: ReplicaSet/nginx-deploy-8556777689
Containers:nginx:Container ID: containerd://3f887301dd84b198c8bfbb03bcd9bdaf02f5f40603dffaaded1ebb8ac9fa6337Image: nginx:1.26.0Image ID: docker.io/library/nginx@sha256:192e88a0053c178683ca139b9d9a2afb0ad986d171fae491949fe10970dd9da9Port: 80/TCPHost Port: 0/TCPState: RunningStarted: Fri, 26 Sep 2025 02:04:19 +0000Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-kkpnp (ro)
Conditions:Type StatusInitialized TrueReady TrueContainersReady TruePodScheduled True
Volumes:kube-api-access-kkpnp:Type: Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds: 3607ConfigMapName: kube-root-ca.crtConfigMapOptional: <nil>DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal Scheduled 23s default-scheduler Successfully assigned default/nginx-deploy-8556777689-2dd92 to node-002Normal Pulled 23s kubelet Container image "nginx:1.26.0" already present on machineNormal Created 23s kubelet Created container nginxNormal Started 23s kubelet Started container nginxazureuser@master-001:~$ kubectl describe pod nginx-deploy-8556777689-2dd92
Name: nginx-deploy-8556777689-2dd92
Namespace: default
Priority: 0
Service Account: default
Node: node-002/10.0.0.5
Start Time: Fri, 26 Sep 2025 02:04:18 +0000
Labels: app=nginxpod-template-hash=8556777689
Annotations: <none>
Status: Running
IP: 10.244.2.18
IPs:IP: 10.244.2.18
Controlled By: ReplicaSet/nginx-deploy-8556777689
Containers:nginx:Container ID: containerd://3f887301dd84b198c8bfbb03bcd9bdaf02f5f40603dffaaded1ebb8ac9fa6337Image: nginx:1.26.0Image ID: docker.io/library/nginx@sha256:192e88a0053c178683ca139b9d9a2afb0ad986d171fae491949fe10970dd9da9Port: 80/TCPHost Port: 0/TCPState: RunningStarted: Fri, 26 Sep 2025 02:04:19 +0000Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-kkpnp (ro)
Conditions:Type StatusInitialized TrueReady TrueContainersReady TruePodScheduled True
Volumes:kube-api-access-kkpnp:Type: Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds: 3607ConfigMapName: kube-root-ca.crtConfigMapOptional: <nil>DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal Scheduled 44s default-scheduler Successfully assigned default/nginx-deploy-8556777689-2dd92 to node-002Normal Pulled 44s kubelet Container image "nginx:1.26.0" already present on machineNormal Created 44s kubelet Created container nginxNormal Started 44s kubelet Started container nginx
#设置升级回滚到之前的版本
azureuser@master-001:~$ kubectl rollout undo deployment/nginx-deploy
deployment.apps/nginx-deploy rolled back
#查看状态
azureuser@master-001:~$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deploy-789bf7b8fc-bsr87 1/1 Running 0 32s
pod/nginx-deploy-789bf7b8fc-c9jhq 1/1 Running 0 31s
pod/nginx-deploy-789bf7b8fc-frvht 1/1 Running 0 31s
pod/nginx-deploy-789bf7b8fc-q94sb 1/1 Running 0 32s
pod/nginx-deploy-789bf7b8fc-szzm6 1/1 Running 0 32sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42h
service/nodeport-svc NodePort 10.101.111.134 <none> 80:30443/TCP 41h
service/web-svc ClusterIP 10.104.249.89 <none> 80/TCP 41hNAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deploy 5/5 5 5 3m10sNAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deploy-789bf7b8fc 5 5 5 3m10s
replicaset.apps/nginx-deploy-8556777689 0 0 0 79s
azureuser@master-001:~$ kubectl describe pod nginx-deploy-789bf7b8fc-bsr87
Name: nginx-deploy-789bf7b8fc-bsr87
Namespace: default
Priority: 0
Service Account: default
Node: node-002/10.0.0.5
Start Time: Fri, 26 Sep 2025 02:03:18 +0000
Labels: app=nginxpod-template-hash=789bf7b8fc
Annotations: <none>
Status: Running
IP: 10.244.2.15
IPs:IP: 10.244.2.15
Controlled By: ReplicaSet/nginx-deploy-789bf7b8fc
Containers:nginx:Container ID: containerd://2211ce5c614e87614738504a7a62890a5728e443de103dc270f2a779cc21cd76Image: nginx:1.25.3Image ID: docker.io/library/nginx@sha256:c7a6ad68be85142c7fe1089e48faa1e7c7166a194caa9180ddea66345876b9d2Port: 80/TCPHost Port: 0/TCPState: RunningStarted: Fri, 26 Sep 2025 02:03:19 +0000Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6ltbd (ro)
Conditions:Type StatusInitialized TrueReady TrueContainersReady TruePodScheduled True
Volumes:kube-api-access-6ltbd:Type: Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds: 3607ConfigMapName: kube-root-ca.crtConfigMapOptional: <nil>DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal Scheduled 49s default-scheduler Successfully assigned default/nginx-deploy-789bf7b8fc-bsr87 to node-002Normal Pulled 49s kubelet Container image "nginx:1.25.3" already present on machineNormal Created 49s kubelet Created container nginxNormal Started 49s kubelet Started container nginx#对外发布service
azureuser@master-001:~$ kubectl expose deployment nginx-deploy --port=80 --target-port=80 --type=ClusterIP --name=nginx-svc
service/nginx-svc exposed
#查看状态azureuser@master-001:~$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deploy-8556777689-2dd92 1/1 Running 0 57s
pod/nginx-deploy-8556777689-9r8w2 1/1 Running 0 57s
pod/nginx-deploy-8556777689-b9hwl 1/1 Running 0 56s
pod/nginx-deploy-8556777689-h5fcm 1/1 Running 0 57s
pod/nginx-deploy-8556777689-sm89m 1/1 Running 0 56sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42h
service/nginx-svc ClusterIP 10.102.29.115 <none> 80/TCP 17s
service/nodeport-svc NodePort 10.101.111.134 <none> 80:30443/TCP 41h
service/web-svc ClusterIP 10.104.249.89 <none> 80/TCP 41hNAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deploy 5/5 5 5 4m35sNAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deploy-789bf7b8fc 0 0 0 4m35s
replicaset.apps/nginx-deploy-8556777689 5 5 5 2m44s
azureuser@master-001:~$ curl http://10.102.29.115:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
azureuser@master-001:~$