关于云计算:在-Kubernetes-中基于-StatefulSet-部署-MySQL上

3次阅读

共计 11062 个字符,预计需要花费 28 分钟才能阅读完成。

大家好,我是老 Z!

本文实现了 MySQL 数据库在基于 KubeSphere 部署的 K8s 集群上的装置部署,部署形式采纳了图形化这种模式。下一篇文章将会波及 GitOps 的根底操作,部署过程波及的所有 YAML 文件都会应用 Git 进行版本治理,并存放在 Git 仓库中,敬请期待!

本文部署的 MySQL 抉择了比拟激进的 5.7 系列,其余版本可能会有不同。本文的操作仅实用于小规模数据量且对可靠性和性能要求不高的数据库应用场景,例如开发测试环境、例如我生产环境的 Nacos 服务。生产环境或是重要的数据库集体不倡议将数据放到 K8s 上,优先采纳云服务商提供的 RDS,其次本人利用虚拟机搭建 MySQL 主从或是 Galera Cluster,且肯定做好备份计划。

数据库的可靠性、可用性是运维的重中之重,不容忽视,切记!!!

本文知识点

  • 定级:入门级
  • 单节点 MySQL 在 K8s 上的装置配置
  • KubeSphere 图形化部署工作负载
  • GitOps 入门
  • Git 罕用操作
  • 配置代码如何实现在 GitHub 和 Gitee 放弃同步
  • MySQL 性能测试根底
  • 运维思维、思路

演示服务器配置

主机名 操作系统 IP CPU 内存 系统盘 数据盘 用处
zdeops-master CentOS-7.9-x86_64 192.168.9.9 2 4 40 200 Ansible 运维管制节点
ks-k8s-master-0 CentOS-7.9-x86_64 192.168.9.91 8 32 40 200 KubeSphere/k8s-master/k8s-worker
ks-k8s-master-1 CentOS-7.9-x86_64 192.168.9.92 8 32 40 200 KubeSphere/k8s-master/k8s-worker
ks-k8s-master-2 CentOS-7.9-x86_64 192.168.9.93 8 32 40 200 KubeSphere/k8s-master/k8s-worker
glusterfs-node-0 CentOS-7.9-x86_64 192.168.9.95 4 8 40 200 GlusterFS/Elasticsearch
glusterfs-node-1 192.168.9.96 4 8 40 200 GlusterFS/Elasticsearch
glusterfs-node-2 CentOS-7.9-x86_64 192.168.9.97 4 8 40 200 GlusterFS/Elasticsearch

MySQL 装置之旅

寻找参考文档

我集体查找参考文档习惯的的寻找门路

  • 官方网站- 精准定位

    • 官网有时没有相干文档、或是文档不够具体
    • 英文文档、浏览艰难
  • 搜寻关键字- 海底捞针

    • CSDN
    • 博客园
    • 某个人博客
    • 问答网站
    • 其余

关上 MySQL 官方网站

抉择 MySQL5.7 版本的 Reference Manual。

Installing MySQL on Linux 章节中,搜查一番,发现在 Deploying MySQL on Linux with Docker 大节下两篇具备参考价值的文档,先去看看。

浏览完当前你会发现,只是学会了利用 Docker Image 装置 MySQL 的根本办法,细节不上图了。

尽管官网没有提到如何在 K8s 上部署 MySQL,然而我曾经有 Docker 和 K8s 的基础知识了,先不去进行搜寻吃他人的了,本人尝试在 K8s 上部署一个单节点的 MySQL。

尝试部署单节点 MySQL

先梳理一下思路,部署一个 MySQL 咱们须要筹备哪些资源

  • 在 Docker Hub 获取 MySQL 镜像。
  • 查看 MySQL 镜像阐明,确定装置初始化参数。
  • MySQL 属于有状态服务,所以咱们须要定义 StatefulSet 类型的资源。
  • 编写 StatefulSet 类型的 MySQL 资源定义文件 -YAML。

查看官网镜像阐明,确定初始化参数

如果之前有 Docker 部署 MySQL 的教训,这一步就很简略了,间接把参数配置搬过去就行。

关上 https://hub.docker.com,搜寻 mysql。

搜寻后果中会有很多的 mysql,我重点关注了两个镜像。

  • Docker 官网保护的 mysql 仓库
  • Oracle 的 MySQL 团队保护的 mysql 仓库

本次试验我应用了 Docker 官网保护的仓库,进入 MySQL 仓库页面。

大略浏览一遍,确认了几个必须要配置的中央(确定过程须要教训和技术积攒)。

  • 镜像:mysql:5.7.38
  • root 明码:MYSQL_ROOT_PASSWORD
  • 数据长久化存储目录:/var/lib/mysql

利用 KubeSphere 部署 MySQL(V1 版)

确定了初始化的参数,接下来就开始部署 MySQL。

按 K8s 惯例套路编写资源定义 YAML 文件?NO!我当初是小白,手写配置文件太高端了,还不适宜我。

咱们这里投机取巧一下,利用 KubeSphere 的图形化操作一波,这样能够保障部署的一次成功率 (还有一个暗藏的益处,先卖个关子)。

应用 企业空间管理员 权限的账户,登录 KubeSphere 控制台。

这一步没有应用 admin 用户,采纳多租户户模式,模仿实在的生产环境

<,>

点击 我的项目,点击 lstack 我的项目,进入我的项目的治理页面 (如无非凡阐明,前面的很多界面操作都是在该页面实现)。

<,>

利用负载 -> 工作负载 -> 有状态正本集 ,点击 创立

弹出 创立有状态正本集 页面,根本信息 页,名称 输出 mysql

容器组设置 页。

  • 容器组正本数量:1
  • 点击 增加容器,镜像搜寻栏输出 mysql:5.7.38
  • 容器名称:lstack-mysql
  • CPU(Core)资源:预留 0.5,限度 2
  • 内存(Mi):预留 500i,限度 4000
  • 端口设置:协定 TCP,名称 tcp-mysql,容器端口:3306,服务端口 3306
  • 环境变量

    • 援用配置字典或窃密字典
    • 创立窃密字典,键(MYSQL_ROOT_PASSWORD),值(P@88w0rd)
  • 同步主机时区:勾选上
  • 其余未说明的配置采纳默认值

<,,,>

创立窃密字典 :在 环境变量 选项中,点击 创立窃密字典,按后续图示操作。

<,,,>

点击 创立 ,返回 容器组设置 页面。

按以上信息配置实现后,点击 对号 按钮。

容器组设置 实现后,点击 下一步 ,进入 存储卷设置

存储卷设置 -> 存储卷模板 -> 增加存储卷模板

  • 存储卷名称:data

    • 这个中央不要多写,零碎会主动增加 StatefulSet 的名称作为名称后缀,生成相似 data-mysql-0 命名模式的存储卷
  • 存储类型:glusterfs
  • 拜访模式:ReadWriteOnce
  • 存储卷容量:5Gi
  • 挂载门路:读写 /var/lib/mysql

按以上信息配置实现后,点击 对号 按钮。

存储卷设置 实现后,点击 下一步 ,进入 高级设置 ,放弃默认值,点击 创立 按钮。

创立 胜利后,主动返回工作负载页面。第一次创立会去 DockerHub 下载镜像,所以初始显示状态为 更新中

镜像下载实现并且容器配置正确时,状态变成 运行中

点击 mysql,进入有状态正本集具体页面。

<,,>

监控,能够看到初始启动时的资源应用状况,后续能够依据监控数据调整咱们的资源的配置。

环境变量,能够看到咱们新减少的 Secret 字典失效了,并且明码是暗藏显示的。

<,>

再来看看 容器组 的详细信息,在 资源状态 页面,点击容器组 mysql-0。

<,,,,,>

再来看看 StatefulSet 对应的服务 (Service),利用负载 -> 服务

能够看到主动创立了一个 StatefulSet MySQL 对应的有状态服务 (Headless),mysql-2v7f(mysql)

点击 mysql-2v7f(mysql),能够查看服务详情。

<,,>

最初验证一下,咱们的 MySQL 服务是否失常 (这里只看服务自身,先不测试内部连贯)。

利用负载 -> 工作负载 -> 有状态正本集 ->mysql-> 容器组 ->mysql-0-> 终端。

<,>

至此,MySQL 在 K8s 的根本装置就实现了,K8s 集群内的其余利用能够通过 svc 的地址拜访 MySQL 服务 (svc 地址就是 mysql-2v7f.lstack),此时名字看着还是很不敌对,咱们先不必它。

MySQL 配置进阶

下面实现了 MySQL 的根本装置配置。然而,理论应用中咱们通常还有如下需要,须要咱们对 MySQL 进行配置。

开启内部拜访

开启内部拜访不便管理员操作 MySQL 数据库,也能够满足 K8s 集群之外的服务拜访 MySQL 数据库的需要。

在 KubeSphere 中开启服务的内部拜访须要先设置我的项目网关。

用我的项目管理员用户登录控制台。

工作台 -> 我的项目 -> 点击 具体的我的项目 -> 我的项目设置 -> 网关设置 ,点击 开启网关

目前拜访模式有 NodePort 和 LoadBalancer,然而 LoadBalancer 只反对私有云提供商云上的负载均衡器,所以咱们只能抉择 NodePort,点击确定。

NodePort 模式里会创立一个采纳了 nginx-ingress 的 kubesphere-router 的容器组,细节咱们会在当前的专文探讨。

<,>

网关设置的细节不在本文深刻探讨,后续会有专文探讨。当初,做到这一步就 OK 了。

接下来创立一个 MySQL 服务用于对外提供服务。

利用负载 ->- 服务 > 创立 -> 抉择 自定义服务 -> 指定工作负载

这里有一个内部服务的选项,那个是基于 Ingress 应用域名拜访的,不是目前咱们想要的形式。

指定工作负载创立服务 - 根本信息

  • 名称:mysql-external

指定工作负载创立服务 - 服务设置 ,点击 指定工作负载 ,抉择 有状态正本集 ->mysql,点击 确定

指定工作负载创立服务 - 服务设置 端口 配置。

  • 协定: TCP
  • 名称: tcp-mysql-external
  • 容器端口: 3306
  • 服务端口: 3306

指定工作负载创立服务 - 高级设置

  • 内部拜访: 拜访模式 抉择 NodePort

实现所有设置后,点击 创立,创立胜利会主动返回服务列表,在服务列表中能够看到咱们新创建的服务 mysql-external 及主动调配的内部拜访端口号。

先用 telnet 命令测试一下,MySQL 服务的连通性,能看到上面的后果就阐明 MySQL 曾经能够在 K8s 集群内部拜访了。

[root@ks-k8s-master-0 ~]# telnet 192.168.9.91 32529
Trying 192.168.9.91...
Connected to 192.168.9.91.
Escape character is '^]'.
EHost '10.233.117.0' is not allowed to connect to this MySQL serverConnection closed by foreign host.

# 细节!下面的 EHost 地址是 192.168.9.91 这个节点在 K8s 集群外部调配的 IP
[root@ks-k8s-master-0 ~]# ip add | grep 117 -B 2 -A 1
7: tunl0@NONE: <NOARP,UP,LOWER_UP> mtu 1440 qdisc noqueue state UNKNOWN group default qlen 1000
    link/ipip 0.0.0.0 brd 0.0.0.0
    inet 10.233.117.0/32 scope global tunl0
       valid_lft forever preferred_lft forever
[root@ks-k8s-master-0 ~]# ip add | grep 91
    inet 192.168.9.91/24 brd 192.168.9.255 scope global noprefixroute ens160
    link/ether c6:d3:91:95:f1:0f brd ff:ff:ff:ff:ff:ff

自定义 MySQL 配置文件

默认装置的 MySQL 应用的 my.cnf 配置文件,适配的应用场景无限,所以自定义 mysql 配置文件是必然要做的一项配置。

这里我随机找了一份配置文件,仅仅是为了实现自定义配置的性能,请依据本人的应用场景应用适合的自定义配置文件。

应用自定义配置前,咱们先须要理解目前 mysql 容器的配置文件构造。

应用 KubeSphere 提供的 终端 工具,进入 mysql 容器外部,执行上面的命令,剖析执行后果 (终端登录形式参考前文截图)。

# bash
root@mysql-0:/# ls /etc/mysql/ -l
total 8
drwxr-xr-x 2 root root   62 Apr 28 06:20 conf.d
lrwxrwxrwx 1 root root   24 Apr 28 06:20 my.cnf -> /etc/alternatives/my.cnf
-rw-r--r-- 1 root root  839 Aug  3  2016 my.cnf.fallback
-rw-r--r-- 1 root root 1200 Mar 22 01:44 mysql.cnf
drwxr-xr-x 2 root root   24 Apr 28 06:20 mysql.conf.d

root@mysql-0:/# ls /etc/mysql/conf.d/ -l
total 12
-rw-r--r-- 1 root root 43 Apr 28 06:20 docker.cnf
-rw-r--r-- 1 root root  8 Aug  3  2016 mysql.cnf
-rw-r--r-- 1 root root 55 Aug  3  2016 mysqldump.cnf

root@mysql-0:/# ls /etc/mysql/mysql.conf.d/ -l
total 4
-rw-r--r-- 1 root root 1589 Apr 28 06:20 mysqld.cnf

root@mysql-0:/# ls -l /etc/alternatives/my.cnf
lrwxrwxrwx 1 root root 20 Apr 28 06:20 /etc/alternatives/my.cnf -> /etc/mysql/mysql.cnf

root@mysql-0:/# cat /etc/mysql/mysql.cnf
# Copyright (c) 2016, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

## 挑两个配置文件看看
root@mysql-0:~# cat /etc/mysql/conf.d/docker.cnf
[mysqld]
skip-host-cache
skip-name-resolve

root@mysql-0:~# cat /etc/mysql/mysql.conf.d/mysqld.cnf
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
#log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

剖析下面的输入咱们失去以下论断。

  • 根配置文件:/etc/mysql/mysql.cnf
  • 自定义的配置文件能够寄存在 /etc/mysql/conf.d//etc/mysql/mysql.conf.d/ 目录下
  • 通过下面的论断,发现有两种形式实现自定义配置文件。

    • 间接替换 /etc/mysql/mysql.cnf

      实用于个性化配置较多较简单的场景,比方 50+ 的配置项。

    • 将自定义的配置放在 /etc/mysql/conf.d//etc/mysql/mysql.conf.d/ 目录下,依据官网配置应用状况,倡议抉择 /etc/mysql/conf.d/

      实用于自定义配置较少的场景,比方只是为了开启个别性能,或是个别默认参数不合乎应用需要

本文采纳第二种形式,采纳一个独立的 custom.cnf 文件配置以下参数。

[mysqld]
#performance setttings
lock_wait_timeout = 3600
open_files_limit    = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
thread_stack = 512K
sort_buffer_size = 4M
join_buffer_size = 4M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
thread_cache_size = 768
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
  

实现思路。

  • k8s 中咱们能够通过配置 ConfigMap 的形式将文件挂载给容器
  • 将自定义的 mysql 配置文件,定义为一个 ConfigMap
  • 将 ConfigMap 挂载给 mysql 的容器

创立 ConfigMap 配置文件,配置 -> 配置字典 ,点击 创立

创立配置字典 - 根本信息

  • 名称:mysql-cnf

创立配置字典 - 数据设置

  • 点击 增加数据
  • 键: custom.cnf
  • 值: 粘贴下面的配置参数

<,>

填写完键值信息后,点击 对号 确定,最初点击 创立 ,创立实现后会返回 配置字典 页面。

<,>

接下来将自定义配置文件,挂载到 mysql 容器。

利用负载 -> 工作负载 -> 有状态正本集 -> 点击 mysql-> 进入具体配置页面 -> 更多操作 - 点击 编辑设置

编辑设置 -> 存储卷 -> 挂载配置字典或窃密字典

<,>

存储卷

  • 抉择配置字典:mysql-cnf
  • 只读
  • 挂载门路:/etc/mysql/conf.d/custom.cnf
  • 指定子门路:custom.cnf

    • 此处必须这么写,否则会笼罩掉指定目录下的所有已存在文件
    • 底层就是 subPath
    • 具体操作看下图图示,留神细节
  • 抉择特定键:

    • 键:custom.cnf
    • 门路:custom.cnf

<,,>

输出实现后,点击 对号

再次点击 对号 ,点击 确定,mysql 容器会主动开始重建。

<,>

重建胜利后咱们验证一下配置文件是否胜利挂载。

先看一下容器组的配置,发现新增了一个存储卷 volume-xxxx

终端-> 进入容器外部查看。

查看配置文件挂载和文件内容。

# bash
root@mysql-0:/# ls /etc/mysql/conf.d/
custom.cnf  docker.cnf  mysql.cnf  mysqldump.cnf

root@mysql-0:/# ls -l /etc/mysql/conf.d/
total 16
-rw-r--r-- 1 root root 463 May 11 11:07 custom.cnf
-rw-r--r-- 1 root root  43 Apr 28 06:20 docker.cnf
-rw-r--r-- 1 root root   8 Aug  3  2016 mysql.cnf
-rw-r--r-- 1 root root  55 Aug  3  2016 mysqldump.cnf

root@mysql-0:/# cat /etc/mysql/conf.d/custom.cnf
[mysqld]
#performance setttings
lock_wait_timeout = 3600
open_files_limit    = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
thread_stack = 512K
sort_buffer_size = 4M
join_buffer_size = 4M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
thread_cache_size = 768
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M

查看配置参数是否失效。

root@mysql-0:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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 GLOBAL VARIABLES LIKE 'max_connect%';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_connect_errors | 1000000 |
| max_connections    | 512     |
+--------------------+---------+
2 rows in set (0.02 sec)

mysql>

执行后果跟咱们的配置统一,阐明配置胜利。

导入数据库数据

将数据库文件(SQL),挂载到容器的指定目录下 /docker-entrypoint-initdb.d,容器创立时会主动导入(非必要不举荐)。

用数据库管理工具远程管理数据库(举荐)。

总结

本文具体介绍了 KubeSphere 图形化部署单节点 MySQL 上的装置配置过程,如何利用 KubeSphere 的图形化性能创立资源配置清单 YAML 文件的思路和具体操作过程,当前再部署其余在官网找不到具体配置指南的服务都能够借鉴这个办法。

下篇文章将会介绍 GitOps 的基本概念并演示如何用 GitOps 理念在原生 K8S 上部署 MySQL 服务。

本文由博客一文多发平台 OpenWrite 公布!

正文完
 0