控制器资源

一、控制器

1、介绍

    k8s内拥有许多的控制器类型,用来控制pod的状态、行为、副本数量等等,控制器通过pod的标签来控制pod。
    Pod通过控制器实现应用的运维,如伸缩、升级等,控制器决定了创建pod资源的方式和类型,在集群上管理和运行容器的对象通过label-selector 相关联。

2、控制器常见类型

1、Deployment:一般用来部署长期运行的、无状态的应用
        特点:集群之中,随机部署

2、DaemonSet:每一个节点上部署一个Pod,删除节点自动删除对应的POD(zabbix-agent)
        特点:每一台上有且只有一台

3、StatudfluSet: 部署有状态应用
        特点:有启动顺序

3、Deployment

    在Deployment对象中描述所需的状态,然后Deployment控制器将实际状态以受控的速率更改为所需的状态。
    部署无状态应用

1)创建Depoyment资源

[root@k8s-master-01 ~]# vim test1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      release: stable
  template:
    metadata:
      name: test-tag
      labels:
        release: stable
    spec:
      containers:
        - name: nginx
          image: nginx

参数示意

nginx-deployment:Deployment的名称。
replicas:创建Pod的副本数。
selector:定义Deployment如何找到要管理的Pod,与template的label(标签)对应。
template字段包含以下字段:
    app:nginx使用label(标签)标记Pod
    spec:表示Pod运行一个名字为nginx的容器。
    image:运行此Pod使用的镜像
    Port:容器用于发送和接收流量的端口

2)执行创建命令

[root@k8s-master-01 ~]# kubectl apply -f test1.yaml

[root@k8s-master-01 ~]# kubectl get deployments.apps
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
deployment   1/1     1            1           14h

3)注意

​ 将kubectl标志设置–record为true允许您将当前命令记录在正在创建或更新的资源的注释中。这对于将来的检查很有用。

[root@k8s-master-01 ~]# kubectl apply -f test1.yaml --record
deployment.apps/deployment configured

[root@k8s-master-01 ~]# kubectl rollout  history deployment deployment
deployment.apps/deployment
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=test1.yaml --record=true

4)查看部署状态

[root@k8s-master-01 ~]# kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
deployment-5849786498-4dnsb   1/1     Running   1          17h

5)查看部署详情

[root@k8s-master-01 ~]# kubectl describe  deployments.apps deployment
Name:                   deployment
Namespace:              default
CreationTimestamp:      Wed, 31 Mar 2021 21:17:14 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
                        kubernetes.io/change-cause: kubectl apply --filename=test1.yaml --record=true
Selector:               release=stable
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  release=stable
  Containers:
   nginx:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   deployment-5849786498 (1/1 replicas created)
Events:          <none>

6)弹性扩容

①修改配置清单
[root@k8s-master-01 ~]# kubectl edit deployments.apps deployment
...
spec:
  progressDeadlineSeconds: 600
  replicas: 3
...

#查看
[root@k8s-master-01 ~]# kubectl get pod -w
NAME                          READY   STATUS    RESTARTS   AGE
deployment-5849786498-4dnsb   1/1     Running   1          17h
deployment-5849786498-ddndh   1/1     Running   0          66s
deployment-5849786498-n64hn   1/1     Running   0          67s
②scale
[root@k8s-master-01 ~]# kubectl scale deployment/deployment --replicas=1
deployment.apps/deployment scaled
③打标签
[root@k8s-master-01 ~]# kubectl patch deployments.apps deployment -p '{"spec":{"replicas":10}}'

deployment.apps/deployment patched

#查看
[root@k8s-master-01 ~]# kubectl get pod
NAME                          READY   STATUS              RESTARTS   AGE
deployment-5849786498-4dnsb   1/1     Running             1          17h
deployment-5849786498-4lpjm   1/1     Running             0          47s
deployment-5849786498-4qjx2   0/1     ContainerCreating   0          47s
deployment-5849786498-ddndh   1/1     Running             0          3m56s
deployment-5849786498-jrg9r   1/1     Running             0          47s
deployment-5849786498-n64hn   1/1     Running             0          3m57s
deployment-5849786498-nhdf4   1/1     Running             0          47s
deployment-5849786498-rkhqd   1/1     Running             0          47s
deployment-5849786498-wxs7t   1/1     Running             0          47s
deployment-5849786498-xbwnd   1/1     Running             0          47s
④修改配置清单
[root@k8s-master-01 ~]# vim test1.yaml
...
spec:
  replicas: 3
...
[root@k8s-master-01 ~]# kubectl apply -f test1.yaml

#查看
[root@k8s-master-01 ~]# kubectl get pod -w
NAME                          READY   STATUS    RESTARTS   AGE
deployment-58f6fd8cd7-m6wvh   1/1     Running   0          2m48s
deployment-58f6fd8cd7-txknt   1/1     Running   0          2m48s
deployment-58f6fd8cd7-vhfdr   1/1     Running   0          6m2s

7)更新镜像

①修改主机清单
[root@k8s-master-01 ~]# kubectl edit deployments.apps deployment
...
    spec:
      containers:
      - image: nginx:1.18.0
...

#查看
[root@k8s-master-01 ~]# kubectl describe pod -n default deployment-75d785f89b-sv4vq |grep Image:
    Image:          nginx:1.18.0
②打标签
[root@k8s-master-01 ~]# kubectl patch deployments.apps deployment -p '{"spec":{"template":{"spec":{"containers":[{"image":"nginx:1.16.0","name":"nginx"}]}}}}'
deployment.apps/deployment patched

#查看
[root@k8s-master-01 ~]# kubectl describe pod -n default deployment-75d785f89b-sv4vq |grep Image:
    Image:          nginx:1.16.0
③修改配置清单
[root@k8s-master-01 ~]# vim test1.yaml
...
    spec:
      containers:
        - name: nginx
          image: nginx:1.17.0

...

[root@k8s-master-01 ~]# kubectl apply -f test1.yaml
deployment.apps/deployment configured

[root@k8s-master-01 ~]# kubectl describe pod -n default deployment-58f6fd8cd7-vhfdr |grep Image:
    Image:          nginx:1.17.0
④设置镜像
[root@k8s-master-01 ~]# kubectl set image deployment/deployment nginx=nginx:1.19.0
deployment.apps/deployment image updated

[root@k8s-master-01 ~]# kubectl describe pod -n default deployment-854c7cd5d9-zccw5 |grep Image:
    Image:          nginx:1.19.0

8)回滚

①回滚到上一版本

联系管理员微信tutu19192010,注册账号

上一篇
下一篇
Copyright © 2022 Egon的技术星球 egonlin.com 版权所有 青浦区尚茂路798弄 联系方式-13697081366