Helm概述
Helm 是 Kubernetes 的软件包管理工具。类似于Ubuntu的包管理工具apt以及Centos的包管理工具yum。 可以根据文档安装 helm v3 https://helm.sh/docs/intro/install/
Helm安装
安装 Helm
# Use homebrew on Mac
brew install helm
# Upgrade helm
brew upgrade helm
维护 Helm 仓库
# 添加微软Azure云服务的中国镜像仓库
helm repo add azure http://mirror.azure.cn/kubernetes/charts/
# 更新仓库
helm repo update
# 移除仓库
helm repo remove azure
# 列举仓库
$ helm repo list
NAME URL
stable https://charts.helm.sh/stable
ingress-nginx https://kubernetes.github.io/ingress-nginx
azure http://mirror.azure.cn/kubernetes/charts/
# 搜索仓库
helm search repo mysql
Helm安装MySQL
1、在仓库搜索MySQL版本
$ helm search repo mysql
NAME CHART VERSION APP VERSION DESCRIPTION
azure/mysql 1.6.8 5.7.30 Fast, reliable, scalable, and easy to use open-...
azure/mysqldump 2.6.2 2.4.1 DEPRECATED! - A Helm chart to help backup MySQL...
azure/prometheus-mysql-exporter 0.7.1 v0.11.0 DEPRECATED A Helm chart for prometheus mysql ex...
stable/mysql 1.6.7 5.7.30 Fast, reliable, scalable, and easy to use open-...
stable/mysqldump 2.6.2 2.4.1 DEPRECATED! - A Helm chart to help backup MySQL...
stable/prometheus-mysql-exporter 0.7.1 v0.11.0 DEPRECATED A Helm chart for prometheus mysql ex...
azure/percona 1.2.2 5.7.26 free, fully compatible, enhanced, open source d...
azure/percona-xtradb-cluster 1.0.7 5.7.19 free, fully compatible, enhanced, open source d...
azure/phpmyadmin 4.3.5 5.0.1 DEPRECATED phpMyAdmin is an mysql administratio...
stable/percona 1.2.1 5.7.26 free, fully compatible, enhanced, open source d...
stable/percona-xtradb-cluster 1.0.6 5.7.19 free, fully compatible, enhanced, open source d...
stable/phpmyadmin 4.3.5 5.0.1 DEPRECATED phpMyAdmin is an mysql administratio...
azure/gcloud-sqlproxy 0.6.1 1.11 DEPRECATED Google Cloud SQL Proxy
azure/mariadb 7.3.14 10.3.22 DEPRECATED Fast, reliable, scalable, and easy t...
stable/gcloud-sqlproxy 0.6.1 1.11 DEPRECATED Google Cloud SQL Proxy
stable/mariadb 7.3.14 10.3.22 DEPRECATED Fast, reliable, scalable, and easy t...
2、根据CHART VERSION
安装5.7.30版本的MySQL,注意⚠️:使用微软中国Azure镜像安装会更快。
# 使用指定的服务名
$ helm install mysql-totoro-identity azure/mysql --version 1.6.7
# 使用随机的服务名
$ helm install azure/mysql --version 1.6.7 --generate-name
NAME: mysql-1603188850
LAST DEPLOYED: Tue Oct 20 18:14:14 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql-1603188850.default.svc.cluster.local
To get your root password run:
MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql-1605336565 -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 mysql-1603188850 -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/mysql-1603188850 3306
mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
3、查询Pod的状态
# 查看Pods状态为初始化
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-1603188850-7854c4cc75-fh7fs 0/1 PodInitializing 0 4m39s
# 查看Pods状态为运行中
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-1603188850-7854c4cc75-fh7fs 1/1 Running 0 8m29s
4、查询数据库
# 查看数据库
helm ls
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
mysql-1603188850 default 1 2020-10-20 18:14:14.346005 +0800 CST deployed mysql-1.6.7 5.7.30
5、删除数据库
# 删除数据库
$ helm delete mysql-1603188850
release "mysql-1603188850" uninstalled
k8s集群外部访问MySQL
1.通过kubectl port-forward
命令执行端口转发
$ kubectl port-forward svc/mysql-1603188850 3306
Forwarding from 127.0.0.1:3306 -> 3306
Forwarding from [::1]:3306 -> 3306
2.查看数据库root账户初始密码
$ echo $(kubectl get secret --namespace default mysql-1603188850 -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)
sw7dsJVZJB
3.从k8s集群外部访问数据库
$ mysql -h 127.0.0.1 -P 3306 -uroot -p'sw7dsJVZJB'
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 112
Server version: 5.7.30 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>
4、在k8s集群外访问数据库,每次都必须通过命令行开启端口转发,比较繁琐。可以创建一个端口转发的service来后台运行。
# 创建端口转发的svc文件
$ cat mysql-port-forward-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql-port-forward-svc
namespace: default
spec:
ports:
- name: mysql
port: 3306
protocol: TCP
targetPort: 3306
nodePort: 30000
selector:
app: mysql-1603188850
type: NodePort
# 运行svc
$ kubectl apply -f mysql-port-forward-svc.yaml
5、查看创建的mysql-port-forward-svc
服务,会将k8s集群内部3306端口的流量转发到外部的30000端口。
# 查看服务
$ kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7h5m <none>
mysql-1603188850 ClusterIP 10.96.122.242 <none> 3306/TCP 38m app=mysql-1603188850
mysql-port-forward-svc NodePort 10.102.213.6 <none> 3306:30000/TCP 9s app=mysql-1603188850
# 删除服务
$ kubectl delete svc mysql-port-forward-svc
注意⚠️:默认NodePort分配的端口范围是30000-32767,可以编辑 kube-apiserver.yaml 文件进行修改。
# 找到 —service-cluster-ip-range 这一行,在这一行的下一行增加如下内容:—service-node-port-range=1-65535
$ vim /etc/kubernetes/manifests/kube-apiserver.yaml