實驗環境
[root@agent ~]# cat /etc/redhat-release CentOS release 6.7 (Final) [root@agent ~]# sestatus SELinux status: disabled [root@agent ~]# /etc/init.d/iptables stop
ssh優化
GSSAPIAuthentication no
UseDNS no
本次實驗一共准備了,三台機器
主機名 |
ip |
軟件環境 |
zabbix-server01 |
192.168.56.29 |
httpd+php+keepalived+sshpass zabbix server+agent |
zabbix-server02 |
192.168.56.30 |
httpd+php+keepalived+sshpass zabbix server+agent |
agent |
192.168.56.99 |
zabbix agent+mysql |
VIP規划:192.168.56.66
本文中用到了sshpass免密鑰交互,也可以做ssh相互免密鑰登錄
zabbix server的高可用,我試過在agent配置文件中,使用VIP,發現不能正常監控,於是才有本文。
我的做法agent中的配置文件,Server和ServerActive中分別都配置了兩個ip,就是為了保證,zabbix-server01宕機了,還可以把監控數據發往zabbix-server02,由於兩個zabbix-server共用一個數據庫,肯定不能同時往數據庫寫數據,於是我想到了使用keepalived來控制zabbix server的開關,做到VIP在哪,那台機器的zabbix server就啟動,另外一台關閉,而zabbix的web頁面使用VIP去訪問
本文把zabbix數據庫獨立出來,為了方便做mysql高可用集群
首先在zabbix-server01
rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/6/x86_64/zabbix-release-3.0-1.el6.noarch.rpm vim /etc/yum.repos.d/zabbix.repo 修改兩處bashurl baseurl=http://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/6/x86_64/ baseurl=http://mirrors.aliyun.com/zabbix/non-supported/rhel/6/x86_64/
因為zabbix3.0需要php5.4以上,這里安裝一個php的yum源
rpm -ivh http://mirror.webtatic.com/yum/el6/latest.rpm
安裝httpd和php
yum install httpd -y yum install php55w php55w-mysql php55w-common php55w-gd php55w-mbstring php55w-mcrypt php55w-devel php55w-xml php55w-bcmath -y
安裝zabbix
yum install -y zabbix-agent.x86_64 zabbix-get.x86_64 zabbix-server-mysql.x86_64 zabbix-web.noarch zabbix-web-mysql.noarch \cp -R /usr/share/zabbix/ /var/www/html/ sed -i 's#^post_max_size = 8M#post_max_size = 16M#g' /etc/php.ini sed -i 's#^max_execution_time = 30#max_execution_time = 300#g' /etc/php.ini sed -i 's#^max_input_time = 60#max_input_time = 300#g' /etc/php.ini sed -i '883a date.timezone = Asia/shanghai' /etc/php.ini sed -i '276a ServerName 127.0.0.1:80' /etc/httpd/conf/httpd.conf scp /usr/share/doc/zabbix-server-mysql-3.0.5/create.sql.gz root@192.168.56.99:/opt
在agent上
yum install -y zabbix-agent.x86_64 yum install -y mysql-server mysql-devel mysql /etc/init.d/mysqld start mysql -e "create database zabbix character set utf8 collate utf8_bin;" mysql -e "show databases;" mysql -e "grant all on zabbix.* to zabbix@'192.168.56.%' identified by '123456';" mysql -e "select user,host from mysql.user" zcat /opt/create.sql.gz |mysql -uzabbix -p123456 zabbix
在zabbix-server01上
sed -i '81a DBHost=192.168.56.99' /etc/zabbix/zabbix_server.conf sed -i '116a DBPassword=123456' /etc/zabbix/zabbix_server.conf /etc/init.d/httpd start /etc/init.d/zabbix-server start /etc/init.d/zabbix-agent start
打開瀏覽器進行安裝
安裝完成后
scp /etc/zabbix/web/zabbix.conf.php root@192.168.56.30:/opt
在zabbix-server02
rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/6/x86_64/zabbix-release-3.0-1.el6.noarch.rpm
修改兩處bashurl
vim /etc/yum.repos.d/zabbix.repo baseurl=http://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/6/x86_64/ baseurl=http://mirrors.aliyun.com/zabbix/non-supported/rhel/6/x86_64/
安裝httpd和php
rpm -ivh http://mirror.webtatic.com/yum/el6/latest.rpm yum install httpd -y yum install php55w php55w-mysql php55w-common php55w-gd php55w-mbstring php55w-mcrypt php55w-devel php55w-xml php55w-bcmath -y
安裝zabbix
yum install -y zabbix-agent.x86_64 zabbix-get.x86_64 zabbix-server-mysql.x86_64 zabbix-web.noarch zabbix-web-mysql.noarch \cp -R /usr/share/zabbix/ /var/www/html/ sed -i 's#^post_max_size = 8M#post_max_size = 16M#g' /etc/php.ini sed -i 's#^max_execution_time = 30#max_execution_time = 300#g' /etc/php.ini sed -i 's#^max_input_time = 60#max_input_time = 300#g' /etc/php.ini sed -i '883a date.timezone = Asia/shanghai' /etc/php.ini sed -i '276a ServerName 127.0.0.1:80' /etc/httpd/conf/httpd.conf sed -i '81a DBHost=192.168.56.99' /etc/zabbix/zabbix_server.conf sed -i '116a DBPassword=123456' /etc/zabbix/zabbix_server.conf \mv /opt/zabbix.conf.php /etc/zabbix/web/ /etc/init.d/httpd start
注意:一個主機上有兩個IP,限制我從那個IP地址出去取值
修改如下三處:
vi /etc/zabbix/zabbix_agentd.conf Server=192.168.56.30,192.168.56.29 ServerActive=192.168.56.30,192.168.56.29 Hostname=192.168.56.99
修改完成如下
/etc/init.d/zabbix-agent start
在zabbix-server01和zabbix-server02上分別安裝keepalived的
zabbix-server01
配置文件如下
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id LVS_DEVEL 29 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.56.66 } notify_master /opt/to_master.sh } cat /opt/to_master.sh #!/bin/bash sshpass -p 123456 ssh -o StrictHostKeyChecking=no root@192.168.56.30 "/etc/init.d/zabbix-server stop" /etc/init.d/zabbix-server start zabbix-server02 配置文件如下 cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL 30 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.56.66 } notify_master /opt/to_master.sh } [root@zabbix3 zabbix]# cat /opt/to_master.sh #!/bin/bash sshpass -p 123456 ssh -o StrictHostKeyChecking=no root@192.168.56.29 "/etc/init.d/zabbix-server stop" /etc/init.d/zabbix-server start
下面是測試
兩台zabbix server都啟動keepalived的
zabbix-server01 啟動zabbix server
zabbix-server02不啟動zabbix server
zabbix-server01狀態如下
zabbix-server02狀態如下
模擬zabbix-server01宕機,keepalived不工作了
再測試飄回來
查看數據歷史間隔,發現會丟失個別監控項的一次監控數據
由於兩台zabbix-server只會同時開啟一台,所有agent的日志如下