搭建keepalived服务

搭建keepalived+httpd服务

master节点操作:

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

setenforce 0

systemctl stop firewalld.service && systemctl disable firewalld.service && iptables -F &&setenforce 0

yum -y install keepalived ipvsadm

vim /etc/keepalived/keepalived.conf
global_defs {
 notification_email {
        root@localhost       
 }
 notification_email_from root@localhost
 smtp_server localhost
 smtp_connect_timeout 30
 router_id dns-master      					##主机名字
}
vrrp_instance apache {
 state MASTER            					##MASTER不是主节点就设BACKUP
 interface ens32           					##网卡名字
 virtual_router_id 51
 priority 100								##权重备用节点90
 advert_int 1
 authentication {        
        auth_type PASS
        auth_pass 1111
 }
 virtual_ipaddress {
        192.168.10.120           
 }
}
virtual_server 192.168.10.120 80 {     
 delay_loop 6
 lb_algo rr
 lb_kind DR
 nat_mask 255.255.255.0
 persistence_timeout 50
 protocol TCP
 real_server 192.168.10.30 80 {      
        weight 1
        TCP_CHECK {
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
                connect_port 80
 }
 }
 real_server 192.168.10.40 80 {
        weight 1
        TCP_CHECK {
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
                connect_port 80
 }
 }
}

backup节点操作

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

setenforce 0

systemctl stop firewalld.service && systemctl disable firewalld.service && iptables -F &&setenforce 0

yum -y install keepalived ipvsadm

vim /etc/keepalived/keepalived.conf
global_defs {
 notification_email {
        root@localhost       
 }
 notification_email_from root@localhost
 smtp_server localhost
 smtp_connect_timeout 30
 router_id dns-master      					##主机名字
}
vrrp_instance apache {
 state BACKUP            					##MASTER不是主节点就设 BACKUP
 interface ens32           					##网卡名字
 virtual_router_id 51
 priority 90								##权重备用节点90
 advert_int 1
 authentication {        
        auth_type PASS
        auth_pass 1111
 }
 virtual_ipaddress {
        192.168.10.120           
 }
}
virtual_server 192.168.10.120 80 {     
 delay_loop 6
 lb_algo rr
 lb_kind DR
 nat_mask 255.255.255.0
 persistence_timeout 50
 protocol TCP
 real_server 192.168.10.30 80 {      
        weight 1
        TCP_CHECK {
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
                connect_port 80
 }
 }
 real_server 192.168.10.40 80 {
        weight 1
        TCP_CHECK {
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
                connect_port 80
 }
 }
}


httpd负载均衡节点1

yum -y install httpd net-tools

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

setenforce 0

echo "第一个httpd" >> /var/www/html/index.html


vim /etc/init.d/lvs
echo "/etc/init.d/lvs start" >> /etc/rc.local

vim /etc/init.d/lvs
#!/bin/bash
# 
# Script to start LVS DR real server.
# description: LVS DR real server
# .  /etc/rc.d/init.d/functions

VIP=192.168.10.120
host=`/bin/hostname`
 case "$1" in
start) 
       # Start LVS-DR real server on this machine.
        /sbin/ifconfig lo down
        /sbin/ifconfig lo up
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev lo:0
;; 
stop) 
        # Stop LVS-DR real server loopback device(s).
        /sbin/ifconfig lo:0 down
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;; 
status) 

        # Status of LVS-DR real server.
        islothere=`/sbin/ifconfig lo:0 | grep $VIP`
        isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
        if [ ! "$islothere" -o ! "isrothere" ];then
            # Either the route or the lo:0 device
            # not found.            
    		echo "LVS-DR real server Stopped."
        else
            echo "LVS-DR real server Running."
        fi
;; 
*) 
            # Invalid entry.
            echo "$0: Usage: $0 {start|status|stop}"
            exit 1
;; esac

chmod a+x /etc/init.d/lvs

httpd负载均衡节点2

yum -y install httpd net-tools

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

setenforce 0

echo "第二个httpd" >> /var/www/html/index.html

vim /etc/init.d/lvs
echo "/etc/init.d/lvs start" >> /etc/rc.local

vim /etc/init.d/lvs
#!/bin/bash
# 
# Script to start LVS DR real server.
# description: LVS DR real server
# .  /etc/rc.d/init.d/functions

VIP=192.168.10.120
host=`/bin/hostname`
 case "$1" in
start) 
       # Start LVS-DR real server on this machine.
        /sbin/ifconfig lo down
        /sbin/ifconfig lo up
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev lo:0
;; 
stop) 
        # Stop LVS-DR real server loopback device(s).
        /sbin/ifconfig lo:0 down
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;; 
status) 

        # Status of LVS-DR real server.
        islothere=`/sbin/ifconfig lo:0 | grep $VIP`
        isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
        if [ ! "$islothere" -o ! "isrothere" ];then
            # Either the route or the lo:0 device
            # not found.            
    		echo "LVS-DR real server Stopped."
        else
            echo "LVS-DR real server Running."
        fi
;; 
*) 
            # Invalid entry.
            echo "$0: Usage: $0 {start|status|stop}"
            exit 1
;; esac

chmod a+x /etc/init.d/lvs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

殇彧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值