k8s之helm安装mysql

1, helm安装mysql

  • 如果没有指明root密码,则使用随即生成的密码: helm install --name mysql stable/mysql

a, 手动分配pv

helm现有的各应用程序的charts仓库: https://github.com/helm/charts/tree/master/stable/mysql

 [root@master ~]# helm install --name mysql1 \
  --set mysqlRootPassword=root123456,mysqlUser=test,mysqlPassword=test,mysqlDatabase=test \
stable/mysql

NAME:   mysql1
LAST DEPLOYED: Mon Oct 28 12:47:53 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/ConfigMap
NAME         DATA  AGE
mysql1-test  1     1s
==> v1/Deployment
NAME    READY  UP-TO-DATE  AVAILABLE  AGE
mysql1  0/1    1           0          1s
==> v1/PersistentVolumeClaim
NAME    STATUS   VOLUME  CAPACITY  ACCESS MODES  STORAGECLASS  AGE
mysql1  Pending  1s
==> v1/Pod(related)
NAME                    READY  STATUS   RESTARTS  AGE
mysql1-bddc7c6bf-8qc45  0/1    Pending  0         1s
==> v1/Secret
NAME    TYPE    DATA  AGE
mysql1  Opaque  2     1s
==> v1/Service
NAME    TYPE       CLUSTER-IP    EXTERNAL-IP  PORT(S)   AGE
mysql1  ClusterIP  10.1.171.158  <none>       3306/TCP  1s

NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql1.default.svc.cluster.local
To get your root password run:
    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql1 -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 mysql1 -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/mysql1 3306
    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
.
#1, 根据提示,获取mysql root 密码: 
[root@master ~]#  MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql1 -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)
[root@master ~]# echo $MYSQL_ROOT_PASSWORD
root123456

#2, 查看k8s运行的结果:pod的由于pvc没有挂载到pv, 一直pending 
[root@master ~]# kubectl get all |grep mysql1
pod/mysql1-bddc7c6bf-8qc45                    0/1     Pending   0          88m
service/mysql1        ClusterIP   10.1.171.158   <none>        3306/TCP           88m
deployment.apps/mysql1                   0/1     1            0           88m
replicaset.apps/mysql1-bddc7c6bf                    1         1         0       88m

[root@master ~]# kubectl describe pod/mysql1-bddc7c6bf-8qc45
........
Events:
  Type     Reason            Age                   From               Message
  ----     ------            ----                  ----               -------
  Warning  FailedScheduling  3m26s (x66 over 93m)  default-scheduler  pod has unbound immediate PersistentVolumeClaims

#3, 查看此pod相关的pv,pvc
[root@master ~]# kubectl get pv,pvc |grep mysql1
persistentvolumeclaim/mysql1                               Pending                                                                                              97m

[root@master ~]# kubectl edit persistentvolumeclaim/mysql1
....
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 8Gi
  volumeMode: Filesystem
status:
  phase: Pending

#4, 手动创建pv
[root@master helm]# cat pv1.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv1-mysql
spec:
  capacity:
    storage: 8Gi
  accessModes:
   - ReadWriteOnce
#  nfs:
#    path: /data/nfs/helm
#    server: 192.168.56.201
  hostPath:
    path: /tmp/helm1-mysql
    type: Directory
    
[root@master helm]# mkdir  /tmp/helm1-mysql
[root@master helm]# kubectl apply -f pv1.yaml
persistentvolume/pv1-mysql created
[root@master helm]# kubectl get pvc |grep mysql1
mysql1                               Bound     pv1-mysql                                  8Gi        RWO                                  103m
[root@master helm]# kubectl get all |grep mysql1
pod/mysql1-bddc7c6bf-789xf                    0/1     Running   0          9s
service/mysql1        ClusterIP   10.1.171.158   <none>        3306/TCP           105m
deployment.apps/mysql1                   0/1     1            0           105m
replicaset.apps/mysql1-bddc7c6bf                    1         1         0       105m

#查看主机上被挂载的目录
[root@master ~]# ls /tmp/helm1-mysql
auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  mysql  performance_schema  sys  test

#5, 修改service 类型为:NodePort, 方便访问mysql
 [root@master ~]# kubectl edit service/mysql1
 #type: clusterIP ---> 修改为 type: NodePort

[root@master ~]#  kubectl get svc |grep mysql1
mysql1        NodePort    10.1.171.158   <none>        3306:31248/TCP     113m
....

#6, 从其他机器,远程登陆mysql
[root@aa ~]# mysql -utest -ptest -hkmaster -P31248
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 76
Server version: 5.7.14 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)

b, 自动分配pv

在k8s集群中先配置好nfs的自动供给, 参考之前的博客:https://blog.csdn.net/eyeofeagle/article/details/101031494

[root@master ~]# helm install --name mysql2 \
--set mysqlRootPassword=root123456,mysqlUser=test,mysqlPassword=test,mysqlDatabase=test,persistence.storageClass=managed-nfs-storage \
stable/mysql

#查看结果
[root@master ~]# helm ls
NAME  	REVISION	UPDATED                 	STATUS  	CHART      	APP VERSION	NAMESPACE
mysql1	1       	Tue Oct 22 18:03:45 2019	DEPLOYED	mysql-1.4.0	5.7.27     	default  
mysql2	1       	Mon Oct 28 12:14:37 2019	DEPLOYED	mysql-1.4.0	5.7.27     	default  

[root@master ~]# kubectl get all |grep mysql2
pod/mysql2-86dfb558df-nm484                   1/1     Running   0          7m30s
service/mysql2        ClusterIP   10.1.87.240   <none>        3306/TCP           7m34s
deployment.apps/mysql2                   1/1     1            1           7m33s
replicaset.apps/mysql2-86dfb558df                   1         1         1       7m30s

#修改service 类型为:NodePort, 方便访问mysql
 [root@master ~]# kubectl edit service/mysql2  
 #type: clusterIP ---> 修改为 type: NodePort
 
[root@master ~]#  kubectl get svc |grep mysql2
mysql2        NodePort    10.1.87.240    <none>        3306:30531/TCP     147m

#从其他机器,远程登陆mysql
[root@aa ~]# mysql -utest -ptest -hkmaster -P30531
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 130
Server version: 5.7.14 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.01 sec)

2, helm定制安装mysql

配置k8s集群pv自动供给:https://blog.csdn.net/eyeofeagle/article/details/101031494

  • 下载helm提供的charts,定制修改配置
  • 配置mysql为utf8编码 --> 修改charts里面的values.yaml
  • helm安装mysql:使用pv自动供给

a, helm下载mysql, 定制配置

#1,搜索mysql的charts
[root@master ~]# helm search mysql
NAME                            	CHART VERSION	APP VERSION	DESCRIPTION                                                 
stable/mysql                    	1.4.0        	5.7.27     	Fast, reliable, scalable, and easy to use open-source rel...
stable/mariadb                  	6.12.0       	10.3.18    	Fast, reliable, scalable, and easy to use open-source rel...

#2,下载对应的mysql-charts
[root@master ~]# helm fetch stable/mysql
[root@master ~]# ls
 mysql         
[root@master ~]# ls mysql
Chart.yaml  README.md  templates  values.yaml

#3,修改配置: 复制127-129行的配置,添加字符编码配置
[root@master ~]# vi mysql/values.yaml 
    127 # Custom mysql configuration files used to override default mysql settings
    128 #configurationFiles: {}
    129 #  mysql.cnf: |-
    130      #[mysqld]
    131      #skip-name-resolve
    132      #ssl-ca=/ssl/ca.pem
    133      #ssl-cert=/ssl/server-cert.pem
    134      #ssl-key=/ssl/server-key.pem
    135 configurationFiles:
    136   mysql.cnf: |-
    137     [client]
    138     default-character-set=utf8
    139     [mysql]
    140     default-character-set=utf8
    141     [mysqld]
    142     collation-server = utf8_unicode_ci
    143     init-connect='SET NAMES utf8'
    144     character-set-server = utf8

# 4,保存本地的charts
[root@master ~]# helm package mysql 
Successfully packaged chart and saved it to: /root/mysql-1.4.0.tgz

[root@master ~]# helm repo list
NAME  	URL                                             
stable	https://kubernetes-charts.storage.googleapis.com
myrepo	http://192.168.56.190:8879                      
local 	http://192.168.56.190:8879         

[root@master ~]# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Skip local chart repository
...Successfully got an update from the "myrepo" chart repository
...Successfully got an update from the "stable" chart repository
Update Complete.

[root@master ~]# helm search mysql
NAME                            	CHART VERSION	APP VERSION	DESCRIPTION                                                 
myrepo/mysql                    	1.4.0        	5.7.27     	Fast, reliable, scalable, and easy to use open-source rel...
stable/mysql                    	1.4.0        	5.7.27     	Fast, reliable, scalable, and easy to use open-source rel...
......

b, helm运行定制的mysql

#1, 运行本地修改的mysql-charts
[root@master ~]# helm install  myrepo/mysql   \
 --name mysql-utf8  --set    \
mysqlRootPassword=root123456,mysqlUser=test,mysqlPassword=test,\
mysqlDatabase=test,persistence.storageClass=managed-nfs-storage 

NAME:   mysql-utf8
LAST DEPLOYED: Tue Oct 29 12:06:14 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/ConfigMap
NAME                      DATA  AGE
mysql-utf8-configuration  1     0s
mysql-utf8-test           1     0s
==> v1/Deployment
NAME        READY  UP-TO-DATE  AVAILABLE  AGE
mysql-utf8  0/1    1           0          0s
==> v1/PersistentVolumeClaim
NAME        STATUS   VOLUME               CAPACITY  ACCESS MODES  STORAGECLASS  AGE
mysql-utf8  Pending  managed-nfs-storage  0s
==> v1/Pod(related)
NAME                        READY  STATUS   RESTARTS  AGE
mysql-utf8-d6c84b746-mlqfp  0/1    Pending  0         0s
==> v1/Secret
NAME        TYPE    DATA  AGE
mysql-utf8  Opaque  2     1s
==> v1/Service
NAME        TYPE       CLUSTER-IP   EXTERNAL-IP  PORT(S)   AGE
mysql-utf8  ClusterIP  10.1.162.18  <none>       3306/TCP  0s
....

#2,查看数据字符编码
[root@master ~]# kubectl exec -it $(kubectl get pod |grep mysql-utf8 |awk '{print $1}') sh
# mysql -utest -ptest
show variables like '%char%';mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 48
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> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.02 sec)
发布了276 篇原创文章 · 获赞 37 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/eyeofeagle/article/details/102703065