使用openEBS可以方便的把本地磁盘作为k8s的存储, 比如hostpath存储类让有状态副本集调度到对应的节点然后使用存储,
可以参考:
https://openebs.io/
https://zhuanlan.zhihu.com/p/519172233?utm_id=0
本文的使用环境
服务器有块磁盘, 一块系统盘,一块数据盘, 数据盘用来存东西, 挂载目录为/data, 格式化为ext4, 作为openebs host-path存储类使用,
如果需要使用块存储类,则不需要分区格式化,保持磁盘原始状态即可
使用方式比较多
比如可以通过lvm挂载数据卷的方式进行扩缩容,这种方式是挂载到已存在路径,且需要保留原有文件的情况下
另外一个推荐方法就是把磁盘分区格式化后挂载到目录/data(注意这里的路径可以根据实际情况设置, 如果本机已存在/data路径会导致/data路径内文件丢失, 所以挂载前需要检查是否存在同名路径,以免数据丢失)
本文使用的第二种方法, 把新盘格式化为ext4,然后挂载到/data目录,然后使用k8s部署openebs使用/data/openebs/目录作为host-path存储类使用,
/data目录下除了openebs可以使用, 系统其他软件也可以使用, 比较方便
下面是部署的一些步骤,部署比较简单
部署openEBS
前提条件
1. 已经安装好kubernetes
2. 已分好区,设置好挂载(块存储跳过)
安装常用软件
# 安装 iscsi
yum install iscsi-initiator-utils -y
# 查看 InitiatorName 是否正常配置
cat /etc/iscsi/initiatorname.iscsi
# 启动查看状态
systemctl enable --now iscsid
systemctl start iscsid.service
systemctl status iscsid.service
分区 格式化 挂载
注意: 这里非必须, 根据实际情况选择
这里是刚开通或者新添加了额外的磁盘,为了方便没有使用lvm方式,具体挂载方式可以根据实际情况选择
关于分区等操作可以参照这篇文章
部署openEBS
kubectl apply -f https://openebs.github.io/charts/openebs-operator.yaml
查看pod是否就绪
kubectl get pods -n openebs
查看存储类是否创建
kubectl get sc
创建数据目录
mkdir -p /data/openebs/local
ll /data/openebs/local
修改存储类配置
如果使用默认的 /var/openebs/local/ 需要修改为 /data/openebs/local/
value: "/var/openebs/local/" #这里修改为/data/openebs/local
kubectl -n openebs edit sc openebs-hostpath
测试可用性
创建pvc
cat > local-hostpath-pvc.yaml <<EOF
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: local-hostpath-pvc
spec:
storageClassName: openebs-hostpath
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5G
EOF
kubectl apply -f local-hostpath-pvc.yaml
创建pod
cat > local-hostpath-pod.yaml <<EOF
---
apiVersion: v1
kind: Pod
metadata:
name: hello-local-hostpath-pod
spec:
volumes:
- name: local-storage
persistentVolumeClaim:
claimName: local-hostpath-pvc
containers:
- name: hello-container
image: busybox
command:
- sh
- -c
- 'while true; do echo "`date` [`hostname`] Hello from OpenEBS Local PV." >> /mnt/store/greet.txt; sleep $(($RANDOM % 5 + 300)); done'
volumeMounts:
- mountPath: /mnt/store
name: local-storage
EOF
kubectl apply -f local-hostpath-pod.yaml
验证数据是否写入磁盘
kubectl get pvc # 查看pvc状态
kubectl exec hello-local-hostpath-pod -- cat /mnt/store/greet.txt
验证物理路径
kubectl get -o yaml pv pvc-df4c6f18-c659-4e7c-941f-276b0fbc37d1 | grep 'path:'
kubectl get pod -owide
#可以看到调度到app-1
# 在app-1上执行命令查看
ll /data/openebs/local/
磁盘性能基准测试
#disk-bench.yaml
---
#pvc
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: dbench-pv-claim
spec:
storageClassName: openebs-hostpath
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5G
---
#pod
apiVersion: batch/v1
kind: Job
metadata:
name: dbench
spec:
template:
spec:
containers:
- name: dbench
image: openebs/perf-test:latest
imagePullPolicy: Always
env:
- name: DBENCH_MOUNTPOINT
value: /data
# - name: DBENCH_QUICK
# value: "yes"
# - name: FIO_SIZE
# value: 1G
# - name: FIO_OFFSET_INCREMENT
# value: 256M
# - name: FIO_DIRECT
# value: "0"
volumeMounts:
- name: dbench-pv
mountPath: /data
restartPolicy: Never
volumes:
- name: dbench-pv
persistentVolumeClaim:
claimName: dbench-pv-claim
backoffLimit: 4
kubectl apply -f disk-bench.yaml
查看测试结果
kubectl logs -f dbench-****