kubernetes-资源管理
资源对象
Namespace(命名空间)
隔离
- 资源对象的隔离:
Service
,Deployment
,pod
- 资源配额的隔离:
cpu
,Memory
- 资源对象的隔离:
yaml
配置文件创建namespace:(namespace-dev.yaml
)1
2
3
4
5apiVersion: v1
kind: Namespace
metadata:
# namespace名字
name: devkubectl create -f namespace-dev.yaml
创建在指定命名空间下的pod
1 | #deploy |
隔离特点
- Namespace的隔离仅仅是名字的隔离,而非物理上的隔离
划分方式
- 按环境划分:
dev
,test
- 按团队划分
- 自定义多级划分
Resources(资源管理)
资源管理内容:
cpu
,gpu
,内存,持久化存储资源单位
memory
:Ti, Gi, Mi, Kicpu
:cpu的测量单位是cpus,允许分数值。你可以使用前缀m来表示mili(千分之一)。例如100mcpu就是100 milicpu,等价于0.1CPU。
Requests
- 设置各容器需要的最小资源
Limits
- 用于限制运行时容器占用的资源,用来限制容器的最大CPU、内存的使用率
yaml
使用Resources
案例:web-resources-dev.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-demo
namespace: dev
spec:
selector:
matchLabels:
app: web-demo
replicas: 1
template:
metadata:
labels:
app: web-demo
spec:
containers:
- name: web-demo
image: tomcat:8-slim
ports:
- containerPort: 8080
# 使用resources
resources:
requests:
memory: 100Mi
cpu: 100m
limits:
memory: 100Mi
cpu: 200m
#service
apiVersion: v1
kind: Service
metadata:
name: web-demo
namespace: dev
spec:
ports:
- port: 8080
protocol: TCP
targetPort: 8080
selector:
app: web-demo
type: ClusterIP
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: web-demo
namespace: dev
spec:
rules:
- host: web-dev.chc.com
http:
paths:
- path: /
backend:
serviceName: web-demo
servicePort: 8080执行命令
kubectl apply -f web-resources-dev.yaml
注意点:
requests
:配置大于服务器当前可用配置会导致服务一直处于pending
状态
LimitRange
LimitRange
(简称limits
)基于namespace
的资源管理,包括pod
和container
的最小、最大和default
、defaultrequests
等。
一旦创建limits
,以后创建资源时,K8S将该limits资源限制条件默认/强制给pod,创建后发现不符合规则,将暂停创建pod
。
在创建资源时,用户可以为pod自定义资源管理限制,在创建时会去检查和匹配limits值,发现不匹配将在创建时报错。创建后,该pod的资源使用遵守自定义规则,而不会遵守namespace
的limits
限制。
limitRange.yaml
的配置1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48apiVersion: v1
kind: LimitRange
metadata:
name: test-limits
spec:
limits:
- max:
# 设置最大cpu为4核
cpu: 4000m
# 最大内存2g
memory: 2Gi
min:
# 最小cpu为0.1核
cpu: 100m
# 最小内存100M
memory: 100Mi
maxLimitRequestRatio: #Limit和Request的比值最大配置
# cpu的Limit和Request的比例不能大于3
cpu: 3
# 内存的Limit和Request的最大比例不能大于2
memory: 2
# 以上配置为pod的资源配置
type: Pod
- default:
# 默认Limit的cpu为0.3核
cpu: 300m
# 默认Limit内存为200M
memory: 200Mi
defaultRequest:
# 默认Request的cpu
cpu: 200m
# 默认Request的内存
memory: 100Mi
max:
# Container的最大cpu
cpu: 2000m
# Container的最大内存
memory: 1Gi
min:
# Container的最小cpu
cpu: 100m
# Container的最小cpu
memory: 100Mi
maxLimitRequestRatio: #Limit和Request的比值最大配置
cpu: 5
memory: 4
# 以上资源配置为Container的配置
type: Container使用
limitRange.yaml
1
2
3
4
5
6# 创建一个namespace
kubectl create ns test
# 使用配置
kubectl create -f limitRange.yaml -n test
# 查看namespace的limitRange的配置信息
Kubectl describe limits -n test各值的情况限制
1
2
3
4
5
6
7每容器(type: container)
max>=default>=defaultRequest>min
每pod(type: pod)
max>=min
整个
容器的max*容器数<=pod的max
容器的min*容器数<=pod的min使用建议
只使用limits的pod或者container中的一种,尽量不使用同时使用,特别在pod中有多容器需求的情况下。
尽量使用max,尽量不同时使用max和min
由于limits会针对该namespace下的所有新建的pods,所以在该namespace下应该运行哪些资源需求相同的业务
在复杂的limits配置下,不要在创建资源时使用自定义配置。
ResourceQuotas
Resource Quotas(资源配额,简称quota)是对namespace进行资源配额,限制资源使用的一种策略。 K8S是一个多用户架构,当多用户或者团队共享一个K8S系统时,SA使用quota防止用户(基于namespace的)的资源抢占,定义好资源分配策略。
Quota应用在Namespace上,默认情况下,没有Resource Quota的,需要另外创建Quota,并且每Namespace最多只能有一个Quota对象。
resourceQuotas.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30apiVersion: v1
kind: ResourceQuota
metadata:
name: resource-quota
spec:
hard:
# namespace下允许存在的非终止状态的pod数量
pods: 4
# CPU需求总量不能超过该值
cpu: 4000m
# 内存需求总量不能超过该值
memory: 8Gi
# namespace的CPU需求总量不能超过该值
requests.cpu: 2000m
# namespace的内存需求总量不能超过该值
requests.memory: 4Gi
# namespace的CPU限额总量不能超过该值
limits.cpu: 4000m
# namespace的内存限额总量不能超过该值
limits.memory: 8Gi
# namespace下允许存在的configmap的数量
configmaps: 10
# namespace下允许存在的PVC的数量
persistentvolumeclaims: 4
# namespace下允许存在的replication controllers的数量
replicationcontrollers: 20
# namespace下允许存在的secret的数量
secrets: 10
# namespace下允许存在的service的数量
services: 10使用配置
1
2
3
4kubectl apply -f resourceQuotas.yaml -n test
# 查看quota配置和使用情况
kubectl get quota -n test
kubectl desctibe quota resource-quota -n test
Eviction (Pod驱逐)
常见的驱逐策略配置
```sh
–eviction-soft=memory.available<1.2Gi
–eviction-soft-grace-period=memory.available=1m30s1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
当内存持续1分30秒小于1.2G则会触发pod驱逐
* `--eviction-hard=memory.available<100Mi,nodefs.availble<1Gi,nodefs.inodesFree<5%`
当内存小于100M或者磁盘小于1G或者inodesFree小于5%则触发驱逐策略
### 驱逐策略
* 磁盘紧缺
1. 删除死掉的pod,容器
2. 删除没用的镜像
3. 按优先级,资源占用情况驱逐pod(优先级->资源占用)
* 内存紧缺
1. 驱逐不可靠的pod
2. 驱逐基本可靠的pod
3. 驱逐可靠的pod
### 服务可靠性判断
| Requests==Limits | 可靠 |
| ----------------- | -------- |
| 不设置(不建议) | 不可靠 |
| Limits > Requests | 基本可靠 |
# Label(标签)
Label是Kubernetes系统中的一个核心概念。
Label以key/value键值对的形式附加到各种对象上,如Pod、Service、RC、Node等。
Label定义了这些对象的可识别属性,用来对它们进行管理和选择。Label可以在创建时附加到对象上,也可以在对象创建后通过API进行管理。
* `web-label.yaml`
```yaml
#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-demo-label
namespace: dev
spec:
selector:
# 匹配的标签
matchLabels:
# 匹配的key-value
app: web-demo
replicas: 1
template:
metadata:
# 创建标签的名字
labels:
# 定义的标签key-value
app: web-demo
spec:
nodeSelector:
disktype: ssd # 选择node标签为disktype: ssd的机器
containers:
- name: web-demo
image: tomcat:8-slim
ports:
- containerPort: 8080
resources: # 使用resources
requests:
memory: 100Mi
cpu: 100m
limits:
memory: 110Mi
cpu: 200m
---
#service
apiVersion: v1
kind: Service
metadata:
name: web-demo
namespace: dev
spec:
ports:
- port: 8080
protocol: TCP
targetPort: 8080
selector:
# service选择带有app: web-demo标签的pod
app: web-demo
type: ClusterIP
---
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: web-demo
namespace: dev
spec:
rules:
- host: web-dev.chclabel.com
http:
paths:
- path: /
backend:
serviceName: web-demo
servicePort: 8080使用配置
1
2
3
4
5
6
7
8
9
10
11kubectl apply -f web-label.yaml
# 给node打标签
kubectl label node [nodename] disktype=ssd
# 查node的标签
kubectl get nodes --show-labels
# 查看pod部署位置
kubectl get pods -n dev -o wide
# 删除node的标签
kubectl label nodes [nodename] disktype-
# 修改node的标签
$ kubectl label nodes [nodename] disktype=hdd --overwrite