本文主要是介绍控制器详解、临时任务与计划任务、Headless服务/弹性云服务部署(HPA)、弹性云项目实战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
kubernetes
控制器
Deployment
Deploy 图例
Deploy 控制器
# 清理 Pod ,使用控制器创建
[root@master ~]# kubectl delete pod --all
# 资源对象模板
[root@master ~]# kubectl create deployment myweb --image=myos:httpd --dry-run=client -o yaml
[root@master ~]# vim mydeploy.yaml
---
kind: Deployment # 资源对象类型
apiVersion: apps/v1 # 版本
metadata: # 元数据name: myweb # 名称
spec: # 详细定义replicas: 2 # 副本数量selector: # 定义标签选择器matchLabels: # 支持 matchExpressions 表达式语法app: httpd # 通过标签来确定那个 Pod 由它来管理template: # 定义用来创建 Pod 的模板,以下为 Pod 定义metadata:labels:app: httpdspec:restartPolicy: Alwayscontainers:- name: apacheimage: myos:httpdports:- name: myhttpprotocol: TCPcontainerPort: 80[root@master ~]# kubectl apply -f mydeploy.yaml
deployment.apps/myweb created
[root@master ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
myweb 2/2 2 2 68s
[root@master ~]# kubectl get replicasets
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
ClusterIP 服务
[root@master ~]# vim websvc.yaml
---
kind: Service
apiVersion: v1
metadata:name: websvc
spec:type: ClusterIPclusterIP: 10.245.1.80selector:app: httpdports:- protocol: TCPport: 80targetPort: 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.
Pod维护管理
# 自维护自治理
[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
集群扩缩容
# 设置 1 Pod 集群
[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
# 设置 3 Pod 集群
[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 rollout history deployment myweb
deployment.apps/myweb
REVISION CHANGE-CAUSE
1 <none># 添加注释信息
[root@master ~]# kubectl annotate deployments myweb kubernetes.io/change-cause="httpd.v1"
deployment.apps/myweb annotated
[root@master ~]# kubectl rollout history deployment myweb
deployment.apps/myweb
REVISION CHANGE-CAUSE
1 httpd.v1
滚动更新
# 修改镜像,滚动更新集群
[root@master ~]# kubectl set image deployment myweb apache=myos:nginx
deployment.apps/myweb image updated
# 给新版本添加注释信息
[root@master ~]# kubectl annotate deployments myweb kubernetes.io/change-cause="nginx.v1"
deployment.apps/myweb annotated
# 查看历史版本信息
[root@master ~]# kubectl rollout history deployment myweb
deployment.apps/myweb
REVISION CHANGE-CAUSE
1 httpd.v1
2 nginx.v1# 访问验证服务
[root@master ~]# curl -m 3 http://10.245.1.80
Nginx is running !
版本回滚
# 历史版本与回滚
[root@master ~]# kubectl rollout undo deployment myweb --to-revision=1
deployment.apps/myweb rolled back
[root@master ~]# curl -m 3 http://10.245.1.80
Welcome to The Apache.[root@master ~]# kubectl rollout history deployment myweb
deployment.apps/myweb
REVISION CHANGE-CAUSE
2 nginx.v1
3 httpd.v1# 删除控制器方法1
[root@master ~]# kubectl delete deployments myweb
deployment.apps "myweb" deleted
# 删除控制器方法2
[root@master ~]# kubectl delete -f mydeploy.yaml
deployment.apps "myweb" deleted
DaemonSet
DS图例
资源对象案例
[root@master ~]# cp -a mydeploy.yaml myds.yaml
[root@master ~]# vim myds.yaml
---
kind: DaemonSet # 资源对象类型
apiVersion: apps/v1
metadata:name: myds # 控制器名称
spec:# replicas: 2 删除掉副本数量selector:matchLabels:app: httpdtemplate:metadata:labels:app: httpdspec:restartPolicy: Alwayscontainers:- name: apacheimage: myos:httpdports:- name: myhttpprotocol: TCPcontainerPort: 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-msrcx 1/1 Running 0 31s 10.244.1.11 node-0001
myds-lwq8l 1/1 Running 0 31s 10.244.2.17 node-0002
myds-4wt72 1/1 Running 0 31s 10.244.3.14 node-0003
myds-6k82t 1/1 Running 0 31s 10.244.4.15 node-0004
myds-9c6wc 1/1 Running 0 31s 10.244.5.19 node-0005
污点干扰
# 设置污点,重建 daemonset
[root@master ~]# kubectl taint node node-0001 k=v:NoSchedule
node/node-0001 tainted
[root@master ~]# kubectl delete -f myds.yaml
daemonset.apps "myds" deleted
[root@master ~]# kubectl apply -f myds.yaml
daemonset.apps/myds created
# 有污点不会部署,特殊需求可以设置容忍策略
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myds-pkdn9 1/1 Running 0 2s
myds-pkp6b 1/1 Running 0 2s
myds-j84cw 1/1 Running 0 2s
myds-5c69p 1/1 Running 0 2s
# 删除污点后会立即部署
[root@master ~]# kubectl taint node node-0001 k=v:NoSchedule-
node/node-0001 untainted
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myds-2c86p 1/1 Running 0 1s
myds-pkdn9 1/1 Running 0 9s
myds-pkp6b 1/1 Running 0 9s
myds-j84cw 1/1 Running 0 9s
myds-5c69p 1/1 Running 0 9s# 删除控制器
[root@master ~]# kubectl delete -f myds.yaml
daemonset.apps "myds" deleted
Job/CronJob
Job图例
Job 控制器
# 资源对象模板
[root@master ~]# kubectl create job myjob --image=myos:8.5 --dry-run=client -o yaml -- sleep 3
[root@master ~]# vim myjob.yaml
---
kind: Job
apiVersion: batch/v1
metadata:name: myjob
spec:template:spec:restartPolicy: OnFailurecontainers:- name: myjobimage: myos:8.5command: ["/bin/bash"]args:- -c- |sleep 3exit $((RANDOM%2))[root@master ~]# kubectl apply -f myjob.yaml
job.batch/myjob created# 失败了会重启
[root@master ~]# kubectl get pods -l job-name=myjob -w
NAME READY STATUS RESTARTS AGE
myjob--1-lrtbk 1/1 Running 0 2s
myjob--1-lrtbk 0/1 Error 0 4s
myjob--1-lrtbk 1/1 Running 1 (1s ago) 5s
myjob--1-lrtbk 0/1 Completed 1 9s[root@master ~]# kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
myjob 1/1 8s 12s# 删除Job控制器
[root@master ~]# kubectl delete -f myjob.yaml
job.batch "myjob" deleted
CJ图例
资源对象案例
# 资源对象模板
[root@master ~]# kubectl create cronjob mycj --image=myos:8.5 --schedule='* * * * *' --dry-run=client -o yaml -- sleep 3
[root@master ~]# vim mycj.yaml
---
kind: CronJob
apiVersion: batch/v1
metadata:name: mycj
spec:schedule: "*/1 * * * 1-5"jobTemplate:spec:template:spec:restartPolicy: OnFailurecontainers:- name: myjobimage: myos:8.5command: ["/bin/bash"]args:- -c- |sleep 3exit $((RANDOM%2))[root@master ~]# kubectl apply -f mycj.yaml
cronjob.batch/mycj created
[root@master ~]# kubectl get cronjobs
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
mycj */1 * * * 1-5 False 0 <none> 4s# 按照时间周期,每分钟触发一个任务
[root@master ~]# kubectl get jobs -w
NAME READY STATUS RESTARTS AGE
mycj-27808172--1-w6sbx 0/1 Pending 0 0s
mycj-27808172--1-w6sbx 0/1 ContainerCreating 0 0s
mycj-27808172--1-w6sbx 1/1 Running 0 1s
mycj-27808172--1-w6sbx 0/1 Completed 1 4s# 保留三次结果,多余的会被删除
[root@master ~]# kubectl get jobs
NAME COMPLETIONS DURATION AGE
mycj-27605367 1/1 31s 3m30s
mycj-27605368 1/1 31s 2m30s
mycj-27605369 1/1 31s 90s
mycj-27605370 0/1 30s 30s
[root@master ~]# kubectl get jobs
NAME COMPLETIONS DURATION AGE
mycj-27605368 1/1 31s 2m33s
mycj-27605369 1/1 31s 93s
mycj-27605370 1/1 31s 33s# 删除CJ控制器
[root@master ~]# kubectl delete -f mycj.yaml
cronjob.batch "mycj" deleted
StatefulSet
STS图例
headless服务
# 配置headless服务
[root@master ~]# vim mysts.yaml
---
kind: Service
apiVersion: v1
metadata:name: mysvc2
spec:type: ClusterIP # 类型clusterIP: None # IP 设置为 Noneselector:app: apacheports:- protocol: TCPport: 80targetPort: 80[root@master ~]# kubectl apply -f mysts.yaml
service/mysvc2 created
[root@master ~]# kubectl get service mysvc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysvc2 ClusterIP None <none> 80/TCP 61s
资源对象文件
[root@master ~]# vim mysts.yaml
---
kind: Service
apiVersion: v1
metadata:name: mysvc2
spec:type: ClusterIPclusterIP: Noneselector:app: apacheports:- protocol: TCPport: 80targetPort: 80---
kind: StatefulSet
apiVersion: apps/v1
metadata:name: mysts
spec:serviceName: mysvc2replicas: 3selector:matchExpressions:- operator: In #标签选择 有3中匹配模式 In NotIn Exists(模糊)key: appvalues:- apachetemplate:metadata:labels:app: apachespec:restartPolicy: Alwayscontainers:- name: apacheimage: myos:httpdports:- name: webportprotocol: TCPcontainerPort: 80[root@master ~]# kubectl apply -f mysts.yaml
service/mysvc2 unchanged
statefulset.apps/mysts created[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mysts-0 1/1 Running 0 3s
mysts-1 1/1 Running 0 2s
mysts-2 1/1 Running 0 1s[root@master ~]# host mysts-0.mysvc2.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.mysvc2.default.svc.cluster.local has address 10.244.3.81[root@master ~]# host mysvc2.default.svc.cluster.local 10.245.0.10
Using domain server:
Name: 10.245.0.10
Address: 10.245.0.10#53
Aliases: mysvc2.default.svc.cluster.local has address 10.244.2.10
mysvc2.default.svc.cluster.local has address 10.244.1.12
mysvc2.default.svc.cluster.local has address 10.244.3.11#域名访问对应的pod
[root@master ~]# curl -H "Host: mysts-0.mysvc2.default.svc.cluster.local" 10.244.2.10
[root@master ~]# curl -H "Host: mysts-1.mysvc2.default.svc.cluster.local" 10.244.1.12
[root@master ~]# curl -H "Host: mysts-2.mysvc2.default.svc.cluster.local" 10.244.3.11# 删除sts控制器
[root@master ~]# kubectl delete -f mysts.yaml
service "mysvc2" deleted
statefulset.apps "mysts" deleted
HorizontalPodAutoscaling
HPA图例
创建后端服务
# 为 Deploy 模板添加资源配额
[root@master ~]# vim mydeploy.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:name: myweb
spec:replicas: 1selector:matchLabels:app: httpdtemplate:metadata:labels:app: httpdspec:restartPolicy: Alwayscontainers:- name: apacheimage: myos:httpdports:- name: myhttpprotocol: TCPcontainerPort: 80resources: # 为该资源设置配额requests: # HPA 控制器会根据配额使用情况伸缩集群cpu: "200m" # CPU 配额[root@master ~]# vim websvc.yaml
---
kind: Service
apiVersion: v1
metadata:name: websvc
spec:type: ClusterIPclusterIP: 10.245.2.80selector:app: httpdports:- protocol: TCPport: 80targetPort: 80[root@master ~]# kubectl apply -f mydeploy.yaml -f websvc.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.2.80
Welcome to The Apache.
创建HPA控制器
[root@master ~]# vim myhpa.yaml
---
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v1
metadata:name: myweb
spec:minReplicas: 1maxReplicas: 5scaleTargetRef:kind: DeploymentapiVersion: apps/v1name: mywebtargetCPUUtilizationPercentage: 50[root@master ~]# kubectl apply -f myhpa.yaml
horizontalpodautoscaler.autoscaling/myweb created# 刚刚创建 unknown 是正常现象,最多等待 60s 就可以正常获取数据
[root@master ~]# kubectl get horizontalpodautoscalers.autoscaling
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myweb Deployment/myweb <unknown>/50% 1 5 0 4s[root@master ~]# kubectl get horizontalpodautoscalers.autoscaling
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myweb Deployment/myweb 0%/50% 1 5 1 71s
验证测试
# 终端 1 访问提高负载
[root@master ~]# while sleep 1;docurl -s "http://10.245.2.80/info.php?id=80000"
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
项目实战
项目
WEB集群项目:
使用 Nginx 搭建 web 集群,动态页面由 PHP 和 Tomcat 集群解析
使用 NFS 存放网页
将访问日志存放在计算节点的 /var/weblog 目录下
弹性部署 PHP、Tomcat 集群服务
这篇关于控制器详解、临时任务与计划任务、Headless服务/弹性云服务部署(HPA)、弹性云项目实战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!