前言:Kubernetes通过watch的机制进行每个组件的协作,每个组件之间的设计实现了解耦
nodeName用于将Pod调度到指定的Node名称上(跳过调度器直接分配)
nodeSelector用于将Pod调度到匹配Label的Node上
[root@master01 demo]# vim pod5.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
labels:
app: nginx
spec:
nodeName: 192.168.170.145 //指定节点
containers:
- name: nginx
image: nginx:1.15
[root@master01 demo]# kubectl apply -f pod5.yaml
pod/pod-example created
[root@master01 demo]# kubectl get pods //查看pod资源
NAME READY STATUS RESTARTS AGE
pod-example 1/1 Running 0 30s
[root@master01 demo]# kubectl get pods -o wide //查看网络
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
pod-example 1/1 Running 0 103m 172.17.7.2 192.168.170.145
[root@master01 demo]# kubectl describe pod pod-example
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Pulling 105m kubelet, 192.168.170.145 pulling image "nginx:1.15"
Normal Pulled 104m kubelet, 192.168.170.145 Successfully pulled image "nginx:1.15"
Normal Created 104m kubelet, 192.168.170.145 Created container
Normal Started 104m kubelet, 192.168.170.145 Started container
[root@master01 demo]# kubectl label --help
Usage:
kubectl label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N
[--resource-version=version] [options]
[root@master01 demo]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
192.168.170.136 Ready 24d v1.12.3
192.168.170.145 Ready 24d v1.12.3
[root@master01 demo]# kubectl label nodes 192.168.170.145 accp=a
node/192.168.170.145 labeled
[root@master01 demo]# kubectl label nodes 192.168.170.136 accp=b
node/192.168.170.136 labeled
[root@master01 demo]# kubectl get nodes --show-labels //查看标签
NAME STATUS ROLES AGE VERSION LABELS
192.168.170.136 Ready 24d v1.12.3 accp=b,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.170.136
192.168.170.145 Ready 24d v1.12.3 accp=a,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.170.145
//可以看到node1的标签为accp=a,node2的标签为accp=b
[root@master01 demo]# vim pod6.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
labels:
app: nginx
spec:
nodeSelector:
accp: b //使用标签指定节点为node2
containers:
- name: nginx
image: nginx:1.15
[root@master01 demo]# kubectl apply -f pod6.yaml //创建资源
pod/pod-example created
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod-example 1/1 Running 0 53s
[root@master01 demo]# kubectl describe pod pod-example
。。。省略部分内容
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
**Normal Scheduled 65s default-scheduler Successfully assigned default/pod-example to 192.168.170.136**
Normal Pulling 62s kubelet, 192.168.170.136 pulling image "nginx:1.15"
Normal Pulled 17s kubelet, 192.168.170.136 Successfully pulled image "nginx:1.15"
Normal Created 17s kubelet, 192.168.170.136 Created container
Normal Started 16s kubelet, 192.168.170.136 Started container
//可以看到第一步就是经过调度器分配
[root@master01 demo]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
pod-example 1/1 Running 0 3m1s 172.17.12.2 192.168.170.136
//查看pod事件
kubectl describe TYPE NAME_PREFIX
//查看pod日志(Failed状态下)
kubectl logs POD_NAME
//进入pod(状态为running,但是服务没有提供)
kubectl exec –it POD_NAME bash