keepalived高可用

keepalived高可用

主服务器配置

//关闭防火墙和selinux
[root@master ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 
[root@master ~]# setenforce 0

//安装keepalived
[root@master ~]# yum -y install keepalived

[root@master ~]# rpm -ql keepalived 
/etc/keepalived
/etc/keepalived/keepalived.conf		//配置文件
/etc/sysconfig/keepalived
/usr/bin/genhash
/usr/lib/systemd/system/keepalived.service	//服务控制文件
/usr/libexec/keepalived
/usr/sbin/keepalived

备服务器配置

//关闭防火墙和selinux
[root@slave ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@slave ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@slave ~]# setenforce 0

//安装keepalived
[root@slave ~]# yum -y install keepalived

主备服务器上安装httpd

主服务器

//安装httpd,创建测试页面
[root@master ~]# yum -y install httpd
[root@master ~]# echo 'master' > /var/www/html/index.html
[root@master ~]# systemctl start httpd
[root@master ~]# ss -anltu
Netid      State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      
tcp        LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*         
tcp        LISTEN      0           128                          *:80                        *:*         
tcp        LISTEN      0           128                       [::]:22                     [::]:*         

keepalived高可用

备服务器

//安装httpd,创建测试页面
[root@slave ~]# yum -y install httpd
[root@slave ~]# echo 'slave' > /var/www/html/index.html
[root@slave ~]# systemctl start httpd
[root@slave ~]# ss -anltu
Netid      State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      
tcp        LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*         
tcp        LISTEN      0           128                          *:80                        *:*         
tcp        LISTEN      0           128                       [::]:22                     [::]:*         

keepalived高可用

keepalived配置

主服务器配置

[root@master ~]# cd /etc/keepalived/
[root@master keepalived]# ls
keepalived.conf
[root@master keepalived]# cp keepalived.conf{,-bak}	//备份原文件
[root@master keepalived]# ls
keepalived.conf  keepalived.conf-bak
[root@master keepalived]# rm -rf keepalived.conf
[root@master keepalived]# vim keepalived.conf
[root@master keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lb01
}

vrrp_instance VI_1 {
    state MASTER
    interface ens160		//网卡名
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass wys123	//密码
    }
    virtual_ipaddress {
        192.168.237.250		//VIP
    }
}

virtual_server 192.168.237.250 80 {		//VIP加端口号
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.237.167 80 {	//主服务器IP
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.237.170 80 {	//备服务器IP
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

//启动keepalived
[root@master keepalived]# systemctl enable --now keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
上一篇:会说话的HTML--语义化杂谭-TGideas-腾讯游戏官方设计团队


下一篇:开启CSP网页安全政策防止XSS攻击