使用helm 3.0安裝mysql+nfs+動態Pv provisioner

1、前言

Helm是kubernetes上的包管理工具,通過helm可以方便地進行容器應用佈署,helm3.0 beta版是最新的helm,這個版本不再依賴triller,通過kubernetes api進行配置管理。

2、下載helm最新版

mkdir -p ~/helm3
cd ~/helm3
wget https://get.helm.sh/helm-v3.0.0-beta.2-linux-amd64.tar.gz
tar -zxvf helm-v3.0.0-beta.2-linux-amd64.tar.gz
cp helm /usr/local/bin
helm init

3、添加charts的加速倉庫

添加微軟的chart倉庫,這個倉庫與官網的charts倉庫更新比較同步

helm repo add azure http://mirror.azure.cn/kubernetes/charts/
helm repo update
helm search repo mysql  //測試helm charts repo是否正常訪問

4、安裝nfs服務器

爲了簡化測試,我們在Master上部署nfs服務器,爲kuernetes提供PV provisioner

(1)通過yum安裝nfs服務器

yum -y install nfs-utils

(2)配置nfs服務器

創建nfs存儲目錄

mkdir /nfs
chmod  -R 777 /nfs

(3)修改exports配置文件

echo "/nfs *(rw,no_root_squash,sync)" > /etc/exports

(4)啓用配置

#查看是否export生效
exportfs -r

(5)配置rpcbind和Nfs服務

rpcbind是用於Nfs服務跨機器訪問

#啓動rpcbind、nfs服務,配置爲自動啓動
systemctl restart rpcbind && systemctl enable rpcbind
systemctl restart nfs && systemctl enable nfs

(6)查看nfs服務器工作情況

[root@centos75 nfs]# showmount -e 10.0.135.30
Export list for 10.0.135.30:
/nfs *

(7)通過helm安裝nfs client provisioner

使用默認設置安裝helm,會自動創建一個storageClass:nfs-client
helm install my-nfsclient azure/nfs-client-provisioner --set nfs.server=10.0.135.30 --set nfs.path=/nfs/k8s

5、安裝MySQL

helm install my-sql azure/mysql --set persistence.storageClass=nfs-client --set mysqlRootPassword=test123

#輸出結果如下
[root@centos75 ~]# helm install my-sql azure/mysql --set persistence.storageClass=nfs-client --set mysqlRootPassword=test123

NAME: my-sql
LAST DEPLOYED: 2019-09-06 15:04:12.216593381 +0800 CST m=+1.320154309
NAMESPACE: default
STATUS: deployed
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
my-sql-mysql.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default my-sql-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h my-sql-mysql -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306

    # Execute the following command to route the connection:
    kubectl port-forward svc/my-sql-mysql 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
[root@centos75 ~]#

5、測試Mysql的連通性

爲了測試Mysql連通性,我們安裝一個ubuntu的pod,在pod裏測試,避免在Node節點系統上安裝,保持環境的純粹性

檢查pod是否創建完成,因爲helm需要拉取mysql的image,需要等待一段時間
[root@centos75 mysql]# kubectl get po
NAME                                                   READY   STATUS    RESTARTS   AGE
my-nfsclient-nfs-client-provisioner-6554f6dc9f-h9n6x   1/1     Running   0          18m
my-sql-mysql-85d466996f-t8znp                          1/1     Running   0          11m
[root@centos75 mysql]#

#登錄mysql pod 
kubectl exec -it my-sql-mysql-85d466996f-t8znp bash

#測試數據庫是否可以連通

[root@centos75 mysql]# kd exec -it my-sql-mysql-85d466996f-t8znp bash
root@my-sql-mysql-85d466996f-t8znp:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 85
Server version: 5.7.14 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

6、總結

通過helm可以方便的安裝kubernetes應用,並且提供豐富的配置能力,可以通過helm show <chart名>查看安裝包的使用說明。

helm3.0是helm一個比較大的升級,去除了對triller的依賴,非常輕量級,使用方面也有細節不太一樣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章