kubernetes集群编排(8)

编程入门 行业动态 更新时间:2024-10-25 10:21:16

kubernetes<a href=https://www.elefans.com/category/jswz/34/1771240.html style=集群编排(8)"/>

kubernetes集群编排(8)

k8s资源监控

资源限制

上传镜像

[root@k8s2 limit]# vim limit.yaml
apiVersion: v1
kind: Pod
metadata:name: memory-demo
spec:containers:- name: memory-demoimage: stressargs:- --vm- "1"- --vm-bytes- 200Mresources:requests:memory: 50Milimits:memory: 100Mi

limitrange

[root@k8s2 limit]# cat range.yaml
apiVersion: v1
kind: LimitRange
metadata:name: limitrange-memory
spec:limits:- default:cpu: 0.5memory: 512MidefaultRequest:cpu: 0.1memory: 256Mimax:cpu: 1memory: 1Gimin:cpu: 0.1memory: 100Mitype: Container

[root@k8s2 limit]# kubectl apply -f range.yaml
[root@k8s2 limit]# kubectl describe limitrange

创建的pod自动添加限制

[root@k8s2 limit]# kubectl run demo --image nginx

自定义限制的pod也需要在limitrange定义的区间内

ResourceQuota

[root@k8s2 limit]# vim range.yaml
apiVersion: v1
kind: LimitRange
metadata:name: limitrange-memory
spec:limits:- default:cpu: 0.5memory: 512MidefaultRequest:cpu: 0.1memory: 256Mimax:cpu: 1memory: 1Gimin:cpu: 0.1memory: 100Mitype: Container---
apiVersion: v1
kind: ResourceQuota
metadata:name: mem-cpu-demo
spec:hard:requests.cpu: "1"requests.memory: 1Gilimits.cpu: "2"limits.memory: 2Gipods: "2"

[root@k8s2 limit]# kubectl apply -f range.yaml[root@k8s2 limit]# kubectl describe  resourcequotas

配额是针对namespace施加的总限额,命名空间内的所有pod资源总和不能超过此配额

创建的pod必须定义资源限制

kubectl edit quota mem-cpu-demo   编辑指定名称的资源配额对象

 

metrics-server

官网:GitHub - kubernetes-sigs/metrics-server: Scalable and efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines.

下载部署文件

[root@k8s2 metrics]# wget .yaml

修改部署文件

上传镜像到harbor

[root@k8s2 metrics]# kubectl apply -f components.yaml
[root@k8s2 metrics]# kubectl -n kube-system get pod

如有问题,可以查看日志

[root@k8s2 metrics]# kubectl -n kube-system logs metrics-server-5d54764497-8vjm5

[root@k8s2 metrics]# kubectl top node
[root@k8s2 metrics]# kubectl top pod -A --sort-by cpu

dashboard

官网:GitHub - kubernetes/dashboard: General-purpose web UI for Kubernetes clusters

下载部署文件

[root@k8s2 dashboard]# wget .7.0/aio/deploy/recommended.yaml

上传所需镜像到harbor

修改svc为loadbalancer

[root@k8s2 dashboard]# kubectl apply -f recommended.yaml
[root@k8s2 dashboard]# kubectl -n kubernetes-dashboard edit svc kubernetes-dashboard

集群需要部署metallb-system,如果没有可以使用NodePort方式

访问https://192.168.81.101

授权

[root@k8s2 dashboard]# vim rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:name: admin-user
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-admin
subjects:
- kind: ServiceAccountname: kubernetes-dashboardnamespace: kubernetes-dashboard

获取token

[root@k8s2 dashboard]# kubectl apply -f rbac.yaml
[root@k8s2 dashboard]# kubectl -n kubernetes-dashboard create token kubernetes-dashboard

使用token登录网页

 

k9s

[root@k8s2 dashboard]# tar zxf k9s.tar
[root@k8s2 dashboard]# ./k9s

hpa

官网:HorizontalPodAutoscaler 演练 | Kubernetes

上传镜像

[root@k8s2 hpa]# vim hpa.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: php-apache
spec:selector:matchLabels:run: php-apachereplicas: 1template:metadata:labels:run: php-apachespec:containers:- name: php-apacheimage: hpa-exampleports:- containerPort: 80resources:limits:cpu: 500mrequests:cpu: 200m
---
apiVersion: v1
kind: Service
metadata:name: php-apachelabels:run: php-apache
spec:ports:- port: 80selector:run: php-apache

[root@k8s2 hpa]# kubectl apply -f hpa.yaml
[root@k8s2 hpa]# kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
//当 CPU 利用率超过 50% 时,HPA 将根据当前 CPU 利用率来动态地扩展 Deployment 的副本数,使其保持在 1 到 10 之间
[root@k8s2 hpa]# kubectl get hpa

压测

[root@k8s2 hpa]# kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"

pod负载上升

触发hpa扩容pod

结束压测后,默认等待5分钟冷却时间,pod会被自动回收

多项量度指标

[root@k8s2 hpa]# kubectl get hpa php-apache -o yaml > hpa-v2.yaml

修改文件,增加内存指标

  - resource:name: memorytarget:averageValue: 50Mitype: AverageValuetype: Resource

[root@k8s2 hpa]# kubectl apply -f hpa-v2.yaml[root@k8s2 hpa]# kubectl get hpa

更多推荐

kubernetes集群编排(8)

本文发布于:2023-11-15 20:31:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:集群   kubernetes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!