容器之間通信方式\與pod關係

1.概述

k8s裏面容器是存在於pod裏面的,因此容器之間通信,通常分爲三種類型:
1. pod內部容器之間 2. pod 與 pod 容器之間 3. pod 訪問service服務

 (1) pod內部容器之間html

k8s pod內部容器是共享網絡空間的,因此容器直接可使用localhost訪問其餘容器。
k8s 在啓動容器的時候會先啓動一個pause容器,這個容器就是實現這個功能的。

 (2) pod 與 pod容器之間node

  a. 兩個pod在一臺主機上面python

docker默認的docker網橋互連容器

  b. 兩個pod不在同一個主機上面  nginx

k8s官方推薦的是使用flannel組建一個大二層扁平網絡,pod的ip分配由flannel統一分配,通信過程也是走flannel的網橋

(3) pod訪問service服務docker

 Service分配的ip叫cluster ip是一個虛擬ip(相對固定,除非刪除service),這個ip只能在k8s集羣內部使用,若是service須要對外提供,只能使用Nodeport方式映射到主機上,使用主機的ip和端口對外提供服務

 

2. pod與容器區別

pod是k8s的最小單元,容器包含在pod中,一個pod中有一個pause容器和若干個業務容器,而容器就是單獨的一個容器,簡而言之,pod是一組容器,而容器單指一個容器

3.pod 配置管理

Name:         task-pv-pod
Namespace:    default // 沒有指定namespace的就是default
Node:         docker-for-desktop/192.168.65.3 // Pod所在的節點
Start Time:   Mon, 08 Jul 2019 14:05:52 +0800 // pod啓動的時間
Labels:       <none> // 說明沒有設置標籤
Annotations:  kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"task-pv-pod","namespace":"default"},"spec":{"containers":[{"image":"nginx","name":... // 註釋信息
Status:       Running // pod的狀態
IP:           10.1.0.103 // pod的集羣ip
Containers: // 其中包含的容器
  task-pv-container:
    Container ID:   docker://3e9a2ee6b0a13ccee534ec3ffe781adcbff42a7f1851d57e3b374a047a654590
    Image:          nginx // 容器鏡像名稱
    Image ID:       docker-pullable://nginx@sha256:96fb261b66270b900ea5a2c17a26abbfabe95506e73c3a3c65869a6dbe83223a
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 08 Jul 2019 14:05:58 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts: // 這個容器掛載的兩個volume
      /usr/share/nginx/html from task-pv-storage (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-tw8wk (ro)
Conditions:
  Type           Status
  Initialized    True
  Ready          True
  PodScheduled   True
Volumes:
  task-pv-storage: // 掛載的數據卷
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace) // 這個數據卷是共享持久卷
    ClaimName:  task-pv-claim // 使用的聲明
    ReadOnly:   false // 數據卷是否只讀
  default-token-tw8wk:
    Type:        Secret (a volume populated by a Secret) // 這個數據卷是保存密鑰
    SecretName:  default-token-tw8wk
    Optional:    false
QoS Class:       BestEffort // Qos的三個級別,Guaranteed/Burstable/BestEffort,分別對pod的資源限制從嚴到弱
Node-Selectors:  <none> // pod是能夠選擇部署在哪一個node上的,好比部署在有ssd的node上。
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s  // 節點親和性,它使得pod能有傾向性地分配到不一樣節點上。
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events: // 這個pod發生的一些事件
  Type    Reason                 Age   From                         Message
  ----    ------                 ----  ----                         -------
  Normal  Scheduled              21s   default-scheduler            Successfully assigned task-pv-pod to docker-for-desktop
  Normal  SuccessfulMountVolume  20s   kubelet, docker-for-desktop  MountVolume.SetUp succeeded for volume "task-pv-volume"
  Normal  SuccessfulMountVolume  20s   kubelet, docker-for-desktop  MountVolume.SetUp succeeded for volume "default-token-tw8wk"
  Normal  Pulling                19s   kubelet, docker-for-desktop  pulling image "nginx"
  Normal  Pulled                 15s   kubelet, docker-for-desktop  Successfully pulled image "nginx"
  Normal  Created                15s   kubelet, docker-for-desktop  Created container
  Normal  Started                14s   kubelet, docker-for-desktop  Started container

  有的時候咱們可能會忘記了咱們啓動的pod的yaml配置文件地址,咱們能夠經過kubectl get pod task-pv-pod -o=yaml命令來獲取某個已經啓動的 pod 的配置文件,這裏的配置文件會比咱們配置的配置項全不少,由於咱們寫配置文件的時候,不少配置項沒有設置實際上就是使用默認的配置值來實現。後端

kubectl get pod task-pv-pod -o=yaml
apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"task-pv-pod","namespace":"default"},"spec":{"containers":[{"image":"nginx","name":"task-pv-container","ports":[{"containerPort":80,"name":"http-server"}],"volumeMounts":[{"mountPath":"/usr/share/nginx/html","name":"task-pv-storage"}]}],"volumes":[{"name":"task-pv-storage","persistentVolumeClaim":{"claimName":"task-pv-claim"}}]}}
  creationTimestamp: 2019-07-08T06:05:51Z
  name: task-pv-pod
  namespace: default
  resourceVersion: "1439249"
  selfLink: /api/v1/namespaces/default/pods/task-pv-pod
  uid: 7090642e-a146-11e9-89ff-025000000001
spec:
  containers:
  - image: nginx
    imagePullPolicy: Always
    name: task-pv-container
    ports:
    - containerPort: 80
      name: http-server
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /usr/share/nginx/html
      name: task-pv-storage
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-tw8wk
      readOnly: true
  dnsPolicy: ClusterFirst
  nodeName: docker-for-desktop
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: task-pv-storage
    persistentVolumeClaim:
      claimName: task-pv-claim
  - name: default-token-tw8wk
    secret:
      defaultMode: 420
      secretName: default-token-tw8wk
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: 2019-07-08T06:05:52Z
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: 2019-07-08T06:05:58Z
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: 2019-07-08T06:05:51Z
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://3e9a2ee6b0a13ccee534ec3ffe781adcbff42a7f1851d57e3b374a047a654590
    image: nginx:latest
    imageID: docker-pullable://nginx@sha256:96fb261b66270b900ea5a2c17a26abbfabe95506e73c3a3c65869a6dbe83223a
    lastState: {}
    name: task-pv-container
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: 2019-07-08T06:05:58Z
  hostIP: 192.168.65.3
  phase: Running
  podIP: 10.1.0.103
  qosClass: BestEffort
  startTime: 2019-07-08T06:05:52Z

  

spec.container.volumeMounts

spec.container.volumeMounts 其中一個 /usr/share/nginx/html根據 task-pv-storage 掛載到 task-pv-claim 這個共享存儲中。這個pvc 是對應哪一個共享存儲呢?
咱們能夠查看 kubectl get pvcapi

NAME            STATUS    VOLUME           CAPACITY   ACCESS MODES   STORAGECLASS   AGE
task-pv-claim   Bound     task-pv-volume   1Gi        RWO            manual         5h

再經過 kubectl get pv 對應到 pv:網絡

NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                   STORAGECLASS   REASON    AGE
task-pv-volume   1Gi        RWO            Retain           Bound     default/task-pv-claim   manual                   5h

再查看這個 pv 的詳細狀況:kubectl describe pv task-pv-volumeapp

Name:            task-pv-volume
Labels:          type=local
Annotations:     kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"PersistentVolume","metadata":{"annotations":{},"labels":{"type":"local"},"name":"task-pv-volume","namespace":""},"spec":{"ac...
                 pv.kubernetes.io/bound-by-controller=yes
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    manual
Status:          Bound
Claim:           default/task-pv-claim
Reclaim Policy:  Retain
Access Modes:    RWO
Capacity:        1Gi
Node Affinity:   <none>
Message:
Source:
    Type:          HostPath (bare host directory volume)
    Path:          /Users/yejianfeng/Documents/workspace/kubernets_example/data
    HostPathType:
Events:            <none>

  

看到這個pv對應的是宿主機 HostPath 中的 /Users/yejianfeng/Documents/workspace/kubernets_example/data 這個目錄。ui

因此共享存儲的映射關係是 pod -- volume -- pvc -- pv。

其實這裏咱們之因此說是共享存儲,就是說這個存儲應該是一個共享網盤,好比 cephFS,而不該該僅僅只是宿主機上的一個目錄。宿主機上的目錄只是爲了調試方便而已。

  (1) PV與PVC關係

1.accessMode:訪問模型;對象列表:
    ReadWriteOnce – the volume can be mounted as read-write by a single node:  RWO - ReadWriteOnce一人讀寫
    ReadOnlyMany – the volume can be mounted read-only by many nodes:          ROX - ReadOnlyMany 多人只讀
    ReadWriteMany – the volume can be mounted as read-write by many nodes:     RWX - ReadWriteMany多人讀寫
2.resource:資源限制(好比:定義5GB空間,咱們指望對應的存儲空間至少5GB。)    
3.selector:標籤選擇器。不加標籤,就會在全部PV找最佳匹配。
4.storageClassName:存儲類名稱:
5.volumeMode:指後端存儲卷的模式。能夠用於作類型限制,哪一種類型的PV能夠被當前claim所使用。
6.volumeName:卷名稱,指定後端PVC(至關於綁定) 
PV和PVC是一一對應關係,當有PV被某個PVC所佔用時,會顯示banding,其它PVC不能再使用綁定過的PV。
PVC一旦綁定PV,就至關因而一個存儲卷,此時PVC能夠被多個Pod所使用。(PVC支不支持被多個Pod訪問,取決於訪問模型accessMode的定義)。
PVC若沒有找到合適的PV時,則會處於pending狀態。
PV是屬於集羣級別的,不能定義在名稱空間中。
PVC時屬於名稱空間級別的。
PV的reclaim policy選項:
   默認是Retain保留,保留生成的數據。
   能夠改成recycle回收,刪除生成的數據,回收pv
   delete,刪除,pvc解除綁定後,pv也就自動刪除。
相關文章
相關標籤/搜索