K8s资源对象文件模版获取、控制器、高级控制器详解
K8S资源对象模板获取方式、K8S控制器讲解
·
Kubernetes
资源文件模板获取
#获取Pod模板:kubectl run mypod --image=nginx --dry-run=client -o yaml
#获取deployment模板:kubectl create deployment myweb --image=myos:httpd --dry-run=client -o yaml
#获取HPA模板(必须有deployment控制器)
kubectl autoscale deployment myweb --min=1 --max=3 --cpu-present=50 --dry-run=client -o yaml
控制器
控制器是什么
控制器是K8S内置的管理工具,可以帮助用户实现Pod的自动部署、自动维护、扩容、滚动更新等功能的自动化程序
Deployment控制器
资源对象案例
[root@master ~]# vim mydeploy.yaml
---
kind: Deployment #资源对象类型
apiVersion: apps/v1 #版本
metadata: #元数据
name: myweb #名称
spec: #详细定义
replicas: 2 #副本数量
selector: #定义标签选择器
matchLabels: #同样支持matchExpression表达式语法
app: myhttp #ReplicaSet通过标签来确定哪个Pod由它来管理
template: #定义用来创建Pod的模板
metadata: #Pod元数据
labels: #名称由控制器生成
app: myhttp #定义标签
spec: #Pod的详细定义
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
# 创建服务访问集群
[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 '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控制器
资源对象案例
[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控制器
资源对象案例
[root@master ~]# vim myjob.yaml
---
kind: Job
apiVersion: batch/v1
metadata:
name: myjob
spec:
template:
spec:
restartPolicy: OnFailure #重启策略只支持OnFailure、Never
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
资源对象案例
[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服务
[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控制器
资源对象案例
# 为 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
更多推荐
已为社区贡献6条内容
所有评论(0)