LVS+Keepalived負載均衡配置部署
相關概念
一、準備工作
1. 伺服器、作業系統(當前CentOS6.6 x86_64)
、網路環境(服務期間網路互通)
;
# 作業系統:CentOS6.6 x86_64
# 伺服器 (LVS主備各1臺,WEB伺服器3臺,圖片伺服器2臺)
WEBServer負載虛擬IP:192.168.0.20
IMGServer負載虛擬IP:192.168.0.30
LVS主:192.168.0.11
LVS備:192.168.0.12
WebServer1: 192.168.0.21
WebServer2: 192.168.0.22
WebServer3: 192.168.0.23
IMGServer1: 192.168.0.31
IMGServer2: 192.168.0.32
2. 關閉SELinux(改完需重啟伺服器)
;
# 關閉SELinux
sed -i 's#^SELINUX=.*#SELINUX=disabled#' /etc/sysconfig/selinux
# 重啟伺服器
reboot
3. Iptables開啟需要埠,如80;
# Iptables 開啟相應埠
iptables -A INPUT -p tcp -s 0/0 --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
二、LVS主機安裝Keepalived,ipvsadm
1. 安裝依賴包
yum -y install wget popt popt-devel popt-static libnl libnl-devel kernel-devel;
ln -s /usr/src/kernels/$(uname -r)/ /usr/src/linux;
2. 安裝 Keepalived,ipvsadm(YUM安裝或TarBall安裝)
# YUM安裝 Keepalived,ipvsadm
yum -y install keepalived ipvsadm;
# TarBall安裝Keepalived
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz;
tar xvf keepalived-1.2.15.tar.gz -C /usr/local/src/;
cd /usr/local/src/keepalived-1.2.15/;
./configure \
--bindir=/usr/bin \
--sbindir=/usr/sbin \
--sysconf=/etc \
--with-kernel-dir=/usr/src/kernels/$(uname -r)/;
make;
make install;
# TarBall安裝ipvsadm
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz;
tar xvf ipvsadm-1.26.tar.gz -C /usr/local/src/;
cd /usr/local/src/ipvsadm-1.26/;
make;
make install;
3. 建立ipvsadm配置檔案,啟動並加入開機啟動
/etc/init.d/ipvsadm save;
/etc/init.d/ipvsadm start;
chkconfig ipvsadm on;
4. 配置並啟動keepalived
# 備份keepalived配置檔案
cd /etc/keepalived/;
[ -f "keepalived.conf" -a ! -f "keepalived.conf.default" ] && mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.default;
[ -f "keepalived.conf" ] && mv keepalived.conf keepalived.conf.$(date +%F_%T);
# 建立 Keepalived 配置檔案(需先修改 virtual_ipaddress,virtual_server,real_server 配置的IP)
cat >> /etc/keepalived/keepalived.conf <<KEEPALIVED
! Configuration File for keepalived
global_defs {
notification_email {
mr@mruse.cn
}
notification_email_from xxx@163.com
smtp_server smtp@163.com
smtp_connect_timeout 30
router_id LVS_1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 60
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass MrUse
}
virtual_ipaddress {
192.168.0.20
192.168.0.30
}
}
virtual_server 192.168.0.21 80 {
delay_loop 3
lb_algo rr
lb_kind DR
persistence_timeout 3
protocol TCP
real_server 192.168.0.22 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.0.23 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
virtual_server 172.16.100.130 80 {
delay_loop 3
lb_algo rr
lb_kind DR
persistence_timeout 3
protocol TCP
real_server 192.168.0.31 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.0.32 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
KEEPALIVED
# 啟動Keepalived並加入開機啟動
/etc/init.d/keepalived restart;
chkconfig keepalived on;
5. 記錄Keepalived日誌
# 註釋Keepalived預設配置
grep ^KEEPALIVED_OPTIONS /etc/sysconfig/keepalived && sed -i 's#^KEEPALIVED_OPTIONS#\#KEEPALIVED_OPTIONS#g' /etc/sysconfig/keepalived;
# 更新keepalived配置
echo 'KEEPALIVED_OPTIONS="-D -d -S 0"' >> /etc/sysconfig/keepalived;
/etc/init.d/keepalived restart
grep "# CmsTop Keepalived" /etc/rsyslog.conf||cat >> /etc/rsyslog.conf <<SYSLOG
# CmsTop Keepalived $(date +%F)
# keepalived -D -d -S 0
local0.* /var/log/keepalived.log
SYSLOG
/etc/init.d/rsyslog restart
6. 為真實伺服器 WebServer, IMGServe建立配置啟動指令碼(需修改IP地址)
[ -f "/etc/init.d/lvsr" ] && mv /etc/init.d/lvsr /etc/init.d/lvsr.$(date +%F_%T)
cat >> /etc/init.d/lvsr <<LVSR
#!/bin/bash
SNS_VIP=172.16.100.120
/etc/rc.d/init.d/functions
start()
{
/sbin/ifconfig lo:0 \$SNS_VIP netmask 255.255.255.255 broadcast \$SNS_VIP up
/sbin/route add -host \$SNS_VIP 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 2>&1
echo "LVS RealServer Start OK"
}
stop()
{
/sbin/ifconfig lo:0 down
/sbin/route del \$SNS_VIP >/dev/null 2>&1
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
echo "LVS RealServer Stoped"
}
case "\$1" in
start)
start;
;;
stop)
stop;
;;
restart)
stop;
start;
;;
status)
/sbin/ifconfig|tail -5
;;
*)
echo "Usage: \$0 {start|stop|restart|status}"
exit 1
esac
exit 0
LVSR
# 修改依賴檔案及啟動指令碼許可權並啟動
chmod +x /etc/init.d/lvsr;
chmod +x /etc/rc.d/init.d/functions;
/etc/init.d/lvsr start
# 加入開機啟動
grep ^/etc/init.d/lvsr /etc/rc.local || echo '/etc/init.d/lvsr start' >> /etc/rc.local
三、檢視狀態、測試排錯
1. 重啟Keepalived,在LVS伺服器上檢視狀態
/etc/init.d/keepalived restart;
/etc/init.d/ipvsadm status;
2. 測試排查思路
# 檢視配置檔案,檢視ip
# 檢視Iptables是否開啟相應埠
# 檢視網路,伺服器間及與虛擬IP是否可以通訊
# 檢視Keepalived,ipvsadm是否正常啟動
# 檢視錯誤日誌
相關文章
- LVS+KEEPALIVED負載均衡實驗負載
- lvs+keepAlived→效率最高的負載均衡負載
- LVS+Keepalived 實現高可用負載均衡負載
- 基於MySQL雙主複製架構下部署LVS+KeepAlived負載均衡MySql架構負載
- LVS負載均衡群集概念、NAT模式LVS負載均衡實戰部署負載模式
- Nginx+Tomcat部署負載均衡NginxTomcat負載
- nginx配置+uwsgi+負載均衡配置Nginx負載
- Nginx/Httpd負載均衡tomcat配置Nginxhttpd負載Tomcat
- 使用Nginx配置TCP負載均衡NginxTCP負載
- IdentityServer4 負載均衡配置IDEServer負載
- Kuberntes部署MetalLB負載均衡器負載
- 負載均衡是什麼?怎麼理解負載均衡的部署方式和工作原理負載
- nginx安裝及負載均衡配置Nginx負載
- 做了反向代理和負載均衡的nginx配置檔案簡單示例(nginx.conf) HTTP負載均衡/TCP負載均衡負載NginxHTTPTCP
- LVS負載均衡配置與keepalive服務配置負載
- gRPC負載均衡(自定義負載均衡策略)RPC負載
- gRPC負載均衡(客戶端負載均衡)RPC負載客戶端
- 負載均衡負載
- Ribbon負載均衡策略與自定義配置負載
- nginx部署基於http負載均衡器NginxHTTP負載
- Nginx反向代理負載均衡的容器化部署Nginx負載
- docker下nginx反向代理和負載均衡配置DockerNginx負載
- RHEL 7配置HAProxy實現Web負載均衡Web負載
- Nginx 兩臺伺服器配置負載均衡!!!Nginx伺服器負載
- nginx配置web服務|反向代理|負載均衡NginxWeb負載
- 阿里雲負載均衡SSL證書配置(更新)阿里負載
- IP負載均衡負載
- WebSocket負載均衡Web負載
- nginx負載均衡Nginx負載
- NGINX 負載均衡Nginx負載
- 【Nginx】負載均衡Nginx負載
- 負載均衡---ribbon負載
- LoadBalancer負載均衡負載
- LVS 負載均衡負載
- 負載均衡技術(一)———負載均衡技術介紹負載
- 解密負載均衡技術和負載均衡演算法解密負載演算法
- 負載均衡技術(二)———常用負載均衡服務介紹負載
- 【知識分享】四層負載均衡和七層負載均衡負載
- docker初體驗:docker部署nginx負載均衡叢集DockerNginx負載