nginx single ip
keepalived源码:https://github.com/acassen/keepalived
nginx源码:https://github.com/nginx/nginx
环境:10.18.1.185(主) 10.18.1.183(备)
1.安装nginx + keep
sudo apt install nginx
sudo apt install keepalived
2.修改配置文件
sudo vi /etc/nginx/nginx.conf
配置文件改为以下内容,其中root代表html文件位置:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
修改nginx183和185的返回:
/var/www/html/index.html的内容修改为对应的ip
sudo echo "185" /var/www/html/index.html
3.修改keepalived的配置信息
sudo vi /etc/keepalived/keepalived.conf
主185的如下:
global_defs {
router_id nginx_lb01
}
vrrp_script check_ng
{
script "/etc/keepalived/script/check_nginx.sh"
interval 1
}
vrrp_instance VI_1 {
virtual_router_id 51
state MASTER
priority 101
advert_int 1
interface ens3
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.18.1.195
}
unicast_src_ip 10.18.1.185
unicast_peer {
10.18.1.183
}
track_script {
check_ng
}
}
备183的如下:
global_defs {
router_id nginx_lb02
}
vrrp_instance VI_1 {
virtual_router_id 51
state BACKUP
priority 100
advert_int 1
interface ens3
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.18.1.195
}
unicast_src_ip 10.18.1.183
unicast_peer {
10.18.1.185
}
}
主185还需要nginx检测脚本,当nginx挂掉时,自动关闭keepalived:
sudo mkdir /etc/keepalived/script
4.改变mysql目录权限
sudo chown -R mysql.mysql /usr/local/mysql/
sudo vi /etc/keepalived/script/check_nginx.sh
脚本如下:
#!/bin/bash
#
# Define variables
RETVAL=0
NginxStats=$(netstat -lntup|grep ":80 "|grep -v grep|wc -l)
## $NginxStats = 0 and stop keepalived
if [ "$NginxStats" -eq 0 ];then
service keepalived stop
fi
5.重启nginx和keepalived
sudo service nginx restart (或sudo service nginx reload )
sudo service keepalived restart
看到如下输出:
● keepalived.service - Keepalive Daemon (LVS and VRRP)
Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-02-27 16:45:57 CST; 4s ago
Process: 16369 ExecStart=/usr/sbin/keepalived $DAEMON_ARGS (code=exited, status=0/SUCCESS)
Main PID: 16371 (keepalived)
Tasks: 3 (limit: 4915)
CGroup: /system.slice/keepalived.service
├─16371 /usr/sbin/keepalived
├─16375 /usr/sbin/keepalived
└─16377 /usr/sbin/keepalived
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: Registering Kernel netlink command channel
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: Registering gratuitous ARP shared channel
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: Opening file '/etc/keepalived/keepalived.conf'.
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: Unsafe permissions found for script '/etc/keepalived/script/check_nginx.sh'.
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: SECURITY VIOLATION - scripts are being executed but script_security not enabled. There are insecure scripts.
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: Using LinkWatch kernel netlink reflector...
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: VRRP_Script(check_ng) succeeded
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: VRRP_Instance(VI_1) Transition to MASTER STATE
2月 27 16:45:58 sder_plt_185 Keepalived_vrrp[16377]: VRRP_Instance(VI_1) Entering MASTER STATE
输出如下:
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-02-27 16:45:50 CST; 40s ago
Docs: man:nginx(8)
Process: 16017 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 16286 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 16278 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 16288 (nginx)
Tasks: 33 (limit: 4915)
CGroup: /system.slice/nginx.service
├─16288 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─16290 nginx: worker process
├─16292 nginx: worker process
├─16293 nginx: worker process
├─16294 nginx: worker process
├─16295 nginx: worker process
├─16298 nginx: worker process
├─16299 nginx: worker process
├─16300 nginx: worker process
├─16301 nginx: worker process
├─16302 nginx: worker process
├─16303 nginx: worker process
6.测试185nginx挂掉,183启用
先找一台其它机器:
curl 10.18.1.195
显示如下:
185
关闭185主的nginx服务:
sudo service nginx stop
再次执行,结果如下:
183
看到185的keepalived挂掉,inactive (dead)