目录
二、两台nginx服务器作为代理服务器,配置nginx的负载均衡
Proxy-master 与 Proxy-slave机器同时操作安装
主机清单
主机名 | ip | 系统 | 用途 |
---|---|---|---|
Proxy-master | 10.12.153.49 | centos7 | 主负载 |
Proxy-slave | 10.12.153.108 | centos7 | 主备 |
Real-server1 | 10.12.153.113 | Centos7 | web1 |
Real-server2 | 10.12.153.115 | centos7 | Web2 |
1、关闭防火墙和selinux
2、更改主机名
3、域名解析
4、四台机器分别安装nginx,并启动
详细过程可参考之前的文章
这里我们选择的是ngixn1.20.1版本
两台机器的配置完全一样
vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
upstream backend {
server 10.12.153.113:80 weight=1 max_fails=3 fail_timeout=20s;
server 10.12.153.115:80 weight=1 max_fails=3 fail_timeout=20s;
}
server {
listen 80;
server_name localhost;location / {
proxy_pass http://backend;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
保存退出后
查看ngnix配置文件是否语法出错
nginx -t
重新加载配置文件
nginx -s reload
注:主/备调度器均能够实现正常调度
1. 主/备调度器安装软件
yum install -y keepalived
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
! Configuration File for keepalived
global_defs {
router_id directory1
}vrrp_instance VI_1 {
state MASTER #定义主还是备
interface ens33 #VIP绑定接口
virtual_router_id 80 #整个集群的调度器一致
priority 100 #back改为50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.12.153.109/24 # keepalived提供的对外开放的vip
}
}
Configuration File for keepalived
! Configuration File for keepalived
global_defs {
router_id directory2 #注意这里的id区别
}vrrp_instance VI_1 {
state BACKUP #设置为backup
interface ens33
nopreempt #设置到back上面,不抢占资源
virtual_router_id 80
priority 50 #辅助改为50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.12.153.109/24
}
}
- systemctl enable keepalived
-
- systemctl start keepalived
到此:
可以解决心跳故障keepalived
不能解决Nginx服务故障
页面访问测试
我们正常访问为方便识别Real-server1和Real-server2页面是这样的
现在我们通过访问Keepalived我们定义的对外开放的vip的ip来看看
再来测试一下高可用
查看Proxy-master的ip
这时候 Proxy-slaver是没有这个vip的ip的处于备用状态
我们通过关闭Proxy-master服务器的keepalived服务查看vip的ip会不会漂移到 Proxy-slaver服务器
这样就算master宕机,通过高可用备成为新的主,我们对于Real-server1和Real-server2的访问也是不受影响的!!
希望能够帮助到大家!!!