K3S 指定 MySql 作为后端存储
1. 现有环境
单节点安装 k3s-v1.22.5(脚本形式,附一键安装脚本)
这里使用了单节点安装的 K3S 环境,只有一个 master 节点:192.168.10.20
[root@c7-1 ~]#cat k3s.sh
#!/bin/bash
# 定义变量
local_ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|head -n 1`
# 环境配置
systemctl stop firewalld && systemctl disable firewalld && iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
sed -ri 's/.*swap.*/#&/' /etc/fstab
swapoff -a
# hostnamectl set-hostname k8s-master && su
# cat >> /etc/hosts <<EOF
# ${local_ip} k8s-master
# EOF
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo "net.ipv4.conf.all.proxy_arp = 1" >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf
yum install ntpdate -y && ntpdate time.windows.com
# 升级内核
# 安装 docker
yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
yum -y install yum-utils device-mapper-persistemt-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce docker-ce-cli containerd.io
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://4iv7219l.mirror.aliyuncs.com"]
}
EOF
grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"
sudo systemctl daemon-reload
sysctl -p
systemctl enable docker && systemctl restart docker
# 安装 k3s
wget http://49.232.8.65/k3s/v1.22.5/k3s -O /usr/local/bin/k3s && chmod +x /usr/local/bin/k3s
yum -y install epel-release https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
yum -y install yum-plugin-elrepo
yum -y install kmod-wireguard wireguard-tools
touch /etc/systemd/system/k3s.service.env
cat > /etc/systemd/system/k3s.service <<EOF
[Unit]
Description=Lightweight Kubernetes
Documentation=https://k3s.io
Wants=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
Type=notify
EnvironmentFile=-/etc/systemd/system/k3s.service.env
KillMode=process
Delegate=yes
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Restart=always
RestartSec=5s
ExecStartPre=-/sbin/modprobe br_netfilter
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/k3s server --docker --no-deploy traefik
EOF
systemctl enable k3s --now
ln -s /usr/local/bin/k3s /usr/local/bin/kubectl
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/kubelet.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
systemctl status k3s
systemctl status docker
kubectl get nodes -o wide
[root@c7-1 ~]#bash k3s.sh &> /dev/null
[root@c7-1 ~]#kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
c7-1 Ready control-plane,master 12m v1.22.5+k3s1 192.168.10.20 <none> CentOS Linux 7 (Core) 3.10.0-1062.el7.x86_64 docker://20.10.17
[root@c7-1 ~]#kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system metrics-server-9cf544f65-tp47x 1/1 Running 0 12m
kube-system coredns-85cb69466-w6tjq 1/1 Running 0 12m
kube-system local-path-provisioner-64ffb68fd-zfns8 1/1 Running 0 12m
2. 安装 mysql-5.7.37
下载
:链接
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
wget http://49.232.8.65/mysql-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
安装
rpm -qa | grep mysql
rpm -qa | grep mariadb
yum -y remove mariadb mysql*
yum -y install wget vim
-------------------------------
[root@c7-1 ~]#ls
k3s.sh mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
[root@c7-1 ~]#tar zxvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
......
[root@c7-1 ~]#ls
k3s.sh mysql-5.7.37-linux-glibc2.12-x86_64 mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
[root@c7-1 ~]#mv mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql5.7.37/
[root@c7-1 ~]#ll /usr/local/mysql5.7.37/
总用量 272
drwxr-xr-x 2 root root 4096 7月 4 13:55 bin
drwxr-xr-x 2 root root 55 7月 4 13:55 docs
drwxr-xr-x 3 root root 4096 7月 4 13:55 include
drwxr-xr-x 5 root root 230 7月 4 13:55 lib
-rw-r--r-- 1 7161 31415 259253 11月 30 2021 LICENSE
drwxr-xr-x 4 root root 30 7月 4 13:55 man
-rw-r--r-- 1 7161 31415 566 11月 30 2021 README
drwxr-xr-x 28 root root 4096 7月 4 13:55 share
drwxr-xr-x 2 root root 90 7月 4 13:55 support-files
[root@c7-1 ~]#groupadd mysql
[root@c7-1 ~]#useradd -r -g mysql mysql
[root@c7-1 ~]#mkdir -p /data/mysql
[root@c7-1 ~]#chown mysql:mysql -R /data/mysql
[root@c7-1 ~]#vim /etc/my.cnf
[root@c7-1 ~]#cat /etc/my.cnf
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql5.7.37
datadir=/data/mysql
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
explicit_defaults_for_timestamp=true
character_set_server=utf8mb4
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@c7-1 ~]#cd /usr/local/mysql5.7.37/bin/
### 初始化获取密码
[root@c7-1 /usr/local/mysql5.7.37/bin]#./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql5.7.37/ --datadir=/data/mysql/ --user=mysql --initialize
2022-07-04T06:16:54.696189Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-04T06:16:54.726164Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-04T06:16:54.783186Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e6b70fe2-fb60-11ec-8b81-000c294404bf.
2022-07-04T06:16:54.784227Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-04T06:16:55.314692Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-04T06:16:55.314709Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-04T06:16:55.321026Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-04T06:16:55.904729Z 1 [Note] A temporary password is generated for root@localhost: v?V<*ivir0Gz
### 启动 mysql,并更改 root 密码
# 将 mysql.server 放置到 /etc/init.d/mysql 中
[root@c7-1 ~]#cp /usr/local/mysql5.7.37/support-files/mysql.server /etc/init.d/mysql
# 启动
root@c7-1 ~]#service mysql start
Starting MySQL.Logging to '/data/mysql/mysql.err'.
SUCCESS!
[root@c7-1 ~]#ps -ef | grep mysql
root 99200 1 0 14:19 pts/1 00:00:00 /bin/sh /usr/local/mysql5.7.37/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/c7-1.pid
mysql 99425 99200 1 14:19 pts/1 00:00:00 /usr/local/mysql5.7.37/bin/mysqld --basedir=/usr/local/mysql5.7.37 --datadir=/data/mysql --plugin-dir=/usr/local/mysql5.7.37/lib/plugin --user=mysql --log-error=/data/mysql/mysql.err --pid-file=/data/mysql/c7-1.pid --socket=/tmp/mysql.sock --port=3306
root 99531 3295 0 14:20 pts/1 00:00:00 grep --color=auto mysql
# 修改密码,首先登录 mysql,使用随机生成的密码
[root@c7-1 /usr/local/mysql5.7.37/bin]#./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37
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>
# 登录成功后修改密码,重新登录
......
mysql> SET PASSWORD = PASSWORD('120604');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@c7-1 /usr/local/mysql5.7.37/bin]#./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 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> exit
Bye
# 添加软连接
[root@c7-1 /usr/local/mysql5.7.37/bin]#ln -s /usr/local/mysql5.7.37/bin/mysql /usr/local/bin/
[root@c7-1 /usr/local/mysql5.7.37/bin]#cd
[root@c7-1 ~]#mysql -uroot -p
......
3. K3S 使用 mysql 作为数据库
mysql 授权远程登录
[root@c7-1 ~]#mysql -uroot -p
......
mysql> grant all privileges on *.* to 'root'@'%' identified by '120604' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
创建 k3s 库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database k3s;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| k3s |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
修改 k3s.service 并重启
[root@c7-1 ~]#vim /etc/systemd/system/k3s.service
[root@c7-1 ~]#cat /etc/systemd/system/k3s.service
[Unit]
Description=Lightweight Kubernetes
Documentation=https://k3s.io
Wants=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
Type=notify
EnvironmentFile=-/etc/systemd/system/k3s.service.env
KillMode=process
Delegate=yes
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Restart=always
RestartSec=5s
ExecStartPre=-/sbin/modprobe br_netfilter
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/k3s server --docker --no-deploy traefik --datastore-endpoint=mysql://root:120604@tcp(192.168.10.20:3306)/k3s
[root@c7-1 ~]#systemctl daemon-reload
[root@c7-1 ~]#systemctl restart k3s
[root@c7-1 ~]#systemctl status k3s | grep active # 重启正常
Active: active (running) since 一 2022-07-04 16:42:54 CST; 5min ago
[root@c7-1 ~]#mysql -uroot -p120604 # 会在 k3s 里生成 kine 表
......
mysql> use k3s;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------+
| Tables_in_k3s |
+---------------+
| kine |
+---------------+
1 row in set (0.00 sec)
4. Mysql 一键安装脚本
#!/bin/bash
## 可先将包放在 /opt 目录下,避免下载时间过长
function loginfo() {
if [[ $? -eq 0 ]];then
echo -e "\033[32m[INFO][$(date +"%F %T")] $1 succeed! \033[0m"
else
echo -e "\033[31m[ERROR][$(date +"%F %T")] $1 failed! \033[0m"
fi
}
function install_mysql() {
echo -e "\033[32mBegin install mysql V5.7.37 ...\033[0m"
# 安装依赖
sudo yum install libaio -y >/dev/null 2>&1
loginfo "libaio install"
BASE_DIR=$(pwd)
MYSQL_PKG_PATH=$BASE_DIR/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
DEPLOY_PATH="/opt"
USER=$(whoami)
GROUP=$(groups)
MYSQL_URL="http://49.232.8.65/mysql-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz"
# 下载解压创建目录
echo -e "\033[32mBegin download mysql V5.7.37 ...\033[0m"
if [[ -f ${DEPLOY_PATH}/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz ]];then
sudo tar xf ${DEPLOY_PATH}/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C ${DEPLOY_PATH}/
else
sudo curl -o ${DEPLOY_PATH}/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz ${MYSQL_URL} >/dev/null 2>&1 && sudo tar xf ${DEPLOY_PATH}/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C ${DEPLOY_PATH}/
fi
loginfo "mysql software download"
#sudo tar xf ${MYSQL_PKG_PATH} -C ${DEPLOY_PATH}/
loginfo "mysql software decompression"
if [[ -d ${DEPLOY_PATH}/mysql ]];then
rm -rf ${DEPLOY_PATH}/mysql
fi
sudo ln -s ${DEPLOY_PATH}/mysql-5.7.37-linux-glibc2.12-x86_64 ${DEPLOY_PATH}/mysql
loginfo "create mysql dir soft link"
if [[ -d /data/mysql ]];then
rm -rf /data/mysql
fi
sudo mkdir -p /data/mysql
loginfo "create mysql data dir"
# 修改启动脚本
sudo sed -i '46s#basedir=#basedir=/opt/mysql#' ${DEPLOY_PATH}/mysql/support-files/mysql.server
sudo sed -i '47s#datadir=#datadir=/data/mysql#' ${DEPLOY_PATH}/mysql/support-files/mysql.server
sudo cp ${DEPLOY_PATH}/mysql/support-files/mysql.server /etc/init.d/mysqld
sudo chmod 755 /etc/init.d/mysqld
# 创建用户
if ! grep -q '^mysql:' /etc/group
then
sudo groupadd mysql
loginfo "create user mysql"
fi
if ! grep -q '^mysql:' /etc/passwd
then
sudo useradd -r -g mysql -s /bin/false mysql
loginfo "create group mysql"
fi
# 赋予data目录和base目录普通用户组
sudo chown -R ${USER}.${GROUP} ${DEPLOY_PATH}/mysql/
sudo chown -R ${USER}.${GROUP} /data/
if [ ! -f /usr/bin/mysql ]
then
sudo ln -s /opt/mysql/bin/mysql /usr/bin/
fi
# 创建配置文件
if [ -f /etc/my.cnf ]
then
sudo rm -f /etc/my.cnf
fi
sudo bash -c "cat >> /etc/my.cnf" <<EOF
[mysqld]
datadir = /data/mysql
basedir = /opt/mysql
#tmpdir = /data/mysql/tmp_mysql
port = 3306
socket = /data/mysql/mysql.sock
pid-file = /data/mysql/mysql.pid
max_connections = 8000
max_connect_errors = 100000
max_user_connections = 3000
check_proxy_users = on
mysql_native_password_proxy_users = on
local_infile = OFF
symbolic-links = FALSE
group_concat_max_len = 4294967295
max_join_size = 18446744073709551615
max_execution_time = 20000
lock_wait_timeout = 60
autocommit = 1
lower_case_table_names = 1
thread_cache_size = 64
disabled_storage_engines = "MyISAM,FEDERATED"
character_set_server = utf8mb4
character-set-client-handshake = FALSE
collation_server = utf8mb4_general_ci
init_connect = 'SET NAMES utf8mb4'
transaction-isolation = "READ-COMMITTED"
#skip_name_resolve = ON
explicit_defaults_for_timestamp = ON
log_timestamps = SYSTEM
local_infile = OFF
event_scheduler = OFF
query_cache_type = OFF
query_cache_size = 0
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO
log_error = /data/mysql/mysql.err
slow_query_log = ON
slow_query_log_file = /data/mysql/slow.log
long_query_time = 1
general_log = OFF
general_log_file = /data/mysql/general.log
expire_logs_days = 99
log-bin = /data/mysql/mysql-bin
log-bin-index = /data/mysql/mysql-bin.index
max_binlog_size = 500M
binlog_format = mixed
binlog_rows_query_log_events = ON
binlog_cache_size = 128k
binlog_stmt_cache_size = 128k
log-bin-trust-function-creators = 1
max_binlog_cache_size = 2G
max_binlog_stmt_cache_size = 2G
relay_log = /data/mysql/relay
relay_log_index = /data/mysql/relay.index
max_relay_log_size = 500M
relay_log_purge = ON
relay_log_recovery = ON
server_id = 1
read_buffer_size = 1M
read_rnd_buffer_size = 2M
sort_buffer_size = 64M
join_buffer_size = 64M
tmp_table_size = 64M
max_allowed_packet = 128M
max_heap_table_size = 64M
connect_timeout = 43200
wait_timeout = 43200
back_log = 512
interactive_timeout = 300
net_read_timeout = 30
net_write_timeout = 30
skip_external_locking = ON
key_buffer_size = 16M
bulk_insert_buffer_size = 16M
concurrent_insert = ALWAYS
open_files_limit = 65000
table_open_cache = 16000
table_definition_cache = 16000
default_storage_engine = InnoDB
default_tmp_storage_engine = InnoDB
internal_tmp_disk_storage_engine = InnoDB
[client]
socket = /data/mysql/mysql.sock
default_character_set = utf8mb4
[mysql]
default_character_set = utf8mb4
[ndatad default]
TransactionDeadLockDetectionTimeOut = 20000
EOF
sudo chown -R ${USER}.${GROUP} /etc/my.cnf
loginfo "configure my.cnf"
# 创建SSL证书
# sudo mkdir -p ${DEPLOY_PATH}/mysql/ca-pem/
# sudo ${DEPLOY_PATH}/mysql/bin/mysql_ssl_rsa_setup -d ${DEPLOY_PATH}/mysql/ca-pem/ --uid=mysql
# sudo chown -R ${USER}.${GROUP} ${DEPLOY_PATH}/mysql/ca-pem/
# sudo bash -c "cat >> /data/mysql/init_file.sql" <<EOF
# set global sql_safe_updates=0;
# set global sql_select_limit=50000;
# EOF
# sudo chown -R ${USER}.${GROUP} /data/mysql/init_file.sql
# sudo chown -R ${USER}.${GROUP} /etc/init.d/mysqld
# 初始化
${DEPLOY_PATH}/mysql/bin/mysqld --initialize --user=mysql --basedir=${DEPLOY_PATH}/mysql --datadir=/data/mysql
loginfo "initialize mysql"
# 过滤初始密码
mysql_passwd=$(grep 'A temporary password is generated' /data/mysql/mysql.err |awk '{print $NF}')
# 启动服务
/etc/init.d/mysqld start
loginfo "start mysqld"
# 修改初始密码
${DEPLOY_PATH}/mysql/bin/mysqladmin -uroot -p${mysql_passwd} password '120604'
if [ $? -ne 0 ];then
loginfo "initialize root password"
fi
# 客户端环境变量
echo "export PATH=\$PATH:${DEPLOY_PATH}/mysql/bin" | sudo tee /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
loginfo "configure envirement"
}
install_mysql
5. 使用官方脚本安装时指定后端存储为 mysql
一键安装脚本
#!/bin/bash
# 定义变量
# 如果是公有云,获取公网 IP:curl http://ifconfig.io
local_ip=`hostname -I | awk '{print $1}'`
### 环境配置
# 关闭 FW,SE,Swap
systemctl stop firewalld && systemctl disable firewalld && iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
sed -ri 's/.*swap.*/#&/' /etc/fstab
swapoff -a
# hostnamectl set-hostname k8s-master && su
# cat >> /etc/hosts <<EOF
# ${local_ip} k8s-master
# EOF
# 将桥接的 IPV4 流量传递到 iptables 链
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo "net.ipv4.conf.all.proxy_arp = 1" >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf
# 同步时间
yum install ntpdate -y && ntpdate time.windows.com
### 安装 docker
# 卸载先前的 docker
yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
yum -y install yum-utils device-mapper-persistemt-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce docker-ce-cli containerd.io
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://4iv7219l.mirror.aliyuncs.com"]
}
EOF
grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"
sudo systemctl daemon-reload
sysctl -p
systemctl enable docker && systemctl restart docker
### 安装 mysql-5.7.37,参考以上步骤
function loginfo() {
if [[ $? -eq 0 ]];then
echo -e "\033[32m[INFO][$(date +"%F %T")] $1 succeed! \033[0m"
else
echo -e "\033[31m[ERROR][$(date +"%F %T")] $1 failed! \033[0m"
fi
}
function install_mysql() {
echo -e "\033[32mBegin install mysql V5.7.37 ...\033[0m"
# 安装依赖
sudo yum install libaio -y >/dev/null 2>&1
loginfo "libaio install"
BASE_DIR=$(pwd)
MYSQL_PKG_PATH=$BASE_DIR/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
DEPLOY_PATH="/opt"
USER=$(whoami)
GROUP=$(groups)
MYSQL_URL="http://49.232.8.65/mysql-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz"
# 下载解压创建目录
echo -e "\033[32mBegin download mysql V5.7.37 ...\033[0m"
if [[ -f ${DEPLOY_PATH}/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz ]];then
sudo tar xf ${DEPLOY_PATH}/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C ${DEPLOY_PATH}/
else
sudo curl -o ${DEPLOY_PATH}/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz ${MYSQL_URL} >/dev/null 2>&1 && sudo tar xf ${DEPLOY_PATH}/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C ${DEPLOY_PATH}/
fi
loginfo "mysql software download"
#sudo tar xf ${MYSQL_PKG_PATH} -C ${DEPLOY_PATH}/
loginfo "mysql software decompression"
if [[ -d ${DEPLOY_PATH}/mysql ]];then
rm -rf ${DEPLOY_PATH}/mysql
fi
sudo ln -s ${DEPLOY_PATH}/mysql-5.7.37-linux-glibc2.12-x86_64 ${DEPLOY_PATH}/mysql
loginfo "create mysql dir soft link"
if [[ -d /data/mysql ]];then
rm -rf /data/mysql
fi
sudo mkdir -p /data/mysql
loginfo "create mysql data dir"
# 修改启动脚本
sudo sed -i '46s#basedir=#basedir=/opt/mysql#' ${DEPLOY_PATH}/mysql/support-files/mysql.server
sudo sed -i '47s#datadir=#datadir=/data/mysql#' ${DEPLOY_PATH}/mysql/support-files/mysql.server
sudo cp ${DEPLOY_PATH}/mysql/support-files/mysql.server /etc/init.d/mysqld
sudo chmod 755 /etc/init.d/mysqld
# 创建用户
if ! grep -q '^mysql:' /etc/group
then
sudo groupadd mysql
loginfo "create user mysql"
fi
if ! grep -q '^mysql:' /etc/passwd
then
sudo useradd -r -g mysql -s /bin/false mysql
loginfo "create group mysql"
fi
# 赋予data目录和base目录普通用户组
sudo chown -R ${USER}.${GROUP} ${DEPLOY_PATH}/mysql/
sudo chown -R ${USER}.${GROUP} /data/
if [ ! -f /usr/bin/mysql ]
then
sudo ln -s /opt/mysql/bin/mysql /usr/bin/
fi
# 创建配置文件
if [ -f /etc/my.cnf ]
then
sudo rm -f /etc/my.cnf
fi
sudo bash -c "cat >> /etc/my.cnf" <<EOF
[mysqld]
datadir = /data/mysql
basedir = /opt/mysql
#tmpdir = /data/mysql/tmp_mysql
port = 3306
socket = /data/mysql/mysql.sock
pid-file = /data/mysql/mysql.pid
max_connections = 8000
max_connect_errors = 100000
max_user_connections = 3000
check_proxy_users = on
mysql_native_password_proxy_users = on
local_infile = OFF
symbolic-links = FALSE
group_concat_max_len = 4294967295
max_join_size = 18446744073709551615
max_execution_time = 20000
lock_wait_timeout = 60
autocommit = 1
lower_case_table_names = 1
thread_cache_size = 64
disabled_storage_engines = "MyISAM,FEDERATED"
character_set_server = utf8mb4
character-set-client-handshake = FALSE
collation_server = utf8mb4_general_ci
init_connect = 'SET NAMES utf8mb4'
transaction-isolation = "READ-COMMITTED"
#skip_name_resolve = ON
explicit_defaults_for_timestamp = ON
log_timestamps = SYSTEM
local_infile = OFF
event_scheduler = OFF
query_cache_type = OFF
query_cache_size = 0
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO
log_error = /data/mysql/mysql.err
slow_query_log = ON
slow_query_log_file = /data/mysql/slow.log
long_query_time = 1
general_log = OFF
general_log_file = /data/mysql/general.log
expire_logs_days = 99
log-bin = /data/mysql/mysql-bin
log-bin-index = /data/mysql/mysql-bin.index
max_binlog_size = 500M
binlog_format = mixed
binlog_rows_query_log_events = ON
binlog_cache_size = 128k
binlog_stmt_cache_size = 128k
log-bin-trust-function-creators = 1
max_binlog_cache_size = 2G
max_binlog_stmt_cache_size = 2G
relay_log = /data/mysql/relay
relay_log_index = /data/mysql/relay.index
max_relay_log_size = 500M
relay_log_purge = ON
relay_log_recovery = ON
server_id = 1
read_buffer_size = 1M
read_rnd_buffer_size = 2M
sort_buffer_size = 64M
join_buffer_size = 64M
tmp_table_size = 64M
max_allowed_packet = 128M
max_heap_table_size = 64M
connect_timeout = 43200
wait_timeout = 43200
back_log = 512
interactive_timeout = 300
net_read_timeout = 30
net_write_timeout = 30
skip_external_locking = ON
key_buffer_size = 16M
bulk_insert_buffer_size = 16M
concurrent_insert = ALWAYS
open_files_limit = 65000
table_open_cache = 16000
table_definition_cache = 16000
default_storage_engine = InnoDB
default_tmp_storage_engine = InnoDB
internal_tmp_disk_storage_engine = InnoDB
[client]
socket = /data/mysql/mysql.sock
default_character_set = utf8mb4
[mysql]
default_character_set = utf8mb4
[ndatad default]
TransactionDeadLockDetectionTimeOut = 20000
EOF
sudo chown -R ${USER}.${GROUP} /etc/my.cnf
loginfo "configure my.cnf"
# 创建SSL证书
# sudo mkdir -p ${DEPLOY_PATH}/mysql/ca-pem/
# sudo ${DEPLOY_PATH}/mysql/bin/mysql_ssl_rsa_setup -d ${DEPLOY_PATH}/mysql/ca-pem/ --uid=mysql
# sudo chown -R ${USER}.${GROUP} ${DEPLOY_PATH}/mysql/ca-pem/
# sudo bash -c "cat >> /data/mysql/init_file.sql" <<EOF
# set global sql_safe_updates=0;
# set global sql_select_limit=50000;
# EOF
# sudo chown -R ${USER}.${GROUP} /data/mysql/init_file.sql
# sudo chown -R ${USER}.${GROUP} /etc/init.d/mysqld
# 初始化
${DEPLOY_PATH}/mysql/bin/mysqld --initialize --user=mysql --basedir=${DEPLOY_PATH}/mysql --datadir=/data/mysql
loginfo "initialize mysql"
# 过滤初始密码
mysql_passwd=$(grep 'A temporary password is generated' /data/mysql/mysql.err |awk '{print $NF}')
# 启动服务
cat >> /usr/lib/systemd/system/mysql.service << EOF
[Unit]
Description=MySQL Community Server
After=network.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/opt/mysql-5.7.37-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/etc/my.cnf --user=root
WorkingDirectory=/opt/mysql-5.7.37-linux-glibc2.12-x86_64/
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable mysqld && systemctl start mysqld
loginfo "start mysqld"
# 修改初始密码
${DEPLOY_PATH}/mysql/bin/mysqladmin -uroot -p${mysql_passwd} password '120604'
if [ $? -ne 0 ];then
loginfo "initialize root password"
fi
# 客户端环境变量
echo "export PATH=\$PATH:${DEPLOY_PATH}/mysql/bin" | sudo tee /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
loginfo "configure envirement"
}
install_mysql
# 创建 k3s 库
mysql -uroot -p120604 -e "create database k3s;"
### 安装 k3s,这里不使用二进制安装,使用官方脚本指定后端存储参数
curl -sfL http://49.232.8.65/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn INSTALL_K3S_VERSION=v1.22.5+k3s1 sh -s - --docker --datastore-endpoint='mysql://root:120604@tcp(${local_ip}:3306)/k3s'
systemctl status k3s
systemctl status docker
kubectl get nodes -o wide
[root@c7-1 ~]#mysql -uroot -p120604 -e "show tables in k3s;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+
| Tables_in_k3s |
+---------------+
| kine |
+---------------+
[root@c7-1 ~]#kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-85cb69466-8tpc7 1/1 Running 0 4m42s
kube-system local-path-provisioner-64ffb68fd-b86m2 1/1 Running 0 4m42s
kube-system helm-install-traefik-crd--1-vlfst 0/1 Completed 0 4m43s
kube-system metrics-server-9cf544f65-vgldv 1/1 Running 0 4m42s
kube-system helm-install-traefik--1-9z6rw 0/1 Completed 0 4m43s
kube-system svclb-traefik-mqfbf 2/2 Running 0 115s
kube-system traefik-786ff64748-q6fdn 1/1 Running 0 115s
[root@c7-1 ~]#kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
c7-1 Ready control-plane,master 5m19s v1.22.5+k3s1 192.168.10.20 <none> CentOS Linux 7 (Core) 3.10.0-1062.el7.x86_64 docker://20.10.17
k3s 搭建 MySQL 数据库及数据持久化及 rancher 管理