centos7使用haproxy+keepalived搭建负载均衡调度器--yum方式

server/2024/12/17 19:36:49/

一、实验规划

  node1:haproxy+keepalived  IP地址:192.168.200.111(主)

  node2:haproxy+keepalived  IP地址:192.168.200.111(从)

  nginx1:nginx IP地址:192.168.200.113

  nginx2:nginx IP地址:192.168.200.114

  VIP地址:192.168.200.200

二、前期准备工作

  1. 修改主机名 

#node1
hostname node1
bash
#node2
hostname node2
bash
#nginx1
hostname nginx1
bash
#nginx2
hostname nginx2

  2. 配置主机名与IP地址的映射关系(修改hosts文件)

192.168.200.111 node1
192.168.200.112 node2
192.168.200.113 nginx1
192.168.200.114 nginx2

  3. 关闭防火墙与安全机制

systemctl stop firewalld
setenforce 0
iptables -F

   4. 配置yum源及epel源(不再介绍)

三、node1的操作

   1. 安装haproxy

#安装haproxyyum -y install haproxy
#配置haproxy
vim /etc/haproxy/haproxy.cfg# 配置说明:本配置基于yum安装后默认的配置文件上修改而来的
# 其中 `listen admin_stats` 的配置是启用haproxy的监控web界面,
# 跟本次实验无关,是可选的
# 修改 `backend app`,转到nginx1与nginx2
global
log 127.0.0.1 local2chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon# turn on stats unix socket
stats socket /var/lib/haproxy/stats#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000#---------------------------------------------------------------
listen admin_stats
bind 0.0.0.0:48800
bind-process 1
stats enable
mode http
log global
stats hide-version
stats uri /haproxy #访问的uri ip:8888/haproxy
stats realm Haproxy\ Statistics
stats auth admin:admin #访问帐密
stats admin if TRUE # 管理界面,如果认证成功,是否可通过WebUI操作节点
stats refresh 30s #监控状态刷新频率
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:5000  #本次采用5000端口访问
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .jsuse_backend static if url_static
default_backend app#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 192.168.200.113:80 check
server app2 192.168.200.114:80 check

  2. 安装keepalived

#安装keepalived
yum -y install keepalived 
#配置keepalived
vim  /etc/keepalived/keepalived.conf 
vrrp_script chk_http_port {script "/etc/keepalived/check_haproxy.sh"interval 2weight 2
}
vrrp_instance VI_1 {state MASTERinterface eno16777728virtual_router_id 51 priority 100     advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {chk_http_port}virtual_ipaddress {192.168.200.200 }
}
#准备检查脚本
vim /etc/keepalived/check_haproxy.sh 
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`if [ $A -eq 0 ];thensystemctl restart haproxy &> /dev/nullsleep 3if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];thensystemctl stop keepalived fi
fi
#给脚本加执行权限
chmod 755 /etc/keepalived/check_haproxy,sh复制代码

四、node2的操作

  1. 安装haproxy

#安装haproxyyum -y install haproxy
#配置haproxy
vim /etc/haproxy/haproxy.cfg# 配置说明:本配置基于yum安装后默认的配置文件上修改而来的
# 其中 `listen admin_stats` 的配置是启用haproxy的监控web界面,
# 跟本次实验无关,是可选的
# 修改 `backend app`,转到nginx1与nginx2
global
log 127.0.0.1 local2chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon# turn on stats unix socket
stats socket /var/lib/haproxy/stats#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000#---------------------------------------------------------------
listen admin_stats
bind 0.0.0.0:48800
bind-process 1
stats enable
mode http
log global
stats hide-version
stats uri /haproxy #访问的uri ip:8888/haproxy
stats realm Haproxy\ Statistics
stats auth admin:admin #访问帐密
stats admin if TRUE # 管理界面,如果认证成功,是否可通过WebUI操作节点
stats refresh 30s #监控状态刷新频率
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:5000  #本次采用5000端口访问
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .jsuse_backend static if url_static
default_backend app#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 192.168.200.113:80 check
server app2 192.168.200.114:80 check

  2. 安装keepalived

#安装keepalived
yum -y install keepalived 
#配置keepalived
vim  /etc/keepalived/keepalived.conf 
global_defs {  #全局设置,可以不用加script_user root  #这里指定脚本运行用户,小编这里不指定用户脚本不启动所以加上enable_script_security }
vrrp_script chk_http_port {script "/etc/keepalived/check_haproxy.sh"interval 2weight 2
}
vrrp_instance VI_1 {state BACKUPinterface eno16777736virtual_router_id 51 priority 99     advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {chk_http_port}virtual_ipaddress {192.168.200.200 }
}
#准备检查脚本
vim /etc/keepalived/check_haproxy.sh 
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`if [ $A -eq 0 ];thensystemctl restart haproxy &> /dev/nullsleep 3if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];thensystemctl stop keepalived fi
fi
#给脚本加执行权限
chmod 755 /etc/keepalived/check_haproxy,sh

五、nginx1与nginx2的操作

#两台机器相同操作
yum -y install nginx
#nginx1准备访问网页
cat >> /usr/share/nginx/html/index.html << EOF
113
EOF
#nginx2准备访问网页
cat >> /usr/share/nginx/html/index.html << EOF
114
EOF
#启动nginx
systemctl restart nginx

六、测试

#启动haproxykeepalived
systemctl start haproxy
systemctl enable haproxy
systemctl start keepalived
systemctl enable keepalived

1.网页访问测试

 

2. 服务停止测试

haproxy服务停止

 keepalive停止


http://www.ppmy.cn/server/150981.html

相关文章

11.python文件

文章目录 Python 文件 I/O 总结1. **打印到屏幕**2. **读取键盘输入**3. **打开和关闭文件**3.1 open() 函数3.2 close() 方法 4. **文件读写操作**4.1 write() 方法4.2 read() 方法4.3 readline() 方法4.4 readlines() 方法4.5 seek() 和 tell() 方法 5. **文件重命名和删除**…

docker redis 详细教程

1. 拉取镜像 docker pull redis 2. 创建数据存储目录 cd /home/ mkdir redis cd redis mkdir data mkdir log mkdir conf 3.创建容器并且运行 docker run \ -p 6379:6379 \ --name redis \ -v /home/redis/data:/data \ -d redis 参考链接 史上最详细Docker安装Redis &am…

如何通过编译器标志增强移动应用的安全性

作为一名 Android 或 iOS 开发者&#xff0c;您可能已经熟悉一些常见的安全开发最佳实践&#xff0c;比如验证外部输入、合理管理内存以及避免使用弱加密算法。然而&#xff0c;即便是最精心编写的代码&#xff0c;也可能包含一些 bug&#xff0c;其中一些可能会导致可被利用的…

Java学习Day08——泛型

1. 泛型的理解和好处 1.1 使用传统方法的问题 1. 不能对加入到集合 ArrayList中的数据类型进行约束(不安全) 2. 遍历的时候&#xff0c;需要进行类型转换,如果集合中的数据量较大&#xff0c;对效率有影响 1.2 泛型的好处 1. 编译时&#xff0c;检查添加元素的类型&…

如何使用 uni-app 构建直播应用程序?

使用uni-app构建直播应用程序涉及前端和后端的开发&#xff0c;以及音视频处理技术的选择。下面我将概述一个典型的直播应用架构&#xff0c;并详细说明如何在uni-app中实现关键功能。 直播应用架构 前端&#xff08;uni-app&#xff09;&#xff1a;负责用户界面展示、互动逻…

240004基于Jamva+ssm+maven+mysql的房屋租赁系统的设计与实现

基于ssmmavenmysql的房屋租赁系统的设计与实现 1.项目描述2.运行环境3.项目截图4.源码获取 1.项目描述 该项目在原有的基础上进行了优化&#xff0c;包括新增了注册功能&#xff0c;房屋模糊查询功能&#xff0c;管理员和用户信息管理等功能&#xff0c;以及对网站界面进行了优…

Spring Security前置内容+关键内部类

目录 前置内容认证与授权配置和配置类 security内部的关键类UserDetailsUserUserDetailsService&#xff08;加载用户数据&#xff0c;提供用户信息&#xff09; 前置内容 认证与授权 认证即系统判断用户的身份是否合法&#xff0c;合法可继续访问&#xff0c;不合法则拒绝访…

vue2中echarts

如图示&#xff1a; 第一个&#xff1a; <div ref"charts" style"width:100%;height:99px;"></div> import * as echarts from echarts;mounted() {this.drawLine();},methods: {drawLine() {// 基于准备好的dom&#xff0c;初始化echarts实例…