emptyDir:数据卷在创建pod时分配给该pod,并且直到pod被移除,该数据卷才被释放。该数据卷初始分配时,始终是一个空目录。同一个pod中的不同容器都可以对该目录执行读写操作,并且共享其中的数据(尽管不同容器可能将该数据卷挂载到容器中的不同路径)。当pod被删除后,emptyDir数据卷中的数据将被永久删除。(PS:容器奔溃时,kubelet并不会删除pod,而仅仅是将容器重启,因此emptyDir中的数据在容器崩溃并重启后,仍然是存在的)。简单说就是当你使用emptyDir持久化数据时当你删除pod中的容器时,你的数据不会受到影响,但是当你删除pod以后,你的数据就会丢失,这也是它的弱点所在.
实现emptyDir代码如下
[root@k8s-master tomcat]# cat mysql-rc.yml
apiVersion: v1
kind: ReplicationController
metadata:
namespace: tomcat
name: mysql
spec:
replicas: 1
selector:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
volumes:
- name: tomcat-db
emptyDir: {}
containers:
- name: mysql
image: 10.0.0.11:5000/mysql:5.7
volumeMounts:
- name: tomcat-db
mountPath: /var/lib/mysql
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: '123456'
[root@k8s-master tomcat]# cat mysql-svc.yml
apiVersion: v1
kind: Service
metadata:
namespace: tomcat
name: mysql
spec:
ports:
- port: 3306
targetPort: 3306
selector:
app: mysql
[root@k8s-master tomcat]# cat tomcat-rc.yml
apiVersion: v1
kind: ReplicationController
metadata:
namespace: tomcat
name: myweb
spec:
replicas: 1
selector:
app: myweb
template:
metadata:
labels:
app: myweb
spec:
containers:
- name: myweb
image: 10.0.0.11:5000/tomcat-app:v2
ports:
- containerPort: 8080
env:
- name: MYSQL_SERVICE_HOST
value: 'mysql'
- name: MYSQL_SERVICE_PORT
value: '3306'
[root@k8s-master tomcat]# cat tomcat-svc.yml
apiVersion: v1
kind: Service
metadata:
namespace: tomcat
name: myweb
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30008
selector:
app: myweb
hostPath 类型的数据卷将 Pod(容器组)所在节点的文件系统上某一个文件或目录挂载进容器组(容器内部),类似于docker中的bind mount挂载方式。
这种数据持久化的方式,使用场景不多,**因为它增加了pod与节点之间的耦合。**这种类型的持久化必须将pod指定到指定的宿主机上,因为换了宿主机之后数据的持久化就不存在了
[root@k8s-master wordpress]# cat wordpress-db-rc.yml
apiVersion: v1
kind: ReplicationController
metadata:
namespace: wordpress
name: wordpress-db
spec:
replicas: 1
selector:
app: wordpress-db
template:
metadata:
labels:
app: wordpress-db
spec:
nodeName: k8s-node-1
volumes:
- name: share-db
hostPath:
path: /data/wordpress-db
containers:
- name: wordpress-db
image: 10.0.0.11:5000/mysql:5.7
volumeMounts:
- name: share-db
mountPath: /var/lib/mysql
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: 'somewordpress'
- name: MYSQL_DATABASE
value: 'wordpress'
- name: MYSQL_USER
value: 'wordpress'
- name: MYSQL_PASSWORD
value: 'wordpress'
这种类型的持久化,是在master节点上,或者专门指定的机子上装上nfs服务端,并配置共享目录,服务中的pod挂载共享目录
nfs配置文件
vim /etc/exports
/oldboy 10.0.0.0/24(rw,async,no_root_squash,no_all_squash)
在对应的node节点上装上nfs客户端
showmount -e 10.0.0.11
查看是否成功
spec:
volumes:
- name: share-db
nfs: #挂载类型
path: /oldboy/wordpress-db #nfs共享目录
server: 10.0.0.11 #nfs服务端IP
pv是一个全局资源,连接后端的存储服务器(nfs,gluster)
官方文档解释:
A PersistentVolume (PV) is a piece of storage in the cluster that has
been provisioned by an administrator or dynamically provisioned using
Storage Classes. It is a resource in the cluster just like a node is a
cluster resource. PVs are volume plugins like Volumes, but have a
lifecycle independent of any individual pod that uses the PV. This API
object captures the details of the implementation of the storage, be that
NFS, iSCSI, or a cloud-provider-specific storage system
PersistentVolume(PV) 是集群中的分页存储,它是管理员使用Storage Classes为集群分配的。
它是k8s集群中的一种资源类似于deployment,就像节点是集群的资源一样。
pv是类似于卷的卷插件(因为pv连接的是后端的Storage Classes),
但是有一个独立于使用PV的任何pod的生命周期(干掉pod不会影响pv)
什么是pvc
A PersistentVolumeClaim (PVC) is a request for storage by a user. It is
similar to a pod. Pods consume node resources and PVCs consume PV
resources. Pods can request specific levels of resources (CPU and Memory).
Claims can request specific size and access modes (e.g., can be mounted
once read/write or many times read-only).
PVC是用户请求存储的方式,pvc就像是pod一样,pod是占用node节点的资源,pvc是占用pv的资源但是一个pv只能一个pvc使用,pod可以使用指定的cpu和内存,pvc可以设置规范的大小和读写方式.
代码实现:
[root@k8s-master pv]# cat mysql_pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: tomcat-mysql
labels:
type: nfs001
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Recycle
nfs:
path: "/oldboy/tomcat-mysql"
server: 10.0.0.1
[root@k8s-master pv]# cat mysql_pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: tomcat-mysql
namespace: tomcat
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
[root@k8s-master tomcat]# cat mysql-rc.yml
apiVersion: v1
kind: ReplicationController
metadata:
namespace: tomcat
name: mysql
spec:
replicas: 1
selector:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
volumes:
- name: nfs
persistentVolumeClaim:
claimName: tomcat-mysql