keepalived(三)LVS+Keepalived
實驗
內容
keepalived+lvs實現後端web服務的負載均衡與高可用
架構圖
環境
ip | 系統 | 軟體 | 角色 |
---|---|---|---|
192.168.253.128 | CentOS7 | keepalived | keepalived的MASTER節點 |
192.168.253.158 | CentOS7 | keepalived | keepalived的BACKUP節點 |
192.168.253.145 | CentOS7 | nginx | 後端web伺服器 |
192.168.253.129 | CentOS6 | nginx | 後端web伺服器 |
第一步:192.168.253.145端安裝nginx
systemctl stop firewalld
setenforce 0
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo如果已經有epel源不需要執行
yum install nginx -y #nginx需要epel源才可以yum安裝
第二步:192.168.253.145上建立nginx起始頁,啟動nginx
echo "192.168.253.145" > /usr/share/nginx/html/index.html
systemctl start nginx
curl 192.168.253.145 測試一下
第三步:192.168.253.129上安裝nginx
/etc/init.d/iptables stop
setenforce 0
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo 已經有epel源不需要執行
yum install nginx -y
第四步:192.168.253.129上建立nginx起始頁,啟動nginx
echo "192.168.253.129" > /usr/share/nginx/html/index.html
/etc/init.d/nginx start nginx
curl 192.168.253.129測試一下
第五步:在192.168.253.129和192.168.253.145建立一個指令碼用於修改核心引數,阻止arp通告和響應,以及在本地環回介面建立vip192.168.253.190/32
[root@localhost ~]# cat RS.sh
#!/bin/bash
#
vip=192.168.253.190 ##vip
mask='255.255.255.255' ##掩碼
case $1 in
start)
##阻止arp通告與響應
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:0 $vip netmask $mask broadcast $vip up #設定vip
route add -host $vip dev lo:0 #增加主機路由
;;
stop)
ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
*)
echo "Usage $(basename $0) start|stop"
exit 1
;;
esac
在兩臺機上都執行
sh RS.sh start #啟動指令碼
第六步:192.168.253.128端安裝keepalived並修改/etc/keepalived/keepalived.conf配置檔案
systemctl stop firewalld
setenforce 0
yum install keepalived -y
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id 192.168.253.128
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 15
priority 95
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.253.190/32 dev ens33
}
}
virtual_server 192.168.253.190 80 {
delay_loop 1
lb_algo rr
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.253.145 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
nb_get_retry 3
delay_before_retry 1
connect_timeout 1
}
}
real_server 192.168.253.129 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
nb_get_retry 3
delay_before_retry 1
connect_timeout 1
}
}
}
第七步:192.168.253.158端安裝keepalived並修改/etc/keepalived/keepalived.conf配置檔案
systemctl stop firewalld
setenforce 0
yum install keepalived -y
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id 192.168.253.158
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 15
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.253.190/32 dev ens33
}
}
virtual_server 192.168.253.190 80 {
delay_loop 1
lb_algo rr
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.253.145 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
nb_get_retry 3
delay_before_retry 1
connect_timeout 1
}
}
real_server 192.168.253.129 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
nb_get_retry 3
delay_before_retry 1
connect_timeout 1
}
}
}
第八步:測試
啟動2臺keepalived
systemctl start keepalived
可以看到ipvs規則已經產生了。以及vip在192.168.253.128上
然後我們curl一下192.168.253.190
我們使用的排程方法是輪詢這裡一直是192.168.253.128
解決方法:將配置檔案/etc/keepalived/keepalived.conf的persistence_timeout 1這一項刪掉,這一項是表示持久連結
然後重啟2個keepalived
systemctl restart keepalived
lvs測試沒問題。這時候試試高可用,我們把192.168.253.128這個主機的keepalived down掉
systemctl stop keepalived
這時候再繼續訪問192.168.253.190
同時也可以看到192.168.253.190這個vip飄到192.168.253.158這個BACKUP節點上了。ipvsadm的規則也新增完成。
相關文章
- LVS+Keepalived群集
- LVS+keepalived高可用
- LVS+Keepalived高可用群集
- LVS+KEEPALIVED負載均衡實驗負載
- lvs+keepAlived→效率最高的負載均衡負載
- LVS+Keepalived 實現高可用負載均衡負載
- LVS+Keepalived 高可用群集(理論+實戰部署)
- 基於MySQL雙主複製架構下部署LVS+KeepAlived負載均衡MySql架構負載
- keepalived(四)Keepalived+NginxNginx
- keepalived
- Keepalived - Keepalived 實現 tomcat雙機熱備Tomcat
- Keepalived 高可用
- 快速掌握keepalived
- openGauss+KeepAlived
- keepalived安裝
- LVS搭配Keepalived
- keepalived 安裝部署
- keepalived 主備使用
- KeepAlived+LVS+NginxNginx
- keepalived配置問題
- keepalived(二)vip漂移
- mysql高可用之keepalivedMySql
- Keepalived 高可用詳解
- 高可用(keepalived)部署方案
- 負載均衡之keepalived負載
- Keepalived 原理與實戰
- 高併發和 keepalived
- RabbitMQ+haProxy+keepalived使用MQ
- Keepalived服務詳解
- nginx+keepalived高可用Nginx
- Keepalived高可用叢集部署
- keepalived + nginx 實現高可用Nginx
- keepalived高可用負載均衡負載
- nginx實現keepalived高可用Nginx
- Keepalived之高可用LVS叢集
- Keepalived部署與配置詳解
- mysql高可用衡搭建(Keepalived)MySql
- kube-apiserver 高可用,keepalived + haproxyNXAPIServer