您现在的位置是:首页 >技术教程 >K8s控制器网站首页技术教程
K8s控制器
创建资源文件模板
kubectl create <tab><tab> 下面的所有都可以创建模板文件 --dry-run=client -o yaml
# 以下资源对象都可以用模板生成
[root@master ~]# kubectl create <tab><tab>
clusterrole deployment poddisruptionbudget rolebinding
clusterrolebinding ingress priorityclass secret
configmap job quota service
cronjob namespace role serviceaccount
查询资源对象的帮助信息
kubectl explain pod.spec.restartPolicy #这里对上下层级关系需要清楚
# 查看帮助信息
[root@master ~]# kubectl explain pod.spec.restartPolicy
KIND: Pod
VERSION: v1
FIELD: restartPolicy <string>
DESCRIPTION:
Restart policy for all containers within the pod.
One of Always, OnFailure, Never. Default to Always. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy
获取Pod模板
kubectl run mypod --image=xxxx --dry-run=client -o yaml
# Pod 模板
[root@master ~]# kubectl run mypod --image=myos:httpd --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: mypod
name: mypod
spec:
containers:
- image: myos:httpd
name: mypod
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
获取Deployment
kubectl create dewployment myeb --image=myos:httpd --dry-run=client -o yaml
# Deployment 模板
[root@master ~]# kubectl create deployment myweb --image=myos:httpd --dry-run=client -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: myweb
name: myweb
spec:
replicas: 1
selector:
matchLabels:
app: myweb
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: myweb
spec:
containers:
- image: myos:httpd
name: myos
resources: {}
status: {}
控制器
可以帮助用户实现Pod的自动部署,自维护,扩容,滚动更新等自动化程序
Deployment控制器(副本数量保持)
最常用的无状态服务控制器,由Deployment 、 ReplicaSet 、 Pod组成支持集群扩容缩容,滚动,更新,自动维护Pod可用性及副本数量等功能
ReplicaSet 和 Pod 由 Deployment自动管理,用户无需干预
[root@master ~]# vim mydeploy.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: myweb
spec:
replicas: 2
selector:
matchLabels:
app: myhttp
template:
metadata:
labels:
app: myhttp
spec:
restartPolicy: Always
containers:
- name: apache
image: myos:httpd
ports:
- name:
protocol: TCP
containerPort: 80
[root@master ~]# kubectl apply -f mydeploy.yaml
deployment.apps/myweb created
[root@master ~]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
myweb 2/2 2 2 68s
[root@master ~]# kubectl get replicasets.apps
NAME DESIRED CURRENT READY AGE
myweb-64b544dcbc 2 2 2 73s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-64b544dcbc-5mhqn 1/1 Running 0 76s
myweb-64b544dcbc-nt6tz 1/1 Running 0 76s
测试Deploment
# 创建服务访问集群
[root@master ~]# vim websvc.yaml
---
kind: Service
apiVersion: v1
metadata:
name: websvc
spec:
type: ClusterIP
clusterIP: 10.245.1.80
selector:
app: myhttp
ports:
- protocol: TCP
port: 80
targetPort: 80
[root@master ~]# kubectl apply -f websvc.yaml
service/websvc created
[root@master ~]# curl -m 3 http://10.245.1.80
Welcome to The Apache.
# 自维护自治理
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-64b544dcbc-5mhqn 1/1 Running 0 4m16s
myweb-64b544dcbc-nt6tz 1/1 Running 0 4m16s
# Pod 被删除后,Deploy 会自动创建新的 Pod 来维护集群的完整性
[root@master ~]# kubectl delete pod myweb-64b544dcbc-5mhqn
pod "myweb-64b544dcbc-5mhqn" deleted
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-64b544dcbc-g8l9p 1/1 Running 0 3s
myweb-64b544dcbc-nt6tz 1/1 Running 0 4m25s
# 动态更新集群副本数量
[root@master ~]# kubectl scale deployment myweb --replicas=1
deployment.apps/myweb scaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-64b544dcbc-nt6tz 1/1 Running 0 5m46s
[root@master ~]# kubectl scale deployment myweb --replicas=3
deployment.apps/myweb scaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-64b544dcbc-5sf5z 1/1 Running 0 3s
myweb-64b544dcbc-6r6dw 1/1 Running 0 3s
myweb-64b544dcbc-nt6tz 1/1 Running 0 5m56s
历史版本、滚动更新
# 添加注释信息
[root@master ~]# kubectl annotate deployments.apps myweb kubernetes.io/change-cause="httpd.v1"
deployment.apps/myweb annotated
# 修改镜像,滚动更新集群
[root@master ~]# curl -m 3 http://10.245.1.80
Welcome to The Apache.
[root@master ~]# sed -r 's,(image: ).*,1myos:nginx,' mydeploy.yaml |kubectl apply -f -
deployment.apps/myweb configured
# 更新注释信息
[root@master ~]# kubectl annotate deployments.apps myweb kubernetes.io/change-cause="nginx.v1"
deployment.apps/myweb annotated
[root@master ~]# kubectl get replicasets.apps
NAME DESIRED CURRENT READY AGE
myweb-5bfdc888d7 2 2 2 3m46s
myweb-64b544dcbc 0 0 0 12m
[root@master ~]# curl -m 3 http://10.245.1.80
Nginx is running !
# 历史版本与回滚
[root@master ~]# kubectl rollout history deployment myweb
deployment.apps/myweb
REVISION CHANGE-CAUSE
1 httpd.v1
2 nginx.v1
[root@master ~]# kubectl rollout undo deployment myweb --to-revision=1
deployment.apps/myweb rolled back
[root@master ~]# kubectl rollout history deployment myweb
deployment.apps/myweb
REVISION CHANGE-CAUSE
2 nginx.v1
3 httpd.v1
[root@master ~]# curl -m 3 http://10.245.1.80
Welcome to The Apache.
# 删除控制器方法1
[root@master ~]# kubectl delete deployments.apps myweb
deployment.apps "myweb" deleted
# 删除控制器方法2
[root@master ~]# kubectl delete -f mydeploy.yaml
deployment.apps "myweb" deleted
DaemonSet控制器(根据node)
无法自定义副本数量,所创建的Pod与node节点绑定
每个node上都会运行一个Pod
当有新Node加入集群时会为他新增Pod副本,当Node从集群移除时,这些Pod也会被回收,典型的应用kube-proxy
会受到污点策略的影响
[root@master ~]# vim myds.yaml
---
kind: DaemonSet
apiVersion: apps/v1
metadata:
name: myds
spec:
selector:
matchLabels:
app: myhttp
template:
metadata:
labels:
app: myhttp
spec:
restartPolicy: Always
containers:
- name: apache
image: myos:httpd
ports:
- name:
protocol: TCP
containerPort: 80
[root@master ~]# kubectl apply -f myds.yaml
daemonset.apps/myds created
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
myds-4wt72 1/1 Running 0 31s 10.244.3.14 node-0003
myds-lwq8l 1/1 Running 0 31s 10.244.2.17 node-0002
myds-msrcx 1/1 Running 0 31s 10.244.1.11 node-0001
# 删除DS控制器
[root@master ~]# kubectl delete -f myds.yaml
daemonset.apps "myds" deleted
Job/CronJob
Job是一个单任务控制器,负责执行一次任务,保证任务在一个或多个Pod上执行成功CronJob基于时间管理的Job
执行完才是1/1 没执行完是0/1
[root@master ~]# vim myjob.yaml
---
kind: Job
apiVersion: batch/v1
metadata:
name: myjob
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: myjob
image: myos:latest
command: ["/bin/sleep", "30"]
[root@master ~]# kubectl apply -f myjob.yaml
job.batch/myjob created
[root@master ~]# kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
myjob 0/1 3s 3s
[root@master ~]# kubectl get pods -l job-name=myjob
NAME READY STATUS RESTARTS AGE
myjob--1-kz8mk 1/1 Running 0 6s
[root@master ~]# sleep 30
[root@master ~]# kubectl get pods -l job-name=myjob
NAME READY STATUS RESTARTS AGE
myjob--1-kz8mk 0/1 Completed 0 58s
[root@master ~]# kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
myjob 1/1 31s 64s
# 删除Job控制器
[root@master ~]# kubectl delete -f myjob.yaml
job.batch "myjob" deleted
CronJob
达到条件才运行,保留三次运行结果,多出来的把之前的删除
[root@master ~]# vim mycj.yaml
---
kind: CronJob
apiVersion: batch/v1
metadata:
name: mycronjob
spec:
schedule: "*/1 * * * 1-5" #调度字段周期分时日月周
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: myjob
image: myos:latest
command: ["/bin/sleep", "30"]
[root@master ~]# kubectl apply -f mycj.yaml
cronjob.batch/mycronjob created
[root@master ~]# kubectl get cronjobs.batch
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
mycronjob */1 * * * 1-5 False 0 <none> 4s
[root@master ~]# kubectl get jobs.batch
No resources found in default namespace.
[root@master ~]# kubectl get pods
No resources found in default namespace.
[root@master ~]# sleep 60
[root@master ~]# kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
mycronjob-27605367 0/1 0s 0s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-27605367--1-ps6r8 1/1 Running 0 3s
[root@master ~]# kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
mycronjob-27605367 0/1 12s 12s
[root@master ~]# sleep 200
# 保留三次结果,多余的会被删除
[root@master ~]# kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
mycronjob-27605367 1/1 31s 3m30s
mycronjob-27605368 1/1 31s 2m30s
mycronjob-27605369 1/1 31s 90s
mycronjob-27605370 0/1 30s 30s
[root@master ~]# kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
mycronjob-27605368 1/1 31s 2m33s
mycronjob-27605369 1/1 31s 93s
mycronjob-27605370 1/1 31s 33s
# 删除CJ控制器
[root@master ~]# kubectl delete -f mycj.yaml
cronjob.batch "mycronjob" deleted
高级控制器StatefulSet
与有状态的应用及分布式系统一起使用,涉及了Headless服务,存储卷,DNS等相关知识点,是一个复杂的话题
一个一个创建,无核服务先创建
# 配置headless服务
[root@master ~]# vim mysvc.yaml
---
kind: Service
apiVersion: v1
metadata:
name: mysvc
spec:
type: ClusterIP
clusterIP: None
selector:
app: myhttp
ports:
- protocol: TCP
port: 80
targetPort: 80
[root@master ~]# kubectl apply -f mysvc.yaml
service/mysvc created
[root@master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysvc ClusterIP None <none> 80/TCP 61s
# 创建statefulset控制器
[root@master ~]# vim mysts.yaml
---
kind: StatefulSet
apiVersion: apps/v1
metadata:
name: mysts
spec:
serviceName: "mysvc"
selector:
matchExpressions:
- key: app
operator: In
values:
- myhttp
replicas: 3
template:
metadata:
labels:
app: myhttp
spec:
restartPolicy: Always
containers:
- name: apache
image: myos:httpd
imagePullPolicy: Always
ports:
- protocol: TCP
containerPort: 80
[root@master ~]# kubectl apply -f mysts.yaml
statefulset.apps/mysts created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mysts-0 1/1 Running 0 4s
mysts-1 1/1 Running 0 3s
mysts-2 1/1 Running 0 2s
[root@master ~]# host mysts-0.mysvc.default.svc.cluster.local 10.245.0.10
Using domain server:
Name: 10.245.0.10
Address: 10.245.0.10#53
Aliases:
mysts-0.mysvc.default.svc.cluster.local has address 10.244.3.81
[root@master ~]# host mysvc.default.svc.cluster.local 10.245.0.10
Using domain server:
Name: 10.245.0.10
Address: 10.245.0.10#53
Aliases:
mysvc.default.svc.cluster.local has address 10.244.2.10
mysvc.default.svc.cluster.local has address 10.244.1.12
mysvc.default.svc.cluster.local has address 10.244.3.11
[root@master ~]# curl -m 3 http://10.244.2.10/info.php
<pre>
Array
(
[REMOTE_ADDR] => 10.244.0.0
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /info.php
)
php_host: mysts-2
1229
# 删除sts控制器
[root@master ~]# kubectl delete -f mysts.yaml -f mysvc.yaml
statefulset.apps "mysts" deleted
service "mysvc" deleted
HPA控制器
HorizontalPodAutoscaling简称HPA在集群中基于CPU利用率或者其他程序提供的度量值实现水平自动伸缩的功能,自动缩放Pod的数量
控制器会周期性的获取平均利用率
与目标值相比较后调整副本数量
# 为 Deploy 模板添加资源配额
[root@master ~]# vim mydeploy.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: myweb
spec:
replicas: 1
selector:
matchLabels:
app: myhttp
template:
metadata:
labels:
app: myhttp
spec:
restartPolicy: Always
containers:
- name: apache
image: myos:httpd
ports:
- name:
protocol: TCP
containerPort: 80
resources:
requests:
cpu: "200m"
---
kind: Service
apiVersion: v1
metadata:
name: websvc
spec:
type: ClusterIP
clusterIP: 10.245.1.80
selector:
app: myhttp
ports:
- protocol: TCP
port: 80
targetPort: 80
# 使用 Deploy 和 Cluster IP 创建集群
[root@master ~]# kubectl apply -f mydeploy.yaml
deployment.apps/myweb created
service/websvc created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-5946bb4c68-c8tm2 1/1 Running 0 2m43s
[root@master ~]# curl -s http://10.245.1.80
Welcome to The Apache.
# 创建 HPA 控制器
[root@master ~]# vim myhpa.yaml
---
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v1
metadata:
name: myweb
spec:
minReplicas: 1
maxReplicas: 3
scaleTargetRef:
kind: Deployment
apiVersion: apps/v1
name: myweb
targetCPUUtilizationPercentage: 50
[root@master ~]# kubectl apply -f myhpa.yaml
horizontalpodautoscaler.autoscaling/myweb created
# 刚刚创建 unknown 是正常现象,最多等待 300s 就可以正常获取数据
[root@master ~]# kubectl get horizontalpodautoscalers.autoscaling
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myweb Deployment/myweb <unknown>/50% 1 3 0 4s
[root@master ~]# kubectl get horizontalpodautoscalers.autoscaling
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myweb Deployment/myweb 0%/50% 1 3 1 71s
# 终端 1 访问提高负载
[root@master ~]# while true;do
curl -s "http://10.245.1.80/info.php?id=50000"
sleep 1
done
# 终端 2 监控 HPA 变化
[root@master ~]# kubectl get hpa -w
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myweb Deployment/myweb 0%/50% 1 3 1 8m21s
myweb Deployment/myweb 49%/50% 1 3 1 9m
myweb Deployment/myweb 56%/50% 1 3 1 9m15s
myweb Deployment/myweb 56%/50% 1 3 2 9m30s
myweb Deployment/myweb 37%/50% 1 3 2 9m45s
myweb Deployment/myweb 32%/50% 1 3 2 10m
myweb Deployment/myweb 41%/50% 1 3 2 11m
myweb Deployment/myweb 48%/50% 1 3 2 11m
myweb Deployment/myweb 51%/50% 1 3 2 11m
myweb Deployment/myweb 59%/50% 1 3 2 11m
myweb Deployment/myweb 58%/50% 1 3 3 12m
myweb Deployment/myweb 42%/50% 1 3 3 12m
myweb Deployment/myweb 34%/50% 1 3 3 12m
# 如果 300s 内平均负载小于标准值,就会自动缩减集群规模
[root@master ~]# kubectl get hpa -w
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myweb Deployment/myweb 38%/50% 1 3 3 19m
myweb Deployment/myweb 21%/50% 1 3 3 20m
myweb Deployment/myweb 17%/50% 1 3 3 21m
myweb Deployment/myweb 7%/50% 1 3 3 22m
myweb Deployment/myweb 0%/50% 1 3 3 23m
myweb Deployment/myweb 0%/50% 1 3 2 25m
myweb Deployment/myweb 0%/50% 1 3 1 28m
[root@master ~]# kubectl get horizontalpodautoscalers.autoscaling
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myweb Deployment/myweb 0%/50% 1 3 1 5m41s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-5946bb4c68-f9tw9 1/1 Running 0 6m40s