一、部署环境
先克隆两台centos7的虚拟机

主机名:haproxykeepalived-3、4
关闭防火墙、selinux
1.haproxy部署(两台同步进行)
安装依赖 [root@haproxykeepalived-3 ~]# yum install wget gcc gcc+ -y
安装编译包 [root@haproxykeepalived-3 ~]# wget http://192.168.1.235/haproxy-1.5.14.tar.gz [root@haproxykeepalived-3 ~]# tar -zxvf haproxy-1.5.14.tar.gz [root@haproxykeepalived-3 ~]# cd haproxy-1.5.14 [root@haproxykeepalived-3 ~]# make TARGET=linux26 ARCH=x86_64 [root@haproxykeepalived-3 ~]# make install SBINDIR=/usr/sbin/ MANDIR=/usr/share/man/ DOCDIR=/usr/share/doc/
创建启动脚本 [root@haproxykeepalived-3 ~]# cat /etc/init.d/haproxy #!/bin/sh # # haproxy # # chkconfig: - 85 15 # description: HAProxy is a free, very fast and reliable solution \ # offering high availability, load balancing, and \ # proxying for TCP and HTTP-based applications # processname: haproxy # config: /etc/haproxy/haproxy.cfg # pidfile: /var/run/haproxy.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 exec="/usr/sbin/haproxy" prog=$(basename $exec) [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog cfgfile=/etc/haproxy/haproxy.cfg pidfile=/var/run/haproxy.pid lockfile=/var/lock/subsys/haproxy check() { $exec -c -V -f $cfgfile $OPTIONS } start() { $exec -c -q -f $cfgfile $OPTIONS if [ $? -ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi echo -n $"Starting $prog: " # start it up here, usually something like "daemon $exec" daemon $exec -D -f $cfgfile -p $pidfile $OPTIONS retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " # stop it here, often "killproc $prog" killproc $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { $exec -c -q -f $cfgfile $OPTIONS if [ $? -ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi stop start } reload() { $exec -c -q -f $cfgfile $OPTIONS if [ $? -ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi echo -n $"Reloading $prog: " $exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile) retval=$? echo return $retval } force_reload() { restart } fdr_status() { status $prog } case "$1" in start|stop|restart|reload) $1 ;; force-reload) force_reload ;; check) check ;; status) fdr_status ;; condrestart|try-restart) [ ! -f $lockfile ] || restart ;; *) echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}" exit 2 esac
授权启动脚本 [root@haproxykeepalived-3 ~]# chmod +x /etc/init.d/haproxy 新建haproxy程序目录 [root@haproxykeepalived-3 ~]# mkdir /etc/haproxy [root@haproxykeepalived-3 ~]# mkdir /var/lib/haproxy 新建haproxy用户 [root@haproxykeepalived-3 ~]# useradd -r haproxy
写入haproxy配置文件 [root@haproxykeepalived-3 ~]# cat /etc/haproxy/haproxy.cfg global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon stats socket /var/lib/haproxy/stats defaults mode tcp log global option dontlognull option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 600 listen stats mode http bind :8888 stats enable stats hide-version stats uri /status stats realm Haproxy\ Statistics stats auth admin:admin frontend main bind 0.0.0.0:3306 default_backend mysql backend mysql balance leastconn server mysql1 192.168.1.159:3306 check port 3306 maxconn 300 #两台MySQL的DB server mysql2 192.168.1.160:3306 check port 3306 maxconn 300
修改rsyslog.conf配置文件 [root@haproxykeepalived-3 ~]# cat /etc/rsyslog.conf ...... #### MODULES #### # Provides TCP syslog reception #去掉下面两项的#注释 $ModLoad imtcp $InputTCPServerRun 514 local2.* /var/log/haproxy.log #增加这句 .......
启动Haproxy服务 [root@haproxykeepalived-3 ~]# systemctl start haproxy [root@haproxykeepalived-3 ~]# ps -A |grep haproxy #查看服务状态
2.Keepalive部署 (两台同步进行)
安装依赖 [root@haproxykeepalived-3 ~]# yum -y install openssl openssl-devel
安装编译包 [root@haproxykeepalived-3 ~]# wget http://192.168.1.235/keepalived-1.2.19.tar.gz [root@haproxykeepalived-3 ~]# tar -zxvf keepalived-1.2.19.tar.gz [root@haproxykeepalived-3 ~]# cd keepalived-1.2.19 [root@haproxykeepalived-3 ~]# ./configure --prefix=/usr/local/keepalived --sbindir=/usr/sbin/ --sysconfdir=/etc/ --mandir=/usr/local/share/man/ --with-kernel-dir=/usr/src/kernels/2.6.32-504.el6.x86_64/ [root@haproxykeepalived-3 ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak [root@haproxykeepalived-3 ~]# cat /etc/keepalived/keepalived.conf #192.168.1.180是VIP地址 [root@haproxykeepalived-3 ~]# cat /etc/keepalived/keepalived.conf #Master节点的priority值比backup节点的高 ! Configuration File for keepalived global_defs { notificationd LVS_DEVEL } vrrp_script chk_haproxy { script "/etc/keepalived/chk.sh" interval 2 } vrrp_instance VI_1 { state MASTER nopreempt interface ens192 virtual_router_id 10 priority 130 advert_int 1 authentication { auth_type PASS auth_pass asd } virtual_ipaddress { 192.168.1.180 } track_script { chk_haproxy } notify_backup "/etc/init.d/haproxy restart" notify_fault "/etc/init.d/haproxy stop" } [root@haproxykeepalived-4 ~]# cat /etc/keepalived/keepalived.conf #backup节点 ! Configuration File for keepalived global_defs { notificationd LVS_DEVEL } vrrp_script chk_haproxy { script "/etc/keepalived/chk.sh" interval 2 } vrrp_instance VI_1 { state BACKUP nopreempt interface ens192 virtual_router_id 10 priority 120 advert_int 1 authentication { auth_type PASS auth_pass asd } virtual_ipaddress { 192.168.1.180 } track_script { chk_haproxy } notify_backup "/etc/init.d/haproxy restart" notify_fault "/etc/init.d/haproxy stop" }
创建脚本文件 [root@haproxykeepalived-3 ~]# vi /etc/keepalived/chk.sh #!/bin/bash # if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then /etc/init.d/keepalived stop fi
授予脚本执行权限 [root@haproxykeepalived-3 ~]# chmod +x /etc/keepalived/chk.sh
启动keepalived服务 [root@haproxykeepalived-3 ~]# systemctl start keepalived [root@haproxykeepalived-3 ~]# systemctl enable keepalived
原创文章,作者:admin,如若转载,请注明出处:https://www.starz.top/2021/01/11/blog-db%e9%ab%98%e5%8f%af%e7%94%a8%e5%ae%9e%e6%96%bd/