资源限制
上传镜像
[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
[root@k8s2 limit]# kubectl apply -f limit.yaml[root@k8s2 limit]# kubectl get pod
limitrange
[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
[root@k8s2 limit]# kubectl apply -f range.yaml[root@k8s2 limit]# kubectl describe limitranges
创建的pod自动添加限制
[root@k8s2 limit]# kubectl run demo --image nginx[root@k8s2 limit]# kubectl describe pod demo
自定义限制的pod也需要在limitrange定义的区间内
[root@k8s2 limit]# kubectl apply -f limit.yaml
ResourceQuota
[root@k8s2 limit]# vim quota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:name: mem-cpu-demo
spec:hard:requests.cpu: "2"requests.memory: 2Gilimits.cpu: "2"limits.memory: 2Gipods: "3"
[root@k8s2 limit]# kubectl apply -f quota.yaml[root@k8s2 limit]# kubectl describe resourcequotas
- 配额是针对namespace施加的总限额,命名空间内的所有pod资源总和不能超过此配额
- 创建的pod必须定义资源限制
metrics-server
官网:https://github.com/kubernetes-sigs/metrics-server
下载部署文件
[root@k8s2 metrics]# wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
修改部署文件
[root@k8s2 metrics]# vim components.yaml
- --kubelet-insecure-tlsimage: metrics-server/metrics-server:v0.6.4
上传镜像到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-7wmjg
[root@k8s2 metrics]# kubectl top node[root@k8s2 metrics]# kubectl top pod -A --sort-by cpu
dashboard
官网:https://github.com/kubernetes/dashboard
下载部署文件
[root@k8s2 dashboard]# wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
上传所需镜像到harbor
部署
[root@k8s2 dashboard]# kubectl apply -f recommended.yaml
修改svc
[root@k8s2 dashboard]# kubectl -n kubernetes-dashboard edit svc kubernetes-dashboard
[root@k8s2 dashboard]# kubectl -n kubernetes-dashboard get svc
集群需要部署metallb-system,如果没有可以使用NodePort方式
访问:https:192.168.92.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
[root@k8s2 dashboard]# kubectl apply -f rbac.yaml
获取token
[root@k8s2 dashboard]# kubectl -n kubernetes-dashboard create token kubernetes-dashboard
使用token登录网页
k9s
解压
[root@k8s2 ~]# tar zxf k9s_Linux_amd64.tar.gz
部署
[root@k8s2 ~]# mv k9s /usr/bin
启动
[root@k8s2 ~]# k9s
hpa
官网:https://kubernetes.io/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
上传镜像
[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[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负载上升
[root@k8s2 ~]# watch -n1 kubectl top pod[root@k8s2 ~]# kubectl get hpa
触发hpa扩容pod
结束压测后,默认等待5分钟冷却时间,pod会被自动回收
多项量度指标
[root@k8s2 hpa]# kubectl get hpa php-apache -o yaml > hpa-v2.yaml
修改文件,增加内存指标
[root@k8s2 hpa]# vim 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