Haproxy+Keepalived高可用負載均衡叢集
Haproxy+Keepalived
結構
配置 | |
---|---|
排程器 | 安裝並配置Haproxy、Keepalived,haproxy1的優先順序高於haproxy2 |
web叢集 | 編譯安裝nginx、並將儲存伺服器提供的共享檔案掛載到網站根目錄 |
儲存伺服器 | 安裝nfs-utils、rpcbind;共享檔案 |
裝置 | 安裝軟體 | 作用 |
---|---|---|
haproxy1 | Haproxy、Keepalived | 主排程器、接待使用者 |
haproxy2 | Haproxy、Keepalived | 備用排程器、當主失效後、代替主 |
nginx1 | nginx | 為使用者提供服務 |
nginx2 | nginx | 為使用者提供服務 |
nfs | nfs-utils、rpcbind | 為web叢集提供儲存服務 |
NFS
1.需安裝nfs-utils、rpcbind軟體包
yum -y install nfs-utils rpcbind
2.設定共享目錄
(為了便於測試,所以設定兩個不同的目錄區分)
mkdir /opt/nginx1
mkdir /opt/nginx2
echo "<html><title>nginx1</title><body><h1>This is nginx1</h1></body></html>" >> /opt/nginx1/index.html
echo "<html><title>nginx2</title><body><h1>This is nginx2</h1></body></html>" >> /opt/nginx2/index.html
3.編輯配置檔案,共享目錄
vi /etc/exports
/opt/nginx1 192.168.2.16(ro)
/opt/nginx2 192.168.2.17(ro)
重啟
systemctl restart nfs
檢視本機發布的NFS共享目錄
showmount -e
Web叢集
使用xftp上傳軟體包
yum 安裝依賴包
yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel
建立一個不可登陸賬戶給ngxin使用
useradd -M -s /sbin/nologin nginx
解壓
tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2/
配置內容
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
編譯安裝
make && make install
建立nginx軟連線
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
nginx配置檔案軟連線
ln -s /usr/local/nginx/conf/nginx.conf /etc/
檢查nginx是都可以正常執行
nginx -t
檢視NFS伺服器共享的目錄
showmount -e 192.168.2.18
將目錄掛載到網站根目錄
nginx1
mount 192.168.2.18:/opt/nginx1 /usr/local/nginx/html/
nginx2
mount 192.168.2.18:/opt/nginx2 /usr/local/nginx/html/
檢查一下是否掛載成功
cat /usr/local/nginx/html/index.html
關閉防火牆
systemctl stop firewalld
setenforce 0
vi /etc/selinux/config
SELINUX=disabled
測試
curl http://localhost
排程器
1.安裝配置haproxy(主備都要安裝)
xftp上傳檔案haproxy
yum安裝依賴包、解壓、安裝
yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
tar xf haproxy-1.4.24.tar.gz
cd haproxy-1.4.24/
make TARGET=linux26
make install
建立一個目錄、拷貝
mkdir /etc/haproxy
cp examples/haproxy.cfg /etc/haproxy/
編輯haproxy.cfg
vi /etc/haproxy/haproxy.cfg
註釋
#chroot /usr/share/haproxy
#redispatch
刪除
刪除所有listen選項
新增
listen webcluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server nginx1 192.168.2.16:80 check inter 2000 fall 3
server nginx2 192.168.2.17:80 check inter 2000 fall 3
2.安裝keepalived(主備都要安裝)
xftp上傳keepalived檔案,解壓
tar xf keepalived-2.0.13.tar.gz
安裝編譯工具與外掛
yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
切換目錄、編譯安裝
cd keepalived-2.0.13/
./configure --prefix=/
make && make install
加入系統管理服務、設為開機自啟
cp keepalived/etc/init.d/keepalived /etc/init.d/
systemctl enable keepalived.service
haproxy1配置檔案
(主排程器)
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id Haproxy_01
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.2.100
}
}
haproxy2配置檔案
(從排程器)
! Configuration File for keepalived
global_defs {
router_id Haproxy_02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 145
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.2.100
}
}
測試
1.瀏覽器訪問測試(主備都正常)
2.ip addr 測試(主備都正常)
3.模擬主排程器網路故障測試
Haproxy監控指令碼
假如Haproxy1裝置只是Haproxy服務出現故障,Keepalived沒有出現故障,那麼備用排程器就不會啟用,就會造成服務中斷。因此需要關閉Keepalived,這時候就需要編寫一個指令碼監控Haproxy,假如Haproxy關閉,那麼指令碼就會自動關閉Keepalived
編輯指令碼
vi /etc/keepalived/check_haproxy.sh
#!/bin/bash
#haproxy失效關閉keepalived
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
sleep 3
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
加許可權
chmod 755 /etc/keepalived/check_haproxy.sh
修改配置檔案
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
global_defs {
router_id Haproxy_01
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.2.100
}
}
}
測試
相關文章
- LVS+Keepalive 實現負載均衡高可用叢集負載
- 3.RabbitMQ高階叢集搭建(Haproxy負載均衡、Keepalived高可用)MQ負載
- RabbitMQ(四):使用Docker構建RabbitMQ高可用負載均衡叢集MQDocker負載
- 負載均衡叢集負載
- keepalived高可用負載均衡負載
- Nginx負載均衡高可用Nginx負載
- dubbo叢集和負載均衡負載
- Keepalived實現Nginx負載均衡高可用Nginx負載
- haproxy(單機)+mysql叢集負載均衡MySql負載
- orleans叢集及負載均衡實現負載
- nginx反向大理和負載均衡以及高可用Nginx負載
- LVS+Keepalived 實現高可用負載均衡負載
- Mycat 雙主雙從-負載均衡-高可用負載
- haporxy+keepalived實現負載均衡+高可用負載
- kubernetes叢集內排程與負載均衡負載
- 負載均衡 - MQTT Broker 叢集詳解(一)負載MQQT
- keepalived+haproxy實現mysql負載均衡高可用MySql負載
- 叢集,lvs負載均衡的四種工作模式負載模式
- Linux環境搭建Nginx+Tomcat負載均衡叢集LinuxNginxTomcat負載
- docker初體驗:docker部署nginx負載均衡叢集DockerNginx負載
- PostgreSQL repmgr高可用叢集+keepalived高可用SQL
- Keepalived+Nginx+Tomcat配置高可用負載均衡系統示例NginxTomcat負載
- 叢集、負載均衡、分散式的區別與聯絡負載分散式
- centos7下配置nginx反向代理負載均衡叢集CentOSNginx負載
- 粘性會話負載均衡 - MQTT Broker 叢集詳解(二)會話負載MQQT
- zookeeper 高可用叢集搭建
- MongoDB高可用叢集搭建MongoDB
- 搭建 Kubernetes 高可用叢集
- Redis叢集與高可用Redis
- PostgreSQL patroni高可用叢集SQL
- 基於MySQL Cluster + LVS + KeepAlived部署負載均衡高可用架構MySql負載架構
- CentOS7+ keepalived+ haproxy搭建Mycat高可用及負載均衡CentOS負載
- Dubbo學習筆記(四)叢集容錯與負載均衡筆記負載
- 使用Redis儲存Nginx+Tomcat負載均衡叢集的SessionRedisNginxTomcat負載Session
- Dapr + .NET Core實戰(十一)單機Dapr叢集負載均衡負載
- 藉助 TCP 負載均衡和 Galera 叢集擴充套件 MySQLTCP負載套件MySql
- RabbitMQ從零到叢集高可用(.NetCore5.0) -高可用叢集構建落地MQNetCore
- CentOS7 實現 Keepalived + Nginx 實現高可用 Web 負載均衡CentOSNginxWeb負載