一、概述
MySQL
是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,属于 Oracle 旗下产品。MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL 是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。这里主要讲 mysql 部署在 k8s 上,mysql 部署在 k8s 上的优势主要有以下几点:
- 资源隔离
- 动态弹性扩缩容
- 环境一致性
- 运维方便
官方文档:
https://docs.oracle.com/en-us/iaas/mysql-database/doc/getting-started.html
二、开始部署(一主两从)
1)添加源
helmrepoaddbitnamihttps://charts.bitnami.com/bitnami
helmpullbitnami/mysql
tar-xfmysql-9.3.3.tgz
2)修改配置
...
image:
registry:myharbor.com
repository:bigdata/mysql
tag:8.0.30-debian-11-r15
...
architecture:replication
...
primary:
persistence:
enabled:true
size:10Gi
storageClass:"mysql-local-storage"
#目录需要提前在宿主机上创建
local:
-name:mysql-0
host:"local-168-182-110"
path:"/opt/bigdata/servers/mysql/data/data1"
service:
type:NodePort
nodePorts:
mysql:"30306"
secondary:
replicaCount:2
persistence:
enabled:true
size:10Gi
storageClass:"mysql-local-storage"
#目录需要提前在宿主机上创建
local:
-name:mysql-1
host:"local-168-182-111"
path:"/opt/bigdata/servers/mysql/data/data1"
-name:mysql-2
host:"local-168-182-112"
path:"/opt/bigdata/servers/mysql/data/data1"
service:
type:NodePort
nodePorts:
mysql:"30307"
...
metrics:
##@parammetrics.enabledStartaside-carprometheusexporter
##
enabled:true
image:
registry:myharbor.com
repository:bigdata/mysqld-exporter
tag:0.14.0-debian-11-r33
{{-range.Values.primary.persistence.local}}
---
apiVersion:v1
kind:PersistentVolume
metadata:
name:{{.name}}
labels:
name:{{.name}}
spec:
storageClassName:{{$.Values.primary.persistence.storageClass}}
capacity:
storage:{{$.Values.primary.persistence.size}}
accessModes:
-ReadWriteOnce
local:
path:{{.path}}
nodeAffinity:
required:
nodeSelectorTerms:
-matchExpressions:
-key:kubernetes.io/hostname
operator:In
values:
-{{.host}}
---
{{-end}}
{{-range.Values.secondary.persistence.local}}
---
apiVersion:v1
kind:PersistentVolume
metadata:
name:{{.name}}
labels:
name:{{.name}}
spec:
storageClassName:{{$.Values.secondary.persistence.storageClass}}
capacity:
storage:{{$.Values.secondary.persistence.size}}
accessModes:
-ReadWriteOnce
local:
path:{{.path}}
nodeAffinity:
required:
nodeSelectorTerms:
-matchExpressions:
-key:kubernetes.io/hostname
operator:In
values:
-{{.host}}
---
{{-end}}
kind:StorageClass
apiVersion:storage.k8s.io/v1
metadata:
name:{{.Values.primary.persistence.storageClass}}
provisioner:kubernetes.io/no-provisioner
3)开始安装
#创建持久化目录
mkdir-p/opt/bigdata/servers/mysql/data/data1
#先准备好镜像
dockerpulldocker.io/bitnami/mysql:8.0.30-debian-11-r15
dockertagdocker.io/bitnami/mysql:8.0.30-debian-11-r15myharbor.com/bigdata/mysql:8.0.30-debian-11-r15
dockerpushmyharbor.com/bigdata/mysql:8.0.30-debian-11-r15
#mysqld-exporter
dockerpulldocker.io/bitnami/mysqld-exporter:0.14.0-debian-11-r33
dockertagdocker.io/bitnami/mysqld-exporter:0.14.0-debian-11-r33myharbor.com/bigdata/mysqld-exporter:0.14.0-debian-11-r33
dockerpushmyharbor.com/bigdata/mysqld-exporter:0.14.0-debian-11-r33
#开始安装
helminstallmysql./mysql-nmysql--create-namespace
NAME:mysql
LASTDEPLOYED:MonSep1923182022
NAMESPACE:mysql
STATUS:deployed
REVISION:1
TESTSUITE:None
NOTES:
CHARTNAME:mysql
CHARTVERSION:9.3.3
APPVERSION:8.0.30
**Pleasebepatientwhilethechartisbeingdeployed**
Tip:
Watchthedeploymentstatususingthecommand:kubectlgetpods-w--namespacemysql
Services:
echoPrimary:mysql-primary.mysql.svc.cluster.local:3306
echoSecondary:mysql-secondary.mysql.svc.cluster.local:3306
Executethefollowingtogettheadministratorcredentials:
echoUsername:root
MYSQL_ROOT_PASSWORD=$(kubectlgetsecret--namespacemysqlmysql-ojsonpath="{.data.mysql-root-password}"|base64-d)
Toconnecttoyourdatabase:
1.Runapodthatyoucanuseasaclient:
kubectlrunmysql-client--rm--tty-i--restart='Never'--imagemyharbor.com/bigdata/mysql:8.0.30-debian-11-r15--namespacemysql--envMYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD--command--bash
2.Toconnecttoprimaryservice(read/write):
mysql-hmysql-primary.mysql.svc.cluster.local-uroot-p"$MYSQL_ROOT_PASSWORD"
3.Toconnecttosecondaryservice(read-only):
mysql-hmysql-secondary.mysql.svc.cluster.local-uroot-p"$MYSQL_ROOT_PASSWORD"
ToaccesstheMySQLPrometheusmetricsfromoutsidetheclusterexecutethefollowingcommands:
kubectlport-forward--namespacemysqlsvc/mysql-metrics9104:9104&
curlhttp://127.0.0.1:9104/metrics
kubectlgetpods,svc-nmysql-owide
4)测试验证
【温馨提示】从库(slave)是只读的。就是简单的读写测试,还有就是 pod 挂了,能否正常拉起等等。这个测试验证比较简单。这里就不一步步的演示了。
5)Prometheus 监控
kubectlget--rawhttp://10.244.0.74:9104/metrics
kubectlget--rawhttp://10.244.1.125:9104/metrics
kubectlget--rawhttp://10.244.2.178:9104/metrics
Grafana:
账号:admin
,密码通过下面命令获取
kubectlgetsecret--namespacegrafanagrafana-ojsonpath="{.data.admin-password}"|base64--decode;echo
导入 grafana 模板,集群资源监控:7362
官方模块下载地址:
6)卸载
helmuninstallmysql-nmysql
kubectldeletepod-nmysql`kubectlgetpod-nmysql|awk'NR>1{print$1}'`--force
kubectlpatchnsmysql-p'{"metadata":{"finalizers":null}}'
kubectldeletensmysql--force
总结
审核编辑:汤梓红-
数据库
+关注
关注
7文章
3770浏览量
64301 -
MySQL
+关注
关注
1文章
802浏览量
26466
原文标题:8 张图详解 MySQL 在 K8S 环境中部署与监控
文章出处:【微信号:magedu-Linux,微信公众号:马哥Linux运维】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
评论