一 为什么使用控制器

pod控制器

作用:1.pod类型资源删除,不会重建
           2.控制器可以帮助用户监控,并保证节点上运行定义好的pod副本数
           3.pod超过或低于用户期望,控制器会创建、删除pod副本数量

控制器类型:

  1. RS控制器:按照用户期望的副本数量,创建pod, 用户写yaml,定义RS控制器,去定义pod模板,pod按照这个模板去运行
  2. deployment控制器
  3. DaemonSet

二 详解rs控制器原理

RS控制器原理:副本数保障、标签选择器

2.1 创建RS控制器

[root@k8s-master ~]# cat rs-nginx.yml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-rs
  namespace: lxy

spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      name: nginx-pod
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx-containers
        image: nginx:1.14.0
        imagePullPolicy: IfNotPresent
        ports:
          - name: http
            containerPort: 80
kubectl create -f rs-nginx.yml 
kubectl -n lxy get rs -owide
kubectl -n lxy get pods -l app=nginx -owide

 

2.2 删除Pod,查看rs如何保障副本数 

删掉一个pod,但是会马上创建一个

2.3 pod和rs关系 

kubectl -n lxy describe rs nginx-rs 

2.4 修改RS控制器

1.对yaml修改副本数,修改资源配置:kubectl apply -f rs-nginx.yml

2.编辑RS控制器:kubectl -n lxy edit rs nginx-rs  (有些是无法修改)

三 Deploy控制器与弹性扩缩容

kubectl scale --help

kubectl -n lxy scale --replicas=X rs nginx-rs 

root@k8s-master ~]# kubectl -n lxy get pods -owide
NAME             READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
nginx-2          1/1     Running   2          5d17h   10.2.20.143   k8s-node13   <none>           <none>
nginx-rs-2zhzn   1/1     Running   0          7h25m   10.2.231.96   k8s-node12   <none>           <none>
nginx-rs-7p64w   1/1     Running   0          7h25m   10.2.20.146   k8s-node13   <none>           <none>
[root@k8s-master ~]# kubectl -n lxy scale --replicas=4 rs nginx-rs 
replicaset.apps/nginx-rs scaled
[root@k8s-master ~]# kubectl -n lxy get pods -owide
NAME             READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
nginx-2          1/1     Running   2          5d17h   10.2.20.143   k8s-node13   <none>           <none>
nginx-rs-2zhzn   1/1     Running   0          7h34m   10.2.231.96   k8s-node12   <none>           <none>
nginx-rs-7p64w   1/1     Running   0          7h34m   10.2.20.146   k8s-node13   <none>           <none>
nginx-rs-8jf8p   1/1     Running   0          4s      10.2.20.150   k8s-node13   <none>           <none>
nginx-rs-k9dvr   1/1     Running   0          4s      10.2.20.149   k8s-node13   <none>           <none>
[root@k8s-master ~]# kubectl -n lxy scale --replicas=2 rs nginx-rs 
replicaset.apps/nginx-rs scaled
[root@k8s-master ~]# kubectl -n lxy get pods -owide
NAME             READY   STATUS        RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
nginx-2          1/1     Running       2          5d17h   10.2.20.143   k8s-node13   <none>           <none>
nginx-rs-2zhzn   1/1     Running       0          7h34m   10.2.231.96   k8s-node12   <none>           <none>
nginx-rs-7p64w   1/1     Running       0          7h34m   10.2.20.146   k8s-node13   <none>           <none>
nginx-rs-8jf8p   0/1     Terminating   0          12s     10.2.20.150   k8s-node13   <none>           <none>
nginx-rs-k9dvr   0/1     Terminating   0          12s     10.2.20.149   k8s-node13   <none>           <none>

获取当前rs资源的Yaml: kubectl -n lxy get rs nginx-rs -oyaml

四 Deploy与rs原理

Deployments | Kubernetes

kubectl -n kube-system get deploy coredns -o yaml

[root@k8s-master ~]# cat deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: lxy
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 100m
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
[root@k8s-master ~]# kubectl get deployments.apps -owide -n lxy
NAME               READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES         SELECTOR
nginx-deployment   2/2     2            2           110s   nginx        nginx:1.14.2   app=nginx
[root@k8s-master ~]# kubectl get rs -n lxy
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-58df99679c   2         2         2       115s
nginx-rs                      2         2         2       8h
[root@k8s-master ~]# kubectl get pods -l app=nginx -n lxy
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-58df99679c-tqxps   1/1     Running   0          119s
nginx-deployment-58df99679c-zhvcj   1/1     Running   0          119s
nginx-rs-2zhzn                      1/1     Running   0          8h
nginx-rs-7p64w                      1/1     Running   0          8h

pod和rs和deployment关系
kubectl describe pods nginx-deployment-AAA-XX
kubectl describe rs nginx-deployment-AAA 

五 滚动更新详解原理

蓝绿更新:起一个新的Pod+删除一个旧pod,接替就是滚动更新

六 更新镜像版本其他玩法

1.kubectl edit rs资源
2.
kubectl  scale  --replicas=xxx   rs  rs名
3.edit 编辑deployment资源
4.deployment提供了 修改镜像版本 参数   kubectl  set image

七 版本更新与回滚详解

因为rs控制器还在,数据还在 kubectl get rs -owide -w / kubectl get rs -owide -n lxy

#查看版本是1.14.2
kubectl get deployments.apps -n lxy -owide
#修改版本为1.14.1
kubectl -n lxy set image deployment nginx-deployment nginx=nginx:1.14.1
#这时候在查看是1.14.1,使用命令回归后,恢复成1.14.2
kubectl rollout undo deployment nginx-deployment -n lxy

八 详解版本记录号玩法

1.kubectl create -f last-deploy-nginx.yml --record    ----创建的时候,记录版本号

2.查看deploy版本信息

kubectl rollout history deployment  last-nginx-deployment 

kubectl rollout status  deployment  last-nginx-deployment 

kubectl rollout history deployment last-nginx-deployment --revision=2(指定那个版本的镜像信息)

3.指定回滚到哪个版本

kubectl rollout undo  deployment last-nginx-deployment --to-revision=1

九.DaemonSet 每个节点上运行一个Pod的副本

经典用法:

  • 每个节点运行集群的守护进程
  • 每个节点上运行日志收集守护进程 zabbix-agent /elk efk
  • 每个节点上运行监控守护进程

1.查看系统网络插件部署方式是Daemonset

2.根据标签选择器来保证每个node部署一个Pod的calico网络插件

kubectl -n kube-system get pod -owide --show-labels | grep calico
kubectl -n kube-system get ds --show-labels

3.daemonset 和 node 的关系,Node的亲和性

kubectl -n kube-system get ds calico-node -oyaml

4.可以看到nodeSelector中的信息,kubectl get nodes -owide --show-labels

可以看到labels 中含有该信息,是为node的亲和性

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐