四、keepalived的安装部署及配置文件详解
keepalived的安装部署及配置文件详解
一、实验环境
[root@inode1 ~]# uname -r 3.10.0-862.el7.x86_64 [root@inode1 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) keepalived安装版本: keepalived-2.0.20.tar.gz
二、keepalived的安装
yum install -y keepalived
注意:keepalived与3.10的内核有兼容性问题,不建议源码安装
三、keepalived.conf详解
vim /etc/keepalived/keepalived.conf
#全局定义块 global_defs { notification_email { #指定keepalived在发生切换时需要发送email到的对象,一行一个; wgkgood@gmail.com } notification_email_from root@localhost #指定发件人 smtp_server mail.jfedu.net #指定smtp服务器地址 smtp_connect_timeout 3 #指定smtp连接超时时间 router_id LVS_DEVEL #运行keepalived机器的标识,使用hostname } #监控Nginx进程 vrrp_script chk_nginx { script "/data/script/nginx.sh" #监控服务脚本,脚本x执行权限; interval 2 #检测时间间隔(执行脚本间隔) weight 2 #脚本条件成立,优先级+2("-"为减) } #VRRP实例定义块 vrrp_sync_group VG_1{ #监控多个网段的实例 group { VI_1 #实例名 VI_2 } notify_master /data/sh/nginx.sh #指定当切换到master时,执行的脚本(常用与挂载文件系统) notify_backup /data/sh/nginx.sh #指定当切换到backup时,执行的脚本(常用与挂载文件系统) notify /data/sh/nginx.sh #发生任何切换,均执行的脚本 smtp_alert #使用global_defs中提供的邮件地址和smtp服务器发送邮件通知(不常用); } vrrp_instance VI_1 {
inode1和inode2 yum install -y nginx inode1 echo "www.inode1.com" > /usr/share/nginx/html/index.html [root@inode1 ~]# curl 192.168.32.101 www.inode1.com inode2 echo "www.inode2.com" > /usr/share/nginx/html/index.html [root@inode4 ~]# curl 192.168.32.102 www.inode2.com
}
四、实战案例
1、部署keepalived+nginx高可用
实验环境
keepalived: inode1:192.168.32.101 master inode2:192.168.32.102 backup nginx: inode1:192.168.32.101-----www.inode3.com inode2:192.168.32.102-----www.inode4.com VIP地址:192.168.32.222
nginx部署
inode1和inode2 yum install -y nginx inode1 echo "www.inode1.com" > /usr/share/nginx/html/index.html [root@inode1 ~]# curl 192.168.32.101 www.inode1.com inode2 echo "www.inode2.com" > /usr/share/nginx/html/index.html [root@inode4 ~]# curl 192.168.32.102 www.inode2.com
keepalived部署
inode1和inode2 yum install -y keepalived
inode1 master的keepalived.conf文件
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id LVS_1 } vrrp_script chk_nginx { script "/server/sh/nginx_status.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.222 } track_script { chk_nginx } }
inode2 backup的keepalived.conf文件
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id LVS_1 } vrrp_script chk_nginx { script "/server/sh/nginx_status.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.222 } track_script { chk_nginx } }
nginx_status.sh
#!/bin/bash if [ $(pidof nginx|wc -l) -eq 0 ];then systemctl stop keepalived.service fi chomd o+x /server/sh/nginx_status.sh
启动keepalived
[root@inode1 sh]# systemctl start keepalived [root@inode1 sh]# ps -ef |grep keepalived root 12219 1 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12220 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12221 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12253 2016 0 06:29 pts/0 00:00:00 grep --color=auto keepalived [root@inode1 sh]# systemctl start keepalived [root@inode1 sh]# ps -ef |grep keepalived root 12219 1 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12220 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12221 12219 0 06:29 ? 00:00:00 /usr/sbin/keepalived -D root 12253 2016 0 06:29 pts/0 00:00:00 grep --color=auto keepalived
查看VIP地址
[root@inode1 sh]# ip add list|grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0
访问192.168.32.222的页面
[root@inode1 sh]# curl 192.168.32.222 www.inode1.com
关闭inode1上的nginx
[root@inode1 sh]# nginx -s stop [root@inode1 sh]# ip add list|grep 192.168.32.222 [root@inode1 sh]# ps -ef |grep keepalived root 12688 2016 0 06:33 pts/0 00:00:00 grep --color=auto keepalived
再次访问192.168.32.222的页面
[root@inode1 sh]# curl 192.168.32.222 www.inode2.com 页面内容为inode2的内容
在inode2上查看VIP
[root@inode2 sh]# ip addr list|grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0
VIP地址已经漂移到了inode2上
重启inode1的nginx和keepalived
[root@inode1 sh]# nginx [root@inode1 sh]# systemctl start keepalived [root@inode1 sh]# ip addr |grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0
可以看下inode1上nginx和keepalived启动后,VIP有回到了inode1上,原因为,inode1上的keepalived的优先级高于inode2的优先级。
在一些情况下,由于业务的特殊需求,不要master抢占VIP。如下配置:
在inode1 master下配置
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id inode1 } vrrp_script chk_nginx { script "/server/sh/nginx_status.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP #把state 该为BACKUP,因为不抢占只在BACKUP下有效 nopreempt #不抢占 interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.222 } track_script { chk_nginx } }
关闭inode1上的nginx
[root@inode1 sh]# nginx -s stop [root@inode1 sh]# ip addr |grep 192.168.32.222 [root@inode1 sh]# curl 192.168.32.222 www.inode2.com #VIP已经漂移到了inode2上
inode1重启nginx和keepalived
[root@inode1 sh]# nginx [root@inode1 sh]# systemctl start keepalived [root@inode1 sh]# curl 192.168.32.222 www.inode2.com [root@inode1 sh]# ip addr |grep 192.168.32.222 #可以看到inode1没有抢占VIP
2、部署mysql主主+keepalived
实验环境:
client: inode3:192.168.32.103 mysql: inode1:192.168.32.101 inode2:192.168.32.102 keepalived: inode1:192.168.32.101 inode2:192.168.32.102 VIP: 192.168.32.222 keepalived不抢占VIP
1、mysql部署
inode1和inode2 yum install -y mariadb mariadb-server mariadb-devel
2、mysql启动和初始化
inode1和inode2 systemctl start mariadb
3、修改my.cnf,在[mysqld]模块下添加log_bin和server_id两项,并重启mariadb
inode1
[mysqld] log_bin=inode1-bin server_id=101
inode2
[mysqld] log_bin=inode2-bin server_id=102
inode1和inode2
systemctl restart mariadb
部署mysql主主
inode1和indoe2
配置远程登陆账户和密码 mysql -uroot -e "grant all on *.* to "root"@'192.168.32.%' identified by '123456';" 配置主主 mysql -uroot -e "grant replication slave on *.* to "tongbu"@'192.168.32.%' identified by '123456';"
inode1主 inode2从
[root@inode1 ~]# mysql -uroot -e "show master status;" +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | inode1-bin.000001 | 535 | | | +-------------------+----------+--------------+------------------+ [root@inode2 sh]# mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='192.168.32.101',MASTER_USER='tongbu',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE='inode1-bin.000001',MASTER_LOG_POS=535;"
inode2主 inode1从
[root@inode2 sh]# mysql -uroot -e "show master status;" +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | inode2-bin.000001 | 535 | | | +-------------------+----------+--------------+------------------+ [root@inode1 ~]# mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='192.168.32.102',MASTER_USER='tongbu',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE='inode2-bin.000001',MASTER_LOG_POS=535;"
启动start slave inode1和inode2
mysql -uroot -e "start slave;"
查看主主
[root@inode1 ~]# mysql -uroot -e "show slave status\G;"|awk /Running/ Slave_IO_Running: Yes Slave_SQL_Running: Yes [root@inode2 sh]# mysql -uroot -e "show slave status\G;"|awk /Running/ Slave_IO_Running: Yes Slave_SQL_Running: Yes
4、keepalived部署
inode1和inode2 yum install -y keepalived
inode1 master的keepalived.conf文件
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id inode1 } vrrp_script chk_mysql { script "/server/sh/mysql_status.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.222 } track_script { chk_mysql } }
inode2 backup的keepalived.conf文件
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id inode1 } vrrp_script chk_mysql { script "/server/sh/mysql_status.sh" interval 2 weight 2 } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.222 } track_script { chk_mysql } }
mysql_status.sh
#!/bin/bash NUM=$(ps -ef|grep mysql|grep -v grep|grep -v mysql_status.sh|wc -l) if [ $NUM -eq 0 ];then systemctl stop keepalived fi chomd o+x /server/sh/mysql_status.sh
启动keepalived
systemctl start keepalived [root@inode1 ~]# systemctl start keepalived [root@inode1 ~]# ps -ef |grep keepalived root 13735 1 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D root 13736 13735 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D root 13737 13735 0 06:43 ? 00:00:01 /usr/sbin/keepalived -D root 17793 2016 0 07:21 pts/0 00:00:00 grep --color=auto keepalived [root@inode2 ~]# systemctl start keepalived [root@inode2 ~]# ps -ef |grep keepalived root 13735 1 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D root 13736 13735 0 06:43 ? 00:00:00 /usr/sbin/keepalived -D root 13737 13735 0 06:43 ? 00:00:01 /usr/sbin/keepalived -D root 17793 2016 0 07:21 pts/0 00:00:00 grep --color=auto keepalived
测试:
先查看inode1和inode2上的数据库
[root@inode1 ~]# mysql -uroot -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ [root@inode2 ~]# mysql -uroot -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+
在inode3上可以使用VIP登陆mysql,并创建ywx数据库
[root@inode3 ~]# mysql -uroot -p123456 -h 192.168.32.222 -e "create database ywx charset=utf8;"
再次查看inode1和inode2上的数据库
[root@inode1 ~]# mysql -uroot -e "show databases;"grep ywx +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | ywx | +--------------------+ [root@inode2 ~]# mysql -uroot -e "show databases;"grep ywx +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | ywx | +--------------------+
查看VIP地址
[root@inode2 ~]# ip addr list|grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0 #vip在inode2上
测试:
关闭indoe2上的数据库,再次在inode3上使用VIP查看数据ywx [root@inode2 sh]# ip addr list|grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0 [root@inode2 sh]# systemctl stop mariadb [root@inode2 sh]# ip addr list|grep 192.168.32.222 [root@inode2 sh]# [root@inode1 sh]# ip addr list |grep 192.168.32.222 inet 192.168.32.222/32 scope global eth0 [root@inode3 ~]# mysql -uroot -p123456 -h 192.168.32.222 -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | ywx | +--------------------+
inode3任然可以访问数据库
3、使用keepalived+mysql主主,配置2个VIP
要求:
VIP1:192.168.32.222 inode1为master inode2为backup VIP2: 192.168.32.223 inode2为master inode1为backup
keepalived.conf配置如下:
indoe1:
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id inode1 } vrrp_script chk_mysql { script "/server/sh/mysql_status.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.222 } track_script { chk_mysql } } vrrp_instance VI_3 { state MASTER interface eth0 virtual_router_id 52 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.223 } track_script { chk_mysql } }
indoe1:
! Configuration File for keepalived global_defs { notification_email { yaowangxi@163.com } notification_email_from 1521684269@qq.com smtp_server 183.3.225.42 #qq smtp_server ip smtp_connect_timeout 30 router_id inode1 } vrrp_script chk_mysql { script "/server/sh/mysql_status.sh" interval 2 weight 2 } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.222 } track_script { chk_mysql } } vrrp_instance VI_4 { state MASTER interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.32.223 } track_script { chk_mysql } }
查看VIP分布
[root@inode1 sh]# ip addr list |egrep "192.168.32.22[2|3]" inet 192.168.32.222/32 scope global eth0 [root@inode2 sh]# ip addr list|egrep "192.168.32.22[2|3]" inet 192.168.32.223/32 scope global eth0
在inode3上放为VIP1和VIP2
[root@inode3 ~]# mysql -uroot -p123456 -h 192.168.32.222 -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | ywx | +--------------------+ [root@inode3 ~]# mysql -uroot -p123456 -h 192.168.32.223 -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | ywx | +--------------------+
关闭inode2上的mysql,VIP2会漂移到inode1上
[root@inode2 sh]# ip addr list|egrep "192.168.32.22[2|3]" inet 192.168.32.223/32 scope global eth0 [root@inode2 sh]# systemctl stop mariadb [root@inode2 sh]# ip addr list|egrep "192.168.32.22[2|3]" [root@inode2 sh]# [root@inode1 sh]# ip addr list |egrep "192.168.32.22[2|3]" inet 192.168.32.222/32 scope global eth0 inet 192.168.32.223/32 scope global eth0
I have a dream so I study hard!!!
目录 返回
首页