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也就自动删除。