一、编辑yaml文件###
[root@K8s-Master Tools]# cat hello-world-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: hello-world
spec:
nodeSelector:
type: node1
containers:
- name: hello
image: "ubuntu:14.04"
command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
二、创建并查看运行状态###
[root@K8s-Master Tools]# kubectl create -f hello-world-pod.yaml
pod/hello-world created
[root@K8s-Master Tools]# kubectl get pod hello-world
NAME READY STATUS RESTARTS AGE
hello-world 1/1 Running 0 8m2s
[root@K8s-Master Tools]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default hello-world 1/1 Running 0 3s
kube-system coredns-bccdc95cf-plst5 1/1 Running 13 36d
kube-system coredns-bccdc95cf-vkqzr 1/1 Running 13 36d
kube-system etcd-k8s-master 1/1 Running 15 36d
kube-system kube-apiserver-k8s-master 1/1 Running 15 36d
kube-system kube-controller-manager-k8s-master 1/1 Running 15 36d
kube-system kube-flannel-ds-amd64-4g6qc 1/1 Running 6 36d
kube-system kube-flannel-ds-amd64-ccj8v 1/1 Running 14 36d
kube-system kube-flannel-ds-amd64-fhrfj 1/1 Running 8 36d
kube-system kube-proxy-jp6mp 1/1 Running 13 36d
kube-system kube-proxy-rkbcx 1/1 Running 6 36d
kube-system kube-proxy-rmkqm 1/1 Running 8 36d
kube-system kube-scheduler-k8s-master 1/1 Running 15 36d
kube-system kubernetes-dashboard-6db4897b74-lqfl6 1/1 Running 1 24h
注意点:将pod分配到指定的节点
将pod调度到指定节点有2种方式:
方式一:强制约束Pod调度到指定Node节点上
Pod.spec.nodeName
方式二:通过label-selector机制选择节点
Pod.spec.nodeSelector
以下使用第二种方式实现pod调度到指定节点。
①为Node添加label
kubectl label node k8s-node1 type=node1
kubectl get nodes --show-labels
[root@K8s-Master Tools]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready master 37d v1.15.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master=,type=master
k8s-node1 Ready <none> 37d v1.15.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux,type=node1
k8s-node2 Ready <none> 37d v1.15.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux,type=node2
②将pod分配到带有指定label的node
Pod操作:
删除Pod
kubectl delete pod hello
更新Pod
kubctl replace hello-world.yaml
三、可能遇到的问题###
Error:Back-off restarting failed container
[root@K8s-Master Tools]# kubectl describe pod hello-world
Name: hello-world
Namespace: default
Priority: 0
Node: k8s-node1/10.0.2.15
Start Time: Fri, 18 Oct 2019 17:16:13 +1100
Labels: <none>
Annotations: <none>
Status: Running
IP: 192.168.1.15
Containers:
hello:
Container ID: docker://ae85d78070dc02a19fed619c6270732cd98bfc1d824262b2935bc2362d7b70e5
Image: ubuntu:14.04
Image ID: docker-pullable://ubuntu@sha256:2f7c79927b346e436cc14c92bd4e5bd778c3bd7037f35bc639ac1589a7acfa90
Port: <none>
Host Port: <none>
Command:
/bin/echo
Hello
world
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Fri, 18 Oct 2019 17:18:10 +1100
Finished: Fri, 18 Oct 2019 17:18:10 +1100
Ready: False
Restart Count: 4
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-hctt8 (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-hctt8:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-hctt8
Optional: false
QoS Class: BestEffort
Node-Selectors: type=node1
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m31s default-scheduler Successfully assigned default/hello-world to k8s-node1
Normal Pulling 2m30s kubelet, k8s-node1 Pulling image "ubuntu:14.04"
Normal Pulled 2m10s kubelet, k8s-node1 Successfully pulled image "ubuntu:14.04"
Normal Created 34s (x5 over 2m10s) kubelet, k8s-node1 Created container hello
Normal Started 34s (x5 over 2m9s) kubelet, k8s-node1 Started container hello
Normal Pulled 34s (x4 over 2m8s) kubelet, k8s-node1 Container image "ubuntu:14.04" already present on machine
Warning BackOff 34s (x9 over 2m7s) kubelet, k8s-node1 Back-off restarting failed container
解决方法:
As per Describe Pod command listing, your Container inside the Pod has been already completed with exit code 0, which states about successful completion without any errors/problems, but the life cycle for the Pod was very short. To keep Pod running continuously you must specify a task that will never finish.
apiVersion: v1
kind: Pod
metadata:
name: ubuntu
spec:
containers:
- name: ubuntu
image: ubuntu
command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]