1. 程式人生 > 實用技巧 >二、K8S建立POD

二、K8S建立POD

K8S建立pod時,需要yaml檔案或者json檔案,其中yaml檔案的格式特別重要,這裡被坑了很久。縮排時不允許使用Tab鍵,只允許使用空格。縮排的空格數目不重要,只要相同層級的元素左側對齊即可。具體可參見一下https://blog.csdn.net/yuxiang1014/article/details/85018741?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522160921964616780277852577%252522%25252C%252522scm%252522%25253A%25252220140713.130102334..%252522%25257D&request_id=160921964616780277852577&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-85018741.nonecase&utm_term=k8s%20yaml

[root@k8s_master k8s_pod_yml]# cat k8s_pod_nginx.yml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx20201229
spec:
  containers:
    - name: nginx
      image: 10.0.0.29:5000/nginx:latest
      ports:
        - containerPort: 80

建立前需要保證node節點正常工作

kubectl get nodes

如果出現下發失敗,可在node節點檢視 journalctl -xefu kubelet檢視報過

問題1:Error syncing pod, skipping: failed to “StartContainer” for “POD” with ImagePullBackOff: “Back-off pulling image “registry.access.redhat.com/rhel7/pod-infrastructure:latest””

解決辦法:手動去官網下載後,上次到本地映象倉庫,然後修改node的kubelet檔案,並重啟kubelet服務。

pod-infrastructure這個是k8s啟動pod所需的最基礎映象,所以一定要需要。

[root@k8s_node_1 ~]# docker images 
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
10.0.0.29:5000/nginx latest ae2feff98a0c 13 days ago 133 MB busybox latest 219ee5171f80 3 weeks ago 1.23 MB docker.io/w564791/pod-infrastructure latest f66f4bd9b894 3 years ago 206 MB 10.0.0.29:5000/pod-infrastructure latest f66f4bd9b894 3 years ago 206 MB [root@k8s_node_1 ~]# cat /etc/kubernetes/kubelet ### # pod infrastructure container KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=10.0.0.29:5000/pod-infrastructure:latest" # Add your own! KUBELET_ARGS="" [root@k8s_node_1 ~]#

建立完成後就可以通過k8s的常用命令檢視容器的資訊:

[root@k8s_master k8s_pod_yml]# kubectl get pods 
NAME            READY     STATUS    RESTARTS   AGE
nginx20201229   1/1       Running   0          10m
nginxtest       1/1       Running   1          3h
[root@k8s_master k8s_pod_yml]# kubectl describe pod nginx20201229
Name:        nginx20201229
Namespace:    default
Node:        10.0.0.30/10.0.0.30  #排程到的節點
Start Time:    Tue, 29 Dec 2020 13:32:31 +0800
Labels:        <none>    #label的作用後面慢慢學習
Status:        Running
IP:        200.1.83.3    #容器的IP地址
Controllers:    <none>    #控制器的部分後面慢慢學習
Containers:
  nginx:
    Container ID:        docker://7f358c31235c3c4b648ee699e6ceb7769b3630ed907e4d57e789f9c1eb758312  #docker容器資訊
    Image:            10.0.0.29:5000/nginx:latest  #所使用的映象
    Image ID:            docker-pullable://10.0.0.29:5000/nginx@sha256:13e4551010728646aa7e1b1ac5313e04cf75d051fa441396832fcd6d600b5e71
    Port:            80/TCP  #監聽埠
    State:            Running
      Started:            Tue, 29 Dec 2020 13:32:33 +0800
    Ready:            True
    Restart Count:        0  #重啟次數
    Volume Mounts:        <none>  #掛載的卷,為了保證容器重啟是之前的資訊持久化,就需要掛載一個本地的卷給他
    Environment Variables:    <none>  #環境變數
Conditions:
  Type        Status
  Initialized     True 
  Ready     True 
  PodScheduled     True 
No volumes.
QoS Class:    BestEffort
Tolerations:    <none>
Events:    #詳細事件,這個可以看到pod啟動過程中,kube-scheduler排程,拉取映象和容器建立過程。這個資訊也能幫助拍錯
FirstSeen LastSeen Count From SubObjectPath Type Reason Message
--------- -------- ----- ---- ------------- -------- ------ ------- 11m 11m 1 {default-scheduler } Normal Scheduled Successfully assigned nginx20201229 to 10.0.0.30 11m 11m 2 {kubelet 10.0.0.30} Warning MissingClusterDNS kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy. 11m 11m 1 {kubelet 10.0.0.30} spec.containers{nginx} Normal Pulling pulling image "10.0.0.29:5000/nginx:latest" 11m 11m 1 {kubelet 10.0.0.30} spec.containers{nginx} Normal Pulled Successfully pulled image "10.0.0.29:5000/nginx:latest" 11m 11m 1 {kubelet 10.0.0.30} spec.containers{nginx} Normal Created Created container with docker id 7f358c31235c; Security:[seccomp=unconfined] 11m 11m 1 {kubelet 10.0.0.30} spec.containers{nginx} Normal Started Started container with docker id 7f358c31235c [root@k8s_master k8s_pod_yml]#

[root@k8s_node_1 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE10.0.0.29:5000/nginx latest ae2feff98a0c 13 days ago 133 MBbusybox latest 219ee5171f80 3 weeks ago 1.23 MBdocker.io/w564791/pod-infrastructure latest f66f4bd9b894 3 years ago 206 MB10.0.0.29:5000/pod-infrastructure latest f66f4bd9b894 3 years ago 206 MB[root@k8s_node_1 ~]# cat /etc/kubernetes/kubelet#### kubernetes kubelet (minion) config
# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)KUBELET_ADDRESS="--address=0.0.0.0"
# The port for the info server to serve onKUBELET_PORT="--port=10250"
# You may leave this blank to use the actual hostnameKUBELET_HOSTNAME="--hostname-override=10.0.0.30"
# location of the api-serverKUBELET_API_SERVER="--api-servers=http://10.0.0.29:8080"
# pod infrastructure containerKUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=10.0.0.29:5000/pod-infrastructure:latest"
# Add your own!KUBELET_ARGS=""[root@k8s_node_1 ~]#