Nginx+Keepalived 高可用集群(双主模式)详细教程
时间: 2024-10-13 15:07:41 浏览: 65
Nginx 和 Keepalived 结合构建高可用集群(通常称为 Active-Standby 或双主模式)是为了提供Web服务的高可用性和负载均衡。以下是创建这种环境的一般步骤:
1. **安装依赖**:
- 安装Nginx:`sudo apt-get install nginx`
- 安装Keepalived:`sudo apt-get install keepalived`
2. **配置Keepalived**:
- 创建keepalived配置文件:`sudo nano /etc/keepalived/keepalived.conf`
- 添加VRRP虚拟路由器定义,每个主服务器都有一个虚拟IP(VIP),并设置监控心跳间隔。
```conf
vrrp_script check_nginx {
script "killall -0 nginx || exit 2"
interval 2
weight 25
}
vrrp_instance VI_1 {
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass your_password
}
virtual_ipaddress {
192.168.1.100
}
state MASTER
notifyfacility LOG_LOCAL4
track_script {
check_nginx
}
}
```
3. **启动Keepalived**:
`sudo service keepalived start`
4. **配置Nginx**:
- 配置Nginx,监听VIP而不是实际的物理IP地址。
- 使用`include`指令加载Keepalived的配置,例如:
```conf
http {
upstream backend {
server 192.168.1.100 backup;
}
server {
listen 192.168.1.100;
location / {
proxy_pass http://backend;
}
}
}
```
5. **测试和监控**:
- 确保两个节点都配置好,并且Keepalived在master状态下运行。
- 使用ping或wget VIP测试负载平衡和故障切换。
阅读全文