1. RS(ReplicaSet)使用
确保容器应用的副本数始终保持在用户定义的副本数。即如果有容器异常退出,会自动创建新的Pod来替代,并且异常多出来的容器也会被回收。
RS与RC(ReplicationController)没有本质上的区别,RS支持集合式的selector。
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
name: frontend
spec:
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: myapp
image: hub.zhuangyb.com/library/myapp:v1
env:
- name: GET_HOSTS_FROM
value: dns
ports:
- containerPort: 80
创建RS:
create -f xxx.yaml --record
2. Deployment的使用
Deployment为Pod和RS提供了一个声明式定义的方法(declarative),用来替代以前的RC方便管理应用。
应用场景:
- 定义Deployment来创建Pod和RS
- 滚动升级和回滚应用
- 扩容和缩容
- 暂停和继续Deployment
示例:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
images: hub.zhuangyb.com/library/myapp:v1
ports:
- containerPort: 80
创建Deployment:
kubectl apply -f xxx.yaml --record
record:该参数可以记录命令,方便每次查看revision的变化
扩容:
kubectl scale deployment [deployment name] --replicas [nums]
集群支持horizontal pod autoscaling情况下,deployment设置自动扩容:
kubectl autoscale deployment [deployment name] --min=10 --max=20 --cpu-percent=80
更新镜像:
#app name:yaml内部匹配/容器名
kubectl set image deployment/[deployment name] [app name]=[image name]:[version]
回滚:
kubectl rollout undo deployment/[deployment name]
查看rollout状态
kubectl rollout status deployment/[deployment name]
查看deployment详细
kubectl describe deployment [deployment name]
Rollove(多个并行情况):
如果创建了100个NGINX:1.7.9 replica的Deployment,但是当其中50个创建出来的时候就开始更新为新版本NGINX:2.0。在这种情况下,Deployment会立即杀掉已经创建的50个NGINX:1.7.9的pod,并开始创建NGINX:2.0的pod,不会等到100个pod都创建出来的时候才改变航道。
回退deployment:
kubectl set image deployment/[deployment name] [app name]=[image name]:[version]
kubectl rollout status deployment [deployment name]
kubectl get pod
kubectl rollout history deployment/[deployment name]
kubectl rollout undo deployment/[deployment name]
kubectl rollout undo deployment/[deployment name] --to-revision=2 #指定某个历史版本
kubectl rollout pause deployment/[deployment name] #暂停deployment的更新
通过kubectl rollout status查看deployment是否已经完成,如果rollout已经完成,命令将返回一个0的值Exit Code。
kubectl rollout status deployment/[deployment name]
echo $?
清理policy:
通过设置.spec.revisionHistoryLimit项来指定deployment最多保留多少revision历史记录,默认保留所有记录,如果该项为0,deployment就不予许回退。