资源限制
定义容器使用的最大资源
- resources.limits.cpu
- resources.limits.memory
定义容器使用的最小资源
- resources.requests.cpu
- resources.requests.memory
示例
[root@master manifest]# cat test.yml
[root@master manifest]# cat test.yml
---
apiVersion: v1
kind: Pod
metadata:
name: httpd
namespace: default
spec:
containers:
- name: httpd
image: httpd
imagePullPolicy: IfNotPresent
resources:
requests: //定义最小资源
memory: "60Mi"
cpu: "250m"
limits: //定义最大资源
memory: "120Mi"
cpu: "400m"
[root@master manifest]# kubectl apply -f test.yml
pod/httpd created
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
httpd 1/1 Running 0 2m10s 10.244.2.20 node2.example.com <none> <none>
[root@master manifest]# kubectl describe node node2
Name: node2.example.com
Roles: <none>
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
kubernetes.io/arch=amd64
kubernetes.io/hostname=node2.example.com
kubernetes.io/os=linux
......
Non-terminated Pods: (3 in total)
Namespace Name CPU Requests CPU Limits Memory Requests Memory Limits AGE
--------- ---- ------------ ---------- --------------- ------------- ---
default httpd 250m (12%) 400m (20%) 60Mi (3%) 120Mi (7%) 2m15s //定义成功
kube-system kube-flannel-ds-4tdzp 100m (5%) 100m (5%) 50Mi (2%) 50Mi (2%) 2d21h
kube-system kube-proxy-hldl6 0 (0%) 0 (0%) 0 (0%) 0 (0%) 2d21h
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 350m (17%) 500m (25%)
memory 110Mi (6%) 170Mi (10%)
ephemeral-storage 0 (0%) 0 (0%)
hugepages-1Gi 0 (0%) 0 (0%)
hugepages-2Mi 0 (0%) 0 (0%)
......
nodeSelector
nodeSelector
:用于将Pod调度到匹配Label的Node上,如果没有匹配的标签会调度失败。
作用:
- 约束Pod到特定的节点运行·完全匹配节点标签
应用场景:
- 专用节点:根据业务线将Node分组管理
- 配备特殊硬件:部分Node配有SSD硬盘、GPU
示例:确保Pod分配到具有SSD硬盘的节点上
1、给节点添加标签
- 格式:kubectl label nodes <node-name> <label-key> = <label-value>
- 例如:kubectl label nodes node1 disktype=ssd
- 验证:kubectl get nodes --show-labels
2、添加nodeSelector字段到Pod配置中
---
apiVersion: v1
kind: Pod
metadata:
name: httpd
namespace: default
spec:
nodeSelector: //添加节点选择器
disktype: "ssd" //标签的key和value
containers:
- name: httpd
image: httpd
imagePullPolicy: IfNotPresent
3、验证
kubectl get pods -o wide
演示
//给节点添加标签
[root@master manifest]# kubectl label nodes node2.example.com app=httpd
node/node2.example.com labeled
[root@master manifest]# kubectl get nodes node2.example.com --show-labels
NAME STATUS ROLES AGE VERSION LABELS
node2.example.com Ready <none> 2d22h v1.20.0 app=httpd,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node2.example.com,kubernetes.io/os=linux
//创建pod
[root@master manifest]# cat test.yml
---
apiVersion: v1
kind: Pod
metadata:
n