1.概述
k8s里面容器是存在於pod里面的,所以容器之間通訊,一般分為三種類型:
1. pod內部容器之間 2. pod 與 pod 容器之間 3. pod 訪問service服務
(1) pod內部容器之間
k8s pod內部容器是共享網絡空間的,所以容器直接可以使用localhost訪問其他容器。
k8s 在啟動容器的時候會先啟動一個pause容器,這個容器就是實現這個功能的。
(2) pod 與 pod容器之間
a. 兩個pod在一台主機上面
docker默認的docker網橋互連容器
b. 兩個pod不在同一個主機上面
k8s官方推薦的是使用flannel組建一個大二層扁平網絡,pod的ip分配由flannel統一分配,通訊過程也是走flannel的網橋
(3) pod訪問service服務
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 pvc
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-volume
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 這個目錄。
所以共享存儲的映射關系是 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也就自動刪除。
