openshift 容器云从入门到崩溃之七《数据持久化》

数据持久化常用的有两种:

hostPath 挂载容器宿主机的本地文件夹,直接修改pod的配置

  volumes:
    - hostPath:
        path: /data/logging-es
        type: ''
      name: elasticsearch-storage

这种方式虽然简单但是有个致命缺点就是容器必须运行在某个node节点上

  nodeName: oc-node02

还有就是重点要说的网络存储:

因为oc是基于k8s的所以k8s支持的存储类型oc都支持

k8s支持存储:https://kubernetes.io/docs/concepts/storage/persistent-volumes/

下面主要说两个一个是NFS和glusterfs

1、NFS

为docker-registry配置数据持久化

创建pv

# vim pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
name: registry-storage-pv
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /opt/nfs
server: nfs.server
readOnly: false

# oc create -f pv.yaml

 创建pvc

# vim pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: registry-storage  
spec:
  accessModes:
  - ReadWriteMany      
  resources:
     requests:
       storage: 100Gi 
# oc project default
# oc create -f pvc.yaml

 挂载存储

# oc set volume dc/docker-registry --add --name=registry-storage -t pvc --claim-name=registry-storage --overwrite
# oc set volume dc/docker-registry --add --name=registry-storage -m /registry --overwrite

1、glusterfs

glusterfs是红帽自家的分布式存储部署起来就很简单了

修改/etc/ansible/hosts文件加入以下内容

[OSEv3:children]
glusterfs
[OSEv3:vars]
openshift_storage_glusterfs_namespace=app-storage
openshift_storage_glusterfs_storageclass=true
openshift_storage_glusterfs_storageclass_default=true
openshift_storage_glusterfs_block_deploy=true
openshift_storage_glusterfs_block_host_vol_size=100
openshift_storage_glusterfs_block_storageclass=true
openshift_storage_glusterfs_block_storageclass_default=false
[glusterfs]
n1.example.com glusterfs_devices='[ "/dev/xvdb", "/dev/xvdc" ]'
n1.example.com glusterfs_devices='[ "/dev/xvdb", "/dev/xvdc" ]'
n1.example.com glusterfs_devices='[ "/dev/xvdb", "/dev/xvdc" ]'

运行部署程序

# ansible-playbook /root/openshift-ansible/playbooks/openshift-glusterfs/config.yml

部署好之后查看storageclass

# oc get storageclass

其实oc(k8s)是不能支持直接操作glusterfs的是借助heketi来管理glusterfs卷的,所以heketi的稳定性至关重要

# oc project app-storage
# oc get pod

如果以后在web console 创建pvc都是默认使用glusterfs-storage storageclass动态创建pv和glusterfs卷很方便

可以看到pv已经自动建好了,当然你删了pvc pv也会自动删除,heketi也会删除关联的glusterfs卷

 

原文地址:https://www.cnblogs.com/37yan/p/10413288.html