如何为某些节点分配命名空间? [英] How to assign a namespace to certain nodes?

查看:29
本文介绍了如何为某些节点分配命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在命名空间级别配置nodeSelector?

Is there any way to configure nodeSelector at the namespace level?

我只想在此命名空间的某些节点上运行工作负载.

I want to run a workload only on certain nodes for this namespace.

推荐答案

要实现这一点,您可以使用 PodNodeSelector 准入控制器.

To achieve this you can use PodNodeSelector admission controller.

首先,您需要在 kubernetes-apiserver 中启用它:

First, you need to enable it in your kubernetes-apiserver:

  • 编辑/etc/kubernetes/manifests/kube-apiserver.yaml:
    • 找到--enable-admission-plugins=
    • 添加PodNodeSelector参数

    现在,您可以在命名空间的注释中指定 scheduler.alpha.kubernetes.io/node-selector 选项,例如:

    Now, you can specify scheduler.alpha.kubernetes.io/node-selector option in annotations for your namespace, example:

    apiVersion: v1
    kind: Namespace
    metadata:
     name: your-namespace
     annotations:
       scheduler.alpha.kubernetes.io/node-selector: env=test
    spec: {}
    status: {}
    

    经过这些步骤,在这个命名空间中创建的所有 Pod 都会自动添加这个部分:

    After these steps, all the pods created in this namespace will have this section automatically added:

    nodeSelector
      env: test
    

    有关 PodNodeSelector 的更多信息,您可以在官方 Kubernetes 文档中找到:https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#podnodeselector

    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 部署集群,并且希望使此配置持久化,则必须更新 kubeadm 配置文件:

    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
    

    apiServer 部分下使用自定义值指定 extraArgs:

    specify extraArgs with custom values under apiServer section:

    apiServer: 
      extraArgs: 
        enable-admission-plugins: NodeRestriction,PodNodeSelector
    

    然后在所有控制平面节点上更新您的 kube-apiserver 静态清单:

    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 用户

    你可以只使用 kube_apiserver_enable_admission_plugins 变量作为你的 api-server 配置变量:


    kubespray users

    You can just use kube_apiserver_enable_admission_plugins variable for your api-server configuration variables:

     kube_apiserver_enable_admission_plugins:
       - PodNodeSelector
    

    这篇关于如何为某些节点分配命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆