k8s-控制器deployment弹性扩容,更新镜像,回滚,DaemonSet,StatufluSet

控制器deployment,DaemonSet,StatefulSet

k8s中控制器分为:deployment、DaemonSet、StatufluSet
Deployment:一般用来部署长期运行的、无状态的应用
	特点:集群之中,随机部署 
DaemonSet:每一个节点上部署一个Pod,删除节点自动删除对应的POD(zabbix-agent)
	特点:每一台上有且只有一台
StatudfluSet: 部署有状态应用
	特点:有启动顺序


查看控制器:
kubectl get deployments
kubectl get daemonsets
kubectl get statefulsets
删除控制器:
kubectl delete deployments 名字
kubectl delete daemonsets 名字
kubectl delete statefulsets 名字

1.控制器-deployment

deployment.yaml

replicas: 1   决定pod部署的数量
Deploymnet:在Deployment对象中描述所需的状态,然后Deployment控制器将实际状态以受控的速率更改为所需的状态。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment
spec:
  replicas: 1  		# pod数量
  selector:  			# 选择器
    matchLabels:  # 标签
      release: stable # 选择标签
  template:  				# 模版
    metadata:
      name: test-tag  # 控制器名字
      labels: 
        release: stable # 设置控制器标签
    spec:
      containers:
        - name: nginx  # 镜像名称
          image: nginx # 镜像 

deployment模版

apiVersion: v1
kind: Service
metadata:
  name: nginx1
spec:
  selector:
    app: nginx1
  ports:
    - name: http
      port: 8088
      targetPort: 80
      protocol: "TCP"
    - name: https
      port: 443
      targetPort: 443
      protocol: "TCP"
  type: NodePort

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx1
  template:
    metadata:
      labels:
        app: nginx1
    spec:
      containers:
        - name: nginx1
          image: nginx:1.21.3

pod静态资源模版

apiVersion: v1
kind: Service
metadata:
  name: nginx2
spec:
  selector:
    app: nginx2
  ports:
    - name: http
      port: 8088
      targetPort: 80
      protocol: "TCP"
    - name: https
      port: 443
      targetPort: 443
      protocol: "TCP"
  type: NodePort

---
apiVersion: v1
kind: Pod
metadata:
  name: nginx2
  labels:
    app: nginx2
spec:
  containers:
    - name: nginx2
      image: nginx:1.21.3

弹性扩容

方式1-修改配置清单

kubectl edit 资源类型 资源名称
[root@sg-14 k8s_yaml]# kubectl edit deployments test
修改replicas数量:
replicas: 2

方式2-打标签

[root@sg-14 k8s_yaml]kubectl patch deployments.apps test -p '{"spec":{"replicas":1}}'

方式3-scale

kubectl scale 资源类型/资源名称 --replicas=10

[root@sg-14 k8s_yaml]# kubectl scale deployment/test --replicas=10

更新镜像

方式1:打标签

一层层的按照yaml写
[root@k8s-m-01 ~]# kubectl patch deployments.apps django -p '{"spec":{"template":{"spec":{"containers":[{"image":"nginx:1.18.0", "name":"nginx"}]}}}}'

方式2:修改配置清单

[root@sg-14 k8s_yaml]# vi test.yaml

image: nginx-lalal
#######
修改镜像版本
#######

kubectl apply test.yaml //重新部署下

方式3:设置镜像

kubectl set image 资源类型/资源名称 镜像名字=镜像版本
[root@k8s-m-01 ~]# kubectl set image deployment/django nginx=nginx:1.16.0

方式4:修改edit

kubectl edit [资源类型] [资源名称]

[root@sg-14 k8s_yaml]# kubectl edit deployments test
找到-image修改

回滚

kubectl rollout undo [资源类型] [资源名称]
kubectl rollout history deployment  test // 版本历史
kubectl rollout undo deployment test // 回滚上一个版本
kubectl rollout undo deployment test --to-revision=1 // 回滚指定版本

2.控制器-DaemonSet

daemonset.yaml

# 在集群中所有的节点上部署只部署一个Pod,新加入的node节点也会部署一个

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: zabbix-agent
spec:
  selector:
    matchLabels:
      app: zabbix-agent
  template:
    metadata:
      labels:
        app: zabbix-agent
    spec:
      containers:
        - name: nginx
          image: nginx

修改配置文件daemonset

1、修改配置文件
[root@k8s-m-01 ~]# kubectl edit daemonsets.apps zabbix-agent 

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

回滚

## 回滚到上一个版本
[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

3.StatefulSet

# StatufluSet最主要的是用来部署有状态应用
# StatefulSet部署是有状态应用,拥有统一的网络,并且有启动顺序。

test.yaml

kind: Service
apiVersion: v1
metadata:
  name: wordpress-test
spec:
  ports:
    - name: http
      port: 80
      targetPort: 80
  selector:
    app: wordpress-test
---
kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: wordpress-test
spec:
  serviceName: wordpress-test
  selector:
    matchLabels:
      app: wordpress-test
  template:
    metadata:
      labels:
        app: wordpress-test
    spec:
      containers:
        - name: php
          image: alvinos/php:wordpress-v2
        - name: nginx
          image: alvinos/nginx:wordpress-v2
posted @ 2021-11-23 11:37  Jeff的技术栈  阅读(1148)  评论(2编辑  收藏  举报
回顶部