服务器规划:
serverb(nginx2):192.168.233.144
serverc(客户端):192.168.233.140
serverd(nginx1):192.168.233.141
结构图:
serverd(nginx1):
# 安装nginx
yum install nginx -y
# 进入nginx配置目录
cd /etc/nginx/conf.d/
# 编辑www.conf文件
vim www.conf
server {
listen 80;
server_name localhost;
location / {
root /data/web;
index index.html;
}
}
# 创建web目录
mkdir -p /data/web
# 创建测试页面index.html
echo "web test page, ip is `hostname -I`." >/data/web/index.html
# 启动nginx服务
systemctl start nginx
# 安装keepalived
yum install keepalived -y
# 进入keepalived配置目录
cd /etc/keepalived/
# 备份keepalived.conf文件
cp keepalived.conf{,.bak}
# 编辑keepalived.conf文件
vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_141 # 设置路由器的ID为LVS_141
}
vrrp_instance nginx {
state MASTER # 将该实例设置为MASTER状态,备份节点应设置为BACKUP
interface ens160 # 指定VRRP实例使用的网络接口
virtual_router_id 51 # 虚拟路由器的唯一ID,同一组中MASTER和BACKUP的virtual_router_id必须相同
priority 100 # 优先级设置为100,MASTER的优先级通常比BACKUP的优先级高
advert_int 1 # 发送VRRP通告的时间间隔,单位为秒
authentication {
auth_type PASS # 使用简单密码认证方式
auth_pass 1111 # 认证密码为1111
}
virtual_ipaddress {
192.168.233.50 # 配置虚拟IP地址,客户端将访问该IP以访问服务
}
}
# 启动keepalived服务
systemctl start keepalived
serverb(nginx2):
# 安装 nginx
yum install nginx -y
# 进入 nginx 配置目录
cd /etc/nginx/conf.d/
# 编辑 nginx 配置文件 www.conf
vim www.conf
# 在配置文件中写入以下内容
server {
listen 80;
server_name localhost;
location / {
root /data/web;
index index.html;
}
}
# 创建 web 根目录
mkdir -p /data/web
# 在 web 根目录下创建测试页面 index.html
echo "web test page, ip is `hostname -I`." >/data/web/index.html
# 启动 nginx
systemctl start nginx
# 安装 keepalived
yum install keepalived -y
# 进入 keepalived 配置目录
cd /etc/keepalived/
# 备份 keepalived 配置文件
cp keepalived.conf{,.bak}
# 编辑 keepalived 配置文件
vim keepalived.conf
# 在 keepalived 配置文件中写入以下内容
! Configuration File for keepalived
global_defs {
router_id LVS_144 # 设置路由器标识为LVS_144
}
vrrp_instance nginx { # 创建一个名为nginx的VRRP实例
state BACKUP # 将此节点设置为备用状态,主节点应该设置为MASTER
interface ens160 # 指定VRRP实例要使用的网络接口
virtual_router_id 51 # VRRP实例的虚拟路由器ID,与同一VLAN内其他节点的ID相同
priority 80 # 优先级,数字越大表示优先级越高,主节点通常设置为100
advert_int 1 # 发送VRRP通告的间隔时间,单位为秒
authentication { # 配置认证信息
auth_type PASS # 使用密码认证方式
auth_pass 1111 # 认证密码
}
virtual_ipaddress { # 设置虚拟IP地址列表
192.168.233.50 # 在VRRP实例中使用的虚拟IP地址
}
}
# 启动 keepalived
systemctl start keepalived
serverc(客户端):文章来源:https://www.toymoban.com/news/detail-828560.html
vip访问:
curl 192.168.233.50
日志查看:文章来源地址https://www.toymoban.com/news/detail-828560.html
tail -f /var/log/messages
到了这里,关于Keepalived实现Nginx的高可用集群案例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!