容器編排系統之Pod資源配置清單基礎

1874發表於2020-12-14

  前文我們瞭解了k8s上的叢集管理工具kubectl的基礎操作以及相關資源的管理,回顧請參考https://www.cnblogs.com/qiuhom-1874/p/14130540.html;今天我們來聊一下pod資源配置清單的相關話題;

  我們知道在k8s上管理資源的方式有兩種,一種是陳述式介面,一種是宣告式介面;在前文我們用kubectl create的方式建立pod控制器,建立service,建立名稱空間都是使用的陳述式命令的方式在k8s上管理資源;陳述式命令介面管理k8s上的資源的確很方便,但通常建立出來的資源有很多屬性都不是我們期望的,所以陳述式命令在k8s叢集上管理資源的方式一般不怎麼推薦使用;我們要想自己手動定義一個資源,就需要使用宣告式介面來建立資源;那麼宣告式介面該怎麼用呢?很簡單,我們只需要寫一個描述資源的配置檔案,然後使用apply命令指定應用對應的資源配置檔案即可;

  要想手寫資源的配置檔案,我們就需要先了解對應的資源有哪些屬性,以及配置清單的語法格式;在k8s上資源配置清單有兩種格式,一種是yaml格式,一種是json格式,常用yaml格式;我們在初學時,可以仿照陳述命令建立的資源,輸出為yaml格式的配置檔案來寫;

  示例:使用陳述命令建立一個名稱空間,然後檢視建立的namespace,輸出為yaml格式配置檔案

[root@master01 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   5h16m
kube-node-lease   Active   5h16m
kube-public       Active   5h16m
kube-system       Active   5h16m
[root@master01 ~]# kubectl create ns testing
namespace/testing created
[root@master01 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   5h17m
kube-node-lease   Active   5h17m
kube-public       Active   5h17m
kube-system       Active   5h17m
testing           Active   6s
[root@master01 ~]# kubectl get ns/testing -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2020-12-08T11:56:18Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:status:
        f:phase: {}
    manager: kubectl-create
    operation: Update
    time: "2020-12-08T11:56:18Z"
  name: testing
  resourceVersion: "28764"
  uid: 00280ecd-b570-4d1c-953c-c79411cc88f9
spec:
  finalizers:
  - kubernetes
status:
  phase: Active
[root@master01 ~]# 

  提示:可以看到輸出的yaml配置檔案格式的內容,裡面有很多kv資料,每個欄位都有對應的值,我們把這些欄位叫做testing名稱空間的屬性;除了檢視輸出為yaml格式的配置清單,我們也可以輸出為json格式的清單;

  匯出testing名稱空間的yaml配置檔案,並儲存為一個模板檔案

[root@master01 ~]# kubectl get ns testing -o yaml > ns-template.yaml
[root@master01 ~]# cat ns-template.yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2020-12-08T11:56:18Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:status:
        f:phase: {}
    manager: kubectl-create
    operation: Update
    time: "2020-12-08T11:56:18Z"
  name: testing
  resourceVersion: "28764"
  uid: 00280ecd-b570-4d1c-953c-c79411cc88f9
spec:
  finalizers:
  - kubernetes
status:
  phase: Active
[root@master01 ~]# 

  提示:有了這個配置檔案,我們就可以照貓畫虎定義其他名稱空間;

  示例:使用模板檔案,定義建立一個prod的名稱空間;

[root@master01 ~]# cat ns-prod.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: prod
spec:
  finalizers:
  - kubernetes
status:
  phase: Active
[root@master01 ~]# 

  提示:模板中的很多資訊都是自動生成的,我們在建立一個資源時,只需要指定特定的必要的屬性;在一個資源配置清單中,通常有5個一級欄位,apiVersion欄位用於指定當前資源所屬的群組,在k8s上有很多群組,其中v1是core v1的縮寫,表示核心群組,一般不用更改;kind欄位是用於描述該資源的型別,這裡需要注意,資源型別名稱首字母必須大寫;metadata欄位使用於描述資源的後設資料資訊,其中對於namespace型別資源來說,最終要的後設資料為name表示該資源例項的名稱,這個屬性也是必要屬性;spec欄位是用於描述對應資源我們期望的狀態,對於namespace資源來說,spec欄位可以不用寫;status欄位主要用來描述資源當前的狀態資訊;一般這個欄位由k8s叢集自身維護,使用者可以不用定義;

  使用上述配置清單建立prod名稱空間

[root@master01 ~]# kubectl create -f ns-prod.yaml
namespace/prod created
[root@master01 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   5h45m
kube-node-lease   Active   5h45m
kube-public       Active   5h45m
kube-system       Active   5h45m
prod              Active   5s
testing           Active   28m
[root@master01 ~]# 

  提示:以上使用陳述式命令create指定資源配置檔案建立資源,這種方式能夠建立資源,但是它不可以多次執行,多次執行會報錯對應資源已經存在;

  使用資源配置檔案刪除資源

[root@master01 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   5h47m
kube-node-lease   Active   5h47m
kube-public       Active   5h47m
kube-system       Active   5h47m
prod              Active   2m21s
testing           Active   30m
[root@master01 ~]# kubectl delete -f ns-prod.yaml
namespace "prod" deleted
[root@master01 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   5h47m
kube-node-lease   Active   5h47m
kube-public       Active   5h47m
kube-system       Active   5h47m
testing           Active   30m
[root@master01 ~]# 

  提示:通常情況我們刪除對應的資源不使用以上方式刪除,一般刪除都是直接使用陳述式命令來刪除資源;使用delete命令,指定資源型別以及資源名稱,如果是名稱空間級別的資源,還需要指定名稱空間;

  使用宣告式apply來建立prod名稱空間

[root@master01 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   5h50m
kube-node-lease   Active   5h50m
kube-public       Active   5h50m
kube-system       Active   5h50m
testing           Active   33m
[root@master01 ~]# kubectl apply -f ns-prod.yaml
namespace/prod created
[root@master01 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   5h50m
kube-node-lease   Active   5h50m
kube-public       Active   5h50m
kube-system       Active   5h50m
prod              Active   4s
testing           Active   33m
[root@master01 ~]# kubectl apply -f ns-prod.yaml
namespace/prod unchanged
[root@master01 ~]# kubectl apply -f ns-prod.yaml
namespace/prod unchanged
[root@master01 ~]# 

  提示:apply是一個宣告式介面命令,它可以重複執行多次,只要發現對應配置檔案中定義的資源屬性和當前資源屬性不吻合,它就會嘗試應用配置檔案中屬性,讓當前資源屬性或狀態和配置檔案中定義的屬性和狀態保持一致,如果配置檔案中的資源狀態和屬性和當前資源的狀態和屬性吻合,它就告訴我們配置沒有變化;很顯然apply這個命令是我們想要用的命令;

  以上就是一個最最簡單的使用資源配置清單的方式來建立資源,在k8s上資源有很多型別,不同型別的資源定義的屬性各有不同,我們要想寫好一個資源配置清單,首先就需要了解對應型別的資源,該有哪些屬性,我們怎麼才能知道對應資源該有哪些屬性呢?很簡單查文件呀,我們可以去k8s的官方文件中找對應資源型別的資源清單配置說明;https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20這個網站上k8s-1.20這個版本的相關api說明,我們可以去該網站查詢對應型別資源的api欄位值的型別,以及欄位說明等等;如果你在瀏覽器中由於各種不可描述的原因,你打不開k8s官網,還有一個辦法就是直接在命令列使用命令來查;

  示例:檢視ns型別的資源說明

[root@master01 ~]# kubectl explain ns
KIND:     Namespace
VERSION:  v1

DESCRIPTION:
     Namespace provides a scope for Names. Use of multiple namespaces is
     optional.

FIELDS:
   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata     <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec <Object>
     Spec defines the behavior of the Namespace. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   status       <Object>
     Status describes the current status of a Namespace. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

[root@master01 ~]# 

  提示:上述命令就可以列出ns型別的資源應該怎麼定義;對應欄位的值的型別,其中如果對應欄位的值為object,我們還可以使用點號繼續查詢對應欄位的定義說明;

  示例:檢視ns型別資源中的spec欄位的詳細定義說明

[root@master01 ~]# kubectl explain ns.spec
KIND:     Namespace
VERSION:  v1

RESOURCE: spec <Object>

DESCRIPTION:
     Spec defines the behavior of the Namespace. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

     NamespaceSpec describes the attributes on a Namespace.

FIELDS:
   finalizers   <[]string>
     Finalizers is an opaque list of values that must be empty to permanently
     remove object from storage. More info:
     https://kubernetes.io/docs/tasks/administer-cluster/namespaces/

[root@master01 ~]# 

  提示:如果對應欄位的值有“[]”表示該欄位的值可以是一個對應資料型別的陣列或列表;如果後面還-require-表示該欄位是必選欄位,必須要定義;

  示例:定義自助式pod資源配置清單

[root@master01 ~]# cat pod-demo.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-demo
  namespace: testing
spec:
  containers:
  - image: nginx:1.14-alpine
    imagePullPolicy: IfNotPresent
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
[root@master01 ~]# 

  提示:以上配置清單中定義了建立一個名為nginx-pod-demo的pod建立方法,其中在metadata欄位中最重要的兩個欄位是name和namespace,如果沒有給定這兩個欄位,名字它會隨機生成,名稱空間為default;spec欄位中最核心的就是containers欄位的定義;這個欄位的值為一個陣列,這意味著一個pod裡可以定義多個容器;在配置檔案中使用"-"來表示應用列表或陣列;image欄位用來描述對應容器要用的映象;imagePullPolicy欄位用於描述拖映象的策略,一般這個欄位有三個值,第一個是Never表示從不到網際網路上拖映象,第二個是Always表示不管本地有沒有對應映象,都要到網際網路倉庫中拖映象;第三個是IfNotPresent表示如果本地有就不去網際網路拖映象,如果沒有就去網際網路倉庫拖映象;一般如果對應映象給出了指定的版本,這裡的下載映象的策略為IfNotPresent,如果指定映象的版本為latest,這裡的策略就是Always;name是用來描述容器的名稱;resources這個欄位用於描述對應容器的資源限制,比如最小記憶體和最大記憶體等等資訊;dnsPolicy用於描述使用dns的策略,ClusterFirst表示叢集優先,即k8s叢集內部的dns服務kube-dns;priority用於描述對應資源的優先順序,這個和程式優先順序很類似;restartPolicy用於描述重啟策略,Always表示,只要對應資源故障就重啟;schedulerName用於描述排程器的名稱;預設是default-scheduler,表示使用k8s叢集預設的排程器;

  應用資源配置清單,建立自助式pod

[root@master01 ~]# kubectl get pod -n testing
No resources found in testing namespace.
[root@master01 ~]# kubectl apply -f pod-demo.yaml 
pod/nginx-pod-demo created
[root@master01 ~]# kubectl get pod -n testing     
NAME             READY   STATUS    RESTARTS   AGE
nginx-pod-demo   1/1     Running   0          3s
[root@master01 ~]# 

  提示:可以看到在testing名稱空間下就建立了一個名為nginx-pod-demo的pod;

  示例:定義資源清單建立pod,要求在一個pod中執行兩個容器

[root@master01 ~]# cat pod-demo2.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-demo
  namespace: prod
spec:
  containers:
  - image: nginx:1.14-alpine
    name: nginx
  - image: busybox:latest
    imagePullPolicy: IfNotPresent
    name: bbox
    command: 
    - /bin/sh
    - -c
    - "sleep 86400"    
[root@master01 ~]# 

  提示:command欄位的值是一個字元型列表,主要用於描述對應容器裡執行的程式命令;除了把列表的每個元素用“-”來引用以外,我們也可以使用 “[]”來引用;

[root@master01 ~]# cat pod-demo2.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-demo
  namespace: prod
spec:
  containers:
  - image: nginx:1.14-alpine
    name: nginx
  - image: busybox:latest
    imagePullPolicy: IfNotPresent
    name: bbox
    command: ["/bin/sh","-c","sleep 86400"]
[root@master01 ~]# 

  應用配置清單

[root@master01 ~]# kubectl get pod -n prod
No resources found in prod namespace.
[root@master01 ~]# kubectl apply -f pod-demo2.yaml
pod/nginx-pod-demo created
[root@master01 ~]# kubectl get pod -n prod        
NAME             READY   STATUS              RESTARTS   AGE
nginx-pod-demo   0/2     ContainerCreating   0          5s
[root@master01 ~]# 

  提示:可以看到在prod名稱空間下有一個名叫nginx-pod-demo的pod處於containercreating狀態;表示該pod裡的容器正在處於建立狀態;其中ready欄位下的數字,右邊的2表示pod裡有2個容器,左邊的數字表示有幾個容器準備就緒,0表示一個都沒有;

  進入prod名稱空間下的nginx-pod-demo pod裡的bbox容器

[root@master01 ~]# kubectl exec nginx-pod-demo -c bbox -n prod -it -- /bin/sh 
/ # ifconfig 
eth0      Link encap:Ethernet  HWaddr 76:6E:35:38:55:27  
          inet addr:10.244.3.8  Bcast:10.244.3.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:480 (480.0 B)  TX bytes:42 (42.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # 

  提示:進入pod裡的一個容器,如果對應pod裡只有一個容器,不用-c指定容器名稱,直接指定pod名稱即可;如果一個pod裡有多個容器,需要用-c選項指定容器的名稱;如果進入容器需要執行命令,需要用“--”隔開,後面寫要執行的命令;

  檢視bbox容器裡監聽埠

/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      
/ # ps aux 
PID   USER     TIME  COMMAND
    1 root      0:00 sleep 86400
    8 root      0:00 /bin/sh
   16 root      0:00 ps aux
/ # 

  提示:我們在bbox裡沒有執行任何web服務,它怎麼監聽80埠呢?原因是在同一個pod裡的所有容器都共享同一網路名稱空間,這也意味著我們訪問bbox裡的80,就可以訪問到nginx容器;

  測試:訪問bbox容器裡的80埠,看看是否訪問到nginx容器裡的服務呢?

/ # wget -O - -q http://127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>
/ # 

  提示:可以看到我們在bbox容器裡訪問lo介面上的80埠能夠訪問到nginx容器提供的服務;

  檢視容器日誌

[root@master01 ~]# kubectl get pod -n prod
NAME             READY   STATUS    RESTARTS   AGE
nginx-pod-demo   2/2     Running   0          10m
[root@master01 ~]# kubectl logs -n prod nginx-pod-demo -c nginx
127.0.0.1 - - [08/Dec/2020:13:43:37 +0000] "GET / HTTP/1.1" 200 612 "-" "Wget" "-"
[root@master01 ~]# 

  提示:如果我們要一直監視著對應容器的日誌變化,也可以使用-f,這個有點類似tail命令中的-f選項;

  提示:如果對應pod只有一個容器,我們只需要指定其pod名稱即可;

[root@master01 ~]# kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
myapp-dep-5bc4d8cc74-cvkbc   1/1     Running   0          3h1m
myapp-dep-5bc4d8cc74-gmt7w   1/1     Running   0          3h1m
myapp-dep-5bc4d8cc74-gqhh5   1/1     Running   0          3h6m
ngx-dep-5c8d96d457-w6nss     1/1     Running   0          4h12m
[root@master01 ~]# kubectl logs ngx-dep-5c8d96d457-w6nss
10.244.0.0 - - [08/Dec/2020:09:43:19 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
10.244.0.0 - - [08/Dec/2020:10:07:41 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
10.244.1.0 - - [08/Dec/2020:10:07:46 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
10.244.0.0 - - [08/Dec/2020:10:31:58 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
10.244.0.0 - - [08/Dec/2020:10:32:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
10.244.0.0 - - [08/Dec/2020:10:32:29 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
10.244.0.0 - - [08/Dec/2020:10:36:01 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@master01 ~]# 

  提示:預設不指定名稱空間,就是default名稱空間;

  pod暴露埠

  所謂暴露埠是指把pod裡執行的容器監聽的埠暴露到外部網路能夠訪問的埠上;在k8s上使用資源配置清單建立pod時,我們可以在定義容器時暴露容器的埠;通常暴露埠的方式有兩種,一種是共享宿主機的名稱空間,讓其pod裡的網路和宿主機網路名稱空間在一個名稱空間下;其次是使用埠對映的方式,所謂埠對映就是把pod裡容器監聽的埠對映在宿主機上的某一個埠;外部網路直接訪問對應宿主機埠即可訪問到對應pod裡的容器;pod所屬網路還是pod網路;

  示例:定義資源配置清單,明確定義共享宿主機網路名稱空間

[root@master01 ~]# cat pod-demo3.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-demo3
  namespace: prod
spec:
  containers:
  - image: nginx:1.14-alpine
    imagePullPolicy: IfNotPresent
    name: nginx
  hostNetwork: true
[root@master01 ~]# 

  提示:這裡需要注意一點,hostNetwork這個欄位是spec欄位裡的屬性,縮排要和containers欄位對其;

  應用配置清單

[root@master01 ~]# kubectl get pod -n prod
NAME             READY   STATUS    RESTARTS   AGE
nginx-pod-demo   2/2     Running   0          43m
[root@master01 ~]# kubectl apply -f pod-demo3.yaml 
pod/nginx-pod-demo3 created
[root@master01 ~]# kubectl get pod -n prod -o wide
NAME              READY   STATUS    RESTARTS   AGE   IP             NODE             NOMINATED NODE   READINESS GATES
nginx-pod-demo    2/2     Running   0          44m   10.244.3.8     node03.k8s.org   <none>           <none>
nginx-pod-demo3   1/1     Running   0          15s   192.168.0.45   node02.k8s.org   <none>           <none>
[root@master01 ~]# 

  提示:可以看到應用資源配置清單以後,對應在prod名稱空間下就有一個名為nginx-pod-demo3的pod執行起來了,並且其網路為宿主機網路;

  驗證:登入node02上檢視是否80埠處於監聽?

[root@master01 ~]# ssh node02
Last login: Tue Dec  8 16:28:33 2020 from 192.168.0.232
[root@node02 ~]# ss -tnl
State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port              
LISTEN     0      128                          *:80                                       *:*                  
LISTEN     0      128                          *:22                                       *:*                  
LISTEN     0      100                  127.0.0.1:25                                       *:*                  
LISTEN     0      128                  127.0.0.1:45734                                    *:*                  
LISTEN     0      128                  127.0.0.1:10248                                    *:*                  
LISTEN     0      128                  127.0.0.1:10249                                    *:*                  
LISTEN     0      128                         :::10256                                   :::*                  
LISTEN     0      128                         :::22                                      :::*                  
LISTEN     0      100                        ::1:25                                      :::*                  
LISTEN     0      128                         :::10250                                   :::*                  
[root@node02 ~]# 

  提示:可以看到node02上80埠已經處於監聽狀態;

  驗證:訪問node02的80埠,看看是否訪問到對應nginx-pod-demo3 pod裡執行的nginx容器呢?

  提示:在外部使用瀏覽器訪問pod所在主機的ip地址的80埠能夠正常訪問到對應pod裡的容器;

  示例:使用資源清單定義建立pod時,指定將容器埠對映的對應宿主機的某個埠

[root@master01 ~]# cat pod-demo4.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-demo4
  namespace: prod
spec:
  containers:
  - image: nginx:1.14-alpine
    imagePullPolicy: IfNotPresent
    name: nginx
    ports:
      - containerPort: 80
        hostPort: 8080
        name: web
        protocol: TCP
[root@master01 ~]# 

  提示:在使用資源清單定義暴露容器埠時,需要使用ports欄位,該欄位的值為一個列表物件,裡面主要有containerPort欄位,該欄位用於描述容器監聽埠,hostPort用於描述要把容器埠對映到宿主機上的埠,name用於描述ports這個欄位物件的名字,protocol用於描述使用的協議,預設不指定就是TCP,如果指定對應TCP或UDP必須大寫;

  應用配置清單

[root@master01 ~]# kubectl get pod -n prod
NAME              READY   STATUS    RESTARTS   AGE
nginx-pod-demo    2/2     Running   0          67m
nginx-pod-demo3   1/1     Running   0          23m
[root@master01 ~]# kubectl apply -f pod-demo4.yaml
pod/nginx-pod-demo4 created
[root@master01 ~]# kubectl get pod -n prod -o wide
NAME              READY   STATUS    RESTARTS   AGE   IP             NODE             NOMINATED NODE   READINESS GATES
nginx-pod-demo    2/2     Running   0          68m   10.244.3.8     node03.k8s.org   <none>           <none>
nginx-pod-demo3   1/1     Running   0          24m   192.168.0.45   node02.k8s.org   <none>           <none>
nginx-pod-demo4   1/1     Running   0          14s   10.244.1.7     node01.k8s.org   <none>           <none>
[root@master01 ~]# 

  提示:可以看到對應的pod已經正常處於執行狀態,並且pod網路ip地址也是一個pod網路的地址;該pod被排程到node01上執行;

  驗證:檢視對應node01上是否監聽8080埠?

[root@master01 ~]# ssh node01
Last login: Tue Dec  8 16:30:16 2020 from 192.168.0.232
[root@node01 ~]# ss -tnl
State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port              
LISTEN     0      128                  127.0.0.1:46580                                    *:*                  
LISTEN     0      128                          *:22                                       *:*                  
LISTEN     0      100                  127.0.0.1:25                                       *:*                  
LISTEN     0      128                  127.0.0.1:10248                                    *:*                  
LISTEN     0      128                  127.0.0.1:10249                                    *:*                  
LISTEN     0      128                         :::10256                                   :::*                  
LISTEN     0      128                         :::22                                      :::*                  
LISTEN     0      100                        ::1:25                                      :::*                  
LISTEN     0      128                         :::10250                                   :::*                  
[root@node01 ~]# 

  提示:在pod所在宿主機上並沒有發現8080埠處於監聽狀態;

  驗證:訪問node01的8080埠,看看是否能夠被訪問?

  提示:訪問node01的8080埠能夠正常訪問到對應pod裡的容器;這說明埠對映的方式不會監聽任何埠,它只會體現在iptables或ipvs規則中;

  檢視對應pod所在主機上的iptables規則

[root@node01 ~]# iptables -t nat -nvL|grep 8080
    0     0 CNI-HOSTPORT-SETMARK  tcp  --  *      *       10.244.1.0/24        0.0.0.0/0            tcp dpt:8080
    0     0 CNI-HOSTPORT-SETMARK  tcp  --  *      *       127.0.0.1            0.0.0.0/0            tcp dpt:8080
    1    52 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8080 to:10.244.1.7:80
    1    52 CNI-DN-fca14cb4785a6479cf635  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            /* dnat name: "cbr0" id: "11f45eaf41a77266233e58f57abce144ebf7da0d8924a7ca490d2f64dacc456c" */ multiport dports 8080
[root@node01 ~]# 

  提示:在對應pod所在宿主機上的iptables規則中可以看到有一條DNAT規則,明確寫了任何源地址訪問,訪問到目標埠為8080,就DNAT到10.244.1.7的80埠上;而對應10.244.1.7這個地址恰好就是對應的podip地址;

  以上就是使用資源配置清單定義pod資源常用到的一些屬性說明和演示,以及pod裡的容器埠暴露的兩種方式,但是我們手動定義建立pod資源,一旦刪除,它不會自動恢復,所以這種自主式pod通常在k8s上應用的很少;通常跑一個pod應該使用pod控制器來跑,這樣即便對應的pod故障了,pod控制器能夠幫助我們恢復重建pod;

相關文章