快速开始OpenEBS

Posted by 梁远鹏 on 2022-01-02 | 阅读 |,阅读约 4 分钟

TOC

前言

前提

  • 使用OpenEBS需要机器已经安装了iSCSI
  • 已经安装了kubernetes

ubuntu可以用下面的命令安装和开启:

sudo apt-get update
sudo apt-get install open-iscsi
sudo systemctl enable --now iscsid

centos系列系统可以用下面的命令安装和开启:

yum install iscsi-initiator-utils -y
sudo systemctl enable --now iscsid

安装OpenEBS

Helm方式

helm repo add openebs https://openebs.github.io/charts
helm repo update
helm install openebs --namespace openebs openebs/openebs --create-namespace

上述命令将会安装OpenEBS并且默认安装Jiva和Local PV组件,如果需要其他的存储引擎可以用下面的安装命令开启:

helm install openebs --namespace openebs openebs/openebs --create-namespace --set cstor.enabled=true

等待一会后会出现helm安装成功的提示:

oem@lan:~/k8s/openebs$ helm install openebs --namespace openebs openebs/openebs --create-namespace --set cstor.enabled=true

NAME: openebs
LAST DEPLOYED: Mon Jan  3 00:28:41 2022
NAMESPACE: openebs
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Successfully installed OpenEBS.

Check the status by running: kubectl get pods -n openebs

The default values will install NDM and enable OpenEBS hostpath and device
storage engines along with their default StorageClasses. Use `kubectl get sc`
to see the list of installed OpenEBS StorageClasses.

**Note**: If you are upgrading from the older helm chart that was using cStor
and Jiva (non-csi) volumes, you will have to run the following command to include
the older provisioners:

helm upgrade openebs openebs/openebs \
        --namespace openebs \
        --set legacy.enabled=true \
        --reuse-values

For other engines, you will need to perform a few more additional steps to
enable the engine, configure the engines (e.g. creating pools) and create 
StorageClasses. 

For example, cStor can be enabled using commands like:

helm upgrade openebs openebs/openebs \
        --namespace openebs \
        --set cstor.enabled=true \
        --reuse-values

For more information, 
- view the online documentation at https://openebs.io/ or
- connect with an active community on Kubernetes slack #openebs channel.

可以看一下openebs相关的日志运行情况,耐心等待容器运行成功就可以了,第一次需要拉去镜像,所以需要一点时间:

运行命令watch kubectl get po -n openebs

Every 2.0s: kubectl get po -n openebs                                                                                       lan: Mon Jan  3 00:30:41 2022

NAME                                            READY   STATUS              RESTARTS   AGE
openebs-cstor-admission-server-f7c94c6d-n82n6   0/1     ContainerCreating   0          118s
openebs-cstor-csi-controller-0                  0/6     ContainerCreating   0          118s
openebs-cstor-csi-node-hk6kg                    0/2     ContainerCreating   0          118s
openebs-cstor-cspc-operator-d576bb58d-cqcln     0/1     ContainerCreating   0          118s
openebs-cstor-cvc-operator-867b5697b5-glh76     1/1     Running             0          118s
openebs-localpv-provisioner-6f686f7697-967kj    0/1     ContainerCreating   0          118s
openebs-ndm-operator-5948569558-frhjd           0/1     ContainerCreating   0          118s
openebs-ndm-pjfmz                               1/1     Running             0          118s

过了一会可能会出现失败,原因是 CSI 的镜像的官方地址是k8s.gcr.io,国内访问不了,不过没关系,这时候只需要将k8s.gcr.io修改为lank8s.cn就可以了.

可以运行下下面的命令将 CSI 相关的镜像都设置为国内可访问的镜像地址.

helm upgrade openebs --namespace openebs openebs/openebs --set cstor.enabled=true --create-namespace --set cstor.csiController.resizer.image.registry=lank8s.cn/  --set cstor.csiController.snapshotter.image.registry=lank8s.cn/  --set cstor.csiController.snapshotController.image.registry=lank8s.cn/  --set cstor.csiController.attacher.image.registry=lank8s.cn/    --set cstor.csiController.provisioner.image.registry=lank8s.cn/  --set cstor.csiNode.driverRegistrar.image.registry=lank8s.cn/ 

同时可以运行kubectl get storageClass看看 storageClass:

NAME                 PROVISIONER          RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
longhorn (default)   driver.longhorn.io   Delete          Immediate              true                   4d3h
openebs-device       openebs.io/local     Delete          WaitForFirstConsumer   false                  3m12s
openebs-hostpath     openebs.io/local     Delete          WaitForFirstConsumer   false                  3m12s

试用动态PVC

当 openebs 的所有 pod 都处于 Running 的时候就已经真正安装 OpenEBS 成功了,接下来试用一下动态PVC的效果.

redis作为有状态服务部署.

redis.yaml:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: redis
  name: redis
spec:
  selector:
    matchLabels: 
      app: redis
  serviceName: redis-svc
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - image: redis:7.0.7
          name: redis
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - name: redis-vct
              mountPath: /data
  volumeClaimTemplates:
    - metadata:
        name: redis-vct
      spec:
        storageClassName: openebs-jiva-csi-default
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  labels:
    app: redis
spec:
  ports:
    - port: 6379
      name: redis
  type: ClusterIP
  selector:
    app: redis

写好 yaml 文件后运行kubectl apply -f redis.yaml部署 redis.

redis的pod运行起来后进到 redis 存一条数据:

  1. kubectl exec -it redis-0 sh
  2. redis-cli
  3. set lank8scn 1

这时候数据已经存在pvc里面了,可以验证一下,将redis-0这个pod删除,如果重建后的redis-0这个pod还有lank8scn这个key的数据,那么说明数据已经持久化成功了.

  1. kubectl delete po redis-0
  2. kubectl exec -it redis-0 sh(等待redis的pod成功运行后)
  3. redis-cli
  4. get lank8scn

顺利的话你得到key的数据:

127.0.0.1:6379> get lank8scn
"1"

这时候说明已经使用 openebs 作为 storageclass 并且动态生成 PVC 成功了,甚至还部署了一个 redis 应用并且持久化了数据,最简单的一整个流程都跑通了.

再简化

可以思考一下,在上述一整个过程当中哪一个步骤是最麻烦的?

在我看来是当部署 helm chart 时镜像中包含了k8s.gcr.iogcr.io等国内不可访问的镜像后去helm chart模板里面翻到底是哪个参数可以 将这个镜像仓库地址修改的时候.

本次部署 openebs 的时候我就经过了一番折腾才最终找到这个修改的地方.

首先我去 openebs 的 slack 提问,如果有人回答我那就太好了(很遗憾,到最后也没有人给我回答),同时去找到 openebs 对应的 helm chart 仓库,一步一步翻看这个镜像是哪里用到了,如何通过参数修改.

对于只有很长时间内只有这么一次经历其实也还好,但是我们难免还是会在其他常用的 helm chart 中遇到这样的问题,在这里我推荐一下在线的lank8s webhook服务,只需要在k8s集群当中部署一个MutatingWebhook就可以了,不再需要到处找参数去修改.

当然也正在持续的做些准备,将这个 webhook 的所有内容都开源出来并且手把手教学如何在不可访问外网的k8s集群中使用这个 webhook,可以持续关注上面文章的地址,当做好准备时将会更新上述文章.

如果使用了这个 webhook 后(不论在线服务还是本地部署),你不再需要担心哪里会用到不可访问的k8s.gcr.iogcr.io的镜像,轻松解决全局问题.

以本文为例子的话那就是直接运行helm install openebs --namespace openebs --create-namespace --set cstor.enabled=true openebs/openebs就可以了,不需要那些特殊设置镜像的参数了.

对于 gcr.io 和 registry.k8s.io 的镜像拉取,可以了解下 lank8s.cn服务.

从此告别镜像拉取困难.

心路历程

刚开始设置CSI镜像那块发现无法生效,然后我在slackopenebs提问是否没有开放这个设置.

最后也没有人回答这个问题.

在我从 openebs 的几个 chart 中找了一番终于是找到了设置的地方,在 openebs/openebs 的这个 chart 中的 Chart.yaml 文件中有几个子chart,而CSI的镜像就是在 cstor 这个子 chart 设置的,去看看 cstor 这个 chart 的 values.yaml 后明白了设置的方法.

 ...
 dependencies:
...
  - name: cstor
    version: "3.0.2"
    repository: "https://openebs.github.io/cstor-operators"
...

微信公众号

扫描下面的二维码关注我们的微信公众号,第一时间查看最新内容。同时也可以关注我的Github,看看我都在了解什么技术,在页面底部可以找到我的Github。

wechat-qrcode