Kubernetes

1.污点容忍策略

污点
什么是污点?

污点(Taint)是使节点与Pod产生排斥的一类规则

污点策略是如何实现的?

污点标签通过嵌合在键值对上的污点标签进行声明

污点标签
  • 尽量不调度:PreferNoSchedule
  • 不会被调度:NoSchedule
  • 驱逐节点:NoExecute(节点上的所有容器会被删掉,其它容器也不会被创建)
管理污点标签

污点标签必须绑定在键值对上,格式为:

key=value:[污点标签]

#查看污点标签

kubectl describe nodes [节点名字]

#设置污点标签

kubectl taint node [节点名字] key=value:污点标签

#删除污点标签

kubectl taint node [节点名字] key=value:污点标签-

容忍
什么是容忍?

容忍刚好与污点相反,某些时候我们需要在有污点的节点上运行Pod,这种无视污点标签的调度方式称为容忍

如何定义容忍策略?

在这里插入图片描述

精确匹配策略(Equal):键和值都定义,只对一个节点容忍

模糊匹配策略(Exists):只定义key,可以容忍多个节点

2.抢占与优先级

优先级是什么?

表示一个Pod相对于其它Pod的重要性,可以保证Pod优先被调度

如何使用优先级与抢占?

配置优先级类PriorityClass

创建Pod为其设置对应的优先级

PriorityClass

PriorityClass是一个全局资源对象,它定义了优先级类名称到优先级整数值的映射。优先级在value字段中指定,可以设置小于10亿的整数值,值越大,优先级越高

PriorityClass还有两个可选字段,globalDefault用于设置默认优先级状态,如果没有任何优先级设置,Pod的优先级为0。description用来配置描述性信息,告诉用户优先级的用途

优先级策略
  • 非抢占优先:优先进行调度分配,礼貌插队,资源不足时候,只能等待
  • 抢占优先:强制调度一个Pod,如果资源不足无法调度,会强制删除一个低优先级的Pod

非抢占优先(首先定义优先级)

# 定义优先级(队列优先)
[root@master ~]# vim mypriority.yaml
---
kind: PriorityClass
apiVersion: scheduling.k8s.io/v1
metadata:
  name: high-non
globalDefault: false
preemptionPolicy: Never
value: 1000
description: non-preemptive
---
kind: PriorityClass
apiVersion: scheduling.k8s.io/v1
metadata:
  name: low-non
globalDefault: false
preemptionPolicy: Never
value: 500
description: non-preemptive
[root@master ~]# kubectl apply -f mypriority.yaml
priorityclass.scheduling.k8s.io/high-non created
priorityclass.scheduling.k8s.io/low-non created
[root@master ~]# kubectl get priorityclasses.scheduling.k8s.io
NAME 					VALUE 				GLOBAL-DEFAULT 			AGE
high-non 				1000 					false				12s
low-non 				500 					false 				12s
system-cluster-critical 2000000000 				false 				45h

Pod资源文件

# 无优先级的 Pod
[root@master ~]# cat php1.yaml
---
kind: Pod
apiVersion: v1
metadata:
  name: php1
spec:
  nodeSelector:
    kubernetes.io/hostname: node-0002
  containers:
  - name: php
    image: myos:phpfpm
    resources:
      requests:
        cpu: "1500m"
# 低优先级 Pod
[root@master ~]# cat php2.yaml
---
kind: Pod
apiVersion: v1
metadata:
  name: php2
spec:
  nodeSelector:
    kubernetes.io/hostname: node-0002
  priorityClassName: low-non # 优先级名称
  containers:
  - name: php
    image: myos:phpfpm
    resources:
      requests:
        cpu: "1500m"
# 高优先级 Pod
[root@master ~]# cat php3.yaml
---
kind: Pod
apiVersion: v1
metadata:
  name: php3
spec:
  nodeSelector:
    kubernetes.io/hostname: node-0002
  priorityClassName: high-non # 优先级名称
  containers:
    - name: php
      image: myos:phpfpm
      resources:
        requests:
          cpu: "1500m"

验证非抢占优先

[root@master ~]# kubectl apply -f php1.yaml
pod/php1 created
[root@master ~]# kubectl apply -f php2.yaml
pod/php2 created
[root@master ~]# kubectl apply -f php3.yaml
pod/php3 created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
php1 1/1 Running 0 9s
php2 0/1 Pending 0 6s
php3 0/1 Pending 0 4s
[root@master ~]# kubectl delete pod php1
pod "php1" deleted
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
php2 0/1 Pending 0 20s
php3 1/1 Running 0 18s
# 清理实验 Pod
[root@master ~]# kubectl delete pod php2 php3
pod "php2" deleted
pod "php3" deleted

抢占优先

[root@master ~]# vim mypriority.yaml
---
kind: PriorityClass
apiVersion: scheduling.k8s.io/v1
metadata:
  name: high
globalDefault: false
preemptionPolicy: PreemptLowerPriority   #抢占策略
value: 1000
description: non-preemptive
---
kind: PriorityClass
apiVersion: scheduling.k8s.io/v1
metadata:
  name: low
globalDefault: false
preemptionPolicy: PreemptLowerPriority  #抢占策略
value: 500
description: non-preemptive
[root@master ~]# kubectl apply -f mypriority.yaml
priorityclass.scheduling.k8s.io/high created
priorityclass.scheduling.k8s.io/low created
[root@master ~]# kubectl get priorityclasses.scheduling.k8s.io
NAME    		   		VALUE    		GLOBAL-DEFAULT     		AGE
high    				1000         		false 				12s
low     				500          		false 				12s
system-cluster-critical 2000000000 			false 				45h
system-node-critical 	2000001000 			false 				45h

验证抢占优先

# 默认优先级 Pod
[root@master ~]# kubectl apply -f php1.yaml
pod/php1 created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
php1 1/1 Running 0 6s
# 高优先级 Pod
[root@master ~]# sed 's,-non,,' php3.yaml |kubectl apply -f -
pod/php3 created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
php3 1/1 Running 0 9s
# 低优先级 Pod
[root@master ~]# sed 's,-non,,' php2.yaml |kubectl apply -f -
pod/php2 created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
php2 0/1 Pending 0 3s
php3 1/1 Running 0 9s
# 清理实验 Pod
[root@master ~]# kubectl delete pod php2 php3
pod "php2" deleted
pod "php3" deleted
[root@master ~]# kubectl delete -f mypriority.yaml
priorityclass.scheduling.k8s.io "high-non" deleted
priorityclass.scheduling.k8s.io "low-non" deleted
priorityclass.scheduling.k8s.io "high" deleted
priorityclass.scheduling.k8s.io "low" deleted

3.特权容器

什么是特权容器?

容器是通过名称空间技术隔离的,有时候我们执行一些应用服务,需要使用或修改敏感的系统信息,这时容器需要突破隔离限制,获取更高的权限,这类容器统称为特权容器

运行特权容器会有一定的安全风险,这种模式下运行容器对宿主机拥有root访问权限,可以突破隔离直接控制宿主机的资源配置

在这里插入图片描述
在这里插入图片描述

4.Pod安全性

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

[root@master ~]# sed '36i\ - --feature-gates=PodSecurity=true' -i
/etc/kubernetes/manifests/kube-apiserver.yaml
[root@master ~]# systemctl restart kubelet
# 生产环境设置严格的准入控制
[root@master ~]# kubectl create namespace myprod
namespace/myprod created
[root@master ~]# kubectl label namespaces myprod pod-
security.kubernetes.io/enforce=restricted
namespace/myprod labeled
# 测试环境测试警告提示
[root@master ~]# kubectl create namespace mytest
符合安全规则的Pod
namespace/mytest created
[root@master ~]# kubectl label namespaces mytest pod-
security.kubernetes.io/warn=baseline
namespace/mytest labeled
# 创建特权容器
[root@master ~]# kubectl -n myprod apply -f root.yaml
Error from server (Failure): error when creating "root.yaml": host namespaces
(hostNetwork=true, hostPID=true), privileged (container "linux" must not set
securityContext.privileged=true), allowPrivilegeEscalation != false (container
"linux" must set securityContext.allowPrivilegeEscalation=false), unrestricted
capabilities (container "linux" must set securityContext.capabilities.drop=
["ALL"]), runAsNonRoot != true (pod or container "linux" must set
securityContext.runAsNonRoot=true), seccompProfile (pod or container "linux"
must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")
[root@master ~]#
[root@master ~]# kubectl -n myprod get pods
No resources found in myprod namespace.
[root@master ~]# kubectl -n mytest apply -f root.yaml
Warning: would violate "latest" version of "baseline" PodSecurity profile: host
namespaces (hostNetwork=true, hostPID=true), privileged (container "linux" must
not set securityContext.privileged=true)
pod/root created
[root@master ~]# kubectl -n mytest get pods
NAME 		READY 		STATUS 		RESTARTS 		AGE
root 		1/1 		Running 		0 			7s
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐