资讯专栏INFORMATION COLUMN

在K8S集群下为应用配置本地卷(Local Volume)

alaege / 1770人阅读

摘要:手动配置本地卷此节介绍在不使用情况下如何配置手动本地卷。主机挂载本地卷如本例在主机配置本地卷,需将目录手动挂载到目录,如环境设置权限部署可选。

本地卷描述

HadoopES等系统,其DataNode需大量存储,且其本身提供了冗余功能,那么我们就没必要让其从存储系统中分配卷,而是像裸机部署一样让其使用本地节点上的存储,local volumes出现之前,我们可使用HostPath挂载卷到容器中,但此方案有很多局限性:

The prior mechanism of accessing local storage through hostPath volumes had many challenges. hostPath volumes were difficult to use in production at scale: operators needed to care for local disk management, topology, and scheduling of individual pods when using hostPath volumes, and could not use many Kubernetes features (like StatefulSets). Existing Helm charts that used remote volumes could not be easily ported to use hostPath volumes. The Local Persistent Volumes feature aims to address hostPath volumes’ portability, disk accounting, and scheduling challenges.

注意:本地卷仅适用于少量应用,如同HostPath一样Pod被绑定到特定的主机上,若主机异常,则Pod没法调度到其他节点,其适用场景:

Caching of datasets that can leverage data gravity for fast processing

Distributed storage systems that shard or replicate data across multiplenodes. Examples include distributed datastores like Cassandra, or distributedfile systems like Gluster or Ceph.

localvolumehostpath类似,但其更灵活且统一,如应用使用hostpath,则只能在yaml文件中硬编码,而localvolume如同普通pvc灵活可用,具体见下文。

手动配置本地卷

此节介绍在不使用local provisioner情况下如何配置手动本地卷。

管理员必须为本地卷创建一个存储类,如下所示,创建了一个名为local-storage的存储类:

% kubectl create -f - <

注意

此存储类无法动态提供存储功能,所有PV需手动创建;

volumeBindingMode: WaitForFirstConsumer:Pod调度前不先绑定PVC与PV,而是等待Pod被调度时,这样可根据Pod资源等请求合理调度,如:selectors, affinity and anti-affinity policies

宿主机准备目录,即配置本地硬盘。如实验环境okd-c01okd-c02主机均配置了/mnt/test本地存储,而对于OKD集群需设置SeLinux权限:

chcon -R unconfined_u:object_r:svirt_sandbox_file_t:s0 /mnt/test

手动创建两PVexample-local-pv-1example-local-pv-2分别绑定两主机存储,如绑定okd-c01.zyl.io主机的本地卷如下:

% kubectl create -f - <

创建PVC

% kubectl create -f - <

注意:此时PVC不会立马绑定到PV,其等待Pod被调度时才会绑定到PV上,当前PVC状态为Pending

% oc describe pvc example-local-claim
...
Events:
  Type    Reason                Age               From                         Message
  ----    ------                ----              ----                         -------
  Normal  WaitForFirstConsumer  5s (x2 over 10s)  persistentvolume-controller  waiting for first consumer to be created before binding

配置应用使用存储:

% kubectl create -f - <<"EOF"
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: local-volume-test
  name: local-volume-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: local-volume-test
  template:
    metadata:
      labels:
        app: local-volume-test
    spec:
      containers:
      - image: busybox
        name: local-volume-test
        imagePullPolicy: IfNotPresent
        command: [ "/bin/sh", "-c", "while true; do sleep 2; echo $(date) $(hostname)>> /mnt/test/test.log; done" ]
        volumeMounts:
        - name: local-data
          mountPath: /mnt/test
      volumes:
      - name: local-data
        persistentVolumeClaim:
          claimName: example-local-claim
EOF

Pod调度后会发现PVC被绑定:

% oc get pvc example-local-claim 
NAME                  STATUS    VOLUME             CAPACITY   ACCESS MODES STORAGECLASS    AGE
example-local-claim   Bound     example-local-pv-2 1Gi        RWO          local-storage   7m

注意:

此时删除Pod,可发现其仍然被调度到本地存储example-local-pv-2所在的主机,即okd-c02.zyl.io

删除deployment后,PVC不会与PV解绑,即第一次Pod调度后,PVC就与PV关联了;

删除PVC后,发现PV一直处于Released状态,导致PV无法被重用,需管理员手动删除PV并重建PV以便被重用:

When local volumes are manually created like this, the only supported persistentVolumeReclaimPolicy is “Retain”. When the PersistentVolume is released from the PersistentVolumeClaim, an administrator must manually clean up and set up the local volume again for reuse.
自动配置本地卷

手动配置本地券中描述的PV需手动创建,而PVC删除后PV没法重用,即需重建PV才行,当系统使用大量Local Volume时会加重管理负担,鉴于此,可通过external static provisioner协助简化local volume配置。

当前版本(<=K8S 1.12OKD 1.11)管理员需手动在主机上将volume挂载到特定目录上,而后external static provisioner会扫描此目录,其将自动创建PV,而当PVC被释放后又会清理目录内容并重建PV,其是半自动化的,但并非动态提供,后续版本会提供动态提供支持,如管理员仅需提供LVM VG,此provisioner将自动完成格式化、挂载等步骤:

Dynamic provisioning of local volumes using LVM is under design and an alpha implementation will follow in a future release. This will eliminate the current need for an administrator to pre-partition, format and mount local volumes, as long as the workload’s performance requirements can tolerate sharing disks.

参考:

Local Persistent Volumes for Kubernetes Goes Beta;

[kubernetes-sigs/sig-storage-local-static-provisioner:K8S官方提供的external static provisioner

Configuring Local Volumes:OKD如何配置local provisioner

以下描述如何使用OKD提供的local provisioner

主机挂载本地卷

如本例在okd-c0[1-3]主机配置本地卷,需将目录手动挂载到/mnt/local-storage//目录,如:

cat >> /etc/fstab <

OKD/Openshift环境设置SeLinux权限:

chcon -R unconfined_u:object_r:svirt_sandbox_file_t:s0 /mnt/local-storage/
部署local provisioner

可选。在多带带的项目下部署local provisioner,创建项目:

oc new-project local-storage

创建一个ConfigMap,其被external provisioner使用,用于描述存储类:

% kubectl create -f - <<"EOF"
apiVersion: v1
kind: ConfigMap
metadata:
  name: local-volume-config
data:
    storageClassMap: |
        local-storage-1:
            hostDir: /mnt/local-storage/local-storage-1
            mountDir: /mnt/local-storage/local-storage-1
        local-storage-2:
            hostDir: /mnt/local-storage/local-storage-2
            mountDir: /mnt/local-storage/local-storage-2
EOF

注意

local-storage-1:为StorageClass名称,与/mnt/local-storage/对应;

hostDir:本机目录;

moutDirexternal provisionerhostDir挂载到pod中的目录,与hostDir保持一致即可。

provisionerroot权限运行,OKD集群需赋权:

oc create serviceaccount local-storage-admin
oc adm policy add-scc-to-user privileged -z local-storage-admin

OKD集群安装模板:

oc create -f https://raw.githubusercontent.com/openshift/origin/release-3.11/examples/storage-examples/local-examples/local-storage-provisioner-template.yaml

从以上模板安装应用(PS:其以DS模式运行在所有计算节点上):

oc new-app -p CONFIGMAP=local-volume-config 
  -p SERVICE_ACCOUNT=local-storage-admin 
  -p NAMESPACE=local-storage 
  -p PROVISIONER_IMAGE=quay.io/external_storage/local-volume-provisioner:latest 
  local-storage-provisioner

创建所需的StorageClass

% kubectl create -f - <<"EOF"
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
 name: local-storage-1
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
EOF

% kubectl create -f - <<"EOF"
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
 name: local-storage-2
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
EOF

注意StorageClass创建完成后,PV才会被自动创建。

接着,我们手动创建PVC或者在Statefulset中使用此存储卷:

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: example-local-claim
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage-1
  resources:
    requests:
      storage: 5Gi

---
kind: StatefulSet
...
 volumeClaimTemplates:
  - metadata:
      name: example-local-claim
    spec:
      accessModes:
      - ReadWriteOnce
      storageClassName: local-storage-1
      resources:
        requests:
          storage: 5Gi

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/33176.html

相关文章

  • K8S集群下为应用配置本地Local Volume

    摘要:手动配置本地卷此节介绍在不使用情况下如何配置手动本地卷。主机挂载本地卷如本例在主机配置本地卷,需将目录手动挂载到目录,如环境设置权限部署可选。 本地卷描述 如Hadoop、ES等系统,其DataNode需大量存储,且其本身提供了冗余功能,那么我们就没必要让其从存储系统中分配卷,而是像裸机部署一样让其使用本地节点上的存储,local volumes出现之前,我们可使用HostPath挂载...

    frank_fun 评论0 收藏0
  • Kubernetes 有状态集群服务部署与管理

    摘要:本期主题有状态集群服务部署与管理嘉宾介绍张寿红,从事软件研发工作十余年,目前从事基于和的企业级容器云平台研发工作,主要包括容器服务存储服务和镜像服务等。为此开发了一套以为核心的全新特性,方便了有状态集群服务在上的部署和管理。 极牛技术分享活动 极牛技术实践分享系列活动是极牛联合顶级VC、技术专家,为企业、技术人提供的一种系统的线上技术分享活动。 每期不同的技术主题,和行业专家深度探讨,...

    liukai90 评论0 收藏0
  • K8S | Kubernetes 1.7 本地数据管理

    摘要:本地数据卷管理。本地数据卷的管理是一个比较复杂的工程,需要支持文件系统和块存储。在中,本地数据卷管理暂时不会处理该问题会设计一个外部控制器来定期查询该错误状态,并根据一定的策略重新调度。本地数据卷的创建由新的组件处理,该组件将以的方式部署。 ​本期文章来自才云科技(Caicloud)CTO 邓德源的技术原创。 1Overview Kubernetes 1.7 不会引入过多新功能,比较重...

    kevin 评论0 收藏0
  • Kubernetes 核心概念

    摘要:核心概念是最小的调度单元,可以由一个或者多个容器组成。该模式会跟云服务商有关,比如可以通过等创建一个外部的负载均衡器,将请求转发到对应的服务组。而可以提供外部服务可访问的负载均衡器等。 概述 Kubernetes 有各类资源对象来描述整个集群的运行状态。这些对象都需要通过调用 kubernetes api 来进行创建、修改、删除,可以通过 kubectl 命令工具,也可以直接调用 k8...

    Cobub 评论0 收藏0
  • kubernetes中的local persistent volume

    摘要:的详细内容如下当然,也可以不使用,而是手动创建。前是将作为记录到中,起将其独立为一个字段接下来可以创建各种,记得要在的模板中声明。注意到这里是,即我们一开始创建的实例的名字。为表示中申明的未的,在集群内的中找不到可以匹配的。 什么是Local Persistent Volumes 在kubernetes 1.14版本中, Local Persistent Volumes(以下简称LPV...

    Zhuxy 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<