【zabbix HA】zabbixHA 高可用的实现

zabbix高可用设计目标

1、keepalived服务优先级选择切换机制:对于zabbix服务器来说,只要zabbix存活和mysql存活,就能够正常记录数据,不会丢失数据,php和httpd只是web页面的访问而已,所以我在这里定义mysql和zabbix为主要服务,php和httpd为次要服务,为了实现主要服务存在,次要服务挂了;次要服务器存在,主要服务器挂了,keepalived会优先选择主要服务存在的一方作为master,让keepalived切换更为更合理。

2、数据库主主同步:不管切到哪一边都需要保持数据一致性,不可出现丢数据或者数据重复,保证数据库的高可用。

3、文件双向同步:

        (1)web文件同步:任何做web文件得配置和修改,保证两边一致,确保切换不会发生任何变化。

        (2)zabbix服务文件同步:对zabbix_servr的配置文件做的任何修改,和脚本的修改进行同步,也是为了保证两边的服务一致性,没有落后情况,达到无需人工干预自动切换服务可正常时候。

        (3)到此完成了zabbix得无缝切换,无故障时间!

一、实验环境

操作系统: CentOS7.2 Minial

zabbixA: 192.168.1.102

zabbixB:  192.168.1.103 

vip:          192.168.1.120

二、软件安装

在zabbixA和zabbixB

1.安装zabbix组件

# yum  -y install epel-release

# rpm -hiv   http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm

# yum -y install zabbix-server-mysql zabbix-web.noarch zabbix-web-mysql.noarch  zabbix-java-gateway  zabbix-get

# yum  -y install  zabbix-agent  zabbix-sender 

2. 安装数据库组件

# rpm -ivh     https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

# yum repolist enabled | grep "mysql.*-community.*"

# yum -y install mysql-community-server  mysql-community-client

3.安装php httpd组件

# yum -y install  php php-gd php-mysql php-bcmath php-mbstring php-xml  httpd

4.安装文件同步组件

#  yum  -y install rsync  inotify-tools

5. 双机互信,实现免密登录

# ssh-keygen -t rsa -P '' -q  -f ~/.ssh/id_rsa

# ssh-copy-id  root@$ip

6. 设置selinux模式和防火墙

# sysctl -w net.ipv4.ip_nonlocal_bind=1

# echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf

# setenforce 0

# sed -i 's/^SELINUX=.*/SELINUX=permissive/g'   /etc/selinux/config

# iptables -F

三、zabbixA软件配置

1.数据库配置

# systemctl start mysqld.service

# systemctl enable mysqld.service

# cat /var/log/mysqld.log | grep password 

# mysql -u root -p"XXX"

mysql> set global validate_password_policy=0;

mysql> set global validate_password_length=1;

mysql> set password = password('mysql123');

mysql> create database zabbix character set utf8 collate utf8_bin;

mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix123';

2.zabbix-server配置

# sed -i   '/^# SourceIP/s/=.*$/=192.168.1.120/'                  /etc/zabbix/zabbix_server.conf

# sed -i   '/^# DBHost/s/=.*$/=192.168.1.120/'                     /etc/zabbix/zabbix_server.conf

# sed -i  '/^DBName/s/=.*$/=zabbix/'                                    /etc/zabbix/zabbix_server.conf

# sed -i  '/^# DBPassword/s/.*$/DBPassword=zabbix123/'  /etc/zabbix/zabbix_server.conf

3.php配置

# sed -i      's/post_max_size = 8M/post_max_size = 32M/g'                          /etc/php.ini

# sed -i      's/upload_max_filesize = 2M/upload_max_filesize = 50M/g'        /etc/php.ini

# sed -i       's/\;date.timezone =/date.timezone = Asia\/Shanghai/'               /etc/php.ini

# sed -i      's/max_execution_time = 30/max_execution_time = 600/g'          /etc/php.ini

# sed -i     's/max_input_time = 60/max_input_time = 600/g'                       /etc/php.ini

# sed -i     's/memory_limit = 128M/memory_limit = 256M/g'                      /etc/php.ini 

# systemctl restart zabbix-server

# systemctl enable zabbix-server

# systemctl restart httpd

# systemctl enable httpd

4.keepalived配置

# vim /etc/keepalived/keepalived.conf

############################################

! Configuration File for keepalived

global_defs {

   router_id LVS_DEVEL

}

vrrp_script check { 

    script "/etc/keepalived/check.sh" 

    interval 5     

}   

vrrp_instance VI_1 {

    state BACKUP

    interface eno16777736

    virtual_router_id 100

    priority 100

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {   

      check

    }   

    virtual_ipaddress {

        192.168.1.120

    }

#notify_master "/etc/keepalived/zabbix.sh start"

#notify_backup "/etc/keepalived/zabbix.sh stop"

}

##############################################

# vim /etc/keepalived/check.sh

##############################################

#!/bin/bash

zabbix_server_status1=$(ps -C zabbix_server --no-heading|wc -l)

if [ "${zabbix_server_status1}" = "0" ]; then

  systemctl start zabbix_server.service

  sleep 3

  zabbix_server_status2=$(ps -C zabbix_server --no-heading|wc -l)

  if [ "${zabbix_server_status2}" = "0" ]; then

    systemctl restart keepalived.service

  fi

fi

mysqld_status1=$(ps -C mysqld --no-heading|wc -l)

if [ "${mysqld_status1}" = "0" ]; then

  systemctl start mysqld.service

  sleep 3

  mysqld_status2=$(ps -C mysqld --no-heading|wc -l)

  if [ "${mysqld_status2}" = "0" ]; then

    systemctl restart keepalived.service

  fi

fi

httpd_status1=$(ps -C httpd --no-heading|wc -l)

if [ "${httpd_status1}" = "0" ]; then

  systemctl start httpd.service

  sleep 3

  httpd_status2=$(ps -C httpd --no-heading|wc -l)

  if [ "${httpd_status2}" = "0" ]; then

    systemctl restart keepalived.service

  fi

fi

##########################################

# chmod +x /etc/keepalived/check.sh

5. 文件同步设置

# vim /opt/data_realtime_rsync.sh

####################################################################

#!/bin/bash

# File name:data_realtime_sync.sh

# File path: /opt/data_realtime_sync.sh

src_dir="/etc/zabbix"

dest_dir="/etc/zabbix"

src_ip="192.168.1.102"

dest_ip="192.168.1.103"

. /etc/init.d/functions

cd ${src_dir}

inotifywait -mrq -e modify,attrib,close_write,move,create,delete --format '%e %w%f' ./ |

while read file;do

    INO_EVENT=$(echo $file | awk '{print $1}')      

    INO_FILE=$(echo $file  | awk '{print $2}')      

    if [[ $INO_EVENT =~ 'CREATE' ]] || [[ $INO_EVENT =~ 'MODIFY' ]] || [[ $INO_EVENT =~ 'CLOSE_WRITE' ]] || [[ $INO_EVENT =~ 'MOVED_TO' ]];then

        for ip in ${dest_ip};do

            rsync -avzcR -e ssh  $(dirname ${INO_FILE}) root@${dest_ip}:${dest_dir}

        done

    fi

    if [[ $INO_EVENT =~ 'ATTRIB' ]];then

        if [ ! -d "$INO_FILE" ];then

            for ip in ${dest_ip};do

                rsync -avzcR  -e ssh  $(dirname ${INO_FILE}) root@${dest_ip}:${dest_dir}

            done

        fi

    fi

    if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]];then

        for ip in ${dest_ip};do

            rsync -avzcR --delete  -e ssh  $(dirname ${INO_FILE}) root@${dest_ip}:${dest_dir}

        done

    fi

done

#####################################################################

# chmod +x /opt/data_realtime_rsync.sh

# vim /etc/systemd/system/rsync-inotity.service

###############################################

[Unit]

Description=File instant rsync script

[Service]

ExecStart=/usr/bin/nohup /opt/data_realtime_rsync.sh  &

[Install]

WantedBy=multi-user.target

#################################################

6.数据库主主同步配置

# vim /etc/my.cnf

######################################################

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

symbolic-links=0

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

server-id=1                       

log-bin=mysql-bin                

auto-increment-increment = 2    

auto-increment-offset = 1      

binlog_format = mixed         

sync_binlog=1                

binlog-do-db=zabbix         

binlog-ignore-db=mysql     

binlog-ignore-db=sys

binlog-ignore-db=performance_schema

binlog-ignore-db=information_schema

replicate-do-db=zabbix            

replicate-ignore-db=mysql        

replicate-ignore-db=sys

replicate-ignore-db=performance_schema

replicate-ignore-db=information_schema

######################################################

# systemctl restart mysqld

登录zabbixB的MySQL

创建数据同步账号

# mysql -u root -p"mysql123"

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%' IDENTIFIED BY 'Repl@123';

mysql> grant all privileges on zabbix.* to zabbix@192.168.1.120 identified by 'zabbix123';

mysql> FLUSH PRIVILEGES;

# exit

获取zabbixB二进制日志名和位置

#mysql -uroot -p"mysql123"    --execute='show master status;

# mysql -uroot -p$"mysql123" --execute='show master status;'

在zabbixA 上

#  mysql -uroot -p"mysql123"

mysql> CHANGE MASTER TO

    -> MASTER_HOST='192.168.1.103',

    -> MASTER_USER='repluser',

    -> MASTER_PASSWORD='Repl@123',

    -> MASTER_LOG_FILE='mysql-bin.000002',

    -> MASTER_LOG_POS=50334;

mysql> START SLAVE;

mysql> SHOW SLAVE STATUS\G

四、zabbixB软件配置

1.数据库配置

# systemctl start mysqld.service

# systemctl enable mysqld.service

# cat /var/log/mysqld.log | grep password 

# mysql -u root -p"XXX"

mysql> set global validate_password_policy=0;

mysql> set global validate_password_length=1;

mysql> set password = password('mysql123');

mysql> create database zabbix character set utf8 collate utf8_bin;

mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix123';

2.zabbix-server配置

# sed -i   '/^ SourceIP/s/=.*$/=192.168.1.120/'                     /etc/zabbix/zabbix_server.conf

# sed -i   '/^# DBHost/s/=.*$/=192.168.1.120/'                     /etc/zabbix/zabbix_server.conf

# sed -i  '/^DBName/s/=.*$/=zabbix/'                                    /etc/zabbix/zabbix_server.conf

# sed -i  '/^# DBPassword/s/.*$/DBPassword=zabbix123/'   /etc/zabbix/zabbix_server.conf

3.php配置

# sed -i      's/post_max_size = 8M/post_max_size = 32M/g'                          /etc/php.ini

# sed -i      's/upload_max_filesize = 2M/upload_max_filesize = 50M/g'        /etc/php.ini

# sed -i       's/\;date.timezone =/date.timezone = Asia\/Shanghai/'               /etc/php.ini

# sed -i      's/max_execution_time = 30/max_execution_time = 600/g'          /etc/php.ini

# sed -i     's/max_input_time = 60/max_input_time = 600/g'                       /etc/php.ini

# sed -i     's/memory_limit = 128M/memory_limit = 256M/g'                      /etc/php.ini 

# systemctl restart zabbix-server

# systemctl enable zabbix-server

# systemctl restart httpd

# systemctl enable httpd

4.keepalived配置

# vim /etc/keepalived/keepalived.conf

############################################

! Configuration File for keepalived

global_defs {

   router_id LVS_DEVEL

}

vrrp_script check { 

    script "/etc/keepalived/check.sh" 

    interval 5     

}   

vrrp_instance VI_1 {

    state BACKUP

    interface eno16777736

    virtual_router_id 90

    priority 100

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {   

      check

    }   

    virtual_ipaddress {

        192.168.1.120

    }

#notify_master "/etc/keepalived/zabbix.sh start"

#notify_backup "/etc/keepalived/zabbix.sh stop"

}

##############################################

# vim /etc/keepalived/check.sh

##############################################

#!/bin/bash

zabbix_server_status1=$(ps -C zabbix_server --no-heading|wc -l)

if [ "${zabbix_server_status1}" = "0" ]; then

  systemctl start zabbix_server.service

  sleep 3

  zabbix_server_status2=$(ps -C zabbix_server --no-heading|wc -l)

  if [ "${zabbix_server_status2}" = "0" ]; then

    systemctl restart keepalived.service

  fi

fi

mysqld_status1=$(ps -C mysqld --no-heading|wc -l)

if [ "${mysqld_status1}" = "0" ]; then

  systemctl start mysqld.service

  sleep 3

  mysqld_status2=$(ps -C mysqld --no-heading|wc -l)

  if [ "${mysqld_status2}" = "0" ]; then

    systemctl restart keepalived.service

  fi

fi

httpd_status1=$(ps -C httpd --no-heading|wc -l)

if [ "${httpd_status1}" = "0" ]; then

  systemctl start httpd.service

  sleep 3

  httpd_status2=$(ps -C httpd --no-heading|wc -l)

  if [ "${httpd_status2}" = "0" ]; then

    systemctl restart keepalived.service

  fi

fi

##########################################

# chmod +x /etc/keepalived/check.sh

5. 文件同步设置

# vim /opt/data_realtime_rsync.sh

####################################################################

#!/bin/bash

# File name:data_realtime_sync.sh

# File path: /opt/data_realtime_sync.sh

src_dir="/etc/zabbix"

dest_dir="/etc/zabbix"

src_ip="192.168.1.103"

dest_ip="192.168.1.102"

. /etc/init.d/functions

cd ${src_dir}

inotifywait -mrq -e modify,attrib,close_write,move,create,delete --format '%e %w%f' ./ |

while read file;do

    INO_EVENT=$(echo $file | awk '{print $1}')      

    INO_FILE=$(echo $file  | awk '{print $2}')      

    if [[ $INO_EVENT =~ 'CREATE' ]] || [[ $INO_EVENT =~ 'MODIFY' ]] || [[ $INO_EVENT =~ 'CLOSE_WRITE' ]] || [[ $INO_EVENT =~ 'MOVED_TO' ]];then

        for ip in ${dest_ip};do

            rsync -avzcR -e ssh  $(dirname ${INO_FILE}) root@${dest_ip}:${dest_dir}

        done

    fi

    if [[ $INO_EVENT =~ 'ATTRIB' ]];then

        if [ ! -d "$INO_FILE" ];then

            for ip in ${dest_ip};do

                rsync -avzcR  -e ssh  $(dirname ${INO_FILE}) root@${dest_ip}:${dest_dir}

            done

        fi

    fi

    if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]];then

        for ip in ${dest_ip};do

            rsync -avzcR --delete  -e ssh  $(dirname ${INO_FILE}) root@${dest_ip}:${dest_dir}

        done

    fi

done

#####################################################################

# chmod +x /opt/data_realtime_rsync.sh

# vim /etc/systemd/system/rsync-inotity.service

###############################################

[Unit]

Description=File instant rsync script

[Service]

ExecStart=/usr/bin/nohup /opt/data_realtime_rsync.sh  &

[Install]

WantedBy=multi-user.target

#################################################

 6.数据库主主同步配置

# vim /etc/my.cnf

######################################################

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

symbolic-links=0

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

server-id=2                       

log-bin=mysql-bin                

auto-increment-increment = 2    

auto-increment-offset = 2      

binlog_format = mixed         

sync_binlog=1                

binlog-do-db=zabbix         

binlog-ignore-db=mysql     

binlog-ignore-db=sys

binlog-ignore-db=performance_schema

binlog-ignore-db=information_schema

replicate-do-db=zabbix         

replicate-ignore-db=mysql     

replicate-ignore-db=sys

replicate-ignore-db=performance_schema

replicate-ignore-db=information_schema

######################################################

# systemctl restart mysqld

登录zabbixA的MySQL

创建数据同步账号

# mysql -u root -p"mysql123"

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%' IDENTIFIED BY 'Repl@123';

mysql> grant all privileges on zabbix.* to zabbix@192.168.1.120 identified by 'zabbix123';

mysql> FLUSH PRIVILEGES;

# exit

获取zabbixA二进制日志名和位置

#mysql -uroot -p"mysql123"    --execute='show master status;

# mysql -uroot -p$"mysql123" --execute='show master status;'

在zabbixB上

# mysql -uroot -p"mysql123"

mysql> CHANGE MASTER TO

    -> MASTER_HOST='192.168.1.102',

    -> MASTER_USER='repluser',

    -> MASTER_PASSWORD='Repl@123',

    -> MASTER_LOG_FILE='mysql-bin.000002',

    -> MASTER_LOG_POS=50334;

mysql> START SLAVE;

mysql> SHOW SLAVE STATUS\G

 五、zabbixA  zabbixB服务启动

# systemctl start  zabbix-server

# systemctl enable zabbix-server

# systemctl start  rsync-inotify

# systemctl enable  rsync-inotify

# cat /etc/zabbix/zabbix_server.conf | grep 120

# cat /etc/zabbix/zabbix_server.conf | grep -Ev "^#|^$"

# vim  /etc/zabbix/web/zabbix.conf.php

六、访问测试

http://192.168.1.102/zabbix

http://192.168.1.103/zabbix

http://192.168.1.20/zabbix



七、参考

inotify+rsync+mysql主主复制+keepalived实现zabbix高可用

http://blog.51cto.com/3241766/2137303

Keepalived中Master和Backup主备切换机制浅析

http://blog.51cto.com/3241766/2097483

Zabbix高可用,实现zabbix的无缝切换,无故障时间

http://blog.51cto.com/yigemeng/1738174

实现Zabbix的高可用

https://www.longlong.asia/2016/10/29/zabbix-ha.html

keepalived + rsync +inotify 实现真正的高效数据实时同步

https://fandenggui.com/post/keepalived-rsync-inotify.html

真正的inotify+rsync实时同步 彻底告别同步慢

http://www.ttlsa.com/web/let-infotify-rsync-fast

Zabbix HA Requirment

https://kasperdeng.github.io/zabbix-ha-on-aws

Configuring the Zabbix server for high availability

https://www.packtpub.com/mapt/book/networking_and_servers/9781785289262/3/ch03lvl1sec18/configuring-the-zabbix-server-for-high-availability

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,530评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,403评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,120评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,770评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,758评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,649评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,021评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,675评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,931评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,751评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,410评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,004评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,969评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,042评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,493评论 2 343

推荐阅读更多精彩内容