##定义一个简单的nginx应用
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-test
namespace: test
spec:
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
##创建deploy(部署)
kubectl create -f nginx-deployment
deployment.extensions "nginx-test" created
##查看deploy(部署)
kubectl get deploy --namespace=test
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-test 3 3 3 3 12m
##查看rs(副本集)
kubectl get rs --namespace=test
NAME DESIRED CURRENT READY AGE
nginx-test-75675f5897 3 3 3 11m
##查看pods(容器组)
kubectl get po --namespace=test
NAME READY STATUS RESTARTS AGE
nginx-test-75675f5897-9fcrs 1/1 Running 0 13m
nginx-test-75675f5897-gnc8z 1/1 Running 0 13m
nginx-test-75675f5897-qbsvm 1/1 Running 0 13m
##扩容
将容器组的个数扩容至5个
"replicas": 5
#语法格式
kubectl scale deployment + [deploy的name] + --replicas [count] + --namespace=命名空间
#命令
kubectl scale deployment nginx-test --replicas 5 --namespace=test
deployment.extensions "nginx-test" scaled
##说明
如果集群支持 horizontal pod autoscaling 的话,还可以为Deployment设置自动扩展:
kubectl autoscale deployment nginx-test --min=10 --max=15 --cpu-percent=80 --namespace=test
#查看deploy(部署)
kubectl get deploy --namespace=test
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-test 5 5 5 5 17m
#查看rs(副本集)
kubectl get rs --namespace=test
NAME DESIRED CURRENT READY AGE
nginx-test-75675f5897 5 5 5 18m
#查看pods(容器组)
kubectl get po --namespace=test
NAME READY STATUS RESTARTS AGE
nginx-test-75675f5897-9fcrs 1/1 Running 0 19m
nginx-test-75675f5897-gnc8z 1/1 Running 0 19m
nginx-test-75675f5897-hjvjw 1/1 Running 0 3m
nginx-test-75675f5897-hszs8 1/1 Running 0 3m
nginx-test-75675f5897-qbsvm 1/1 Running 0 19m
##缩容
将容器组的个数缩容至2个
"replicas": 2
#命令
kubectl scale deployment nginx-test --replicas 2 --namespace=test
deployment.extensions "nginx-test" scaled
#查看deploy(部署)
kubectl get deploy --namespace=test
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-test 2 2 2 2 25m
#查看rs(副本集)
kubectl get rs --namespace=test
NAME DESIRED CURRENT READY AGE
nginx-test-75675f5897 2 2 2 25m
#查看pods(容器组)
kubectl get po --namespace=test
NAME READY STATUS RESTARTS AGE
nginx-test-75675f5897-gnc8z 1/1 Running 0 26m
nginx-test-75675f5897-qbsvm 1/1 Running 0 26m
将image: nginx:1.7.9升级为nginx:1.9.1
#语法格式
kubectl scale deployment/[deploy的name] + [升级镜像] + --namespace=命名空间
#命令
kubectl set image deployment/nginx-test nginx=nginx:1.9.1 --namespace=test
deployment.apps "nginx-test" image updated
#查看rs(副本集)
kubectl get rs --namespace=test
NAME DESIRED CURRENT READY AGE
nginx-test-75675f5897 0 0 0 48m
nginx-test-c4747d96c 2 2 2 19m
#查看pods(容器组)
kubectl get po --namespace=test
NAME READY STATUS RESTARTS AGE
nginx-test-c4747d96c-cqnqs 1/1 Running 0 21m
nginx-test-c4747d96c-fpjrk 1/1 Running 0 21m
#查看nginx版本
kubectl describe po nginx-test-c4747d96c-cqnqs --namespace=test
可看到版本信息:
Image: nginx:1.9.1
#命令
kubectl rollout undo deployment/nginx-test --namespace=test
deployment.apps "nginx-test"
#查看rs(副本集)
kubectl get rs --namespace=test
NAME DESIRED CURRENT READY AGE
nginx-test-75675f5897 2 2 2 28m
nginx-test-c4747d96c 0 0 0 3m
#查看pods(容器组)
kubectl get po --namespace=test
NAME READY STATUS RESTARTS AGE
nginx-test-75675f5897-rqnhb 1/1 Running 0 1m
nginx-test-75675f5897-zhwgn 1/1 Running 0 1m
#查看nginx版本
kubectl describe po nginx-test-75675f5897-rqnhb --namespace=test
可看到版本信息:
Image: nginx:1.7.9
原文:http://blog.51cto.com/wutengfei/2116663