nginx keepalived搭建
时间: 2023-10-22 10:29:41 浏览: 122
要搭建nginx和keepalived,可以按照以下步骤进行操作:
1. 首先,安装nginx。在主服务器和备份服务器上都需要安装nginx。可以通过YUM安装或者手动编译安装。如果选择手动编译安装,可以按照以下步骤进行操作:
- 下载nginx安装包并解压缩:
```
cd /tools/
ls nginx-1.12.0.tar.gz
tar -zxf nginxnx-1.12.0.tar.gz
cd nginxnx-1.12.0
```
- 配置并安装nginx:
```
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
make && make install
```
- 添加nginx用户并启动nginx:
```
useradd www
/usr/local/nginx/sbin/nginx
```
2. 接下来,安装keepalived。keepalived用于实现高可用性,将主服务器和备份服务器配置为虚拟IP地址。可以通过以下步骤安装keepalived:
- 创建check_nginx.sh脚本并设置执行权限:
```
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/usr/local/nginx/sbin/nginx
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
systemctl stop keepalived
fi
fi
```
- 配置keepalived的主服务器(192.168.92.100)上的配置文件:
```
Configuration File for keepalived
global_defs {
router_id Nginx_01
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight -5
fall 3
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.92.200
}
track_script {
check_nginx
}
}
```
- 在备份服务器上配置keepalived的配置文件,与主服务器配置文件相似,只需将state设置为BACKUP。
- 启动keepalived服务:`systemctl start keepalived`
通过以上步骤,您可以搭建nginx和keepalived来实现高可用性,当nginx服务出现故障时,keepalived会将虚拟IP地址迁移到备份服务器上,确保服务的持续可用性。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [搭建keepalived+nginx热备高可用(主备+双主模式)](https://blog.csdn.net/qq_38055805/article/details/127916599)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [Keepalived+Nginx搭建](https://blog.csdn.net/qq_44777969/article/details/107436999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文