如何安裝配置Longhorn并實現(xiàn)持久化存儲?今天小編總結(jié)這篇有關(guān)Longhorn的文章,可供感興趣的小伙伴們參考借鑒,希望對大家有所幫助。
創(chuàng)新互聯(lián)建站服務(wù)項目包括赤壁網(wǎng)站建設(shè)、赤壁網(wǎng)站制作、赤壁網(wǎng)頁制作以及赤壁網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,赤壁網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到赤壁省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
在我們使用Longhorn之前,你需要有一個正在運行的Kubernetes集群。你可以簡單地安裝一個k3s集群(https://github.com/rancher/k3s/blob/master/README.md )或者如果你正在使用Civo的Kubernetes服務(wù),你也可以使用它。本文將使用Civo的Kubernetes服務(wù)來創(chuàng)建集群。
我們建議使用最少的Medium實例,因為我們將測試MySQL的狀態(tài)存儲,它可能會占用大量RAM。
$ civo k8s create longhorn-test --wait
Building new Kubernetes cluster longhorn-test: \
Created Kubernetes cluster longhorn-test
你的集群需要在每個節(jié)點上安裝open-iscsi,所以如果你使用的不是civo的Kubernetes服務(wù),除了上述鏈接的說明外,你還需要在每個節(jié)點上運行以下命令:
sudo apt-get install open-iscsi
接著,你既需要下載Kubernetes配置文件并將其保存到~/.kube/config中,還需要將名為KUBECONFIG的環(huán)境變量設(shè)置為其文件名:
cd ~/longhorn-play
civo k8s config longhorn-test > civo-longhorn-test-config
export KUBECONFIG=civo-longhorn-test-config
在現(xiàn)有Kubernetes集群上安裝Longhorn僅需2步:為Longhorn安裝controller以及擴(kuò)展包,然后創(chuàng)建一個可用于pod的StorageClass。第一步:
$ kubectl apply -f https://raw.githubusercontent.com/rancher/longhorn/master/deploy/longhorn.yaml
namespace/longhorn-system created
serviceaccount/longhorn-service-account created
...
創(chuàng)建StorageClass需要使用另一個命令,然而作為附加步驟,你可以將新的class設(shè)置為默認(rèn),這樣你無需每次都指定它:
$ kubectl apply -f https://raw.githubusercontent.com/rancher/longhorn/master/examples/storageclass.yaml
storageclass.storage.k8s.io/longhorn created
$ kubectl get storageclass
NAME PROVISIONER AGE
longhorn rancher.io/longhorn 3s
$ kubectl patch storageclass longhorn -p \
'{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
storageclass.storage.k8s.io/longhorn patched
$ kubectl get storageclass
NAME PROVISIONER AGE
longhorn (default) rancher.io/longhorn 72s
Longhorn有一個十分簡潔的Dashboard,可以在上面看到已使用的空間、可用空間、volume列表等等信息。但首先,我們需要創(chuàng)建身份驗證的詳細(xì)信息:
$ htpasswd -c ./ing-auth admin
$ kubectl create secret generic longhorn-auth \
--from-file ing-auth --namespace=longhorn-system
現(xiàn)在,我們將創(chuàng)建一個Ingress對象,可以使用k3s中內(nèi)置的Traefik,并將dashboard暴露到外部。創(chuàng)建一個名為longhorn-ingress.yaml的文件,并將其放入其中:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: longhorn-ingress
annotations:
ingress.kubernetes.io/auth-type: "basic"
ingress.kubernetes.io/auth-secret: "longhorn-auth"
spec:
rules:
- host: longhorn-frontend.example.com
http:
paths:
- backend:
serviceName: longhorn-frontend
servicePort: 80
然后應(yīng)用它:
$ kubectl apply -f longhorn-ingress.yaml -n longhorn-system
ingress.extensions/longhorn-ingress created
現(xiàn)在,你需要在/etc/hosts文件中添加一個條目,以將你的任意Kubernetes IP地址指向longhorn-frontend.example.com:
echo "1.2.3.4 longhorn-frontend.example.com" >> /etc/hosts
現(xiàn)在,你可以在瀏覽器上訪問http://longhorn-frontend.example.com ,使用admin和使用htpasswd時輸入的密碼進(jìn)行身份驗證之后,可以看到類似下面的內(nèi)容:

在單個容器中運行MySQL毫無意義,因為當(dāng)基礎(chǔ)節(jié)點(容器)死亡時,相關(guān)的業(yè)務(wù)也就無法運行,這時你會失去客戶、失去訂單。在這里,我們要為它配置一個新的Longhorn持久卷。
首先,我們需要在Kubernetes中創(chuàng)建幾個資源。其中每個都是yaml文件,位于一個空目錄中,或者你可以將它們?nèi)糠旁谝粋€文件中,使用---進(jìn)行分隔。
在mysql/pv.yaml中的一個持久卷:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
namespace: apps
labels:
name: mysql-data
type: longhorn
spec:
capacity:
storage: 5G
volumeMode: Filesystem
storageClassName: longhorn
accessModes:
- ReadWriteOnce
csi:
driver: io.rancher.longhorn
fsType: ext4
volumeAttributes:
numberOfReplicates: '2'
staleReplicaTimeout: '20'
volumeHandle: mysql-data
在mysql / pv-claim.yaml中對該卷的聲明(類似于抽象請求,以便某些人可以使用該卷):
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
type: longhorn
app: example
spec:
storageClassName: longhorn
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
在mysql/pod.yaml中還有一個可以運行MySQL并使用上述卷生命的Pod(請注意:我們在此處使用password作為MySQL的root密碼,但在實際操作中你應(yīng)該使用安全密碼,并在Kubernetes secret中存儲密碼而不是在YAML中,這里我們只是為了簡單):
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-mysql
labels:
app: example
spec:
selector:
matchLabels:
app: example
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: example
tier: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
現(xiàn)在,應(yīng)用文件夾或者單個文件(這取決于你之前的選擇):
$ kubectl apply -f mysql.yaml
persistentvolumeclaim/mysql-pv-claim created
persistentvolume/mysql-pv created
deployment.apps/my-mysql created
# or
kubectl apply -f ./mysql/
persistentvolumeclaim/mysql-pv-claim created
persistentvolume/mysql-pv created
deployment.apps/my-mysql created
我們的測試十分簡單,創(chuàng)建一個新的數(shù)據(jù)庫,刪除容器(Kubernetes會幫我們重新創(chuàng)建),然后重新連接,理想的結(jié)果是依舊可以看到我們的新數(shù)據(jù)庫。
好,現(xiàn)在我們來創(chuàng)建一個名為should_still_be_here的數(shù)據(jù)庫:
$ kubectl get pods | grep mysql
my-mysql-d59b9487b-7g644 1/1 Running 0 2m28s
$ kubectl exec -it my-mysql-d59b9487b-7g644 /bin/bash
root@my-mysql-d59b9487b-7g644:/# mysql -u root -p mysql
Enter password:
mysql> create database should_still_be_here;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+----------------------+
| Database |
+----------------------+
| information_schema |
| #mysql50#lost+found |
| mysql |
| performance_schema |
| should_still_be_here |
+----------------------+
5 rows in set (0.00 sec)
mysql> exit
Bye
root@my-mysql-d59b9487b-7g644:/# exit
exit
現(xiàn)在,我們將刪除容器:
kubectl delete pod my-mysql-d59b9487b-7g644
大約一分鐘之后,我們將再次尋找新的容器名稱,連接到該容器名稱,看看我們的數(shù)據(jù)庫是否仍然存在:
$ kubectl get pods | grep mysql
my-mysql-d59b9487b-8zsn2 1/1 Running 0 84s
$ kubectl exec -it my-mysql-d59b9487b-8zsn2 /bin/bash
root@my-mysql-d59b9487b-8zsn2:/# mysql -u root -p mysql
Enter password:
mysql> show databases;
+----------------------+
| Database |
+----------------------+
| information_schema |
| #mysql50#lost+found |
| mysql |
| performance_schema |
| should_still_be_here |
+----------------------+
5 rows in set (0.00 sec)
mysql> exit
Bye
root@my-mysql-d59b9487b-7g644:/# exit
exit以上就是安裝配置Longhorn并實現(xiàn)持久化存儲的詳細(xì)內(nèi)容了,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊!
當(dāng)前題目:如何安裝配置Longhorn并實現(xiàn)持久化存儲
網(wǎng)頁地址:http://www.chinadenli.net/article16/gshidg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站內(nèi)鏈、響應(yīng)式網(wǎng)站、品牌網(wǎng)站設(shè)計、定制開發(fā)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)