After these steps, all the pods created in this namespace will have this section automatically added:
nodeSelector
env: test
More information about the PodNodeSelector you can find in the official Kubernetes documentation: https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#podnodeselector
kubeadm users
If you deployed your cluster using kubeadm and if you want to make this configuration persistent, you have to update your kubeadm config file:
kubectl edit cm -n kube-system kubeadm-config
specify extraArgs with custom values under apiServer section:
then update your kube-apiserver static manifest on all control-plane nodes:
# Kubernetes 1.22 and forward:
kubectl get configmap -n kube-system kubeadm-config -o=jsonpath="{.data}" > kubeadm-config.yaml
# Before Kubernetes 1.22:
# "kubeadmin config view" was deprecated in 1.19 and removed in 1.22
# Reference: https://github.com/kubernetes/kubeadm/issues/2203
kubeadm config view > kubeadm-config.yaml
# Update the manifest with the file generated by any of the above lines
kubeadm init phase control-plane apiserver --config kubeadm-config.yaml
kubespray users
You can just use kube_apiserver_enable_admission_plugins variable for your api-server configuration variables:
apiVersion: v1
kind: Namespace
metadata:
name: gpu-namespace
annotations:
scheduler.alpha.kubernetes.io/node-selector: "project.example.com/GPUsNodePool=true" # poorly documented: format has to be of "selector-label=label-val"
scheduler.alpha.kubernetes.io/defaultTolerations: '[{"operator": "Equal", "value": "true", "effect": "NoSchedule", "key": "project.example.com/GPUsNodePool"}]'
project.example.com/description: 'This namespace is dedicated only to resources that need a GPU.'
完成!在命名空间中创建资源,准入控制器和调度程序将完成其余工作。
测试
创建一个示例pod,不带标签或公差,但位于命名空间中:
kubectl run test-dedicated-ns --image=nginx --namespace=gpu-namespace
# get nodes and nodes
kubectl get po -n gpu-namespace
# get node name
kubectl get po test-dedicated-ns -n gpu-namespace -o jsonpath='{.spec.nodeName}'
# check running pods on a node
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=<node>
3条答案
按热度按时间nnvyjq4y1#
To achieve this you can use
PodNodeSelector
admission controller.First, you need to enable it in your kubernetes-apiserver:
/etc/kubernetes/manifests/kube-apiserver.yaml
:--enable-admission-plugins=
PodNodeSelector
parameterNow, you can specify
scheduler.alpha.kubernetes.io/node-selector
option in annotations for your namespace, example:After these steps, all the pods created in this namespace will have this section automatically added:
More information about the
PodNodeSelector
you can find in the official Kubernetes documentation: https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#podnodeselectorkubeadm users
If you deployed your cluster using kubeadm and if you want to make this configuration persistent, you have to update your kubeadm config file:
specify
extraArgs
with custom values underapiServer
section:then update your kube-apiserver static manifest on all control-plane nodes:
kubespray users
You can just use
kube_apiserver_enable_admission_plugins
variable for your api-server configuration variables:jhdbpxl92#
我完全同意@kvaps的回答,但缺少了一些东西:必须在节点中添加一个标签:
这样,在名称空间中创建的带有
scheduler.alpha.kubernetes.io/node-selector: env=test
的pod将只能在带有env=test
标签的节点上调度pgvzfuti3#
要将节点专用于仅托管属于命名空间的资源,您还必须 * 防止在这些节点上调度其他资源 *。
这可以通过
podSelector
和taint
的组合来实现,当你在命名空间中创建资源时,通过准入控制器注入。这样,你不必手动标记和添加每个资源的容差,但在命名空间中创建它们就足够了。物业目标:
节点/节点池的配置
将污点添加到要专用于名称空间的节点:
此示例将污点添加到已具有标签
nodesWithGPU=true
的节点。您也可以按名称逐个污点节点:kubectl taint node my-node-name project.example.com/GPUsNodePool=true:NoSchedule
添加标签:
例如,如果您使用Terraform和AKS,也会执行相同的操作。节点池配置:
命名空间创建
然后使用准入控制器的说明创建命名空间:
完成!在命名空间中创建资源,准入控制器和调度程序将完成其余工作。
测试
创建一个示例pod,不带标签或公差,但位于命名空间中: