k8s之控制器(工作负载)
k8s-控制器
k8s中控制器分为:deployment、DaemonSet、StatufluSet
使用控制器是为了用来管理pod
控制器通过标签来识别pod
1、Deployment:一般用来部署长期运行的、无状态的应用
特点:集群之中,随机部署
2、DaemonSet:每一个节点上部署一个Pod,删除节点自动删除对应的POD(zabbix-agent)
特点:每一台上有且只有一台
3、StatudfluSet: 部署有状态应用
特点:有启动顺序
#查看控制器版本
[root@k8s-master-01 ~]# kubectl explain deployment
KIND: Deployment
VERSION: apps/v1
1、Deploymnet
1.1、在配置清单中调用Deployment控制器
########控制器########
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
spec:
replicas: 1
selector:
matchLabels:
ddff: qln
template:
########描述所需的状态########
metadata:
labels:
ddff: qln
spec:
containers:
- name: nginx
image: nginx:1.18.0
#实例
#启动pod
[root@k8s-master-01 ~]# kubectl apply -f tag.yaml
deployment.apps/deployment created
#查看pod
[root@k8s-master-01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-98dbf6f45-gzp7l 1/1 Running 0 18s
#删除pod
[root@k8s-master-01 ~]# kubectl delete pod deployment-98dbf6f45-gzp7l
pod "deployment-98dbf6f45-gzp7l" deleted
#再次查看,发现控制器又创建了一个新的pod
[root@k8s-master-01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-98dbf6f45-97w5k 1/1 Running 0 10s
1.2、弹性扩容
1、修改配置清单
[root@k8s-master-01 ~]# kubectl edit deployments.apps deployment
修改 replicas: 2 参数
2、打标签
[root@k8s-master-01 ~]# kubectl patch deployments.apps deployment -p '{"spec":{"replicas":4}}'
3、scale
[root@k8s-master-01 ~]# kubectl scale deployment/deployment --replicas=1
1.3、更新
1、打标签
[root@k8s-master-01 ~]# kubectl patch deployments.apps deployment -p '{"spec":{"template":{"spec":{"containers":[{"image":"nginx:1.17.0", "name":"nginx"}]}}}}'
2、修改配置清单
3、设置镜像
[root@k8s-master-01 ~]# kubectl set image deployment/deployment nginx=nginx:1.16.0
4、edit
[root@k8s-master-01 ~]# kubectl edit deployments.apps deployment
1.4、回滚
# 查看历史版本
kubectl rollout history [资源类型] [资源名]
[root@k8s-master-01 ~]# kubectl rollout history deployment deployment
deployment.apps/deployment
REVISION CHANGE-CAUSE
2 <none>
3 <none>
4 <none>
#回滚到上个版本
kubectl rollout undo [资源类型] [资源名]
[root@k8s-master-01 ~]# kubectl rollout undo deployment deployment
#回滚到指定历史版本
[root@k8s-master-01 ~]# kubectl rollout history deployment deployment
deployment.apps/deployment
REVISION CHANGE-CAUSE
2 <none>
4 <none>
5 <none>
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]# kubectl rollout undo deployment deployment --to-revision=4
deployment.apps/deployment rolled back
2、DaemonSet
2.1、在配置清单中调用DaemonSet 控制器
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: zabbix-agent
spec:
selector:
matchLabels:
app: zabbix-agent
template:
metadata:
labels:
app: zabbix-agent
spec:
containers:
- name: zabbix-agent
image: zabbix/zabbix-agent:5.2.6-centos
2.2、更新
1、修改配置文件
[root@k8s-m-01 ~]# kubectl edit daemonsets.apps zabbix-agent
daemonset.apps/zabbix-agent edited
2、打标签的方式
[root@k8s-m-01 ~]# kubectl patch daemonsets.apps zabbix-agent -p '{"spec":{"template":{"spec":{"containers":[{"image":"zabbix/zabbix-agent:centos-5.2.4", "name":"zabbix-agent"}]}}}}'
daemonset.apps/zabbix-agent patched
3、设置镜像
[root@k8s-m-01 ~]# kubectl set image daemonset/zabbix-agent zabbix-agent=zabbix/zabbix-agent:centos-5.2.3
daemonset.apps/zabbix-agent image updated
2.3、回滚
# 回滚到上一个版本
[root@k8s-m-01 ~]# kubectl rollout undo daemonset zabbix-agent
daemonset.apps/zabbix-agent rolled back
# 回滚到指定版本
[root@k8s-m-01 ~]# kubectl rollout undo daemonset zabbix-agent --to-revision=1
daemonset.apps/zabbix-agent rolled back