您的位置:首页 > 数据库 > Mongodb

第6章将磁盘挂载到容器

2020-04-23 14:16 639 查看


通过卷在容器间共享数据

emptyDir:当pod删除时卷也被删除,用于存放临时数据

pod实例fortune.yaml

apiVersion: v1

kind: Pod

metadata:

    name: fortune

spec:

  containers:

  - image: luksa/fortune

    name: html-generator

    volumeMounts:

    - name: html

      mountPath: /var/htdocs

  - image: nginx:alpine

    name: web-server

    volumeMounts:

    - name: html

      mountPath: /usr/share/nginx/html

      readOnly: true

    ports:

    - containerPort: 80

      protocol: TCP

  volumes:

  - name: html

    emptyDir: {}


kubectl create -f fortune.yaml

kubectl logs fortune html-generator

kubectl logs fortune web-server

kubectl exec -it fortune -c web-server  /bin/sh


hostPath:指向节点文件系统的特定目录

[root@mes2 k8study]# kubectl get pods --namespace kube-system


[root@mes2 k8study]# kubectl describe pods kube-addon-manager-minikube --namespace kube-system

Name:         kube-addon-manager-minikube

Namespace:    kube-system

Priority:     0

Node:         minikube/172.27.14.222

Start Time:   Fri, 23 Aug 2019 05:37:36 -0400

Labels:       component=kube-addon-manager

              kubernetes.io/minikube-addons=addon-manager

              version=v9.0

Annotations:  kubernetes.io/config.hash: 5327162914655af67829868f3ab76393

              kubernetes.io/config.mirror: 5327162914655af67829868f3ab76393

              kubernetes.io/config.seen: 2019-08-23T05:37:15.210580764-04:00

              kubernetes.io/config.source: file

Status:       Running

IP:           172.27.14.222

Containers:

  kube-addon-manager:

    Container ID:   docker://0069f20fe82b89a890f89b13e6f812dca78d81d83ecec0534dc9aaded79a7fd3

    Image:          registry.cn-hangzhou.aliyuncs.com/google_containers/kube-addon-manager:v9.0

    Image ID:       docker-pullable://registry.cn-hangzhou.aliyuncs.com/google_containers/kube-addon-manager@sha256:672794ee3582521eb8bc4f257d0f70c92893f1989f39a200f9c84bcfe1aea7c9

    Port:           <none>

    Host Port:      <none>

    State:          Running

      Started:      Tue, 17 Mar 2020 23:23:17 -0400

    Last State:     Terminated

      Reason:       Error

      Exit Code:    137

      Started:      Fri, 23 Aug 2019 05:37:37 -0400

      Finished:     Tue, 17 Mar 2020 23:23:11 -0400

    Ready:          True

    Restart Count:  1

    Requests:

      cpu:     5m

      memory:  50Mi

    Environment:

      KUBECONFIG:  /var/lib/minikube/kubeconfig

    Mounts:

      /etc/kubernetes/ from addons (ro)

      /var/lib/minikube/ from kubeconfig (ro)

Conditions:

  Type              Status

  Initialized       True

  Ready             True

  ContainersReady   True

  PodScheduled      True

Volumes:

  addons:

    Type:          HostPath (bare host directory volume)

    Path:          /etc/kubernetes/

    HostPathType:

  kubeconfig:

    Type:          HostPath (bare host directory volume)

    Path:          /var/lib/minikube/

    HostPathType:

QoS Class:         Burstable

Node-Selectors:    <none>

Tolerations:       :NoExecute

Events:            <none>


使用持久存储

[root@mes2 k8study]# cat mongonhostpath_pod.yaml

apiVersion: v1

kind: Pod

metadata:

  name: mongodb

spec:

  volumes:

  - name: mongodb-data

    hostPath:

      path: /data/nfs

      type: Directory

  containers:

  - image: mongo

    name: mongodb

    volumeMounts:

    - name: mongodb-data

      mountPath: /data/db

    ports:

    - containerPort: 27017

      protocol: TCP


通过底层持久化存储使用其它类型存储nfs卷

[root@mes2 k8study]# cat mongonfs_pod.yaml

apiVersion: v1

kind: Pod

metadata:

  name: mongodb

spec:

  volumes:

  - name: mongodb-data

    nfs:

      server: 172.27.14.223

      path: /root/data

  containers:

  - image: mongo

    name: mongodb

    volumeMounts:

    - name: mongodb-data

      mountPath: /data/db

    ports:

    - containerPort: 27017

      protocol: TCP



持久卷将底层存储与POD解耦

卷---持久卷---持久卷声明---POD卷

集群管理员将存储卷通过KubenetsAPI服务器创建注册持久卷PV

用户将持久卷声明提交KubenetsAPI服务器匹配到持久卷进行绑定PVC

mongodb-pv-nfs.yaml

apiVersion: v1

kind: PersistentVolume

metadata:

  name: mongodb-pv

spec:

  capacity:

    storage: 1Gi

  accessModes:

  - ReadWriteOnce

  - ReadOnlyMany

  persistentVolumeReclaimPolicy: Retain

gcePersistentDisk:

  pdName: mongodb

  fsType: ext4

创建持久卷声明获取持久卷

mongodb-pvc.yaml

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

  name: mongodb-pvc

spec:

  resources:

    requests:

    storage: 1Gi

  accessModes:

  - ReadWriteOnce

  storageClassName: " "


手动回收持久卷:将persistentVolumeReclaimPolicy设置为Retain

自动回收持久卷:recycle删除卷内容并再次用于卷声明,delete删除底层存储


持久卷的动态配置storageclass

storageclass-fast-gcepd.yaml

apiVersion: storage.k8s.io/v1

kind: StorageClass

metadata:

  name: fast

provisioner: kubernetes.io/gce-pd

parameters:

  type: pd-ssd

  zone: europe-west1-b

请求特定存储类的PVC定义

mongodb-pvc-pd.yaml

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

  name: mongodb-pvc

spec:

  storageClassName: fast

  resources:

    requests:

      storage: 100Mi

accessModes:

  - ReadWriteOnce

创建不指定存储类型的持久卷声明

mongodb-pvc-pd-nostorageclass.yaml

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

  name: mongodb-pvc2

spec:

  resources:

    requests:

      storage: 100Mi

accessModes:

  - ReadWriteOnce


检查存储类

[root@mes2 ~]# kubectl get sc

NAME                 PROVISIONER                AGE

standard (default)   k8s.io/minikube-hostpath   242d

[root@mes2 ~]# kubectl get sc standard -o yaml

apiVersion: storage.k8s.io/v1

kind: StorageClass

metadata:

  annotations:

    storageclass.kubernetes.io/is-default-class: "true"

  creationTimestamp: "2019-08-23T09:37:55Z"

  labels:

    addonmanager.kubernetes.io/mode: EnsureExists

  name: standard

  resourceVersion: "358"

  selfLink: /apis/storage.k8s.io/v1/storageclasses/standard

  uid: 9d6be323-c93d-44f4-bc05-c068cf0897de

provisioner: k8s.io/minikube-hostpath

reclaimPolicy: Delete

volumeBindingMode: Immediate


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息