nginx.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
imagePullPolicy: IfNotPresent # Always/IfNotPresent/Never
ports:
- containerPort: 80

创建nginx-deployment:

kubectl apply -f nginx.yaml

查看pod和deployment实例:

[root@k8s-master test]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-f7ccf9478-bffc8 1/1 Running 0 5m36s 10.122.169.175 k8s-node2 <none> <none>
nginx-deployment-f7ccf9478-n9xkd 1/1 Running 0 5m36s 10.122.36.113 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-zsl9d 1/1 Running 0 5m36s 10.122.36.112 k8s-node1 <none> <none>
[root@k8s-master test]#
[root@k8s-master test]#
[root@k8s-master test]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 5m41s
[root@k8s-master test]#
[root@k8s-master test]#

扩容为20个pod

[root@k8s-master test]# kubectl scale deployment nginx-deployment --replicas 20
deployment.apps/nginx-deployment scaled
[root@k8s-master test]#
[root@k8s-master test]#
[root@k8s-master test]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-f7ccf9478-46jmw 1/1 Running 0 9s 10.122.36.119 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-5l58t 1/1 Running 0 9s 10.122.169.182 k8s-node2 <none> <none>
nginx-deployment-f7ccf9478-5p4jw 1/1 Running 0 9s 10.122.169.184 k8s-node2 <none> <none>
nginx-deployment-f7ccf9478-889qz 1/1 Running 0 9s 10.122.169.181 k8s-node2 <none> <none>
nginx-deployment-f7ccf9478-9htqk 1/1 Running 0 9s 10.122.169.178 k8s-node2 <none> <none>
nginx-deployment-f7ccf9478-bffc8 1/1 Running 0 7m14s 10.122.169.175 k8s-node2 <none> <none>
nginx-deployment-f7ccf9478-d72bx 1/1 Running 0 9s 10.122.169.177 k8s-node2 <none> <none>
nginx-deployment-f7ccf9478-fh69c 1/1 Running 0 9s 10.122.36.120 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-hsx46 1/1 Running 0 9s 10.122.36.121 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-k6zbp 1/1 Running 0 9s 10.122.36.116 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-mk8c8 1/1 Running 0 9s 10.122.36.117 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-mw7fk 1/1 Running 0 9s 10.122.169.180 k8s-node2 <none> <none>
nginx-deployment-f7ccf9478-n9xkd 1/1 Running 0 7m14s 10.122.36.113 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-nm2wp 1/1 Running 0 9s 10.122.169.179 k8s-node2 <none> <none>
nginx-deployment-f7ccf9478-ptcfn 1/1 Running 0 9s 10.122.36.114 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-qp625 1/1 Running 0 9s 10.122.36.115 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-s9fgc 1/1 Running 0 9s 10.122.169.176 k8s-node2 <none> <none>
nginx-deployment-f7ccf9478-s9vft 1/1 Running 0 9s 10.122.36.118 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-zsl9d 1/1 Running 0 7m14s 10.122.36.112 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-ztc59 1/1 Running 0 9s 10.122.169.183 k8s-node2 <none> <none>
[root@k8s-master test]#

缩容为2个pod:

[root@k8s-master test]# kubectl scale deployment nginx-deployment --replicas 2
deployment.apps/nginx-deployment scaled
[root@k8s-master test]#
[root@k8s-master test]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-f7ccf9478-n9xkd 1/1 Running 0 8m12s 10.122.36.113 k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-zsl9d 1/1 Running 0 8m12s 10.122.36.112 k8s-node1 <none> <none>
[root@k8s-master test]#

更新nginx镜像为1.9.1版本

[root@k8s-master test]# kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
deployment.apps/nginx-deployment image updated
[root@k8s-master test]#

滚动状态更新的中间状态:
之前是2个1.7.9的nginx pod,先创建一个1.9.1的nginx pod,然后销毁一个1.7.9的nginx pod。直到全部pod替换完成。

[root@k8s-master test]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-5bfdf46dc6-6r757 1/1 Running 0 100s 10.122.169.185 k8s-node2 <none> <none>
nginx-deployment-5bfdf46dc6-fjvn2 0/1 ContainerCreating 0 44s <none> k8s-node1 <none> <none>
nginx-deployment-f7ccf9478-zsl9d 1/1 Running 0 11m 10.122.36.112 k8s-node1 <none> <none>
[root@k8s-master test]#
[root@k8s-master test]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-5bfdf46dc6-6r757 1/1 Running 0 2m45s 10.122.169.185 k8s-node2 <none> <none>
nginx-deployment-5bfdf46dc6-fjvn2 1/1 Running 0 109s 10.122.36.122 k8s-node1 <none> <none>
[root@k8s-master test]#

回滚到上一次的状态

[root@k8s-master test]# kubectl rollout undo deployment/nginx-deployment

编辑:

kubectl edit deployment nginx-deployment

编辑wq后,会自动按照新的资源描述去更新状态。