【Ansible部署服务】
部署Zabbix首先编辑 hosts 文件定义目标主机及其连接信息为后续执行 Playbook 做好准备[devopsserver1 ansible]$ vim hosts接下来创建 zabbix.yml 文件编写完整的部署 Playbook。该 Playbook 将依次完成 Zabbix 仓库添加、软件包安装、MySQL 数据库配置、初始数据导入以及服务启动等步骤。[devopsserver1 ansible]$ vim zabbix.yml---- hosts: zabbix_serverbecome: yesvars:zabbix_db_password: passwordzabbix_db_name: zabbixzabbix_db_user: zabbixtasks:- name: Install PyMySQL for MySQL modulesansible.builtin.yum:name: python3-PyMySQLstate: present- name: Add Zabbix repoansible.builtin.yum:name: https://repo.zabbix.com/zabbix/6.0/rhel/9/x86_64/zabbix-release-latest-6.0.el9.noarch.rpmstate: presentdisable_gpg_check: yes- name: Install Zabbix packagesansible.builtin.yum:name:- zabbix-server-mysql- zabbix-web-mysql- zabbix-apache-conf- zabbix-sql-scripts- zabbix-selinux-policy- zabbix-agentstate: present- name: Install MySQL serveransible.builtin.yum:name: mysql-serverstate: present- name: Start and enable MySQL serviceansible.builtin.systemd:name: mysqldstate: startedenabled: yes- name: Create Zabbix databasecommunity.mysql.mysql_db:name: {{ zabbix_db_name }}state: presentencoding: utf8mb4collation: utf8mb4_binlogin_unix_socket: /var/lib/mysql/mysql.sock- name: Create Zabbix MySQL usercommunity.mysql.mysql_user:name: {{ zabbix_db_user }}password: {{ zabbix_db_password }}priv: {{ zabbix_db_name }}.*:ALLhost: localhoststate: presentlogin_unix_socket: /var/lib/mysql/mysql.sock- name: Set log_bin_trust_function_creators 1community.mysql.mysql_variables:variable: log_bin_trust_function_creatorsvalue: 1login_unix_socket: /var/lib/mysql/mysql.sock- name: Import Zabbix initial schema and dataansible.builtin.shell: |zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-setutf8mb4 -u{{ zabbix_db_user }} -p{{ zabbix_db_password }} {{ zabbix_db_name }}args:creates: /tmp/.zabbix_schema_importedno_log: trueregister: import_result- name: Mark schema as importedansible.builtin.file:path: /tmp/.zabbix_schema_importedstate: touchwhen: import_result.changed- name: Set log_bin_trust_function_creators 0community.mysql.mysql_variables:variable: log_bin_trust_function_creatorsvalue: 0login_unix_socket: /var/lib/mysql/mysql.sock- name: Configure Zabbix Server DB passwordansible.builtin.lineinfile:path: /etc/zabbix/zabbix_server.confregexp: ^#?DBPasswordline: DBPassword{{ zabbix_db_password }}- name: Start and enable all servicesansible.builtin.systemd:name: {{ item }}state: restartedenabled: yesloop:- zabbix-server- zabbix-agent- httpd- php-fpm安装 community.mysql 集合该集合提供了 MySQL 数据库和用户管理所需的模块供 Playbook 调用[devopsserver1 ansible]$ ansible-galaxy collection install community.mysql执行 Playbook 后Ansible 会按照定义的任务顺序自动完成 Zabbix 的安装与配置。部署完成后可通过浏览器访问 Zabbix Web 界面进行验证。[devopsserver1 ansible]$ ansible-playbook zabbix.yml✅ 验证部署访问http://192.168.247.143/zabbix默认用户名/密码Admin/zabbix使用 ansible-vault 加密密码删掉zabbix_db_password: password[devopsserver1 ansible]$ ansible-vault create group_vars/zabbix_server/vault.yml[devopsserver1 ansible]$ ansible-playbook zabbix.yml --ask-vault-pass结合TiDBIP主机名角色192.168.247.153tidb-01PD1、TiKV1、TiDB-Server1、Monitor(PrometheusGrafana)192.168.247.154tidb-02PD2、TiKV2、TiDB-Server2192.168.247.155tidb-03PD3、TiKV3192.168.247.156zabbix-01Zabbix 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-012. 配置 /etc/hosts4 台全部配置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 EOF3. 关闭防火墙 / SELinux测试环境生产替换为放行端口systemctl stop firewalld systemctl disable firewalld setenforce 0 sed -i s/^SELINUXenforcing/SELINUXdisabled/ /etc/selinux/config4. 关闭 swapswapoff -a sed -i /swap/s/^/#/ /etc/fstab5. 安装 chrony 时间同步dnf install -y chrony systemctl enable --now chronyd chronyc sources二、控制节点192.168.247.153部署 Ansible 免密 SSH1. 安装 ansiblednf install -y ansible git ansible-galaxy collection install community.general2. 生成 ssh 密钥并分发到所有节点ssh-keygen -t ed25519 -N -f /root/.ssh/id_ed25519 ssh-copy-id root192.168.247.153 ssh-copy-id root192.168.247.154 ssh-copy-id root192.168.247.155 ssh-copy-id root192.168.247.1563. 测试连通性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_ansible2. 文件 1inventory.ini[all:vars] ansible_userroot ansible_ssh_common_args-o StrictHostKeyCheckingno [tidb_all] 192.168.247.153 hostnametidb-01 192.168.247.154 hostnametidb-02 192.168.247.155 hostnametidb-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 hostnamezabbix-01cat inventory.ini EOF 粘贴上面内容 EOF3. 文件 2vars/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: ZabbixPass123 zabbix_version: 7.0 tidb_root_password: RootTiDB123456写入命令cat vars/main.yml EOF 粘贴yaml内容 EOF4. 文件 3templates/topology-multi.j2global: 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 粘贴模板内容 EOF5. 文件 4templates/zabbix_server.conf.j2DBHost{{ 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 LogFileSize0 PidFile/run/zabbix/zabbix_server.pid SocketDir/run/zabbix #DBSocket StartPollers5cat templates/zabbix_server.conf.j2 EOF 粘贴内容 EOF6. 文件 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 \ --userroot \ --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/ENGINEMyISAM/ENGINEInnoDB/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_restartcat 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 集群中控节点 153source /etc/profile.d/tiup.sh tiup cluster display tidb-prod所有节点状态Up代表集群正常。2. 测试 TiDB 数据库连接mysql -h192.168.247.153 -P4000 -uroot -pRootTiDB123456 use zabbix; show tables;能看到大量 zabbix 数据表代表初始化成功。3. 访问 Web 页面Zabbix 地址http://192.168.247.156/zabbix 默认账号Admin /zabbix 向导数据库参数填写 数据库主机192.168.247.153 端口4000 库zabbix 用户zabbix 密码ZabbixPass123TiDB Grafana 监控http://192.168.247.153:3000 TiDB Dashboardhttp://192.168.247.153:2379/dashboard六、常见故障排查清单TiUP 部署时报 ssh 失败确认 root 免密互通、关闭防火墙、hosts 解析正常zabbix-server 无法启动核对数据库账号密码、确认 TiDB 4000 端口通、sql 导入完整Web 页面 500 错误检查 httpd、php-fpm 运行状态Zabbix 大量报错确认已经执行tidb_enable_async_commitOFF