k8s 节点的 NodeAffinity 使用

在 k8s 中,pod 会通过 kube-scheduler 按照节占先有的资源平均的调度到这些节点上,但有时候,我们需要将某个应用的pod调度到特定的节点上,
比如:两个应用需要频繁的进行通讯,那么我们希望将它们部署到同一个节点。或者希望访问一些类似需要ssd这样特殊资源的节点等应用场景。

最简单的方法是使用 nodeSelector,但它比较简单粗暴,使用起来不能灵活调度,这个在后续版本中也会慢慢过实,所以我们一般用 nodeAffinity来实现这些需求。

具体演示如下:

# 列出集群所有节点
-[appuser@chenqiang-dev ~]$ kubectl get no
NAME           STATUS    ROLES     AGE       VERSION
10.130.14.67   Ready     <none>    35d       v1.9.3
10.130.14.68   Ready     <none>    90d       v1.9.3
10.130.14.73   Ready     <none>    89d       v1.9.3
# 删除之前打过的标签 tester=chenqiang
-[appuser@chenqiang-dev ~]$ kubectl label no 10.130.14.67 tester-   
node "10.130.14.67" labeled

# 重新打标签 tester=chenqiang
-[appuser@chenqiang-dev ~]$ kubectl label no 10.130.14.67 tester=chenqiang
node "10.130.14.67" labeled

# 查看所有标签
-[appuser@chenqiang-dev ~]$ kubectl get no --show-labels                 
NAME           STATUS    ROLES     AGE       VERSION   LABELS
10.130.14.67   Ready     <none>    35d       v1.9.3    beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress-nginx=true,kubernetes.io/hostname=10.130.14.67,tester=chenqiang
10.130.14.68   Ready     <none>    90d       v1.9.3    beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress-nginx=true,kubernetes.io/hostname=10.130.14.68
10.130.14.73   Ready     <none>    89d       v1.9.3    beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=10.130.14.73

# 通过特定标签进行过滤节点
-[appuser@chenqiang-dev ~]$ kubectl get no -l tester=chenqiang
NAME           STATUS    ROLES     AGE       VERSION
10.130.14.67   Ready     <none>    35d       v1.9.3

如果我不希望pod调度到打了 tester=chenqiang 这个标签的 node,那么需要使用 NotIn 这个介词,否则使用 In
假设不希望调度,则添加如下的 nodeAffinity:

    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: tester
                operator: NotIn
                values:
                - chenqiang

一个完整的例子如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-hello-deployment
  namespace: chenqiang-ns1
  labels:
    app: nginx-hello
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx-hello
  template:
    metadata:
      labels:
        app: nginx-hello
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: tester
                operator: NotIn
                values:
                - chenqiang
      containers:
      - name: nginx-hello
        image: docker-registry.saicstack.com/chenqiang/nginx-hello:v2.0
        ports:
        - containerPort: 80
      imagePullSecrets:
      - name: regcred

从测试结果看,果然没有调度到 10.130.14.67 这个节点。


-[appuser@chenqiang-dev ~]$ kubectl -n chenqiang-ns1 get po -o wide    
NAME                                     READY     STATUS    RESTARTS   AGE       IP               NODE
nginx-hello-deployment-95f99df9d-56fvz   1/1       Running   0          11m       172.12.8.247     10.130.14.73
nginx-hello-deployment-95f99df9d-mj6xs   1/1       Running   0          11m       172.12.185.208   10.130.14.68
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将正在 k8s-node1 节点上运行的名为 apache-pod 的 pod 扩容到 k8s-node2 节点,并同时在这两个节点上运行 pod,请按照以下步骤操作: 1. 创建一个 deployment,指定 pod 的副本数为 2,并使用 nodeSelector 将这两个 pod 分别调度到 k8s-node1 和 k8s-node2 节点上。可以使用以下 YAML 文件创建 deployment: ``` apiVersion: apps/v1 kind: Deployment metadata: name: apache-pod spec: replicas: 2 selector: matchLabels: app: apache-pod template: metadata: labels: app: apache-pod spec: nodeSelector: kubernetes.io/hostname: k8s-node1 containers: - name: apache-container image: httpd:latest ports: - containerPort: 80 ``` 在这个 YAML 文件中,我们使用 nodeSelector 将第一个 pod 调度到 k8s-node1 节点上,第二个 pod 调度到 k8s-node2 节点上。注意,我们在 template.spec.containers 中指定了容器的镜像和端口号,这里使用的是 httpd 镜像,端口号是 80。 2. 使用 kubectl apply 命令应用这个 YAML 文件: ``` kubectl apply -f deployment.yaml ``` 3. 使用 kubectl get pods 命令检查 pod 状态,确认这两个 pod 都在运行: ``` kubectl get pods -o wide ``` 在输出中,你会看到两个 apache-pod 的副本都在运行,其中一个在 k8s-node1 节点上,另一个在 k8s-node2 节点上。 需要注意的是,使用 nodeSelector 指定 pod 调度到特定节点上可能会降低集群的灵活性,因为这样做会使节点的资源分配不均衡。如果你的集群中有多个节点,最好使用 Kubernetes 的调度器来自动地将 pod 调度到空闲节点上。你可以使用 nodeAffinity 和 podAntiAffinity 等特性来控制 pod 的调度行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值