1. 程式人生 > >LVS+keepalived+httpd高可用叢集

LVS+keepalived+httpd高可用叢集

環境:
主keepalived伺服器:192.168.80.100
從keepalived伺服器: 192.168.80.101

Web伺服器:192.168.80.102
192.168.80.103


在80.100虛擬機器上
systemctl stop firewalld //關閉防火牆
setenforce 0 //關閉監控
yum install lrz* -y //安裝上傳軟體
tar xf keepalived-1.4.2.tar.gz -C/opt/ //解壓keepalived-1.4.2.tar.gz檔案到/opt/
cd /opt/ keepalived-1.4.2/
yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
./configure --prefix=/
make && make install
cd /etc/
ls | grep keepalived
LVS+keepalived+httpd高可用叢集

cd keepalived/
vi keepalived.conf
把裡面內容全刪掉換成

! Configuration File for keepalived
global_defs {
router_id LVS_01 //本伺服器的名稱
}
vrrp_instance VI_1 { //定義VRRP熱備例項
state MASTER //熱備狀態,MASTER表示主伺服器,BACKUP表示從伺服器
interface ens32 //承載VIP地址的物理介面
virtual_router_id 51 //虛擬路由器的ID號,每個熱備組保持一致
priority 150 //優先順序,數值越大優先順序越高
advert_int 1 //通告間隔秒數(心跳頻率)
authentication { //熱備認證資訊,每個熱備組保持一致
auth_type PASS //認證型別
auth_pass 6666 //密碼字串
}
virtual_ipaddress { //指定漂移地址(VIP),可以有多個
192.168.80.188
}
}
virtual_server 192.168.80.188 80 { //虛擬伺服器地址(VIP)、埠
delay_loop 6 //健康檢查的間隔時間(秒)
lb_algo rr //輪詢(rr)排程演算法
lb_kind DR //直接路由(DR)群集工作模式
persistence_timeout 60 //連線保持時間(秒)
protocol TCP //應用伺服器採用的是TCP協議
real_server 192.168.80.102 80 { //第一個Web伺服器節點的地址、埠
weight 1 //節點的權重
TCP_CHECK { //健康檢查方式
connect_port 80 //檢查的目標埠
connect_timeout 3 //連線超時(秒)
nb_get_retry 3 //重試次數
delay_before_retry 3 //重試間隔
}
}
real_server 192.168.80.103 80 { //第二個Web伺服器節點的地址、埠
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
:wq
service keepalived start
systemctl status keepalived
LVS+keepalived+httpd高可用叢集


ip add show ens32
LVS+keepalived+httpd高可用叢集
modprobe ip_vs
yum install ipvsadm -y
service keepalived restart
ipvsadm -Ln
LVS+keepalived+httpd高可用叢集


在80.101虛擬機器上
systemctl stop firewalld //關閉防火牆
setenforce 0 //關閉監控
yum install lrz* -y //安裝上傳軟體
tar xf keepalived-1.4.2.tar.gz -C/opt/ //解壓keepalived-1.4.2.tar.gz檔案到/opt/
cd /opt/ keepalived-1.4.2/
yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
./configure --prefix=/
make && make install
modprobe ip_vs
yum install ipvsadm -y
vi /etc/keepalived/keepalived.conf
刪掉裡面的所有內容新增以下內容

! Configuration File for keepalived
global_defs {
router_id LVS_02
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 105
advert_int 1
authentication {
auth_type PASS
auth_pass 6666
}
virtual_ipaddress {
192.168.80.188
}
}
virtual_server 192.168.80.188 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.80.102 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.80.103 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
:wq
service keepalived start
service keepalived status
LVS+keepalived+httpd高可用叢集


在80.102虛擬機器上
systemctl stop firewalld //關閉防火牆
setenforce 0 //關閉監控
yum install httpd -y
vi /etc/httpd/conf/httpd.conf
找到#ServerName www.example.com:80吧#去掉
LVS+keepalived+httpd高可用叢集
cd /var/www/html/
echo "<h1>SERVER AA</h1>" > index.html
systemctl start httpd
./web.sh
ifconfig
LVS+keepalived+httpd高可用叢集


在80.103虛擬機器上
systemctl stop firewalld //關閉防火牆
setenforce 0 //關閉監控
yum install httpd -y
vi /etc/httpd/conf/httpd.conf
找到#ServerName www.example.com:80吧#去掉
LVS+keepalived+httpd高可用叢集
cd /var/www/html/
echo "<h1>SERVER BB</h1>" > index.html
systemctl start httpd
vi web.sh
新增以下內容

#!/bin/bash
#haha
ifconfig lo:0 192.168.80.188 broadcast 192.168.80.188 netmask 255.255.255.255 up
route add -host 192.168.80.188 dev lo:0
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
sysctl -p &>/dev/null
:wq
chmod +x web.sh
./web.sh
ifconfig
LVS+keepalived+httpd高可用叢集
scp web.sh [email protected]:/root/
yes
自己設的root密碼

效果網頁輸入192.168.80.188
LVS+keepalived+httpd高可用叢集
LVS+keepalived+httpd高可用叢集