默认情况下,出于安全原因, Kubernetes 集群不会在主节点上调度 pod

建议保持这种方式,但对于测试环境,可能希望在主节点上调度 Pod 以最大限度地利用资源。

如果希望能够在 Kubernetes 主节点上调度 pod,则需要移除主节点上的一个污点taint)。

查看节点信息:

$ kubectl describe nodes k8s-master-01 | grep Taint
Taints:             node-role.kubernetes.io/master:NoSchedule

主节点(master)属性中node-role.kubernetes.io/master:NoSchedule表示主节点不调度pod

通过如下命令调整:

$ kubectl taint nodes --all node-role.kubernetes.io/master-
node/k8s-master-01 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

示例集群

有一个包含三个工作节点和一个主节点的集群。

$ kubectl get nodes -o wide
NAME            STATUS   ROLES                  AGE     VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION     CONTAINER-RUNTIME
k8s-master-01   Ready    control-plane,master   4h20m   v1.23.2   192.168.1.140   <none>        Ubuntu 20.04.3 LTS   5.4.0-96-generic   containerd://1.4.12
k8s-worker-01   Ready    <none>                 3h28m   v1.23.2   192.168.1.141   <none>        Ubuntu 20.04.3 LTS   5.4.0-96-generic   containerd://1.4.12
k8s-worker-02   Ready    <none>                 3h20m   v1.23.2   192.168.1.142   <none>        Ubuntu 20.04.3 LTS   5.4.0-96-generic   containerd://1.4.12
k8s-worker-03   Ready    <none>                 153m    v1.23.2   192.168.1.143   <none>        Ubuntu 20.04.3 LTS   5.4.0-96-generic   containerd://1.4.12

新建命名空间test

kubectl create namespace test

部署应用

将创建一个包含 4 个副本的部署。

tee nginx-deployment.yaml<<EOF
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: test
  labels:
    app: nginx
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              protocol: TCP
              containerPort: 80
          resources:
            limits:
              cpu: "1.0"
              memory: 512Mi
            requests:
              cpu: "0.5"
              memory: 128Mi
---
apiVersion: v1
kind: Service
metadata:
  annotations:
  name: nginx-test-service
  namespace: test
spec:
  ports:
    - port: 80
      targetPort: 80
      nodePort: 32001
      protocol: TCP
  selector:
    app: nginx
  sessionAffinity: None
  type: NodePort
EOF

部署应用:

kubectl apply -f nginx-deployment.yaml

检查是否有 Pod 调度到主节点

$  kubectl get pods -n test -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP               NODE            NOMINATED NODE   READINESS GATES
nginx-59d7db4f6f-cvbbf   1/1     Running   0          2m49s   10.244.118.69    k8s-worker-02   <none>           <none>
nginx-59d7db4f6f-fpt4v   1/1     Running   0          2m49s   10.244.36.196    k8s-worker-01   <none>           <none>
nginx-59d7db4f6f-sql95   1/1     Running   0          2m49s   10.244.151.135   k8s-master-01   <none>           <none>
nginx-59d7db4f6f-v85wd   1/1     Running   0          2m49s   10.244.7.129     k8s-worker-03   <none>           <none>

可以看到主节点中有一个 pod在运行。

确认服务是否已上线

$ kubectl get svc -n test
NAME                 TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
nginx-test-service   NodePort   10.98.13.210   <none>        80:32001/TCP   3m14s

由于使用的是 NodePort,通过http://<任意节点IP>:32001访问。

$ curl  192.168.1.140:32001
$ curl  192.168.1.141:32001
$ curl  192.168.1.142:32001
$ curl  192.168.1.143:32001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

清理示例

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

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

$ kubectl delete ns test
namespace "test" deleted
Logo

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

更多推荐