欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HAProxy+Keepalived

程序员文章站 2024-03-21 09:29:16
...

环境准备

服务器系统 Centos6.8
内存 1G
CPU 2核
IP地址 10.0.0.20
服务1 Keepalived
服务2 HAProxy
服务器系统 Centos6.8
内存 1G
CPU 2核
IP地址 10.0.0.21
服务1 Keepalived
服务2 HAProxy
服务器系统 Centos6.8
内存 1G
CPU 2核
IP地址 10.0.0.22
服务1 Nginx
服务器系统 Centos6.8
内存 1G
CPU 2核
IP地址 10.0.0.23
服务1 Nginx

内存 1G
CPU 2核
IP地址 10.0.0.20
服务1 Nginx
服务2 Keepalived

yum -y install haproxy keepalived
cd /etc/haproxy/
cp haproxy.cfg haproxy.cfg.bak 
# 在22、23两台主机上安装nginx依赖库和环境
yum -y install gcc gcc-c++ zlib-devel pcre-devel 

在20主机上操作

# 编辑keepalived服务配置文件
vim /etc/keepalived/keepalived.conf 

HAProxy+Keepalived

# 启动keepalived服务
service keepalived start
# 将20主机上修改好的配置文件远程拷贝至21主机上
scp /etc/keepalived/keepalived.conf 10.0.0.21:/etc/keepalived/keepalived.conf 

开始在21主机上操作

vim /etc/keepalived/keepalived.conf

HAProxy+Keepalived

service keepalived start

测试
keepalived高可用服务实现故障漂移,模拟Master20主机故障

切换到20主机上操作

# 终止keepalived服务
service keepalived stop
ip addr

HAProxy+Keepalived

再来21主机上Keepalived-Backup这边查看虚拟IP已经漂移过来了

ip addr

HAProxy+Keepalived

模拟20主机服务恢复,根据优先级自动抢占虚拟IP

# 重启keepalived服务
service keepalived restart

HAProxy+Keepalived

HAProxy+Keepalived

在20主机上操作

# 编辑haproxy服务的配置文件
vim /etc/haproxy/haproxy.cfg

HAProxy+Keepalived

# 启动haproxy服务
service haproxy start
#  将20主机上修改好的配置文件远程拷贝至21主机上
scp /etc/haproxy/haproxy.cfg 10.0.0.21:/etc/haproxy/

在21主机上操作

vim /etc/haproxy/haproxy.cfg 

HAProxy+Keepalived

# 重启keepalived服务
service keepalived restart

在22主机上操作

获取nginx源码包上传至服务器
HAProxy+Keepalived

tar zxf nginx-1.12.0.tar.gz 
cd nginx-1.12.0
./configure && make && make install

HAProxy+Keepalived
HAProxy+Keepalived

# 进入nginx网站根目录
cd /usr/local/nginx/html/
vim index.html 

把这些内容全部删除
HAProxy+Keepalived
自己随便写点内容进去,然后保存退出
HAProxy+Keepalived

 /usr/local/nginx/sbin/nginx 

HAProxy+Keepalived

scp nginx-1.12.0.tar.gz 10.0.0.23:/root/

在23主机上执行同样的操作安装nginx

tar zxf nginx-1.12.0.tar.gz 
cd nginx-1.12.0
./configure && make && make install

HAProxy+Keepalived

HAProxy+Keepalived

vim /usr/local/nginx/html/index.html 

同样跟上面一样的操作,但是内容不能重复,因为要测试负载均衡

HAProxy+Keepalived

/usr/local/nginx/sbin/nginx 

测试负载均衡的页面
HAProxy+Keepalived

HAProxy+Keepalived

相关标签: HAProxy