Haproxy+keepalive-群集架构实验
LVS+Keepalived高可用群集
群集架构----Haproxy群集
实验案例拓补图:
要求:如图所示,有两台haproxy调度器,但是所有的流量全部由主调度器获取,由主调度器可以正常调度web节点。
环境:
调度器(双机热备)
IP地址:192.168.100.11(主)+ 漂移地址:virtual-ip:192.168.100.200 +Haproxy服务器
IP地址:192.168.100.12(备)+漂移地址:virtual-ip:192.168.100.200 +Haproxy服务器
Web服务器两台:
IP地址:192.168.100.2(Web1)
IP地址:192.168.100.3(Web2)
NFS共享服务器:
IP地址:192.168.100.4
客户端一台:用于测试验证
IP地址:192.168.100.5(client)
NFS共享服务器: 192.168.100.4
mkdir /web1
mkdir /web2
echo "<h1>nginx1</h1>" > /web1/index.html '设置共享目录'
echo "<h1>nginx2</h1>" > /web2/index.html '设置共享目录'
vi /etx/exports
/web1 192.168.100.2/32 (ro)
/web2 192.168.100.3/32 (ro)
systemctl restart nfs
systemctl restart rpcbind
Nginx服务器1:192.168.100.2
tar xzvf nginx-1.13.7.tar.gz '先解压'
yum -y install gcc gcc-c++ make pcre-devel zlib-devel
useradd -M -s /sbin/nologin nginx
mount 192.168.100.4:/web1 /var/www/html/ 'nfs共享目录挂载到/var/www/html'
nginx '开启nginx服务'
curl http://localhost
<h1>nginx1</h1> '效果'
//LVS+Keepalived高可用群集
vi web1.sh
#!/bin/bash
# lvs web1
ifconfig lo:0 192.168.100.200 broadcast 192.168.100.200 netmask 255.255.255.255 up '添加虚拟地址的虚接口'
route add -host 192.168.100.200 dev lo:0 '给lo:0添加路由'
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p &> /dev/null
sh web1.sh
ifconfig
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 '添加的虚拟地址'
inet 192.168.100.200 netmask 255.255.255.255
loop txqueuelen 1 (Local Loopback)
route -n
192.168.100.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo
Nginx服务器2:192.168.100.3
tar xzvf nginx-1.13.7.tar.gz '先解压'
yum -y install gcc gcc-c++ make pcre-devel zlib-devel
useradd -M -s /sbin/nologin nginx
mount 192.168.100.4:/web2 /var/www/html/ 'nfs共享目录挂载到/var/www/html'
nginx '开启nginx服务'
curl http://localhost
<h1>nginx2</h1> '效果'
//LVS+Keepalived高可用群集
vi web2.sh
#!/bin/bash
# lvs web2
ifconfig lo:0 192.168.100.200 broadcast 192.168.100.200 netmask 255.255.255.255 up '添加虚拟地址的虚接口'
route add -host 192.168.100.200 dev lo:0 '给lo:0添加路由'
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p &> /dev/null
sh web2.sh
ifconfig
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 '添加的虚拟地址'
inet 192.168.100.200 netmask 255.255.255.255
loop txqueuelen 1 (Local Loopback)
route -n
192.168.100.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo
IP地址:192.168.100.11(主)+ 漂移地址:virtual-ip:192.168.100.200 +Haproxy服务器
Keepalived搭建双机主
tar zxvf keepalived-2.0.13.tar.gz '解压'
yum -y insatll ipvsadm
yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
cd keepalived-2.0.13/
./configure --prefix=/ '配置'
make && make install '编译安装'
modprobe ip_vs
cat /proc/net/ip_vs '以下是效果'
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
systemctl enable keepalived '开机自启'
cp keepalived/etc/init.d/keepalived /etc/init.d/ '方便编辑'
配置keepalived.conf
'先把里面东西删掉'
主配置keepalived
! Configuration File for keepalived
global_defs {
router_id lvs_1
}
vrrp_instance vi_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 6666
}
virtual_ipaddress {
192.168.100.200
}
}
virtual_server 192.168.100.200 80 { '虚拟地址'
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 6
protocol TCP
real_server 192.168.100.2 80 { '节点地址'
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.3 80 { '节点地址'
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
systemctl restart keepalived.service '开启keepalived服务'
ip addr '代表是主会添加虚拟地址'
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:02:b2:9b brd ff:ff:ff:ff:ff:ff
inet 192.168.100.11/24 brd 192.168.100.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.100.200/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe02:b29b/64 scope link
valid_lft forever preferred_lft forever
ipvsadm -Ln
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.100.200:80 rr persistent 6
-> 192.168.100.2:80 Route 1 0 1
-> 192.168.100.3:80 Route 1 0 4
tail -f /var/log/messages '如下面这个结果'
Dec 11 00:26:38 localhost Keepalived_healthcheckers[70323]: TCP connection to [192.168.100.2]:tcp:80 success.
Haproxy服务器:192.168.100.11
tar xzvf haproxy-1.4.24.tar.gz '解压'
yum -y install gcc gcc-c++ make pcre-devel zlib-devel
mkdir /etc/haproxy
cd haproxy-1.4.24/
make TARGET=linux26
make install
cp examples/haproxy.cfg /etc/haproxy/
vi /etc/haproxy/haproxy.cfg
#chroot /usr/share/haproxy '注释掉'
#redispatch
'删除所有listen,留下global defaults 加入下面这段'
listen webcluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server web1 192.168.100.2:80 check inter 2000 fall 3 'web1服务器ip'
server web2 192.168.100.3:80 check inter 2000 fall 3 'web2服务器ip'
//设置开机自启haproxy
cd haproxy-1.4.24/
cp examples/haproxy.init /etc/init.d/haproxy
vi /etc/init.d/haproxy
# chkconfig: 35 85 15 '默认是- 改为35'
chkconfig --add haproxy
chkconfig --list
haproxy 0:关 1:关 2:关 3:开 4:关 5:开 6:关
cd /etc/init.d/
chmod +x haproxy '授予权限'
chkconfig --add haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
systemctl enable haproxy
systemctl start haproxy '开启haproxy服务'
Keepalived搭建双机备
Haproxy服务器:192.168.100.12 跟 Haproxy服务器:192.168.100.11一样,copy上面的就好。
注:Keepalived备份服务器的配置与master的配置有三个选项不同。
vi /etc/keepalived/keepalived.conf
router_id:设为自有名称
state:设为BACKUP
priority:值低于主服务器 145
其他选项与master相同
谷歌浏览器访问漂移地址:192.168.100.200
这是Web2节点的页面
等几分钟,清空浏览器记录访问漂移地址:192.168.100.200
这是Web1节点的页面
客户机
关闭 192.168.100.11主 然后在访问漂移地址:192.168.100.200
再次访问 192.168.100.200 效果一样 会有两个节点页面。
ip addr可以查看漂移地址产生没有。
本文地址:https://blog.csdn.net/weixin_50344807/article/details/111031093