溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何安裝配置Longhorn并實(shí)現(xiàn)持久化存儲(chǔ)

發(fā)布時(shí)間:2020-06-06 10:17:34 來(lái)源:億速云 閱讀:1595 作者:Leah 欄目:云計(jì)算

 如何安裝配置Longhorn并實(shí)現(xiàn)持久化存儲(chǔ)?今天小編總結(jié)這篇有關(guān)Longhorn的文章,可供感興趣的小伙伴們參考借鑒,希望對(duì)大家有所幫助。

在我們使用Longhorn之前,你需要有一個(gè)正在運(yùn)行的Kubernetes集群。你可以簡(jiǎn)單地安裝一個(gè)k3s集群(https://github.com/rancher/k3s/blob/master/README.md )或者如果你正在使用Civo的Kubernetes服務(wù),你也可以使用它。本文將使用Civo的Kubernetes服務(wù)來(lái)創(chuàng)建集群。


我們建議使用最少的Medium實(shí)例,因?yàn)槲覀儗y(cè)試MySQL的狀態(tài)存儲(chǔ),它可能會(huì)占用大量RAM。
 

$ civo k8s create longhorn-test --wait
Building new Kubernetes cluster longhorn-test: \
Created Kubernetes cluster longhorn-test

 
你的集群需要在每個(gè)節(jié)點(diǎn)上安裝open-iscsi,所以如果你使用的不是civo的Kubernetes服務(wù),除了上述鏈接的說(shuō)明外,你還需要在每個(gè)節(jié)點(diǎn)上運(yùn)行以下命令:
 

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

 

安裝Longhorn

 
在現(xiàn)有Kubernetes集群上安裝Longhorn僅需2步:為L(zhǎng)onghorn安裝controller以及擴(kuò)展包,然后創(chuàng)建一個(gè)可用于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需要使用另一個(gè)命令,然而作為附加步驟,你可以將新的class設(shè)置為默認(rèn),這樣你無(wú)需每次都指定它:
 

$ 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

 

訪問(wèn)Longhorn Dashboard

 

Longhorn有一個(gè)十分簡(jiǎn)潔的Dashboard,可以在上面看到已使用的空間、可用空間、volume列表等等信息。但首先,我們需要?jiǎng)?chuàng)建身份驗(yàn)證的詳細(xì)信息:
 

$ htpasswd -c ./ing-auth admin
$ kubectl create secret generic longhorn-auth \
  --from-file ing-auth --namespace=longhorn-system

 
現(xiàn)在,我們將創(chuàng)建一個(gè)Ingress對(duì)象,可以使用k3s中內(nèi)置的Traefik,并將dashboard暴露到外部。創(chuàng)建一個(gè)名為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文件中添加一個(gè)條目,以將你的任意Kubernetes IP地址指向longhorn-frontend.example.com
 

echo "1.2.3.4 longhorn-frontend.example.com" >> /etc/hosts

 
現(xiàn)在,你可以在瀏覽器上訪問(wèn)http://longhorn-frontend.example.com ,使用admin和使用htpasswd時(shí)輸入的密碼進(jìn)行身份驗(yàn)證之后,可以看到類(lèi)似下面的內(nèi)容:
 
如何安裝配置Longhorn并實(shí)現(xiàn)持久化存儲(chǔ)
 

使用持久化存儲(chǔ)安裝MySQL

 

在單個(gè)容器中運(yùn)行MySQL毫無(wú)意義,因?yàn)楫?dāng)基礎(chǔ)節(jié)點(diǎn)(容器)死亡時(shí),相關(guān)的業(yè)務(wù)也就無(wú)法運(yùn)行,這時(shí)你會(huì)失去客戶、失去訂單。在這里,我們要為它配置一個(gè)新的Longhorn持久卷。

 
首先,我們需要在Kubernetes中創(chuàng)建幾個(gè)資源。其中每個(gè)都是yaml文件,位于一個(gè)空目錄中,或者你可以將它們?nèi)糠旁谝粋€(gè)文件中,使用---進(jìn)行分隔。

 

mysql/pv.yaml中的一個(gè)持久卷:
 

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中對(duì)該卷的聲明(類(lèi)似于抽象請(qǐng)求,以便某些人可以使用該卷):
 

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中還有一個(gè)可以運(yùn)行MySQL并使用上述卷生命的Pod(請(qǐng)注意:我們?cè)诖颂幨褂?code>password作為MySQL的root密碼,但在實(shí)際操作中你應(yīng)該使用安全密碼,并在Kubernetes secret中存儲(chǔ)密碼而不是在YAML中,這里我們只是為了簡(jiǎn)單):
 

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)用文件夾或者單個(gè)文件(這取決于你之前的選擇):
 

$ 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

 

測(cè)試MySQL是否能夠持久化存儲(chǔ)

 

我們的測(cè)試十分簡(jiǎn)單,創(chuàng)建一個(gè)新的數(shù)據(jù)庫(kù),刪除容器(Kubernetes會(huì)幫我們重新創(chuàng)建),然后重新連接,理想的結(jié)果是依舊可以看到我們的新數(shù)據(jù)庫(kù)。

 
好,現(xiàn)在我們來(lái)創(chuàng)建一個(gè)名為should_still_be_here的數(shù)據(jù)庫(kù):
 

$ 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

 
大約一分鐘之后,我們將再次尋找新的容器名稱(chēng),連接到該容器名稱(chēng),看看我們的數(shù)據(jù)庫(kù)是否仍然存在:
 

$ 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并實(shí)現(xiàn)持久化存儲(chǔ)的詳細(xì)內(nèi)容了,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI