资源限制
资源限制
当创建一个
$ kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
# Scaling the deployment to 3 replicates
$ kubectl scale deployment nginx --replicas=3
deployment.apps/nginx scaled
# Getting the list of deployments
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 3/3 3 3 24s
然后我们修改
apiVersion : apps/v1
kind : Deployment
metadata :
labels :
app : nginx
name : nginx
namespace : default
spec :
progressDeadlineSeconds : 600
replicas : 3
revisionHistoryLimit : 10
selector :
matchLabels :
app : nginx
strategy :
rollingUpdate :
maxSurge : 25%
maxUnavailable: 25%
type : RollingUpdate
template :
metadata :
creationTimestamp : null
labels :
app : nginx
spec :
containers :
- image : nginx
imagePullPolicy : Always
name : nginx
# Add the following lines
resources :
limits :
memory : " 256Mi "
cpu : "200m "
requests :
memory : " 128Mi "
cpu : " 50m "
terminationMessagePath : / dev / termination-log
terminationMessagePolicy : File
dnsPolicy : ClusterFirst
restartPolicy : Always
schedulerName : default-scheduler
securityContext : {}
terminationGracePeriodSeconds : 30
$ kubectl delete deployments.apps nginx
$ kubectl create -f deployment-limitado.yaml
deployment.apps/nginx created
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 12m
nginx-f89759699-77v8b 1/1 Running 0 117s
nginx-f89759699-ffbgh 1/1 Running 0 117s
nginx-f89759699-vzvlt 1/1 Running 0 2m2s
然后我们可以在
$ kubectl exec -ti nginx-f89759699-77v8b -- /bin/bash
现在在容器中,安装并运行
$ apt-get update && apt-get install -y stress
$ stress --vm 1 --vm-bytes 128M --cpu 1
stress: info: [221] dispatching hogs: 1 cpu, 0 io, 1 vm, 0 hdd
这里我们强调的是容器,使用
stress --vm 1 --vm-bytes 512M --cpu 1
stress: info: [230] dispatching hogs: 1 cpu, 0 io, 1 vm, 0 hdd
stress: FAIL: [230] (415) <-- worker 232 got signal 9
stress: WARN: [230] (417) now reaping child worker processes
stress: FAIL: [230] (451) failed run completed in 0s
要跟踪
$ kubectl top pod --namespace=default nginx-f89759699-77v8b
NAME CPU(cores) MEMORY(bytes)
nginx-85f7fb6b45-b6dsk 201m 226Mi
限制命名空间的资源
在
让我们创建我们的第一个命名空间:
$ kubectl create namespace primeiro-namespace
namespace/primeiro-namespace created
$ kubectl get namespaces
NAME STATUS AGE
default Active 55m
kube-node-lease Active 56m
kube-public Active 56m
kube-system Active 56m
primeiro-namespace Active 5s
$ kubectl describe namespace primeiro-namespace
Name: primeiro-namespace
Labels: <none>
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
我们可以看到,我们的命名空间仍然没有设置,所以我们将使用
vim limitando-recursos.yaml
apiVersion : v1
kind : LimitRange
metadata :
name : limiting-resources
spec :
limits :
- default :
cpu : 1
memory : 100Mi
defaultRequest :
cpu : 0.5
memory : 80Mi
type : Container
现在让我们把这个
$ kubectl create -f limitando-recursos.yaml -n primeiro-namespace
limitrange/limitando-recursos created
$ kubectl get limitrange -n primeiro-namespace
NAME CREATED AT
limitando-recursos 2020-05-10T18:02:51Z
$ kubectl get limitrange --all-namespaces
NAMESPACE NAME CREATED AT
primeiro-namespace limitando-recursos 2020-05-10T18:02:51Z
$ kubectl describe limitrange -n primeiro-namespace
Name: limitando-recursos
Namespace: primeiro-namespace
Type Resource Min Max Default Request Default Limit Max Limit/Request Ratio
---- -------- --- --- --------------- ------------- -----------------------
Container cpu - - 500m 1 -
Container memory - - 80Mi 100Mi -
正如我们所看到的,我们为
vim pod-limitrange.yaml
apiVersion : v1
kind : Pod
metadata :
name : limit-pod
spec :
containers :
- name : my-container
image : nginx
现在让我们在默认命名空间之外创建一个
$ kubectl create -f pod-limitrange.yaml
pod/limit-pod created
$ kubectl create -f pod-limitrange.yaml -n primeiro-namespace
pod/limit-pod created
$ kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default limit-pod 1/1 Running 0 10s
primeiro-namespace limit-pod 1/1 Running 0 5s
$ kubectl describe pod limit-pod
Name: limit-pod
Namespace: default
Priority: 0
Node: elliot-02/172.31.19.123
Start Time: Sun, 10 May 2020 18:03:52 +0000
Labels: <none>
Annotations: <none>
Status: Running
IP: 10.32.0.4
IPs:
IP: 10.32.0.4
Containers:
meu-container:
Container ID: docker://19850dc935ffa41b1754cb58ab60ec5bb3616bbbe6a958abe1b2575bd26ee73d
Image: nginx
...
$ kubectl describe pod limit-pod -n primeiro-namespace
Name: limit-pod
Namespace: primeiro-namespace
Priority: 0
Node: elliot-03/172.31.24.60
Start Time: Sun, 10 May 2020 18:03:57 +0000
Labels: <none>
Annotations: kubernetes.io/limit-ranger:
LimitRanger plugin set: cpu, memory request for container meu-container; cpu, memory limit for container meu-container
Status: Running
IP: 10.46.0.3
IPs:
IP: 10.46.0.3
Containers:
meu-container:
Container ID: docker://ad3760837d71955df47263a2031d4238da2e94002ce4a0631475d301faf1ddef
Image: nginx
...
Limits:
cpu: 1
memory: 100Mi
Requests:
cpu: 500m
memory: 80Mi
我们可以看到,命名空间