当前位置:   article > 正文

Kubernetes学习笔记12

Kubernetes学习笔记12

k8s核心概念:控制器:

我们删除Pod是可以直接删除的,如果生产环境中的误操作,Pod同样也会被轻易地被删除掉。

所以,在K8s中引入另外一个概念:Controller(控制器)的概念,用于在k8s集群中以loop的方式监视pod状态,如果其发现Pod被删除,将会重新拉起一个Pod,以让Pod一直保持在用户期望的状态。

学习目标:

了解Controller作用

了解Controller分类:

了解Deployment控制器的作用:

掌握创建Deployment控制器类型应用方法:

掌握删除Deployment控制器类型应用方法:

课程内容:

Controller介绍:

控制器:

用于对应用运行的资源对象进行监控。使用Loop方式对资源对象Pod监控。

当Pod出现问题的时候,会把Pod重新拉起,以达到用户的期望状态。

Controller分类:

常见Pod控制器:

Deployment控制器介绍:

具有上线部署、滚动升级、创建副本、回滚到以前的某一个版本(成功/稳定)等功能。

Deployment包含ReplicaSet,除非需要自定义升级功能或者根本不需要升级Pod,否则还是建议使用Deployment而不直接使用ReplicaSet。

创建Deployment控制类型的应用:

1)通过kubectl命令行控制:

  1. [root@master1 ~]# kubectl run nginx-app --image=nginx:latest --image-pull-policy=IfNotPresent
  2. pod/nginx-app created
  3. [root@master1 ~]#
  4. [root@master1 ~]#
  5. [root@master1 ~]# kubectl get pods
  6. NAME READY STATUS RESTARTS AGE
  7. nginx-app 1/1 Running 0 10s
  8. pod1 1/1 Running 0 31m

查看了一下kubectl run --help,发现已经没有--replicas选项了。网上建议使用kubectl create deployment。

--image-pull-policy=IfNotPresent: 是container容器镜像的下载策略,如果本地有镜像,使用本地,如果本地没有镜像,下载镜像。

  1. [root@master1 ~]# kubectl create deployment nginx-app2 --image=nginx:latest --port=8080 --replicas=2
  2. deployment.apps/nginx-app2 created
  3. [root@master1 ~]# kubectl get pods
  4. NAME READY STATUS RESTARTS AGE
  5. nginx-app 1/1 Running 0 4m38s
  6. nginx-app2-5b87d59766-86dhn 1/1 Running 0 6s
  7. nginx-app2-5b87d59766-ww9w9 1/1 Running 0 6s
  8. pod1 1/1 Running 0 36m
  9. [root@master1 ~]# kubectl get pods -o wide
  10. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  11. nginx-app 1/1 Running 0 4m50s 10.244.189.70 worker2 <none> <none>
  12. nginx-app2-5b87d59766-86dhn 1/1 Running 0 18s 10.244.189.71 worker2 <none> <none>
  13. nginx-app2-5b87d59766-ww9w9 1/1 Running 0 18s 10.244.235.134 worker1 <none> <none>
  14. pod1 1/1 Running 0 36m 10.244.235.133 worker1 <none> <none>
  15. [root@master1 ~]#

--replicas=2: 是指应用运行的Pod共计2个副本,这是用户的期望值,Deployment控制器中的ReplicaSet控制器会一直监控此应用运行的Pod副本状态,如果数量达不到用户期望,就会重新拉起一个新的Pod,会让Pod数量一直维持在用户期望值数量。

查看Deployment控制器类型的应用,查看Replicaset控制器类型:

Deployment控制器是部署应用的,由Replicaset控制器把它部署为两份。

  1. [root@master1 ~]# kubectl get deployment
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. nginx-app2 2/2 2 2 167m
  4. [root@master1 ~]# kubectl get deployment.apps
  5. NAME READY UP-TO-DATE AVAILABLE AGE
  6. nginx-app2 2/2 2 2 167m

查看Replicaset控制器类型:

  1. [root@master1 ~]# kubectl get replicaset
  2. NAME DESIRED CURRENT READY AGE
  3. nginx-app2-5b87d59766 2 2 2 164m
  4. [root@master1 ~]# kubectl get rs
  5. NAME DESIRED CURRENT READY AGE
  6. nginx-app2-5b87d59766 2 2 2 169m

在名称后面多了一个16进制字符串。

查看Pod状态:

  1. [root@master1 ~]# kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. nginx-app 1/1 Running 0 171m
  4. nginx-app2-5b87d59766-86dhn 1/1 Running 0 166m
  5. nginx-app2-5b87d59766-ww9w9 1/1 Running 0 166m
  6. pod1 1/1 Running 0 3h22m

不建议直接在K8s中直接访问Pod,因为Pod的IP地址是经常发生变化的。

  1. [root@master1 ~]# kubectl get pods -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. nginx-app 1/1 Running 0 176m 10.244.189.70 worker2 <none> <none>
  4. nginx-app2-5b87d59766-86dhn 1/1 Running 0 171m 10.244.189.71 worker2 <none> <none>
  5. nginx-app2-5b87d59766-ww9w9 1/1 Running 0 171m 10.244.235.134 worker1 <none> <none>
  6. pod1 1/1 Running 0 3h27m 10.244.235.133 worker1 <none>
  7. [root@master1 ~]# curl http://10.244.189.71
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <title>Welcome to nginx!</title>
  12. <style>
  13. html { color-scheme: light dark; }
  14. body { width: 35em; margin: 0 auto;
  15. font-family: Tahoma, Verdana, Arial, sans-serif; }
  16. </style>
  17. </head>
  18. <body>
  19. <h1>Welcome to nginx!</h1>
  20. <p>If you see this page, the nginx web server is successfully installed and
  21. working. Further configuration is required.</p>
  22. <p>For online documentation and support please refer to
  23. <a href="http://nginx.org/">nginx.org</a>.<br/>
  24. Commercial support is available at
  25. <a href="http://nginx.com/">nginx.com</a>.</p>
  26. <p><em>Thank you for using nginx.</em></p>
  27. </body>
  28. </html>

二、通过资源清单文件创建

编写用于创建Deployment控制器类型应用清单文件:

  1. [root@master1 ~]# cat 03_create_app.yaml
  2. apiVersion: app/v1
  3. kind: Deployment
  4. metadata:
  5. name: nginx-app3
  6. labels:
  7. app: nginx
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. app: nginx
  13. template:
  14. metadata:
  15. labels:
  16. app: nginx
  17. spec:
  18. containers:
  19. - name: nginxapp3
  20. image: nginx:latest
  21. imagePullPolicy: IfNotPresent
  22. ports:
  23. - name: nginxapp3
  24. containerPort: 80

kind:部署应用类型:

spec:应用期望

replicas:副本数

template:Pod模板

Pod必须加标签,不然没法选择它

selector:选择器

应用来做应用,是通过标签选择器来做选择的。

selector中的标签的值必须要跟模板中的标签的值是一样的。不然选择不上。

imagePullPolicy:IfNotPresent:在生产环境中,我们还是希望每次都进行下载的。

  1. [root@master1 ~]# kubectl apply -f 03_create_app.yaml
  2. deployment.apps/nginx-app3 created

查看deployment控制器类型的应用:

  1. [root@master1 ~]# kubectl get deployment.apps
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. nginx-app2 2/2 2 2 3h17m
  4. nginx-app3 1/1 1 1 44s

查看replicaset类型控制器的应用:

  1. [root@master1 ~]# kubectl get rs
  2. NAME DESIRED CURRENT READY AGE
  3. nginx-app2-5b87d59766 2 2 2 3h18m
  4. nginx-app3-657dcc8694 1 1 1 95s

查看所对应的Pods:

  1. [root@master1 ~]# kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. nginx-app 1/1 Running 0 3h24m
  4. nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h19m
  5. nginx-app2-5b87d59766-ww9w9 1/1 Running 0 3h19m
  6. nginx-app3-657dcc8694-4722k 1/1 Running 0 2m49s
  7. pod1 1/1 Running 0 3h55m
  8. [root@master1 ~]#
  9. [root@master1 ~]#
  10. [root@master1 ~]# kubectl get pods -o wide
  11. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  12. nginx-app 1/1 Running 0 3h24m 10.244.189.70 worker2 <none> <none>
  13. nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h19m 10.244.189.71 worker2 <none> <none>
  14. nginx-app2-5b87d59766-ww9w9 1/1 Running 0 3h19m 10.244.235.134 worker1 <none> <none>
  15. nginx-app3-657dcc8694-4722k 1/1 Running 0 2m59s 10.244.235.135 worker1 <none> <none>
  16. pod1 1/1 Running 0 3h55m 10.244.235.133 worker1 <none> <none>
  17. [root@master1 ~]#

删除Deployment控制器类型的应用:

带有控制器类型的Pod不能随便删除,如果必须删除,请删除控制器类型的应用名称。

  1. [root@master1 ~]# kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. nginx-app 1/1 Running 0 3h28m
  4. nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h23m
  5. nginx-app2-5b87d59766-ww9w9 1/1 Running 0 3h23m
  6. nginx-app3-657dcc8694-4722k 1/1 Running 0 6m54s
  7. pod1 1/1 Running 0 3h59m
  8. [root@master1 ~]#
  9. [root@master1 ~]#
  10. [root@master1 ~]# kubectl delete pods nginx-app
  11. pod "nginx-app" deleted
  12. [root@master1 ~]#
  13. [root@master1 ~]# kubectl get pods
  14. NAME READY STATUS RESTARTS AGE
  15. nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h24m
  16. nginx-app2-5b87d59766-ww9w9 1/1 Running 0 3h24m
  17. nginx-app3-657dcc8694-4722k 1/1 Running 0 7m47s
  18. pod1 1/1 Running 0 4h
  19. [root@master1 ~]# kubectl get pods nginx-app2-5b87d59766-86dhn
  20. NAME READY STATUS RESTARTS AGE
  21. nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h25m
  22. [root@master1 ~]# kubectl get pods
  23. NAME READY STATUS RESTARTS AGE
  24. nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h25m
  25. nginx-app2-5b87d59766-ww9w9 1/1 Running 0 3h25m
  26. nginx-app3-657dcc8694-4722k 1/1 Running 0 8m23s
  27. pod1 1/1 Running 0 4h1m

我们发现deployment类型的Pod是无法删除的,删除之后,就立即重新启动了一个Pod。

从这个地方,我们可以看出K8s集群默认就实现了高可用状态。再也不用在主机时代让虚拟机处于高可用状态。

那我们该如何删除Pods呢?

通过命令行kubectl删除:

  1. [root@master1 ~]# kubectl get deployment.apps
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. nginx-app2 2/2 2 2 3h28m
  4. nginx-app3 1/1 1 1 11m
  5. [root@master1 ~]# kubectl delete deployment nginx-app2
  6. deployment.apps "nginx-app2" deleted
  7. [root@master1 ~]# kubectl get deployment.apps
  8. NAME READY UP-TO-DATE AVAILABLE AGE
  9. nginx-app3 1/1 1 1 11m

可以看到nginx-app2的Pod已经被删除了。

注意:在删除的时候一定要加上类型的名称。

通过kubectl应用资源清单文件删除:

资源清单文件需要进行确认。

  1. [root@master1 ~]# kubectl get deployment.apps
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. nginx-app3 1/1 1 1 13m
  4. [root@master1 ~]# kubectl delete -f 03_create_app.yaml
  5. deployment.apps "nginx-app3" deleted
  6. [root@master1 ~]# kubectl get deployment.apps
  7. No resources found in default namespace.

学习总结:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/399082
推荐阅读
相关标签
  

闽ICP备14008679号