部署Zabbix
首先编辑 hosts 文件,定义目标主机及其连接信息,为后续执行 Playbook 做好准备
[devops@server1 ansible]$ vim hosts

接下来创建 zabbix.yml 文件,编写完整的部署 Playbook。该 Playbook 将依次完成 Zabbix 仓库添加、软件包安装、MySQL 数据库配置、初始数据导入以及服务启动等步骤。
[devops@server1 ansible]$ vim zabbix.yml
---
- hosts: zabbix_server
become: yes
vars:
zabbix_db_password: "password"
zabbix_db_name: "zabbix"
zabbix_db_user: "zabbix"
tasks:
- name: Install PyMySQL for MySQL modules
ansible.builtin.yum:
name: python3-PyMySQL
state: present
- name: Add Zabbix repo
ansible.builtin.yum:
name: https://repo.zabbix.com/zabbix/6.0/rhel/9/x86_64/zabbix-release-latest-6.0.el9.noarch.rpm
state: present
disable_gpg_check: yes
- name: Install Zabbix packages
ansible.builtin.yum:
name:
- zabbix-server-mysql
- zabbix-web-mysql
- zabbix-apache-conf
- zabbix-sql-scripts
- zabbix-selinux-policy
- zabbix-agent
state: present
- name: Install MySQL server
ansible.builtin.yum:
name: mysql-server
state: present
- name: Start and enable MySQL service
ansible.builtin.systemd:
name: mysqld
state: started
enabled: yes
- name: Create Zabbix database
community.mysql.mysql_db:
name: "{{ zabbix_db_name }}"
state: present
encoding: utf8mb4
collation: utf8mb4_bin
login_unix_socket: /var/lib/mysql/mysql.sock
- name: Create Zabbix MySQL user
community.mysql.mysql_user:
name: "{{ zabbix_db_user }}"
password: "{{ zabbix_db_password }}"
priv: "{{ zabbix_db_name }}.*:ALL"
host: localhost
state: present
login_unix_socket: /var/lib/mysql/mysql.sock
- name: Set log_bin_trust_function_creators = 1
community.mysql.mysql_variables:
variable: log_bin_trust_function_creators
value: 1
login_unix_socket: /var/lib/mysql/mysql.sock
- name: Import Zabbix initial schema and data
ansible.builtin.shell: |
zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -u{{ zabbix_db_user }} -p{{ zabbix_db_password }} {{ zabbix_db_name }}
args:
creates: /tmp/.zabbix_schema_imported
no_log: true
register: import_result
- name: Mark schema as imported
ansible.builtin.file:
path: /tmp/.zabbix_schema_imported
state: touch
when: import_result.changed
- name: Set log_bin_trust_function_creators = 0
community.mysql.mysql_variables:
variable: log_bin_trust_function_creators
value: 0
login_unix_socket: /var/lib/mysql/mysql.sock
- name: Configure Zabbix Server DB password
ansible.builtin.lineinfile:
path: /etc/zabbix/zabbix_server.conf
regexp: '^#?DBPassword='
line: "DBPassword={{ zabbix_db_password }}"
- name: Start and enable all services
ansible.builtin.systemd:
name: "{{ item }}"
state: restarted
enabled: yes
loop:
- zabbix-server
- zabbix-agent
- httpd
- php-fpm
安装 community.mysql 集合,该集合提供了 MySQL 数据库和用户管理所需的模块,供 Playbook 调用
[devops@server1 ansible]$ ansible-galaxy collection install community.mysql
执行 Playbook 后,Ansible 会按照定义的任务顺序自动完成 Zabbix 的安装与配置。部署完成后,可通过浏览器访问 Zabbix Web 界面进行验证。
[devops@server1 ansible]$ ansible-playbook zabbix.yml
✅ 验证部署:
访问 http://192.168.247.143/zabbix,默认用户名/密码:Admin/zabbix



使用 ansible-vault 加密密码
删掉zabbix_db_password: "password"

[devops@server1 ansible]$ ansible-vault create group_vars/zabbix_server/vault.yml

[devops@server1 ansible]$ ansible-playbook zabbix.yml --ask-vault-pass
结合TiDB
| IP | 主机名 | 角色 |
|---|---|---|
| 192.168.247.153 | tidb-01 | PD1、TiKV1、TiDB-Server1、Monitor(Prometheus+Grafana) |
| 192.168.247.154 | tidb-02 | PD2、TiKV2、TiDB-Server2 |
| 192.168.247.155 | tidb-03 | PD3、TiKV3 |
| 192.168.247.156 | zabbix-01 | Zabbix Server + Web 前端 |
前置说明: 所有系统:Rocky Linux 9.8 Ansible 控制节点:任选一台(推荐 192.168.247.153) 所有节点使用 root 账号操作,关闭 DNS 反向解析、互通网络
一、所有服务器基础前置操作(4 台机器全部执行)
1. 修改主机名
#153
hostnamectl set-hostname tidb-01
#154
hostnamectl set-hostname tidb-02
#155
hostnamectl set-hostname tidb-03
#156
hostnamectl set-hostname zabbix-01
2. 配置 /etc/hosts(4 台全部配置)
cat >> /etc/hosts <<EOF
192.168.247.153 tidb-01
192.168.247.154 tidb-02
192.168.247.155 tidb-03
192.168.247.156 zabbix-01
EOF
3. 关闭防火墙 / SELinux(测试环境;生产替换为放行端口)
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
4. 关闭 swap
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab
5. 安装 chrony 时间同步
dnf install -y chrony
systemctl enable --now chronyd
chronyc sources
二、控制节点(192.168.247.153)部署 Ansible + 免密 SSH
1. 安装 ansible
dnf install -y ansible git
ansible-galaxy collection install community.general
2. 生成 ssh 密钥并分发到所有节点
ssh-keygen -t ed25519 -N "" -f /root/.ssh/id_ed25519
ssh-copy-id root@192.168.247.153
ssh-copy-id root@192.168.247.154
ssh-copy-id root@192.168.247.155
ssh-copy-id root@192.168.247.156
3. 测试连通性
ansible all -i 192.168.247.153,192.168.247.154,192.168.247.155,192.168.247.156 -m ping
全部返回 SUCCESS 代表免密正常。
三、创建 Ansible 项目目录与全部配置文件
1. 创建目录结构
mkdir -p /opt/zabbix_tidb_ansible/{vars,templates}
cd /opt/zabbix_tidb_ansible
2. 文件 1:inventory.ini
[all:vars]
ansible_user=root
ansible_ssh_common_args="-o StrictHostKeyChecking=no"
[tidb_all]
192.168.247.153 hostname=tidb-01
192.168.247.154 hostname=tidb-02
192.168.247.155 hostname=tidb-03
[pd_servers]
192.168.247.153
192.168.247.154
192.168.247.155
[tikv_servers]
192.168.247.153
192.168.247.154
192.168.247.155
[tidb_servers]
192.168.247.153
192.168.247.154
[tidb_monitor]
192.168.247.153
[zabbix_server]
192.168.247.156 hostname=zabbix-01
cat > inventory.ini <<'EOF'
粘贴上面内容
EOF
3. 文件 2:vars/main.yml
---
timezone: Asia/Shanghai
tidb_version: v7.5.6
tidb_cluster_name: tidb-prod
tidb_user: tidb
tidb_deploy_dir: /tidb-deploy
tidb_data_dir: /tidb-data
tiup_mirror: https://mirrors.tuna.tsinghua.edu.cn/tidb
tidb_db_host: 192.168.247.153
tidb_db_port: 4000
zabbix_db_name: zabbix
zabbix_db_user: zabbix
zabbix_db_pass: "Zabbix@Pass123"
zabbix_version: 7.0
tidb_root_password: "Root@TiDB123456"
写入命令
cat > vars/main.yml <<'EOF'
粘贴yaml内容
EOF
4. 文件 3:templates/topology-multi.j2
global:
user: {{ tidb_user }}
deploy_dir: {{ tidb_deploy_dir }}
data_dir: {{ tidb_data_dir }}
arch: amd64
ssh_port: 22
monitored: true
pd_servers:
{% for host in groups['pd_servers'] %}
- host: {{ host }}
{% endfor %}
tikv_servers:
{% for host in groups['tikv_servers'] %}
- host: {{ host }}
{% endfor %}
tidb_servers:
{% for host in groups['tidb_servers'] %}
- host: {{ host }}
{% endfor %}
monitor_servers:
{% for host in groups['tidb_monitor'] %}
- host: {{ host }}
{% endfor %}
cat > templates/topology-multi.j2 <<'EOF'
粘贴模板内容
EOF
5. 文件 4:templates/zabbix_server.conf.j2
DBHost={{ tidb_db_host }}
DBPort={{ tidb_db_port }}
DBName={{ zabbix_db_name }}
DBUser={{ zabbix_db_user }}
DBPassword={{ zabbix_db_pass }}
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/run/zabbix/zabbix_server.pid
SocketDir=/run/zabbix
#DBSocket=
StartPollers=5
cat > templates/zabbix_server.conf.j2 <<'EOF'
粘贴内容
EOF
6. 文件 5:主剧本 deploy-dist.yml
---
- name: 阶段1 全部主机系统初始化
hosts: all
gather_facts: true
become: true
vars_files:
- vars/main.yml
tags:
- init
- all
tasks:
- name: 设置主机名
hostname:
name: "{{ hostvars[inventory_hostname]['hostname'] | default(inventory_hostname) }}"
tags: init_hostname
- name: 设置时区
community.general.timezone:
name: "{{ timezone }}"
tags: init_timezone
- name: 安装基础依赖
dnf:
name:
- chrony
- wget
- curl
- lsof
- net-tools
- python3-PyMySQL
- openssl-devel
- mysql
- tar
- policycoreutils-python-utils
state: present
tags: init_pkg
- name: 启动chronyd时钟同步
service:
name: chronyd
state: started
enabled: true
tags: init_chrony
- name: 关闭Swap
shell: swapoff -a
tags: init_swapoff
- name: 删除fstab中swap条目
lineinfile:
path: /etc/fstab
regexp: '^[^#].*swap'
state: absent
tags: init_fstab_swap
- name: TiDB内核参数调优
sysctl:
name: "{{ item.key }}"
value: "{{ item.val }}"
state: present
loop:
- {key: "net.ipv4.tcp_syncookies", val: 0}
- {key: "net.ipv4.tcp_tw_reuse", val: 1}
- {key: "vm.swappiness", val: 0}
- {key: "vm.overcommit_memory", val: 1}
- {key: "fs.file-max", val: 1000000}
tags: init_sysctl
- name: tidb用户进程资源限制
copy:
content: |
tidb soft nofile 1048576
tidb hard nofile 1048576
tidb soft nproc unlimited
tidb hard nproc unlimited
dest: /etc/security/limits.d/tidb.conf
tags: init_limits
- name: 阶段2 TiDB集群部署(仅在第一台中控节点执行TiUP)
hosts: tidb_all[0]
gather_facts: true
become: true
vars_files:
- vars/main.yml
tags:
- tidb
- all
tasks:
- name: 创建tidb系统用户
user:
name: "{{ tidb_user }}"
shell: /bin/bash
state: present
tags: tidb_user
- name: 安装tiup本体
shell: |
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
args:
creates: /root/.tiup/bin/tiup
tags: tidb_tiup_install
- name: 写入全局tiup环境变量
copy:
content: 'export PATH=$PATH:/root/.tiup/bin'
dest: /etc/profile.d/tiup.sh
mode: '0644'
tags: tidb_env
- name: 安装tiup cluster组件
shell: |
source /etc/profile.d/tiup.sh
tiup install cluster
args:
creates: /root/.tiup/components/cluster
tags: tidb_tiup_cluster
- name: 生成TiUP分布式拓扑文件
template:
src: templates/topology-multi.j2
dest: /root/{{ tidb_cluster_name }}-topology.yaml
tags: tidb_topo
- name: TiUP部署TiDB分布式集群
shell: |
source /etc/profile.d/tiup.sh
tiup cluster deploy {{ tidb_cluster_name }} {{ tidb_version }} /root/{{ tidb_cluster_name }}-topology.yaml \
--user=root \
--identity_file=/root/.ssh/id_ed25519 \
--yes
tags: tidb_deploy
- name: 启动TiDB集群
shell: |
source /etc/profile.d/tiup.sh
tiup cluster start {{ tidb_cluster_name }}
tags: tidb_start
- name: 等待TiDB 4000端口就绪
wait_for:
host: "{{ tidb_db_host }}"
port: "{{ tidb_db_port }}"
timeout: 180
state: started
tags: tidb_wait_port
- name: 设置TiDB root管理员密码
shell: >
mysql -h{{ tidb_db_host }} -P{{ tidb_db_port }} -uroot -e "SET PASSWORD FOR 'root'@'%' = '{{ tidb_root_password }}';"
tags: tidb_set_root_pwd
- name: 创建zabbix专用数据库
shell: >
mysql -h{{ tidb_db_host }} -P{{ tidb_db_port }} -uroot -p'{{ tidb_root_password }}' -e "CREATE DATABASE IF NOT EXISTS {{ zabbix_db_name }} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
tags: tidb_create_zabbix_db
- name: 创建zabbix数据库账号并授权
shell: >
mysql -h{{ tidb_db_host }} -P{{ tidb_db_port }} -uroot -p'{{ tidb_root_password }}' -e "CREATE USER IF NOT EXISTS '{{ zabbix_db_user }}'@'%' IDENTIFIED BY '{{ zabbix_db_pass }}';GRANT ALL ON {{ zabbix_db_name }}.* TO '{{ zabbix_db_user }}'@'%';"
tags: tidb_zabbix_grant
- name: TiDB全局参数适配Zabbix
shell: >
mysql -h{{ tidb_db_host }} -P{{ tidb_db_port }} -uroot -p'{{ tidb_root_password }}' -e "
SET GLOBAL tidb_enable_async_commit = OFF;
SET GLOBAL sql_mode = 'ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
"
tags: tidb_zabbix_tune
- name: 阶段3 部署Zabbix Server与Web前端
hosts: zabbix_server
gather_facts: true
become: true
vars_files:
- vars/main.yml
tags:
- zabbix
- all
tasks:
- name: 添加Zabbix阿里云YUM源(初始禁用)
yum_repository:
name: zabbix
description: Zabbix {{ zabbix_version }} Rocky Linux 9
baseurl: https://mirrors.aliyun.com/zabbix/zabbix/{{ zabbix_version }}/rhel/9/x86_64/
gpgcheck: 0
enabled: 0
sslverify: 0
tags: zabbix_repo_create
- name: 安装EPEL9仓库(解决fping依赖缺失)
dnf:
name: https://mirrors.aliyun.com/epel/epel-release-latest-9.noarch.rpm
state: present
validate_certs: false
tags: zabbix_epel_install
- name: 刷新DNF缓存(仅基础源+EPEL)
command: dnf makecache
changed_when: false
tags: zabbix_dnf_cache
- name: 预先安装fping(依赖来自EPEL)
dnf:
name: fping
state: present
tags: zabbix_install_fping
- name: 启用Zabbix YUM源
command: dnf config-manager --set-enabled zabbix
changed_when: false
tags: zabbix_repo_enable
- name: 刷新DNF缓存(加载Zabbix源)
command: dnf makecache
changed_when: false
tags: zabbix_dnf_cache_zabbix
- name: 安装Zabbix全套组件(临时禁用epel,避免版本冲突)
dnf:
name:
- zabbix-server-mysql
- zabbix-web-mysql
- zabbix-apache-conf
- zabbix-agent
- httpd
- php-fpm
- mysql
state: present
disablerepo: epel
tags: zabbix_install_pkg
- name: 校验Zabbix数据库账号连通性
shell: >
mysql -h{{ tidb_db_host }} -P{{ tidb_db_port }} -u{{ zabbix_db_user }} -p'{{ zabbix_db_pass }}' {{ zabbix_db_name }} -e "SELECT 1;"
register: db_conn_check
failed_when: db_conn_check.rc != 0
tags: zabbix_db_check
- name: 解压并预处理Zabbix初始SQL(适配TiDB,替换MyISAM为InnoDB)
shell: |
zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | sed 's/ENGINE=MyISAM/ENGINE=InnoDB/g' > /tmp/zabbix_server_tidb.sql
args:
creates: /tmp/zabbix_server_tidb.sql
tags: zabbix_sql_preprocess
- name: 导入预处理后的Zabbix数据表至TiDB
shell: |
mysql -h{{ tidb_db_host }} -P{{ tidb_db_port }} -u{{ zabbix_db_user }} -p'{{ zabbix_db_pass }}' {{ zabbix_db_name }} < /tmp/zabbix_server_tidb.sql
timeout: 600
args:
creates: /tmp/zabbix_sql_imported.flag
register: sql_import_result
tags: zabbix_import_sql
- name: 标记SQL导入完成
file:
path: /tmp/zabbix_sql_imported.flag
state: touch
when: sql_import_result.rc == 0
tags: zabbix_import_sql
- name: 渲染zabbix_server.conf配置文件
template:
src: templates/zabbix_server.conf.j2
dest: /etc/zabbix/zabbix_server.conf
owner: zabbix
group: zabbix
mode: '0640'
tags: zabbix_config
- name: 确保Zabbix日志/PID目录权限正确
file:
path: "{{ item }}"
state: directory
owner: zabbix
group: zabbix
mode: '0755'
loop:
- /var/log/zabbix
- /var/run/zabbix
tags: zabbix_dir_perm
- name: 校验zabbix_server配置文件语法
command: zabbix_server -c /etc/zabbix/zabbix_server.conf -T
register: zabbix_config_check
failed_when: zabbix_config_check.rc != 0
tags: zabbix_config_check
- name: 启动并设置开机自启服务
service:
name: "{{ item }}"
state: started
enabled: true
loop:
- zabbix-server
- zabbix-agent
- httpd
- php-fpm
tags: zabbix_service_start
- name: 重启httpd加载Zabbix配置
service:
name: httpd
state: restarted
tags: zabbix_httpd_restart
cat > deploy-dist.yml <<'EOF'
粘贴上面完整yaml
EOF
四、执行部署
进入项目目录
cd /opt/zabbix_tidb_ansible
# 预检查语法
ansible-playbook -i inventory.ini deploy-dist.yml --syntax-check
# 正式执行部署
ansible-playbook -i inventory.ini deploy-dist.yml
执行耗时:10~20 分钟 出现红色 ERROR 立刻中断排查;正常执行全部绿色 ok
五、部署完成后验证操作
1. 验证 TiDB 集群(中控节点 153)
source /etc/profile.d/tiup.sh
tiup cluster display tidb-prod

所有节点状态 Up 代表集群正常。
2. 测试 TiDB 数据库连接
mysql -h192.168.247.153 -P4000 -uroot -pRoot@TiDB123456
use zabbix;
show tables;

能看到大量 zabbix 数据表代表初始化成功。
3. 访问 Web 页面
Zabbix 地址:http://192.168.247.156/zabbix
默认账号:Admin /zabbix
向导数据库参数填写:
数据库主机:192.168.247.153
端口:4000
库:zabbix
用户:zabbix
密码:Zabbix@Pass123

TiDB Grafana 监控:http://192.168.247.153:3000
默认用户名密码:admin/admin

TiDB Dashboard:http://192.168.247.153:2379/dashboard
默认用户名:root
密码:Root@TiDB123456

六、常见故障排查清单
- TiUP 部署时报 ssh 失败:确认 root 免密互通、关闭防火墙、hosts 解析正常
- zabbix-server 无法启动:核对数据库账号密码、确认 TiDB 4000 端口通、sql 导入完整
- Web 页面 500 错误:检查 httpd、php-fpm 运行状态
- Zabbix 大量报错:确认已经执行
tidb_enable_async_commit=OFF
834



被折叠的 条评论
为什么被折叠?



