kubernetes-pod指定调度

kubernetes-pod指定调度

参考文档

1
https://kubernetes.io/zh-cn/docs/concepts/scheduling-eviction/assign-pod-node/

使用nodeName指定调度

1
调度到kubernetes-node2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 编写yaml文件
[root@kubernetes-master study]# cat pod-nginx-node2.yml
---
apiVersion: v1
kind: Namespace # 创建资源为Namespace
metadata:
name: my-namespace #创建命名空间为my-namespace
---
apiVersion: v1
kind: Pod # 创建资源为Pod
metadata:
namespace: my-namespace # 指定pod的所在空间为my-namespace
name: my-nginx # pod名
spec:
nodeName: kubernetes-node2 # 指定pod在node2上面创建
containers:
  - name: my-nginx-web # 容器名
    image: daocloud.io/library/nginx #容器镜像
    ports:
      - containerPort: 80 # 容器暴漏端口

image-20231107134828726

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 创建pod
[root@kubernetes-master study]# kubectl apply -f pod-nginx-node2.yml
namespace/my-namespace unchanged
pod/my-nginx created

# 查看pod信息
[root@kubernetes-master study]# kubectl get pod -n my-namespace
NAME       READY   STATUS   RESTARTS   AGE
my-nginx   1/1     Running   0         5m

# 查看pod详细信息
[root@kubernetes-master study]# kubectl get pod -n my-namespace -o wide
NAME       READY   STATUS   RESTARTS   AGE   IP           NODE               NOMINATED NODE   READINESS GATES
my-nginx   1/1     Running   0         5m5s   10.10.20.94   kubernetes-node2   <none>           <none>

# 查看pod创建信息
[root@kubernetes-master study]# kubectl describe pod -n my-namespace my-nginx
Name:             my-nginx
Namespace:       my-namespace
Priority:         0
Service Account: default
Node:             kubernetes-node2/10.10.10.23
Start Time:       Tue, 07 Nov 2023 13:44:13 +0800
Labels:           <none>
Annotations:     cni.projectcalico.org/containerID: b85537817d8aa34f290285bc0386a8bcf893ca5cef0634a63ec83cf0434458dd
                cni.projectcalico.org/podIP: 10.10.20.94/32
                cni.projectcalico.org/podIPs: 10.10.20.94/32
Status:           Running
IP:               10.10.20.94
IPs:
IP: 10.10.20.94
Containers:
my-nginx-web:
  Container ID:   docker://42d4eb299157614b733a9c3408d8ed2e106041292a60832c1ac27a85c44a075f
  Image:         daocloud.io/library/nginx
  Image ID:       docker-pullable://daocloud.io/library/nginx@sha256:1a53eb723d17523512bd25c27299046cfa034cce309f4ed330c943a304513f59
  Port:           80/TCP
  Host Port:     0/TCP
  State:         Running
    Started:     Tue, 07 Nov 2023 13:44:44 +0800
  Ready:         True
  Restart Count: 0
  Environment:   <none>
  Mounts:
    /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-fdzzt (ro)
Conditions:
Type             Status
Initialized       True
Ready             True
ContainersReady   True
PodScheduled     True
Volumes:
kube-api-access-fdzzt:
  Type:                   Projected (a volume that contains injected data from multiple sources)
  TokenExpirationSeconds: 3607
  ConfigMapName:           kube-root-ca.crt
  ConfigMapOptional:       <nil>
  DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:             <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                            node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type   Reason   Age   From     Message
----   ------   ----   ----     -------
Normal Pulling 6m7s   kubelet Pulling image "daocloud.io/library/nginx"
Normal Pulled   5m38s kubelet Successfully pulled image "daocloud.io/library/nginx" in 29.670915109s (29.670934718s including waiting)
Normal Created 5m37s kubelet Created container my-nginx-web
Normal Started 5m37s kubelet Started container my-nginx-web

image-20231107135411339

image-20231107135439527

1
2
3
4
# 删除此yaml文件创建的,namespace及pod
[root@kubernetes-master study]# kubectl delete -f pod-nginx-node2.yml
namespace "my-namespace" deleted
pod "my-nginx" deleted

使用node标签指定调度

1
调度到kubernetes-node1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查看node1的标签
[root@kubernetes-master study]# kubectl describe nodes kubernetes-node1
Name:               kubernetes-node1
Roles:             <none>
Labels:             beta.kubernetes.io/arch=amd64
                  beta.kubernetes.io/os=linux
                  kubernetes.io/arch=amd64
                  kubernetes.io/hostname=kubernetes-node1 #此处为标签
                  kubernetes.io/os=linux
Annotations:       csi.volume.kubernetes.io/nodeid: {"csi.tigera.io":"kubernetes-node1"}
                  kubeadm.alpha.kubernetes.io/cri-socket: unix:///var/run/cri-dockerd.sock
                  node.alpha.kubernetes.io/ttl: 0
                  projectcalico.org/IPv4Address: 10.10.10.22/24
                  projectcalico.org/IPv4VXLANTunnelAddr: 10.10.20.192
                  volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp: Thu, 02 Nov 2023 15:10:30 +0800
Taints:             <none>

image-20231107140053830

1
2
# 查看node2
[root@kubernetes-master study]# kubectl describe nodes kubernetes-node2

image-20231107140311964

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 创建tomcat.yaml
[root@kubernetes-master study]# cat tomcat.yaml
---
apiVersion: v1 # yaml版本
kind: Namespace # 创建资源对象为命名空间
metadata: # 元数据
name: my-tomcat # 命名空间名为 my-tomcat
---
apiVersion: v1
kind: Pod # 创建资源对象为pod
metadata: # 元数据
namespace: my-tomcat # 指定pod所在命名空间为 my-tomcat
name: tomcat # pod名为 tomcat
labels: # 标签
  api: tomcat # 标签为 api: tomcat
spec: # 容器属性
nodeSelector: # 指定使用 nodeSelector 方式调度
  kubernetes.io/hostname: kubernetes-node1 #调度pod在 kubernetes.io/hostname: kubernetes-node1 的标签nodo节点上创建
containers: # 定义容器
  - name: my-tomcat-api # 容器名为 my-tomcat-api
    image: daocloud.io/library/tomcat # 镜像
    ports: # 容器暴漏端口
      - containerPort: 8080 # 8080

image-20231107143021153

1
2
3
4
5
6
7
8
9
10
11
12
# 创建pod
[root@kubernetes-master study]# kubectl apply -f tomcat.yaml  
namespace/my-tomcat created
pod/tomcat created

# 查看pod信息
[root@kubernetes-master study]# kubectl get pod -n my-tomcat -o wide
NAME     READY   STATUS   RESTARTS   AGE     IP             NODE               NOMINATED NODE   READINESS GATES
tomcat   1/1     Running   0         3m54s   10.10.20.235   kubernetes-node1   <none>           <none>

# 查看创建pod详细信息
[root@kubernetes-master study]# kubectl describe pod   -n my-tomcat

image-20231107143737064

1
2
3
4
# 删除此yaml文件创建的,namespace及pod
[root@kubernetes-master study]# kubectl delete -f tomcat.yaml
namespace "my-namespace" deleted
pod "my-tomcat" deleted