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命令行控制:
[root@master1 ~]# kubectl run nginx-app --image=nginx:latest --image-pull-policy=IfNotPresent
pod/nginx-app created
[root@master1 ~]#
[root@master1 ~]#
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app 1/1 Running 0 10s
pod1 1/1 Running 0 31m
查看了一下kubectl run --help,发现已经没有--replicas选项了。网上建议使用kubectl create deployment。
--image-pull-policy=IfNotPresent: 是container容器镜像的下载策略,如果本地有镜像,使用本地,如果本地没有镜像,下载镜像。
[root@master1 ~]# kubectl create deployment nginx-app2 --image=nginx:latest --port=8080 --replicas=2
deployment.apps/nginx-app2 created
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app 1/1 Running 0 4m38s
nginx-app2-5b87d59766-86dhn 1/1 Running 0 6s
nginx-app2-5b87d59766-ww9w9 1/1 Running 0 6s
pod1 1/1 Running 0 36m
[root@master1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app 1/1 Running 0 4m50s 10.244.189.70 worker2 <none> <none>
nginx-app2-5b87d59766-86dhn 1/1 Running 0 18s 10.244.189.71 worker2 <none> <none>
nginx-app2-5b87d59766-ww9w9 1/1 Running 0 18s 10.244.235.134 worker1 <none> <none>
pod1 1/1 Running 0 36m 10.244.235.133 worker1 <none> <none>
[root@master1 ~]#
--replicas=2: 是指应用运行的Pod共计2个副本,这是用户的期望值,Deployment控制器中的ReplicaSet控制器会一直监控此应用运行的Pod副本状态,如果数量达不到用户期望,就会重新拉起一个新的Pod,会让Pod数量一直维持在用户期望值数量。
查看Deployment控制器类型的应用,查看Replicaset控制器类型:
Deployment控制器是部署应用的,由Replicaset控制器把它部署为两份。
[root@master1 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app2 2/2 2 2 167m
[root@master1 ~]# kubectl get deployment.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app2 2/2 2 2 167m
查看Replicaset控制器类型:
[root@master1 ~]# kubectl get replicaset
NAME DESIRED CURRENT READY AGE
nginx-app2-5b87d59766 2 2 2 164m
[root@master1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-app2-5b87d59766 2 2 2 169m
在名称后面多了一个16进制字符串。
查看Pod状态:
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app 1/1 Running 0 171m
nginx-app2-5b87d59766-86dhn 1/1 Running 0 166m
nginx-app2-5b87d59766-ww9w9 1/1 Running 0 166m
pod1 1/1 Running 0 3h22m
不建议直接在K8s中直接访问Pod,因为Pod的IP地址是经常发生变化的。
[root@master1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app 1/1 Running 0 176m 10.244.189.70 worker2 <none> <none>
nginx-app2-5b87d59766-86dhn 1/1 Running 0 171m 10.244.189.71 worker2 <none> <none>
nginx-app2-5b87d59766-ww9w9 1/1 Running 0 171m 10.244.235.134 worker1 <none> <none>
pod1 1/1 Running 0 3h27m 10.244.235.133 worker1 <none>
[root@master1 ~]# curl http://10.244.189.71
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
二、通过资源清单文件创建
编写用于创建Deployment控制器类型应用清单文件:
[root@master1 ~]# cat 03_create_app.yaml
apiVersion: app/v1
kind: Deployment
metadata:
name: nginx-app3
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginxapp3
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- name: nginxapp3
containerPort: 80
kind:部署应用类型:
spec:应用期望
replicas:副本数
template:Pod模板
Pod必须加标签,不然没法选择它
selector:选择器
应用来做应用,是通过标签选择器来做选择的。
selector中的标签的值必须要跟模板中的标签的值是一样的。不然选择不上。
imagePullPolicy:IfNotPresent:在生产环境中,我们还是希望每次都进行下载的。
[root@master1 ~]# kubectl apply -f 03_create_app.yaml
deployment.apps/nginx-app3 created
查看deployment控制器类型的应用:
[root@master1 ~]# kubectl get deployment.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app2 2/2 2 2 3h17m
nginx-app3 1/1 1 1 44s
查看replicaset类型控制器的应用:
[root@master1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-app2-5b87d59766 2 2 2 3h18m
nginx-app3-657dcc8694 1 1 1 95s
查看所对应的Pods:
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app 1/1 Running 0 3h24m
nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h19m
nginx-app2-5b87d59766-ww9w9 1/1 Running 0 3h19m
nginx-app3-657dcc8694-4722k 1/1 Running 0 2m49s
pod1 1/1 Running 0 3h55m
[root@master1 ~]#
[root@master1 ~]#
[root@master1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app 1/1 Running 0 3h24m 10.244.189.70 worker2 <none> <none>
nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h19m 10.244.189.71 worker2 <none> <none>
nginx-app2-5b87d59766-ww9w9 1/1 Running 0 3h19m 10.244.235.134 worker1 <none> <none>
nginx-app3-657dcc8694-4722k 1/1 Running 0 2m59s 10.244.235.135 worker1 <none> <none>
pod1 1/1 Running 0 3h55m 10.244.235.133 worker1 <none> <none>
[root@master1 ~]#
删除Deployment控制器类型的应用:
带有控制器类型的Pod不能随便删除,如果必须删除,请删除控制器类型的应用名称。
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app 1/1 Running 0 3h28m
nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h23m
nginx-app2-5b87d59766-ww9w9 1/1 Running 0 3h23m
nginx-app3-657dcc8694-4722k 1/1 Running 0 6m54s
pod1 1/1 Running 0 3h59m
[root@master1 ~]#
[root@master1 ~]#
[root@master1 ~]# kubectl delete pods nginx-app
pod "nginx-app" deleted
[root@master1 ~]#
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h24m
nginx-app2-5b87d59766-ww9w9 1/1 Running 0 3h24m
nginx-app3-657dcc8694-4722k 1/1 Running 0 7m47s
pod1 1/1 Running 0 4h
[root@master1 ~]# kubectl get pods nginx-app2-5b87d59766-86dhn
NAME READY STATUS RESTARTS AGE
nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h25m
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app2-5b87d59766-86dhn 1/1 Running 0 3h25m
nginx-app2-5b87d59766-ww9w9 1/1 Running 0 3h25m
nginx-app3-657dcc8694-4722k 1/1 Running 0 8m23s
pod1 1/1 Running 0 4h1m
我们发现deployment类型的Pod是无法删除的,删除之后,就立即重新启动了一个Pod。
从这个地方,我们可以看出K8s集群默认就实现了高可用状态。再也不用在主机时代让虚拟机处于高可用状态。
那我们该如何删除Pods呢?
通过命令行kubectl删除:
[root@master1 ~]# kubectl get deployment.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app2 2/2 2 2 3h28m
nginx-app3 1/1 1 1 11m
[root@master1 ~]# kubectl delete deployment nginx-app2
deployment.apps "nginx-app2" deleted
[root@master1 ~]# kubectl get deployment.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app3 1/1 1 1 11m
可以看到nginx-app2的Pod已经被删除了。
注意:在删除的时候一定要加上类型的名称。
通过kubectl应用资源清单文件删除:
资源清单文件需要进行确认。
[root@master1 ~]# kubectl get deployment.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app3 1/1 1 1 13m
[root@master1 ~]# kubectl delete -f 03_create_app.yaml
deployment.apps "nginx-app3" deleted
[root@master1 ~]# kubectl get deployment.apps
No resources found in default namespace.
学习总结: