kubernetes-控制器
控制器
Pod 的分类:
自主式 Pod:Pod 退出后不会被创建
控制器管理的 Pod:在控制器的生命周期里,始终要维持 Pod 的副本数目
控制器类型:
Replication Controller和ReplicaSet
Deployment
DaemonSet
StatefulSet
Job
CronJob
ReplicaSet
ReplicaSet 确保任何时间都有指定数量的 Pod 副本在运行。
[root@server2 pod]# cat rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-example
spec:
replicas: 6
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
[root@server2 pod]# kubectl apply -f rs.yaml
replicaset.apps/replicaset-example created
[root@server2 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
replicaset-example-4qfm8 1/1 Running 0 7s
replicaset-example-c7psq 1/1 Running 0 7s
replicaset-example-gx6h7 1/1 Running 0 7s
replicaset-example-lcrj2 1/1 Running 0 7s
replicaset-example-p7h77 1/1 Running 0 7s
replicaset-example-zfwfz 1/1 Running 0 7s
[root@server2 pod]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
replicaset-example-4qfm8 1/1 Running 0 99s 10.244.5.7 server3 <none> <none>
replicaset-example-c7psq 1/1 Running 0 99s 10.244.5.9 server3 <none> <none>
replicaset-example-gx6h7 1/1 Running 0 99s 10.244.5.8 server3 <none> <none>
replicaset-example-lcrj2 1/1 Running 0 99s 10.244.6.6 server4 <none> <none>
replicaset-example-p7h77 1/1 Running 0 99s 10.244.6.5 server4 <none> <none>
replicaset-example-zfwfz 1/1 Running 0 99s 10.244.5.10 server3 <none> <none>
[root@server2 pod]# kubectl get rs
NAME DESIRED CURRENT READY AGE
replicaset-example 6 6 6 2m31s
[root@server2 pod]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-example-4qfm8 1/1 Running 0 4m app=nginx
replicaset-example-c7psq 1/1 Running 0 4m app=nginx
replicaset-example-gx6h7 1/1 Running 0 4m app=nginx
replicaset-example-lcrj2 1/1 Running 0 4m app=nginx
replicaset-example-p7h77 1/1 Running 0 4m app=nginx
replicaset-example-zfwfz 1/1 Running 0 4m app=nginx
[root@server2 pod]# cat rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-example
spec:
replicas: 6
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: myapp:v1
[root@server2 pod]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-example-26kmd 1/1 Running 0 9s app=nginx
replicaset-example-4qfm8 1/1 Running 0 10m app=myapp
replicaset-example-c7psq 1/1 Running 0 10m app=nginx
replicaset-example-gx6h7 1/1 Running 0 10m app=nginx
replicaset-example-lcrj2 1/1 Running 0 10m app=nginx
replicaset-example-p7h77 1/1 Running 0 10m app=nginx
replicaset-example-zfwfz 1/1 Running 0 10m app=nginx
Deployment
典型的应用场景:
用来创建Pod和ReplicaSet
滚动更新和回滚
扩容和缩容
暂停与恢复
更换image
[root@server2 pod]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
[root@server2 pod]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-6799fc88d8 3 3 3 17s
replicaset-example 6 6 6 21s
[root@server2 pod]# kubectl delete -f rs.yaml
replicaset.apps "replicaset-example" deleted
[root@server2 pod]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-6799fc88d8-h87b4 1/1 Running 0 85s 10.244.6.12 server4 <none> <none>
nginx-deployment-6799fc88d8-s222f 1/1 Running 0 85s 10.244.6.13 server4 <none> <none>
nginx-deployment-6799fc88d8-vpl5r 1/1 Running 0 85s 10.244.6.11 server4 <none> <none>
[root@server2 pod]# curl 10.244.6.12
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
[root@server2 pod]# vim deployment.yaml
[root@server2 pod]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: myapp:v1
[root@server2 pod]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-c5fbbb494-kpw2n 1/1 Running 0 22s 10.244.5.14 server3 <none> <none>
nginx-deployment-c5fbbb494-ltn64 1/1 Running 0 20s 10.244.5.15 server3 <none> <none>
nginx-deployment-c5fbbb494-thzkn 1/1 Running 0 18s 10.244.6.14 server4 <none> <none>
[root@server2 pod]# curl 10.244.5.14
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
更换版本
[root@server2 pod]# vim deployment.yaml
[root@server2 pod]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: myapp:v2
[root@server2 pod]# kubectl apply -f deployment.yaml
deployment.apps/nginx-deployment configured
[root@server2 pod]# kubectl get pod -o wide
nginx-deployment-c5fbbb494-kpw2n 1/1 Running 0 55s 10.244.3.16 server3 <none> <none>
nginx-deployment-c5fbbb494-ltn64 1/1 Running 0 53s 10.244.3.14 server3 <none> <none>
nginx-deployment-c5fbbb494-thzkn 1/1 Running 0 51s 10.244.4.12 server4 <none> <none>
[root@server2 pod]# curl 10.244.3.16
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
DaemonSet
DaemonSet 确保节点上运行一个 Pod 。当有节点加入集群时, 为他们新增一个 Pod 。当有节点被移除集群时,Pod 也会被回收。删除 DaemonSet 会删除它创建的所有 Pod。
[root@server2 pod]# cat daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-example
labels:
k8s-app: zabbix-agent
spec:
selector:
matchLabels:
name: zabbix-agent
template:
metadata:
labels:
name: zabbix-agent
spec:
containers:
- name: zabbix-agent
image: zabbix/zabbix-agent
StatefulSets
StatefulSets 对于需要满足以下一个或多个需求的应用程序很有价值:
稳定的、唯一的网络标识符。
稳定的、持久的存储。
有序的、优雅的部署和缩放。
有序的、自动的滚动更新。
在上面描述中,“稳定的”意味着 Pod 调度或重调度的整个过程是有持久性的。 如果应用程序不需要任何稳定的标识符或有序的部署、删除或伸缩,则应该使用 由一组无状态的副本控制器提供的工作负载来部署应用程序,比如 Deployment 或者 ReplicaSet 可能更适用于你的无状态应用部署需要。
Jobs
Job 会创建一个或者多个 Pods,并将继续重试 Pods 的执行,直到指定数量的 Pods 成功终止。 随着 Pods 成功结束,Job 跟踪记录成功完成的 Pods 个数。 当数量达到指定的成功个数阈值时,任务(即 Job)结束。 删除 Job 的操作会清除所创建的全部 Pods。 挂起 Job 的操作会删除 Job 的所有活跃 Pod,直到 Job 被再次恢复执行。
[root@server2 pod]# docker pull perl
Using default tag: latest
latest: Pulling from library/perl
bd8f6a7501cc: Pull complete
44718e6d535d: Pull complete
efe9738af0cb: Pull complete
f37aabde37b8: Pull complete
3923d444ed05: Pull complete
6c4f79c865a9: Pull complete
d5994d8abc17: Pull complete
Digest: sha256:0245ddad7966262b2df36c6e7effb406b6eee45c1d7cb654097b574bf51e70b5
Status: Downloaded newer image for perl:latest
docker.io/library/perl:latest
[root@server2 pod]# cat job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
[root@server2 pod]# kubectl apply -f job.yaml
job.batch/pi created
[root@server2 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pi-zwfc5 0/1 ContainerCreating 0 22s
[root@server2 pod]# kubectl logs pi-zwfc5
^[[A3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
CronJob
ronJob 创建基于时隔重复调度的 Jobs。
一个 CronJob 对象就像 crontab (cron table) 文件中的一行。 它用 Cron 格式进行编写, 并周期性地在给定的调度时间执行 Job。