TiDB集群安装维护

发布时间 2023-04-11 17:17:24作者: moxunjy

TIDB安装操作文档-https://docs.pingcap.com/zh/tidb/stable/production-deployment-using-tiup

1、环境准备(每台)

1-查看系统盘是否为ext4并自动挂载 ,fdisk -l | mkfs.ext4 /dev/ | mount /dev/ | vim /etc/fstab
2-关闭swap :一起执行 swapoff -a 和 swapon -a 命令是为了刷新 swap,将 swap 里的数据转储回内存,并清空 swap 里的数据。
echo "vm.swappiness = 0">> /etc/sysctl.conf
swapoff -a && swapon -a
sysctl -p
3-关闭防火墙:systemctl stop firewalld.service | systemctl disable firewalld.service
4-时间需要同步,建议使用ntp服务。(最好添加 crontab -e 定期任务 :30 10 * * * /sbin/ntpdate xx.xx.xx.xx)记得写入硬件时间中,重启不恢复。 hwclock -w
5-优化环境参数:使用 tuned。

1.执行tuned-adm list 0, Current active profile: balanced 表示当前操作系统的 tuned 策略使用 balanced,建议在当前策略的基础上添加操作系统优化配置。
2.创建新的 tuned 策略
mkdir /etc/tuned/balanced-tidb-optimal/
vi /etc/tuned/balanced-tidb-optimal/tuned.conf
[main]
include=balanced#“include=balanced 表示在现有的 balanced 策略基础上添加操作系统优化配置。”
[cpu]
governor=performance
[vm]
transparent_hugepages=never
[disk]
devices_udev_regex=(ID_SERIAL=36d0946606d79f90025f3e09a0c1fc035)|(ID_SERIAL=36d0946606d79f90025f3e09a0c1f9e81)
elevator=noop
3.应用新的tuned策略 **
tuned-adm profile balanced-tidb-optimal
** 4.检查:

1)cat /sys/kernel/mm/transparent_hugepage/enabled 执行以下命令验证透明大页的状态 返回值应为 always madvise [never]
2)cat /sys/block/sd[bc]/queue/scheduler 执行以下命令验证数据目录所在磁盘的 I/O 调度器。返回值 [noop] deadline cfq

3)cpupower frequency-info --policy 执行以下命令查看 cpufreq 模块选用的节能策略。返回:
analyzing CPU 0:
current policy: frequency should be within 1.20 GHz and 3.10 GHz.
The governor "performance" may decide which speed to use within this range.
4)执行以下命令修改 sysctl 参数。
echo "fs.file-max = 1000000">> /etc/sysctl.conf
echo "net.core.somaxconn = 32768">> /etc/sysctl.conf
echo "net.ipv4.tcp_tw_recycle = 0">> /etc/sysctl.conf
echo "net.ipv4.tcp_syncookies = 0">> /etc/sysctl.conf
echo "vm.overcommit_memory = 1">> /etc/sysctl.conf
sysctl -p
5)执行以下命令配置用户的 limits.conf 文件。
cat << EOF >>/etc/security/limits.conf
tidb soft nofile 1000000
tidb hard nofile 1000000
tidb soft stack 32768
tidb hard stack 32768
EOF

cat << EOF >>/etc/security/limits.conf
tidb soft nofile 1000000
tidb hard nofile 1000000
tidb soft stack 32768
tidb hard stack 32768
EOF

6)互信(可不做
6-安装 numactl 工具:在生产环境中,因为硬件机器配置往往高于需求,为了更合理规划资源,会考虑单机多实例
部署TiDB 或者 TiKV。NUMA 绑核工具的使用,主要为了防止 CPU 资源的争抢,引发性能衰退。
1.登录所有节点安装 yum -y install numactl

==================================================================================================================
2、安装部署(安装前一定确定时间校准!)
现有环境三台机器:
172.16.80.31
172.16.80.32
172.16.80.33
1.离线安装包传至主节点解压
2.开始安装:
1)sh /date/tidb-community-server-v5.1.1-linux-amd64/local_install.sh
根据回显看 shell protile: 显示目录
2)执行环节变量 :source / (shell protile: 显示目录)
3)查看tidb list : tiup list tidb 查看版本号
4)按需编辑配置文件 topology.yaml,配置文件不用放到文件夹中,只是安装配置文件
5)执行 tiup cluster deploy lgtidb v5.1.1 ./topology.yaml --user root -p 输入服务器本机密码,Y确认。
#注意 lgtidb<命名集群名> v5.1.1<根据tiup list tidb查询到的版本号修改>
6)根据提示启动集群 tiup cluster start lgtidb
7)查看集群状态 tiup cluster display lgtidb
** 3.安装完毕:**
1)查看集群 pd|L|UP 领导节点 172.16.80.31:2379 密码默认为空
2)查看grafana监测页面 172.16.80.31:3000 默认账号密码为admin/admin


安装后调试--TIDB
1、检查集群状态:tiup cluster display lgtidb <集群名称> , http://172.16.80.31:3000 查看Overview(IP为grafana部署地址)。
2、启动集群:tiup cluster start lgtidb <集群名称>。
3、查看 TiUP 管理集群情况:tiup cluster list该命令会输出当前通过 TiUP cluster 管理的所有集群信息,包括集群名称、部署用户、版本、密钥信息等。
4、扩容与缩容
扩容 编辑一个 scale-out.yaml 的文件,tiup cluster scale-out lgtidb<集群名称> scale-out.yaml
预期输出 Scaled cluster lgtidb<集群名称> out successfully 信息,表示扩容操作成功。
内容格式:
TiDB 配置文件参考:
tidb_servers:

  • host: 10.0.1.5
    ssh_port: 22
    port: 4000
    status_port: 10080
    deploy_dir: /data/deploy/deploy/tidb-4000
    log_dir: /data/deploy/log/tidb-4000
    TiKV 配置文件参考:
    tikv_servers:

  • host: 10.0.1.5
    ssh_port: 22
    port: 20160
    status_port: 20180
    deploy_dir: /data/deploy/deploy/tikv-20160
    data_dir: /data/deploy/data/tikv-20160
    log_dir: /data/deploy/log/tikv-20160
    PD 配置文件参考:
    pd_servers:

  • host: 10.0.1.5
    ssh_port: 22
    name: pd-1
    client_port: 2379
    peer_port: 2380
    deploy_dir: /data/deploy/deploy/pd-2379
    data_dir: /data/deploy/data/pd-2379
    log_dir: /data/deploy/log/pd-2379

    可以使用 tiup cluster edit-config lgtidb 查看当前集群的配置信息,
    因为其中的 global 和 server_configs 参数配置默认会被 scale-out.yaml 继承,
    因此也会在 scale-out.yaml 中生效。
    缩容 查看集群运行状态后(tiup cluster display lgtidb),根据node(ID ,为IP加端口号)
    关闭、移除服务。
    移除PD服务: tiup cluster scale-in lgtidb --node 172.16.80.32:2379
    移除TIDB服务:tiup cluster scale-in lgtidb --node 172.16.80.32:4000
    移除KITV服务:tiup cluster scale-in lgtidb --node 172.16.80.32:20160

tidb扩容ticdc组件
1.在安装路径/opt/tidb-community-toolkit-v6.5.1-linux-amd64下创建scale-out.yaml文件
cdc_servers:

  • host: 192.168.44.81
    gc-ttl: 86400
    data_dir: /yw-data/tidb-deploy/ticdc/cdc-8300
  • host: 192.168.44.82
    gc-ttl: 86400
    data_dir: /yw-data/tidb-deploy/ticdc/cdc-8300
  • host: 192.168.44.83
    gc-ttl: 86400
    data_dir: /yw-data/tidb-deploy/ticdc/cdc-8300

host为节点ip
gc-ttl为gc大小 86400为默认值
data_dir为ticdc的路径

2.执行以下命令扩容
tiup cluster scale-out yw-test scale-out.yaml --user root -p
yw-test 集群名称
3.查看集群状态
tiup cluster display yw-test

终止 TiCDC 节点:tiup cluster stop -R cdc
启动 TiCDC 节点:tiup cluster start -R cdc
重启 TiCDC 节点:tiup cluster restart -R cdc

---------------------------------------------ticdc维护------------------------------------------------------------------------
使用 TiUP 在原有 TiDB 集群上移除或缩容 TiCDC 组件
推荐使用 TiUP 完成对 TiCDC 集群节点的缩容。使用类似下面的命令完成缩容:

tiup cluster scale-in --node 182.168.44.81:8300
更多用例说明,请参考缩容 TiCDC 节点。

使用 TiUP 升级 TiCDC 集群
TiUP 支持升级 TiDB 集群,包括 TiCDC 组件。执行升级指令时,TiUP 会自动升级 TiCDC 组件,无需额外操作。操作示例如下:

tiup update --self &&
tiup update --all &&
tiup cluster upgrade --transfer-timeout 600
注意
命令中的 需要替换为集群名字, 需要替换为目标版本号,例如 v6.5.1。

升级的注意事项
升级 TiCDC 集群时,需要注意以下事项:

TiCDC v4.0.2 对 changefeed 的配置做了调整,请参阅配置文件兼容注意事项。

升级期间遇到的问题及其解决办法,请参阅使用 TiUP 升级 TiDB。

TiCDC 自 v6.3.0 起支持滚动升级,使用 TiUP 升级 TiCDC 节点期间,能够保证同步延迟稳定,不发生剧烈波动。满足以下条件将自动启用滚动升级:

TiCDC 版本大于等于 v6.3.0。
TiUP 版本大于等于 v1.11.0。
集群中至少有两个正在运行的 TiCDC 实例。
使用 TiUP 变更 TiCDC 集群配置
本节介绍如何使用 TiUP 的 tiup cluster edit-config 命令来修改 TiCDC 的配置。在以下例子中,假设需要把 TiCDC 的 gc-ttl 从默认值 86400 修改为 172800,即 48 小时。

执行 tiup cluster edit-config 命令,注意将 替换成实际的集群名:

tiup cluster edit-config
在 vi 编辑器页面,修改 server-configs 下的 cdc 配置:

server_configs:
tidb: {}
tikv: {}
pd: {}
tiflash: {}
tiflash-learner: {}
pump: {}
drainer: {}
cdc:
gc-ttl: 172800
以上把 TiCDC 的 gc-ttl 的值设置为 48 小时。

执行 tiup cluster reload -R cdc 命令重新加载配置。

使用 TiUP 终止和启动 TiCDC 节点
使用 TiUP 可以方便地终止和启动 TiCDC 节点,命令如下:

终止 TiCDC 节点:tiup cluster stop -R cdc
启动 TiCDC 节点:tiup cluster start -R cdc
重启 TiCDC 节点:tiup cluster restart -R cdc
使用加密传输 (TLS) 功能
请参阅为 TiDB 组件间通信开启加密传输。

使用 TiCDC 命令行工具来查看集群状态
执行以下命令来查看 TiCDC 集群运行状态,注意需要将 v<CLUSTER_VERSION> 替换为 TiCDC 集群版本,例如 v6.5.1:

tiup ctl:v<CLUSTER_VERSION> cdc capture list --server=http://10.0.10.25:8300
[
{
"id": "806e3a1b-0e31-477f-9dd6-f3f2c570abdd",
"is-owner": true,
"address": "127.0.0.1:8300",
"cluster-id": "default"
},
{
"id": "ea2a4203-56fe-43a6-b442-7b295f458ebc",
"is-owner": false,
"address": "127.0.0.1:8301",
"cluster-id": "default"
}
]
id:表示服务进程的 ID。
is-owner:表示该服务进程是否为 owner 节点。
address:该服务进程对外提供接口的地址。
cluster-id:该 TiCDC 的集群 ID,默认值为 default。

-------------------------------------------------------------------------------‘

-----------------------一定检查TIDB用户的密码有效期要设置为用不过期---------------------------不设置过期后无法执行tiup重启和关闭

chage -l tidb
chage -M 99999 -W 7 tidb

-----------------------一定检查TIDB迁移后的存储文件目录是否为tidb用户组---------------------------不修改无法修改reload配置文件

chown -R tidb:tidb /目标路径

-----------------------一定检查TIDB所有节点的last_tikv.toml,修改后再同步配置文件-----------------具体参考https://asktug.com/t/topic/63294

将所有节点的last_tikv.toml文件mv 为 last_tikv.toml.bak ,修改后再同步配置文件。


安装HA
HAProxy安装文档——参考https://www.cnblogs.com/chen110xi/p/6208553.html、https://blog.51cto.com/johnsz/715922
参数说明:
TARGET=linux3100
使用uname -r查看内核,如:2.6.18-371.el5,此时该参数就为linux26
kernel 大于2.6.28的用:TARGET=linux2628
CPU=x86_64 #使用uname -r查看系统信息,如x86_64 x86_64 x86_64 GNU/Linux,此时该参数就为x86_64
PREFIX=/usr/local/haprpxy #/data/haprpxy为haprpxy安装路径

1、解压 :tar xf 压缩包
2、安装 :
进入解压后的文件夹,make TARGET=linux310(查询linux内核版本 rname -r 3.10.0-862.el7.x86_64) PREFIX=/data/haproxy
make install PREFIX=/data/haproxy
3、创建配置文件并创建软连接。
mkdir -p /usr/local/haproxy/conf #创建配置文件目录

mkdir -p /etc/haproxy #创建配置文件目录

touch /usr/local/haproxy/conf/haproxy.cfg #创建配置文件 --安装包中有配置好的,需要修改tidb的ip地址

ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg #添加配置文件软连接

cp -r /usr/local/src/haproxy-1.6.9/examples/errorfiles /usr/local/haproxy/errorfiles #拷贝错误页面

ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles #添加软连接

mkdir -p /usr/local/haproxy/log #创建日志文件目录

touch /usr/local/haproxy/log/haproxy.log #创建日志文件

ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log #添加软连接

cp /usr/local/src/haproxy-1.6.9/examples/haproxy.init /etc/rc.d/init.d/haproxy #拷贝开机启动文件

chmod +x /etc/rc.d/init.d/haproxy #添加脚本执行权限

chkconfig haproxy on #设置开机启动

ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin #添加软连接

配置文件中,stats中配置的是可视化页面访问地址和端口和后缀。http://192.169.144.167:8080/haproxy