deployment(deploy)是一个控制器
修改deployment副本数
#method 1
kubectl scale deployment 名称 --replicas=新的副本数
#method 2
通过编辑deployment的方式修改
kubectl edit deployments 名称
#method 3
通过修改yaml文件的方式
修改yaml文件之后再重新应用即可kubectl apply -f deployment1.yaml
水平自动更新(Horizontal Pod Autoscalers) HPA
通过检测pod的cpu负载通知deployment,让其更新pod数目以减轻pod的负载
配置HPA
kubectl autoscale deployment test1 -n nsdeploy --min=N --max=M --cpu-percent=X
至少会有N个pod在运行,如果--cpu-percent没有设置的话,默认值是80
如果要让--cpu-percent生效,需要启用deployment的资源请求
root@k8sdbamaster:~# kubectl edit deployment test1 -n nsdeploy
修改yaml file给每一个pod增加资源限制
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources:
requests:
cpu: 400m
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
#重新设置hpa
root@k8sdbamaster:~# kubectl autoscale deployment test1 -n nsdeploy --min=1 --max=5 --cpu-percent=80
root@k8sdbamaster:~# kubectl get hpa -n nsdeploy
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
test1 Deployment/test1 0%/80% 1 5 1 17s
升级镜像
Flag --record has been deprecated, --record will be removed in the future
可以采用annotate记录镜像的变更
kubectl annotate deployment test1 -n nsdeploy kubernetes.io/change-cause="version change from latest to 1.7.9" --overwrite=true
滚动更新
滚动更新就是不是一次性把所有pod的镜像全部更新完毕,而是先更新几个pod,更新完成之后,再更新几个pod,直到把所有的pod全部更新完毕 每次更新几个pod是由以下参数决定的
maxSurge 指定最多一次创建几个pod,可以是百分比也可以是具体数目
maxUnavailable 指定最多删除几个pod,可以是数字或者百分比
daemonset及其他控制器
daemonset和deployment的区别是daemonet会在所有的节点(包括master)上创建一个pod,有多少节点就创建几个pod,每个节点只有一个 常见的如kube-proxy这个pod就是由daemonset控制的 可以通过设置nodeSelector来指定pod运行在含有标签的节点上
daemonset和deployment的yaml文件的区别
kind字段不同,一个是DaemonSet一个是Deployment
deployment有副本数,daemonset没有副本数这个选项
daemonset没有.spec.strategy:{}
daemonset没有最后一行的status:{}
其他控制器ReplicationCOntroller(rc)
rc的作用和deployment是一样的,使用方法也是一样的
kind: ReplicationController
metadata:
creationTimestamp: null
labels:
app: myrc
name: myrc
namespace: nsds
spec:
replicas: 3
selector:
app: myrc
template:
metadata:
creationTimestamp: null
labels:
app: myrc
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
resources: {}
status: {}
其他控制器ReplicaSet(rs) rs的作用和deployment是一样的
kind: ReplicaSet
metadata:
creationTimestamp: null
labels:
app: myrs
name: myrs
namespace: nsds
spec:
replicas: 3
selector:
matchLabels:
app: myrs
template:
metadata:
creationTimestamp: null
labels:
app: myrs
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
resources: {}
status: {}
deployment,rc,rs之间yaml文件的对比
deployment, daemonset, ReplicaSet api是apps/v1, select是
matchLabels:
ReplicationController api是v1,select是
selector: