企業級負載均衡--LVS+KeepAlived

調度器的備份

集羣(不再是單純的調度器)

當調度器掛了,客戶就徹底不能訪問了,這時我們可以設置調度器的高可用

時刻保證調度器的正常工作:設置調度器master(主)和調度器backup(備)

我們在兩臺調度器上使用keepalived服務就可以將兩個調度器關聯在一起,keepalived檢測兩個調度器的工作情況

我們使用keeplived代理LVS,它具有LVS的功能,但同時可以有多個,有主有備

當主調度器掛了備用調度器就去做輪詢,當主調度器好了備用調度器就把任務還給主調度器

當主調度器正常的時候,keepalived通知備份調度器自己可以正常工作,不需要接管自己的任務

當主調度器掛了,備份調度器會自動去接管主調度器的工作

這個時候客戶端依然可以正常訪問,在接管的時候,訪問不受影響

搭建DR模式環境

lVS主服務器(server1): 192.168.1.1/24 192.168.1.100/24
修改server1的ip地址,並且添加虛擬ip地址
server2: 192.168.1.2/24 192.168.1.100/32
修改server2的ip地址,並且添加虛擬ip地址
server3: 192.168.1.3/24 192.168.1.100/32
修改server3的ip地址,並且添加虛擬ip地址
lVS備服務器(server2): 192.168.1.4/24 192.168.1.100/24
systemctl stop firewalld
關閉 firewall
setenforce 0
關閉 SELinux

server1

yum install -y ipvsadm

安裝 ipvsadm
vim /etc/sysconfig/ipvsadm-config
修改LVS服務的配置文件:no->yes
重啓服務的時候保存策略
systemctl start ipvsadm
首次開啓會報錯,無法開啓服務
cat /var/log/messages
在日誌裏面查看服務啓動錯誤的原因
touch /etc/sysconfig/ipvsadm
建立編寫策略的文件
systemctl start ipvsadm
再次開啓服務,正常
systemctl enable ipvsadm
開啓自啓

設置DR模式的訪問策略

ipvsadm -A -t 192.168.1.100:80 -s rr

添加vip

-A:添加虛擬服務器

-t :指定vip及tcp端口

-s:指定算法

rr:輪詢

ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.2:80 -g -w 1

ipvsadm -a -t 1192.168.1.100:80 -r 192.168.1.3:80 -g -w 1

-a :添加節點

-t :指定vip和端口

-r :指定節點ip及端口

-m:表示使用NAT模式

-w:設置權重

ipvsadm -ln

查看當前節點信息

systemctl restart ipvsadm

重啓服務

server2

yum install -y http
安裝 http
systemctl start httpd
開啓
systemctl enable httpd
開機自啓
vim /var/www/html/index.html
建立測試頁面
server2.example.com
測試頁面內容
yum install -y arptables
安裝 arptables
arptables -nL
查看策略

設置arptables的訪問策略

arptables -A INPUT -d 192.168.1.100 -j DROP
192.168.1.100來源輸入全部丟棄
arptables -A OUTPUT -s 192.168.1.100 -j mangle --mangle-ip-s 192.168.1.2
192.168.1.100輸出轉化爲192.168.1.2
cat /etc/sysconfig/arptables
查看規則
arptables-save > /etc/sysconfig/arptables
保存策略
cat /etc/sysconfig/arptables
再次查看,策略已保存
systemctl restart arptables
重啓服務

server3

yum install -y http
安裝 http
systemctl start httpd
開啓
systemctl enable httpd
開機自啓
vim /var/www/html/index.html
建立測試頁面
server3.example.com
測試頁面內容

yum install -y arptables
安裝 arptables
arptables -nL
查看策略

設置arptables的訪問策略

arptables -A INPUT -d 192.168.1.100 -j DROP
192.168.1.100來源輸入全部丟棄
arptables -A OUTPUT -s 192.168.1.100 -j mangle --mangle-ip-s 192.168.1.3
192.168.1.100輸出轉化爲192.168.1.3
cat /etc/sysconfig/arptables
查看規則
arptables-save > /etc/sysconfig/arptables
保存策略
cat /etc/sysconfig/arptables
再次查看,策略已保存
systemctl restart arptables
重啓服務

server4

yum install -y ipvsadm

安裝 ipvsadm
vim /etc/sysconfig/ipvsadm-config
修改LVS服務的配置文件:no->yes
重啓服務的時候保存策略
systemctl start ipvsadm
首次開啓會報錯,無法開啓服務
cat /var/log/messages
在日誌裏面查看服務啓動錯誤的原因
touch /etc/sysconfig/ipvsadm
建立編寫策略的文件
systemctl start ipvsadm
再次開啓服務,正常
systemctl enable ipvsadm
開啓自啓

設置DR模式的訪問策略

ipvsadm -A -t 192.168.1.100:80 -s rr

添加vip

-A:添加虛擬服務器

-t :指定vip及tcp端口

-s:指定算法

rr:輪詢

ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.2:80 -g -w 1

ipvsadm -a -t 1192.168.1.100:80 -r 192.168.1.3:80 -g -w 1

-a :添加節點

-t :指定vip和端口

-r :指定節點ip及端口

-m:表示使用NAT模式

-w:設置權重

ipvsadm -ln

查看當前節點信息

systemctl restart ipvsadm

重啓服務

操作流程

server1

tar -zxvf keepalived-*.tar.gz

解壓

cd / keepalived-*

yum install -y gcc openssl-devel

需要源碼編譯

./configure --prefix=/usr/local/keepalived --with-init=systemd

編譯

make && make install

安裝

ln -s /usr/local/keepalived/etc/keepalived /etc

配置文件做軟鏈接

vim /etc/keepalived/keepalived.conf

編輯配置文件

global_defs {
       notification_email {
       	     root@localhost
    }
       notification_email_from  keepalived@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id LVS_DEVEL
       vrrp_skip_check_adv_addr
       #vrrp_strict--註釋,打開無法正常開啓服務
       vrrp_garp_interbval 0
       vrrp_gna_interval 0
vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id 30
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.1.100
        }
}  
        virtual_ipaddress {
          192.168.1.100
        }
}  
virtual_server 192.168.1.100 80 {
        delay_loop 3
        lb_algo rr
        lb_kind DR
        persistence_timeout 50
        protocol TCP
    
real_server 192.168.1.2 80 {
         TCP_CHECK {
           weight 1
                connect_timeout 3
                retry 3
                delay_before_retry 3   
        }
  }
    
real_server 192.168.1.100 80 {
          TCP_CHECK {
            weight 1
                connect_timeout 3
                retry 3
                delay_before_retry 3
        }
  }
}

ip addr del 192.168.1.100/24 dev eth0
systemctl restart keepalived
重啓

server4

tar -zxvf keepalived-*.tar.gz

解壓

cd / keepalived-*

yum install -y gcc openssl-devel

需要源碼編譯

./configure --prefix=/usr/local/keepalived --with-init=systemd

編譯

make && make install

安裝

ln -s /usr/local/keepalived/etc/keepalived /etc

配置文件做軟鏈接

server1

scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived
配置文件傳輸給 server4
vim /etc/keepalived/keepalived.conf

 vrrp_instance VI_1 {
            state BACKUP        ---備份
            interface eth0          ---網卡
            virtual_router_id 30 ---序號
            priority 50                ---權重
            advert_int 1      
            authentication {
                auth_type PASS
                auth_pass 1111
            }
  }

ip addr del 192.168.1.100/24 dev eth0
systemctl restart keepalived

重啓

測試
情況一:

server1(主)與server2(備)同時開啓 keepalived 服務

curl 192.168.1.100
訪問192.168.1.100可以看到 server2 與 server3 輪流響應請求
server1
ifconfig
eth0 網卡添加 vip 192.168.1.100/24
server2
ifconfig
eth0 網卡沒有 vip 192.168.1.100/24 

情況二:

server1(備)開啓 keepalived 服務,server1(主) 關閉 keepalived 服務

server1
systemctl stop keepalived

curl 192.168.1.100
訪問192.168.1.100可以看到 server2 與 server3 輪流響應請求(切換至備用服務器)
server1
ifconfig
eth0 網卡刪除 vip 192.168.1.100/24
server2
ifconfig
eth0 網卡添加 vip 192.168.1.100/24

情況三:

server2(備)開啓 keepalived 服務,server1(主) 再次開啓 keepalived 服務

server1
systemctl restart keepalived

curl 192.168.1.100
訪問192.168.1.100可以看到 server2 與 server3 輪流響應請求(切換回主要服務器)
server1
ifconfig
eth0 網卡添加 vip 192.168.1.100/24
server2
ifconfig
eth0 網卡刪除 vip 192.168.1.100/24
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章