Brief analysis Kubernetes Create PV, PVC, Deployment
- Basic Environment
#系统环境cat /etc/redhat-releaseCentOS Linux release 7.4.1708 (Core)#k8s client 和 server 的版本信息kubectl versionClient Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e9f0eaead", GitTreeState:"clean", BuildDate:"2018-03-26T16:55:54Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.1", GitCommit:"d4ab47518836c750f9949b9e0d387f20fb92260b", GitTreeState:"clean", BuildDate:"2018-04-12T14:14:26Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
- Order of operations for creating stateful services
#值得注意的是创建顺序非常关键(1)Volume(2)Persistent Volume(3)Persistent Volume Claim(4)Service(5)StatefulSet
- Create PV
I use the NFS service here, click on NFS Deployment and optimization to build your own NFS service.#注意两点:(1)请先部署好自己的NFS服务;(2)在使用共享之前,必须运行自己的NFS服务器并运行共享。
##持久性存储卷apiVersion: v1kind: PersistentVolumemetadata:name: data-nfs ##名字任意取labels: type: nfsspec:capacity: storage: 8GivolumeMode: FilesystemaccessModes: - ReadWriteOncepersistentVolumeReclaimPolicy: RecyclemountOptions: - hard - nfsvers=4.1nfs: path: /data ##NFS服务器上的共享目录 server: 192.168.246.168 ##NFS服务器的ip地址
- Create PVC
##pvc的yamlapiVersion: v1kind: PersistentVolumeClaimmetadata:name: datadir-nginx-0spec:accessModes: - ReadWriteOnceresources: requests: storage: 1Giselector: matchLabels:
#说明:创建PVC的名称和Deployment中的名称要对应上,要不然Deployment中的Pod就肯定创建不成功!简单说一下PVC和Deployment中的命名的规律:如上面创建PVC的yaml文件,我们在这里创建了一个叫做datadir-nginx-0的PVC,粗略一看这个名字有点奇怪,因为这个yaml文件中并没有提到PV的名字,所以PV和PVC是怎么bound起来的呢?是通过labels标签下的type: nfs键值对来进行匹配的,我们在创建PV时指定了label的键值对,在PVC里通过selector可以指定label。关于这个PVC的名称定义:datadir-nginx-0,我们需要看一下下面创建Deployment中的yaml:
- Create deployment
Apiversion:apps/v1kind:deploymentmetadata:name:nginx-deploymentlabels:app:nginxspec:replicas: 1selector:matchlabels:app:nginxtemplate:metadata:labels:app:nginx spec:containers:-Name:ngi NX image:nginx:1.7.9 volumemounts:-Mountpath: "/WTF" name:datadir volumes:-Name:dat Adir persistentvolumeclaim:claimname:datadir-nginx-0 volumeclaimtemplates:-Metadata:name:datadir Labels:type:nfs annotations:volume.alpha.kubernetes.io/storage-class:anything Spec:accessmodes : ["Readwriteonce"] resources:requests:storage:1gi
#说明:接着说PVC的名称定义,首先我们看到Deployment的name叫nginx-deployment,设置的replicas为1个,volumeMounts和volumeClaimTemplates的name必须相同,为datadir,所以Deployment创建的第一个Pod的name应该为nginx-0,第二个为nginx-1,依此类推,这里只定义了一个replicas,所以pod的名字是nginx-0。这里Deployment中的Pod与PVC之间的绑定关系是通过名称来匹配的,即:PVC_name === volumeClaimTemplates_name + "-" + pod_name
Brief analysis Kubernetes Create PV, PVC, Deployment