如何在Kubernetes控制平面(主)节点上调度Pod

时间:2020-02-23 14:31:38  来源:igfitidea点击:

默认情况下,出于安全原因,Kubernetes集群不会在控制平面节点上调度Pod。建议我们保持这种方式,但是对于测试环境,我们可能希望在控制平面节点上安排Pod以最大程度地利用资源。

如果我们希望能够在Kubernetes控制平面节点上调度Pod,则需要在主节点上删除污点。

kubectl taint nodes --all node-role.kubernetes.io/master

输出将类似于:

node/k8smaster01.theitroad.local untainted
taint "node-role.kubernetes.io/master" not found
taint "node-role.kubernetes.io/master" not found
taint "node-role.kubernetes.io/master" not found

这将从具有该节点的任何节点(包括控制平面节点)中删除node-role.kubernetes.io/master污点,这意味着调度程序将能够在任何地方调度pod。

在Kubernetes控制平面节点上测试Pod调度

我有一个包含三个工作程序节点和一个控制平面节点的群集。

$kubectl get nodes
NAME                                STATUS   ROLES    AGE   VERSION
k8smaster01.theitroad.local   Ready    master   12h   v1.17.0
k8snode01.theitroad.local     Ready    <none>   12h   v1.17.0
k8snode02.theitroad.local     Ready    <none>   12h   v1.17.0
k8snode03.theitroad.local     Ready    <none>   9h    v1.17.0

创建一个演示名称空间:

kubectl create namespace demo

将创建具有5个副本的部署。

$vim nginx-deployment.yaml

它具有以下数据:

--
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: demo
  labels:
    app: nginx
    color: green
spec:
  replicas: 5
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
        color: green
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              protocol: TCP
              containerPort: 80
          resources:
            limits:
              cpu: "200m"
              memory: "256Mi"
            requests:
              cpu: 100m
              memory: 128Mi
--
apiVersion: v1
kind: Service
metadata:
  annotations:
  name: nginx-demo-service
  namespace: demo
spec:
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
  selector:
    app: nginx
  sessionAffinity: None
  type: NodePort

应用列表:

$kubectl apply -f nginx-deployment.yaml

检查是否将Pod调度到控制节点平面。

$kubectl get pods -n demo -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP                NODE                                NOMINATED NODE   READINESS GATES
nginx-675bf5bc87-666jg   1/1     Running   0          17m   192.168.213.131   k8snode01.theitroad.local                
nginx-675bf5bc87-mc6px   1/1     Running   0          17m   192.168.94.13     k8smaster01.theitroad.local              
nginx-675bf5bc87-v5q87   1/1     Running   0          17m   192.168.144.129   k8snode03.theitroad.local                
nginx-675bf5bc87-vctqm   1/1     Running   0          17m   192.168.101.195   k8snode02.theitroad.local                
nginx-675bf5bc87-w5pmh   1/1     Running   0          17m   192.168.213.130   k8snode01.theitroad.local

我们可以看到主节点中有一个pod。确认服务已启动。

$kubectl get svc -n demo
NAME            TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
nginx-service   NodePort   10.96.184.67   <none>        80:31098/TCP   21m

由于使用的是NodePort,因此我们应该能够在端口31098上的任何群集节点IP上访问服务。

现在,我们可以清理演示对象。

$kubectl delete -f nginx-deployment.yaml
deployment.apps "nginx" deleted
service "nginx-service" deleted

$kubectl get pods,svc -n demo
No resources found in demo namespace.