目录
上传镜像
- vim limit.yaml
-
- apiVersion: v1
- kind: Pod
- metadata:
- name: memory-demo
- spec:
- containers:
- - name: memory-demo
- image: stress
- args:
- - --vm
- - "1"
- - --vm-bytes
- - 200M
- resources:
- requests:
- memory: 50Mi
- limits:
- memory: 100Mi
-
- kubectl apply -f limit.yaml
- kubectl get pod
- vim range.yaml
-
- apiVersion: v1
- kind: LimitRange
- metadata:
- name: limitrange-memory
- spec:
- limits:
- - default:
- cpu: 0.5
- memory: 512Mi
- defaultRequest:
- cpu: 0.1
- memory: 256Mi
- max:
- cpu: 1
- memory: 1Gi
- min:
- cpu: 0.1
- memory: 100Mi
- type: Container
-
- kubectl apply -f range.yaml
LimitRange 在 namespace 中施加的最小和最大内存限制只有在创建和更新 Pod 时才会被应用。改变 LimitRange 不会对之前创建的 Pod 造成影响。
创建的pod自动添加限制
kubectl run demo --image nginx
自定义限制的pod也需要在limitrange定义的区间内
- vim range.yaml
-
- 添加进
- ---
- apiVersion: v1
- kind: ResourceQuota
- metadata:
- name: mem-cpu-demo
- spec:
- hard:
- requests.cpu: "1"
- requests.memory: 1Gi
- limits.cpu: "2"
- limits.memory: 2Gi
- pods: "2"
-
- kubectl apply -f range.yaml
- kubectl describe resourcequotas
官网:https://github.com/kubernetes-sigs/metrics-server
下载部署文件
wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
修改部署文件
上传镜像到harbor
- kubectl apply -f components.yaml
- kubectl -n kube-system logs metrics-server-
官网:https://github.com/kubernetes/dashboard
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
上传所需镜像到harbor
部署
kubectl apply -f recommended.yaml
修改svc
kubectl -n kubernetes-dashboard edit svc kubernetes-dashboard
访问:
授权 获取token
- vim rbac.yaml
-
- apiVersion: rbac.authorization.k8s.io/v1
- kind: ClusterRoleBinding
- metadata:
- name: admin-user
- roleRef:
- apiGroup: rbac.authorization.k8s.io
- kind: ClusterRole
- name: cluster-admin
- subjects:
- - kind: ServiceAccount
- name: kubernetes-dashboard
- namespace: kubernetes-dashboard
-
- kubectl apply -f rbac.yaml
- kubectl -n kubernetes-dashboard create token kubernetes-dashboard
使用token登录网页
使用图像化创建
- vim hpa.yaml
-
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: php-apache
- spec:
- selector:
- matchLabels:
- run: php-apache
- replicas: 1
- template:
- metadata:
- labels:
- run: php-apache
- spec:
- containers:
- - name: php-apache
- image: hpa-example
- ports:
- - containerPort: 80
- resources:
- limits:
- cpu: 500m
- requests:
- cpu: 200m
- ---
- apiVersion: v1
- kind: Service
- metadata:
- name: php-apache
- labels:
- run: php-apache
- spec:
- ports:
- - port: 80
- selector:
- run: php-apache
-
- kubectl apply -f hpa.yaml
-
- kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
-
- kubectl get 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
多项量度指标
- kubectl get hpa php-apache -o yaml > hpa-v2.yaml
-
- 修改文件,增加内存指标
- - resource:
- name: memory
- target:
- averageValue: 50Mi
- type: AverageValue
- type: Resource
-
-
- kubectl apply -f hpa-v2.yaml
- kubectl get hpa