以nginx-test-limit1,nginx-test-limit2为例
1,查出容器id
kubectl describe pod nginx-test-limit1
2,容器id:3b4a485c589176227157cf394e31f8d4962a7700d1585f58002356ce9affb061
2,通过容器id查到Cgroup的路径:
docker inspect 3b4a485c5891|grep Cgroup
3,进入容器的cpu配置文件目录:
/sys/fs/cgroup/cpu/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod6fb1f21d_c7ab_4d2f_a621_daea4f8aa113.slice
4,cpu配额文件cpu.cfs_quota_us,默认为-1
5,将cpu.cfs_quota_us修改成30%cpu配额,不需要重启pod:
echo 30000 > cpu.cfs_quota_us
此时nginx-test-limit1的cpu使用率不超过30%
nginx-test-limit2的cpu使用率在150%左右
6,将nginx-test-limit的cpu配额文件cpu.cfs_quota_us,改回为-1,不需要重启pod:
echo -1 > cpu.cfs_quota_us
此时nginx-test-limit1资源在50%-120%之间浮动
nginx-test-limit2资源同样在50%-120%之间浮动
7,提高cpu使用率脚本:
#! /bin/sh
stop()
{
echo “”
}
start()
{
for i in seq $1
do
echo "
i=0;
while true
do
i=i+1;
done" | /bin/sh &
echo $! >> pid.txt
done
}
case $1 in
stop)
stop
;;
*)
start $1
;;
Esac
8,pod编排文件:
1,nginx-test-limit1:
apiVersion: v1
kind: Pod
metadata:
name: nginx-test-limit1
annotations:
lwt1: add
labels:
app: nginx-test-limit1
spec:
nodeName: moss-node48
containers:
- name: nginx-test-limit1
image: harbor.com/library/nginx:1.23
command: [“/bin/sleep”,“infinity”]
imagePullPolicy: IfNotPresent
#resources:limits:
#requests:#cpu: "3000m" #memory: "100Mi"
#cpu: “1000m”
#memory: “100Mi”
ports:
- containerPort: 180
priorityClassName: high-priority-nonpreempting
2,nginx-test-limit2
apiVersion: v1
kind: Pod
metadata:
name: nginx-test-limit2
annotations:
lwt1: add
labels:
app: nginx-test-limit2
spec:
nodeName: moss-node48
containers:
- name: nginx-test-limit2
image: harbor.com/library/nginx:1.23
command: [“/bin/sleep”,“infinity”]
imagePullPolicy: IfNotPresent
ports:
- containerPort: 181
priorityClassName: second-nonpreempting