大家好,我是老 Z!
本文实现了 MySQL 数据库在基于 KubeSphere 部署的 K8s 集群上的安装部署,部署方式采用了图形化这种形式。下一篇文章将会涉及 GitOps 的基础操作,部署过程涉及的所有 YAML 文件都会使用 Git 进行版本管理,并存放在 Git 仓库中,敬请期待!
本文部署的 MySQL 选择了比较保守的 5.7 系列,其他版本可能会有不同。本文的操作仅适用于小规模数据量且对可靠性和性能要求不高的数据库使用场景,例如开发测试环境、例如我生产环境的 Nacos 服务。生产环境或是重要的数据库个人不建议将数据放到 K8s 上,优先采用云服务商提供的 RDS,其次自己利用虚拟机搭建 MySQL 主从或是 Galera Cluster,且一定做好备份方案。
数据库的可靠性、可用性是运维的重中之重,不容忽视,切记!!!
| 主机名 | 操作系统 | 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 |
选择 MySQL5.7 版本的 Reference Manual。

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

浏览完以后你会发现,只是学会了利用 Docker Image 安装 MySQL 的基本方法,细节不上图了。
虽然官方没有提到如何在 K8s 上部署 MySQL,但是我已经有 Docker 和 K8s 的基础知识了,先不去进行搜索吃别人的了,自己尝试在 K8s 上部署一个单节点的 MySQL。
如果之前有 Docker 部署 MySQL 的经验,这一步就很简单了,直接把参数配置搬过来就行。
打开 https://hub.docker.com,搜索 mysql。
搜索结果中会有很多的 mysql,我重点关注了两个镜像。
本次实验我使用了 Docker 官方维护的仓库,进入 MySQL 仓库页面。
大概浏览一遍,确认了几个必须要配置的地方(确定过程需要经验和技术积累)。
确定了初始化的参数,接下来就开始部署 MySQL。
按 K8s 常规套路编写资源定义 YAML 文件?NO!我现在是小白,手写配置文件太高端了,还不适合我。
我们这里投机取巧一下,利用 KubeSphere 的图形化操作一波,这样可以保证部署的一次成功率 (还有一个隐藏的好处,先卖个关子)。
使用企业空间管理员权限的账户,登录 KubeSphere 控制台。
这一步没有使用 admin 用户,采用多租户户形式,模拟真实的生产环境
<
,
>
点击项目,点击 lstack 项目,进入项目的管理页面 (如无特殊说明,后面的很多界面操作都是在该页面完成)。
<
,
>
应用负载->工作负载->有状态副本集,点击创建。

弹出创建有状态副本集页面,基本信息页,名称输入 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 数据库,也可以满足 K8s 集群之外的服务访问 MySQL 数据库的需求。
在 KubeSphere 中开启服务的外部访问需要先设置项目网关。
用项目管理员用户登录控制台。
工作台->项目-> 点击具体的项目->项目设置->网关设置,点击开启网关。

目前访问模式有 NodePort 和 LoadBalancer,但是 LoadBalancer 只支持公有云提供商云上的负载均衡器,所以我们只能选择 NodePort,点击确定。
NodePort 模式里会创建一个采用了 nginx-ingress 的 kubesphere-router 的容器组,细节我们会在以后的专文探讨。
<
,
>
网关设置的细节不在本文深入讨论,后续会有专文探讨。现在,做到这一步就 OK 了。
接下来创建一个 MySQL 服务用于对外提供服务。
应用负载->-服务>创建-> 选择自定义服务->指定工作负载。
这里有一个外部服务的选项,那个是基于 Ingress 使用域名访问的,不是目前我们想要的方式。

指定工作负载创建服务-基本信息。

指定工作负载创建服务-服务设置,点击指定工作负载,选择有状态副本集->mysql,点击确定。

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

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

完成所有设置后,点击创建,创建成功会自动返回服务列表,在服务列表中可以看到我们新创建的服务 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 使用的 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
实现思路。
创建 ConfigMap 配置文件,配置->配置字典,点击创建。

创建配置字典-基本信息。

创建配置字典-数据设置。
<
,
>
填写完键值信息后,点击对号确定,最后点击创建,创建完成后会返回配置字典页面。
<
,
>
接下来将自定义配置文件,挂载到 mysql 容器。
应用负载->工作负载->有状态副本集-> 点击 mysql-> 进入详细配置页面->更多操作-点击编辑设置。

编辑设置->存储卷->挂载配置字典或保密字典。
<
,
>
存储卷。
<
,
,
>
输入完成后,点击对号。

再次点击对号,点击确定,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 发布!
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我可以在Azure网站上部署RubyonRails吗? 最佳答案 还没有。目前仅支持.NET和PHP。 关于ruby-on-rails-RubyonRails可以部署在Azure网站上吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/12964010/
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
前置步骤我们都操作完了,这篇开始介绍jenkins的集成。话不多说,看操作1、登录进入jenkins后会让你选择安装插件,选择第一个默认的就行。安装完成后设置账号密码,重新登录。2、配置JDK和Git都需要执行路径,所以需要先把执行路径找到,先进入服务器的docker容器,2.1JDK的路径root@69eef9ee86cf:/usr/bin#echo$JAVA_HOME/usr/local/openjdk-82.2Git的路径root@69eef9ee86cf:/#whichgit/usr/bin/git3、先配置JDK和Git。点击:ManageJenkins>>GlobalToolCon
深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal
Ocra无法处理需要“tk”的应用程序require'tk'puts'nope'用奥克拉http://github.com/larsch/ocra不起作用(如链接中的一个问题所述)问题:https://github.com/larsch/ocra/issues/29(Ocra是1.9的"new"rubyscript2exe,本质上它用于将rb脚本部署为可执行文件)唯一的问题似乎是缺少tcl的DLL文件我不认为这是一个问题据我所知,问题是缺少tk的DLL文件如果它们是已知的,则可以在执行ocra时将它们包括在内有没有办法知道tk工作所需的DLL依赖项? 最佳答
我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin
我已经开始使用mysql2gem。我试图弄清楚一些基本的事情——其中之一是如何明确地执行事务(对于批处理操作,比如多个INSERT/UPDATE查询)。在旧的ruby-mysql中,这是我的方法:client=Mysql.real_connect(...)inserts=["INSERTINTO...","UPDATE..WHEREid=..",#etc]client.autocommit(false)inserts.eachdo|ins|beginclient.query(ins)rescue#handleerrorsorabortentirelyendendclient.commi
我有一个类unzipper.rb,它使用Rubyzip解压文件。在我的本地环境中,我可以成功解压缩文件,而无需使用require'zip'明确包含依赖项但是在Heroku上,我得到一个NameError(uninitializedconstantUnzipper::Zip)我只能通过使用明确的require来解决问题:为什么这在Heroku环境中是必需的,但在本地主机上却不是?我的印象是Rails自动需要所有gem。app/services/unzipper.rbrequire'zip'#OnlyrequiredforHeroku.Workslocallywithout!class
出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t