6.k8s.存储Volume.PV.PVC

#Volume

Volume 解决数据持久化和容器间共享数据
Kubernetes支持几十种类型的后端存储卷

hostPath挂载实例,挂载Node节点/tmp/test-volume目录到容器/test-pd

#volume-test.yaml
apiVersion: v1
kind: Pod
metadata:
  name: volume-test
spec:
  containers:
  - image: alivv/nginx:node
    name: volume-test
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /tmp/test-volume
      type: DirectoryOrCreate  #node节点不存在则创建空目录
#创建pod
kubectl create -f volume-test.yaml

#查看pod所在node节点
kubectl get pod volume-test -o wide
#在容器里创建文件
kubectl exec -it volume-test -- sh -c "date >/test-pd/txt"

#删除pod
kubectl delete -f volume-test.yaml

#在node节点检查文件存在
cat /tmp/test-volume/txt

hostPath type类型Directory、File、Socket等
参考 https://feisky.gitbooks.io/kubernetes/concepts/volume.html

#PV PVC

  • PV PersistentVolume 持久卷,作为存储资源,包含存储实现细节

  • PVC PersistentVolumeClaim 用户存储的请求,Pod消耗节点资源

#使用NFS后端存储,创建PV、PVC、Pod实例

#创建nfs存储

#NFS服务端 (centos7)
yum install nfs-utils rpcbind -y
mkdir -p /data/nfs/{download,bakup,www}
echo "/data/nfs *(rw,no_root_squash,sync)">>/etc/exports
exportfs -r
systemctl enable rpcbind nfs-server
systemctl restart rpcbind nfs-server
showmount -e localhost

#NFS客户端(Node节点)
yum install nfs-utils rpcbind -y
systemctl start rpcbind
systemctl enable rpcbind

#创建PV

#pv1-demo.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name:  pv1
spec:
  capacity: #存储能力
    storage: 10Gi
  accessModes:
  - ReadWriteMany  #读写权限,多节点挂载
  persistentVolumeReclaimPolicy: Retain  #回收策略 保留数据
  nfs:
    path: /data/nfs
    server: 172.16.11.141
#创建pv
kubectl create -f pv1-demo.yaml
#查看
kubectl get pv

创建PVC

#pvc-nfs-demo.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-nfs
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

kubectl create -f pvc-nfs-demo.yaml
kubectl get pvc
kubectl get pv

部署Pod使用pvc实例

#nfs-pvc-deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nfs-pvc
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nfs-pvc
    spec:
      containers:
      - name: nginx
        image: alivv/nginx:node
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          name: web
        #使用volume
        volumeMounts:
        - name: www
          subPath: nginx-pvc  #远程子路径
          mountPath: /usr/share/nginx/html
      volumes:
      - name: www
        persistentVolumeClaim:
          claimName: pvc-nfs
#部署Pod使用pvc实例
kubectl apply -f nfs-pvc-deploy.yaml
#查看
kubectl get pod
#NFS节点写入index.html文件
echo nfs pvs test $(date +"%F %T") |tee /data/nfs/nginx-pvc/index.html

#查看Pod ip
kubectl get pod -o wide

#访问Pod ip,查看内容为NFS节点写入内容
kubectl get pod -o wide |awk '/nfs-pvc/{print "curl "$6}'
kubectl get pod -o wide |awk '/nfs-pvc/{print "curl "$6}' |sh
#删除demo
kubectl delete -f nfs-pvc-deploy.yaml
kubectl delete -f pvc-nfs-demo.yaml
kubectl delete -f pv1-demo.yaml

Blog地址 https://www.cnblogs.com/elvi/p/11755813.html
本文git地址 https://gitee.com/alivv/k8s/tree/master/notes

posted @ 2019-10-28 22:26  Elvin-vip  阅读(699)  评论(0编辑  收藏  举报
https://rpc.cnblogs.com/metaweblog/elvi