Centos 7 安装zabbix HA
Zabbix HA部署文档
一、 节点准备及前期工作
所需要的yum源打包在repo.tar.gz 中 放在10.100.217.150/data/
• 本次部署方案为2个zab
• bix-server
#需要二个节点
zabbix-server1(node1) Memory:8G CPU:4 disk:40G disk2:40G nic:1
zabbix-server2(node2) Memory:8G CPU:4 disk:40G disk2:40G nic:1
• 关闭防火墙、selinux、
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -r -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -r -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config
• 设置主机名和hosts文件
hostnamectl set-hostname node1
hostnamectl set-hostname node2
vi /etc/hosts
10.100.219.131 node1
10.100.219.132 node2
•更新系统并且重启
yum clean all && yum update -y
reboot
二、部署mysql高可用
(一)、数据盘准备(all)
#mysql节点的第二块硬盘用作数据库存储
mkfs.xfs -f -L mariadbdata /dev/vdb
mkdir /mariadbdata
cp /etc/fstab /etc/fstab.BAK.NO-ERASE
echo "LABEL=mariadbdata /mariadbdata xfs rw,noauto 0 0" >> /etc/fstab
mount /mariadbdata
(二)、安装配置mysql(all)
• 安装mysql、galera及相关组件(all)
yum -y install MariaDB MariaDB-server MariaDB-client galera
• 配置mysql数据目录并启动mysql(all)
mv /var/lib/mysql /mariadbdata/
ln -s /mariadbdata/mysql /var/lib/mysql
/etc/init.d/mysql start
设置mysql密码P@ssw0rd(all)
mysql_secure_installation
•配置mysql客户端(all)
echo "[client]" > /root/.my.cnf
echo "user = "root"" >> /root/.my.cnf
echo "password = \"P@ssw0rd\"" >> /root/.my.cnf
echo "host = \"localhost\"" >> /root/.my.cnf
chmod 600 /root/.my.cnf
•允许root用户远程登录(all)
mysql
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'P@ssw0rd' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit
•停止数据库运行(all)
/etc/init.d/mysql stop
(三)、配置galera
•配置galera(node1)
vi /etc/my.cnf.d/cluster.cnf
[mysqld]
wsrep_on=ON
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
query_cache_type=0
query_cache_size=0
bind-address=0.0.0.0
max_allowed_packet=1024M
max_connections=1000
innodb_doublewrite=1
datadir=/mariadbdata/mysql
innodb_log_file_size=100M
innodb_file_per_table
innodb_flush_log_at_trx_commit=2
# Galera Provider Configuration
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
#wsrep_provider_options="gcache.size=32G"
# Galera Cluster Configuration
wsrep_cluster_name="MariaDB-Cluster01"
wsrep_cluster_address="gcomm://10.100.219.131,10.100.219.132"
# Galera Synchronization Congifuration
wsrep_sst_method=rsync
#wsrep_sst_auth=user:pass
# Galera Node Configuration
wsrep_node_address="10.100.219.131"
wsrep_node_name="node1"
• 配置galera(node2)
vi /etc/my.cnf.d/cluster.cnf
[mysqld]
wsrep_on=ON
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
query_cache_type=0
query_cache_size=0
bind-address=0.0.0.0
max_allowed_packet=1024M
max_connections=1000
innodb_doublewrite=1
datadir=/mariadbdata/mysql
innodb_log_file_size=100M
innodb_file_per_table
innodb_flush_log_at_trx_commit=2
# Galera Provider Configuration
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
#wsrep_provider_options="gcache.size=32G"
# Galera Cluster Configuration
wsrep_cluster_name="MariaDB-Cluster01"
wsrep_cluster_address="gcomm://10.100.219.131,10.100.219.132"
# Galera Synchronization Congifuration
wsrep_sst_method=rsync
#wsrep_sst_auth=user:pass
# Galera Node Configuration
wsrep_node_address="10.100.219.132"
wsrep_node_name="node2"
(四)、启动集群
•node1 执行
galera_new_cluster
•node2执行
/etc/init.d/mysql start
•NOTES:
The services MUST BE disabled. Do not use chkconfig mysql on or systemctl enable mysql !.
The first node to start with the command "galera_new_cluster" will start the cluster service, and the remaining nodes will join the cluster. All cluster will then fully synchronice up to a "multi-master/multi-active" configuration.
In the cluster goes completelly down (energy disruption, or administrativelly down by human intervention), you should start the same way: One onde with galera_new_cluster command, and the other nodes with /etc/init.d/mysql start. You may need to check wich node have the most up-to-date data. See mariadb documentation in order to know how to perform this check.
•当数据库全部运行时,在任意节点检查数据库
MariaDB [(none)]> SHOW STATUS LIKE 'wsrep%';
#####(五)安装tuned (all)
yum install tuned tuned-utils
Then we activate and enable the profile:
tuned-adm profile virtual-guest
systemctl enable tuned
systemctl restart tuned
systemctl status tuned
node1 已经做完
NOTE: If you are using bare-metal servers, use "throughput-performance" instead of "virtual-guest".
(六)、创建数据备份脚本及定时备份任务(all)
•all nodes
vi /usr/local/bin/server-backup.sh
Contents:
#!/bin/bash
#
# Server Backup Script
#
# By Reinaldo Martínez P.
# Caracas, Venezuela.
# TigerLinux AT gmail DOT com
#
PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
directory="/mnt/backups"
myhostname=`hostname -s`
timestamp=`date +%Y%m%d`
daystokeep=5
databasebackupuser="root"
databasebackuppass="P@ssw0rd"
#databases='
# mysql
# test
#'
databases=`echo "show databases"|mysql -s -u $databasebackupuser -p$databasebackuppass`
for i in $databases
do
nice -n 10 ionice -c2 -n7 \
mysqldump -u $databasebackupuser \
-p$databasebackuppass \
--single-transaction \
--quick \
--lock-tables=false \
$i|gzip > $directory/backup-server-db-$i-$myhostname-$timestamp.gz
done
find $directory/ -name "backup-server-*$myhostname*gz" -mtime +$daystokeep -delete
find $directory/ -name "backup-server-db-*$myhostname*gz" -mtime +$daystokeep -delete
echo ""
echo "Server Backup Ready (Configurations and databases)"
echo "Log at: /var/log/server-backup-last-results.log"
echo "Backup file at: $directory/backup-server-$myhostname-$timestamp.tgz"
echo "Databases Backups at $directory/backup-server-db-DBNAME-$myhostname-$timestamp.gz"
echo ""
•设置权限(all)
chmod 755 /usr/local/bin/server-backup.sh
•创建备份目录(all)
mkdir -p /mnt/backups
•添加自动化执行任务(all)
vi /etc/cron.d/server-db-backup-crontab
#
#
# Server backup crontab
#
30 01 * * * root /usr/local/bin/server-backup.sh > /var/log/last-server-backup-crontab.log 2>&1
•重启crond(all)
systemctl restart crond
三、zabbix HA
(一)、配置节点互信
• node1 执行即可
ssh-keygen -t rsa
ssh-copy-id node1
ssh-copy-id node2
scp -r .ssh/ node2:/root/
(二)、安装php及依赖包 配置php.ini
•安装(all)
yum install zlib-devel mysql-devel glibc-devel curl-devel gcc automake mysql \
libidn-devel openssl-devel net-snmp-devel rpm-devel OpenIPMI-devel net-snmp \
net-snmp-utils php-mysql php-gd php-bcmath php-mbstring php-xml nmap php
•配置php.ini(all)
vi /etc/php.ini
max_execution_time = 300
max_input_time = 300
memory_limit = 256M
post_max_size = 32M
date.timezone = America/Caracas
mbstring.func_overload = 2
(三)安装mysql依赖包(all)
yum install MariaDB-devel MariaDB-shared MariaDB-client MariaDB-common
(四)、创建数据库(node1)
MariaDB [(none)]> create database zabbixdb default character set utf8;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON zabbixdb.* TO 'zabbixuser'@'%' IDENTIFIED BY 'P@ssw0rd' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit;
(五)、安装zabbix
•安装(all)
yum install zabbix-server-mysql zabbix-web-mysql
yum install zabbix-agent
•清楚不需要的容易导致问题的安装包(all)
yum erase zabbix-server-pgsql zabbix-web-pgsql
•导入数据表结构(node1)
mkdir /workdir
cp /usr/share/doc/zabbix-server-mysql-3.0.8/create.sql.gz /workdir
cd /workdir
gunzip create.sql.gz
mysql -u zabbixuser -h 10.100.219.131 -pP@ssw0rd zabbixdb < /workdir/create.sql
(六)、创建zabbix 的 sudo文件(all)
vi /etc/sudoers.d/zabbix
Defaults:zabbix !requiretty
Defaults:zabbixsrv !requiretty
zabbix ALL=(ALL) NOPASSWD:ALL
zabbixsrv ALL=(ALL) NOPASSWD:ALL
chmod 0440 /etc/sudoers.d/zabbix
(七)执行以下命令(all)
systemctl stop zabbix-server.service
systemctl stop httpd.service
systemctl disable zabbix-server.service
systemctl disable httpd.service
(八)配置zabbix_server.conf,修改以下内容:(all)
DBHost=10.100.219.135 #这里要填写你下面在pacemaker中创建的vip
DBName=zabbixdb
DBUser=zabbixuser
DBPassword=P@ssw0rd
DBPort=3306
二、安装pacemaker
(一)、安装软件(all)
yum install pacemaker pcs corosync resource-agents pacemaker-cli
(二)、设置集群密码(all)
echo "hacluster:P@ssW0rd"|chpasswd
(三)、启动软件(all)
systemctl start pcsd
systemctl status pcsd
systemctl enable pcsd
systemctl enable corosync
systemctl enable pacemaker
(四)、集群认证(node1)
pcs cluster auth node1 node2
(五)、创建并启动一个集群(node1)
pcs cluster setup --name cluster_zabbix node1 node2
pcs cluster start --all
•禁用stonith (node1)
pcs property set stonith-enabled=false
•无法仲裁时选择忽略(node1)
pcs property set no-quorum-policy=ignore
•创建一个vip(node1)
pcs resource create virtual_ip ocf:heartbeat:IPaddr2 ip=10.100.219.135 cidr_netmask=32 nic=eth0:0 op monitor interval=30s
•配置corosync.service(all)
After "[service]" section, we add the following line:
ExecStartPre=/usr/bin/sleep 10
修改后的内容为:
[Unit]
Description=Corosync Cluster Engine
ConditionKernelCommandLine=!nocluster
Requires=network-online.target
After=network-online.target
[Service]
ExecStartPre=/usr/bin/sleep 10
ExecStart=/usr/share/corosync/corosync start
ExecStop=/usr/share/corosync/corosync stop
Type=forking
# The following config is for corosync with enabled watchdog service.
#
# When corosync watchdog service is being enabled and using with
# pacemaker.service, and if you want to exert the watchdog when a
# corosync process is terminated abnormally,
# uncomment the line of the following Restart= and RestartSec=.
#Restart=on-failure
# Specify a period longer than soft_margin as RestartSec.
#RestartSec=70
# rewrite according to environment.
#ExecStartPre=/sbin/modprobe softdog soft_margin=60
[Install]
WantedBy=multi-user.target
重启daemon-reload(all)
systemctl daemon-reload
(六)创建zabbix控制脚本(node1)
vi /etc/init.d/zabbixsvc
Containing:
#!/bin/bash
#
case $1 in
start)
echo "Starting Zabbix HA Services"
echo "0" > /var/log/zabbix-ha-started.log
rm -f /var/log/zabbix-ha-stopped.log
exit 0
;;
stop)
echo "Stopping Zabbix HA Services"
rm -f /var/log/zabbix-ha-started.log
echo "0" > /var/log/zabbix-ha-stopped.log
exit 0
;;
status|monitor)
echo "Zabbix HA Services Status"
if [ -f /var/log/zabbix-ha-started.log ]
then
exit 0
else
exit 3
fi
;;
restart)
echo "Zabbix HA Services Restart"
exit 0
;;
esac
•修改文件权限并copy到另一个node
chmod 755 /etc/init.d/zabbixsvc
scp /etc/init.d/zabbixsvc node2:/etc/init.d/
(七)添加zabbix到集群(node1)
pcs resource create svczabbixsvc lsb:zabbixsvc op monitor interval=30s
In order to allow the script to be somewhat independent from the cluster service, we proceed to remove the monitor:(node1)
pcs resource op remove svczabbixsvc monitor
(node1)
pcs constraint colocation add svczabbixsvc virtual_ip INFINITY
pcs constraint order virtual_ip then svczabbixsvc
pcs constraint location svczabbixsvc prefers node1
•In both zabbix nodes, proceed to create the main control script:(all)
#!/bin/bash
#
# Zabbix HA Control Services
case $1 in
start)
echo "Starting Zabbix Services"
systemctl start zabbix-server.service
systemctl start httpd.service
;;
stop)
echo "Stopping Zabbix Services"
systemctl stop zabbix-server.service
systemctl stop httpd.service
;;
status|monitor)
echo "Zabbix Services Status"
systemctl status zabbix-server.service
echo ""
echo ""
systemctl status httpd.service
;;
esac
•Save the file and make it mode 755. Remember: In both zabbix nodes:(all)
chmod 755 /usr/local/bin/zabbix-ha-control.sh
•And in both nodes, proceed to modify the zabbix script LSB file:(all)
#!/bin/bash
#
mystatus=`/usr/local/bin/zabbix-ha-control.sh status 2>/dev/null|grep -ci "server is running"`
case $1 in
start)
echo "Starting Zabbix HA Services"
echo "0" > /var/log/zabbix-ha-started.log
rm -f /var/log/zabbix-ha-stopped.log
/usr/local/bin/zabbix-ha-control.sh start > /dev/null 2>&1
exit 0
;;
stop)
echo "Stopping Zabbix HA Services"
rm -f /var/log/zabbix-ha-started.log
echo "0" > /var/log/zabbix-ha-stopped.log
/usr/local/bin/zabbix-ha-control.sh stop > /dev/null 2>&1
exit 0
;;
status|monitor)
echo "Zabbix HA Services Status"
if [ $mystatus == "0" ]
then
rm -f /var/log/zabbix-ha-started.log
echo "0" > /var/log/zabbix-ha-stopped.log
exit 3
else
echo "0" > /var/log/zabbix-ha-started.log
rm -f /var/log/zabbix-ha-stopped.log
exit 0
fi
;;
restart)
echo "Zabbix HA Services Restart"
/usr/local/bin/zabbix-ha-control.sh stop > /dev/null 2>&1
/usr/local/bin/zabbix-ha-control.sh start > /dev/null 2>&1
rm -f /var/log/zabbix-ha-started.log
echo "0" > /var/log/zabbix-ha-stopped.log
exit 0
;;
esac
•NOTE: It is very important to ensure the exit codes are the right ones. We can verify this:(all)
/etc/init.d/zabbixsvc status;echo "echo result: $?"
/etc/init.d/zabbixsvc monitor;echo "echo result: $?"
/etc/init.d/zabbixsvc start;echo "echo result: $?"
/etc/init.d/zabbixsvc stop;echo "echo result: $?"
/etc/init.d/zabbixsvc restart;echo "echo result: $?"
•In the primary/active node1 start-up the services:(node1)
/usr/local/bin/zabbix-ha-control.sh start
•vi /var/www/html/index.html(all)
<HTML>
<HEAD>
<META HTTP-EQUIV="refresh" CONTENT="0;URL=/zabbix">
</HEAD>
<BODY>
</BODY>
</HTML>
•访问webhttp://10.100.219.135
fist web
Database Type: mysql
Database Host: 10.100.219.135
Database Port: 3306
Database Name: zabbixdb
User: zabbixuser
Password: P@ssw0rd
following web
Host: localhost
Port: 10051
Name:
拷贝文件到第二个节点
scp /etc/zabbix/web/zabbix.conf.php node2:/etc/zabbix/web/
If everything goes OK, you'll be able to enter to your zabbix installation with the following default user/pass:
User: Admin
Pass: zabbix
本文来自博客园,作者:haonan071,转载请注明原文链接:https://www.cnblogs.com/zhanghn8/p/11278282.html