天天看點

k8s使用openebs實作動态持久化存儲

簡介

本文章介紹如何使用openebs為k8s提供動态申請pv的功能。iscsi提供底層存儲功能,openebs管理iscsi。目前隻支援pv的

ReadWriteOnce

通路模式

通路模式隻是能力描述,并不是強制執行的,對于沒有按pvc 聲明的方式使用pv,存儲提供者應該負責通路時的運作錯誤。例如如果設定pvc的通路模式為

ReadOnlyMany

,pod挂載後依然可寫,如果需要真正的不可寫,申請pvc是需要指定

readOnly: true

參數

安裝

實驗用的Vagrantfile

# -*- mode: ruby -*- # vi: set ft=ruby :

ENV["LC_ALL"] = "en_US.UTF-8"

Vagrant.configure("2") do |config|
 (1..3).each do |i|
 config.vm.define "lab#{i}" do |node|
 node.vm.box = "centos-7.4-docker-17"
 node.ssh.insert_key = false
 node.vm.hostname = "lab#{i}"
 node.vm.network "private_network", ip: "11.11.11.11#{i}"
 node.vm.provision "shell",
 inline: "echo hello from node #{i}"
 node.vm.provider "virtualbox" do |v|
 v.cpus = 2
 v.customize ["modifyvm", :id, "--name", "lab#{i}", "--memory", "3096"]
 file_to_disk = "lab#{i}_vdb.vdi"
 unless File.exist?(file_to_disk)
 # 50GB
 v.customize ['createhd', '--filename', file_to_disk, '--size', 50 * 1024]
 end
 v.customize ['storageattach', :id, '--storagectl', 'IDE', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', file_to_disk]
 end
 end
 end
end
複制代碼           

安裝配置iscsi

# 安裝 iscsi
yum install iscsi-initiator-utils -y

# 檢視 InitiatorName 是否正常配置
cat /etc/iscsi/initiatorname.iscsi

# 啟動檢視狀态
systemctl start iscsid.service
systemctl status iscsid.service

systemctl start iscsi.service
systemctl status iscsi.service
複制代碼           

安裝openebs

# 部署
mkdir openebs && cd openebs
wget https://raw.githubusercontent.com/openebs/openebs/v0.6/k8s/openebs-operator.yaml
wget https://raw.githubusercontent.com/openebs/openebs/v0.6/k8s/openebs-storageclasses.yaml
kubectl apply -f openebs-operator.yaml
kubectl apply -f openebs-storageclasses.yaml

# 檢視 openebs 狀态
kubectl get pods -n openebs -o wide
kubectl get svc -n openebs
kubectl get crd
複制代碼           

測試

# 檢視 storage class
kubectl get sc

# 建立pvc測試
cat >openebs-pvc-test.yaml<<EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
 name: openebs1
spec:
 storageClassName: openebs-standard
 accessModes:
 - ReadWriteOnce
 resources:
 requests:
 storage: 5Gi
EOF
kubectl apply -f openebs-pvc-test.yaml
 
# 檢視
kubectl get pvc
kubectl get pv
 
# 建立 nginx pod 挂載測試
cat >nginx-pod.yaml<<EOF
apiVersion: v1
kind: Pod
metadata:
 name: nginx-pod1
 labels:
 name: nginx-pod1
spec:
 containers:
 - name: nginx-pod1
 image: nginx:alpine
 ports:
 - name: web
 containerPort: 80
 volumeMounts:
 - name: openebs1-vol1
 mountPath: /usr/share/nginx/html
 volumes:
 - name: openebs1-vol1
 persistentVolumeClaim:
 claimName: openebs1
EOF
kubectl apply -f nginx-pod.yaml
 
# 檢視
kubectl get pods -o wide
 
# 修改檔案内容
kubectl exec -ti nginx-pod1 -- /bin/sh -c 'echo Hello World from Openebs!!! > /usr/share/nginx/html/index.html' # 通路測試
POD_ID=$(kubectl get pods -o wide | grep nginx-pod1 | awk '{print $(NF-1)}')
curl http://$POD_ID           

本文轉自掘金-

k8s使用openebs實作動态持久化存儲

繼續閱讀