在本文中你將學會如何使用k3s在Civo上運行Longhorn。若是你還沒使用過Civo,能夠到官網註冊(https://www.civo.com/ )還能夠申請免費的使用額度。首先,須要一個Kubernetes集羣,而後咱們將安裝Longhorn並經過一個示例來展示如何使用它。mysql
雲原生應用程序的原理之一是它們旨在成爲無狀態的,所以能夠直接水平擴展應用程序。然而,實際狀況是除非你的網站或應用程序所佔內存很小,不然你必定須要在某個地方存儲這些東西。git
業界巨頭(如Google和Amazon)經常會有適用於本公司產品的可擴展存儲解決方案的自定義系統。可是對於小型公司來講,這要怎麼辦呢?github
業界採用最爲普遍的Kubernetes管理平臺建立者Rancher Labs(如下簡稱Rancher)在2018年3月發佈了容器化分佈式存儲項目Longhorn(現已捐獻給CNCF),這一項目填補了以上的空缺。簡而言之,Longhorn所作的是使用Kubernetes節點的現有磁盤爲Kubernetes Pod提供穩定的存儲。sql
在咱們使用Longhorn以前,你須要有一個正在運行的Kubernetes集羣。你能夠簡單地安裝一個k3s集羣(https://github.com/rancher/k3s/blob/master/README.md )或者若是你正在使用Civo的Kubernetes服務,你也能夠使用它。本文將使用Civo的Kubernetes服務來建立集羣。數據庫
咱們建議使用最少的Medium實例,由於咱們將測試MySQL的狀態存儲,它可能會佔用大量RAM。api
$ civo k8s create longhorn-test --wait Building new Kubernetes cluster longhorn-test: \ Created Kubernetes cluster longhorn-test
你的集羣須要在每一個節點上安裝open-iscsi
,因此若是你使用的不是civo的Kubernetes服務,除了上述連接的說明外,你還須要在每一個節點上運行如下命令:瀏覽器
sudo apt-get install open-iscsi
接着,你既須要下載Kubernetes配置文件並將其保存到~/.kube/config
中,還須要將名爲KUBECONFIG
的環境變量設置爲其文件名:安全
cd ~/longhorn-play civo k8s config longhorn-test > civo-longhorn-test-config export KUBECONFIG=civo-longhorn-test-config
在現有Kubernetes集羣上安裝Longhorn僅需2步:爲Longhorn安裝controller以及擴展包,而後建立一個可用於pod的StorageClass。第一步:bash
$ kubectl apply -f https://raw.githubusercontent.com/rancher/longhorn/master/deploy/longhorn.yaml namespace/longhorn-system created serviceaccount/longhorn-service-account created ...
建立StorageClass須要使用另外一個命令,然而做爲附加步驟,你能夠將新的class設置爲默認,這樣你無需每次都指定它:app
$ 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列表等等信息。但首先,咱們須要建立身份驗證的詳細信息:
$ htpasswd -c ./ing-auth admin $ kubectl create secret generic longhorn-auth \ --from-file ing-auth --namespace=longhorn-system
如今,咱們將建立一個Ingress
對象,能夠使用k3s中內置的Traefik,並將dashboard暴露到外部。建立一個名爲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
而後應用它:
$ kubectl apply -f longhorn-ingress.yaml -n longhorn-system ingress.extensions/longhorn-ingress created
如今,你須要在/etc/hosts
文件中添加一個條目,以將你的任意Kubernetes IP地址指向longhorn-frontend.example.com
:
echo "1.2.3.4 longhorn-frontend.example.com" >> /etc/hosts
如今,你能夠在瀏覽器上訪問http://longhorn-frontend.example.com ,使用admin
和使用htpasswd
時輸入的密碼進行身份驗證以後,能夠看到相似下面的內容:
在單個容器中運行MySQL毫無心義,由於當基礎節點(容器)死亡時,相關的業務也就沒法運行,這時你會失去客戶、失去訂單。在這裏,咱們要爲它配置一個新的Longhorn持久卷。
首先,咱們須要在Kubernetes中建立幾個資源。其中每一個都是yaml文件,位於一個空目錄中,或者你能夠將它們所有放在一個文件中,使用---
進行分隔。
在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密碼,但在實際操做中你應該使用安全密碼,並在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
如今,應用文件夾或者單個文件(這取決於你以前的選擇):
$ 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
咱們的測試十分簡單,建立一個新的數據庫,刪除容器(Kubernetes會幫咱們從新建立),而後從新鏈接,理想的結果是依舊能夠看到咱們的新數據庫。
好,如今咱們來建立一個名爲should_still_be_here
的數據庫:
$ 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
如今,咱們將刪除容器:
kubectl delete pod my-mysql-d59b9487b-7g644
大約一分鐘以後,咱們將再次尋找新的容器名稱,鏈接到該容器名稱,看看咱們的數據庫是否仍然存在:
$ 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
圓滿成功!咱們的存儲在被殺死個各個容器中得以持久保存。