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

  前文咱們瞭解了k8s上的集羣管理工具kubectl的基礎操做以及相關資源的管理,回顧請參考http://www.javashuo.com/article/p-kofwmrxw-ny.html;今天咱們來聊一下pod資源配置清單的相關話題;html

  咱們知道在k8s上管理資源的方式有兩種,一種是陳述式接口,一種是聲明式接口;在前文咱們用kubectl create的方式建立pod控制器,建立service,建立名稱空間都是使用的陳述式命令的方式在k8s上管理資源;陳述式命令接口管理k8s上的資源的確很方便,但一般建立出來的資源有不少屬性都不是咱們指望的,因此陳述式命令在k8s集羣上管理資源的方式通常不怎麼推薦使用;咱們要想本身手動定義一個資源,就須要使用聲明式接口來建立資源;那麼聲明式接口該怎麼用呢?很簡單,咱們只須要寫一個描述資源的配置文件,而後使用apply命令指定應用對應的資源配置文件便可;node

  要想手寫資源的配置文件,咱們就須要先了解對應的資源有哪些屬性,以及配置清單的語法格式;在k8s上資源配置清單有兩種格式,一種是yaml格式,一種是json格式,經常使用yaml格式;咱們在初學時,能夠仿照陳述命令建立的資源,輸出爲yaml格式的配置文件來寫;nginx

  示例:使用陳述命令建立一個名稱空間,而後查看建立的namespace,輸出爲yaml格式配置文件git

[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格式的清單;web

  導出testing名稱空間的yaml配置文件,並保存爲一個模板文件json

[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 ~]# 

  提示:有了這個配置文件,咱們就能夠照貓畫虎定義其餘名稱空間;api

  示例:使用模板文件,定義建立一個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名稱空間bash

[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;

相關文章
相關標籤/搜索