nginx+keepalived实现高可用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+keepalived实现高可用相关的知识,希望对你有一定的参考价值。

配置环境:四台服务器
两台nginx服务器:192.168.200.111 192.168.200.112
两台tomcat服务器:192.168.200.113 192.168.200.114

nginx1 192.168.200.111
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# iptables -F
安装nginx步骤省略
[root@localhost ~]# vim /usr/lcoal/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
upstream tomcat_server {
server 192.168.200.113:8080 weight=1;
server 192.168.200.114:8080 weight=1;
}
server {
listen 80;
server_name localhost;
charset utf-8;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_server;
proxy_set_header Host $http_host;
}
}
}
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 10
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.200.254
}
}
[root@localhost ~]# vim /etc/keepalived/check_nginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/usr/local/nginx/sbin/nginx
sleep 3
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
systemctl stop keepalived
fi
fi
[root@localhost ~]# chmod +x /etc/keepalived/check_nginx.sh
[root@localhost ~]# systemctl start keepalived

nginx2 192.168.200.112
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# iptables -F
安装nginx步骤省略
[root@localhost ~]# vim /usr/lcoal/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
upstream tomcat_server {
server 192.168.200.113:8080 weight=1;
server 192.168.200.114:8080 weight=1;
}
server {
listen 80;
server_name localhost;
charset utf-8;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_server;
proxy_set_header Host $http_host;
}
}
}
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 5
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.200.254
}
}
[root@localhost ~]# vim /etc/keepalived/check_nginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/usr/local/nginx/sbin/nginx
sleep 3
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
systemctl stop keepalived
fi
fi
[root@localhost ~]# chmod +x /etc/keepalived/check_nginx.sh
[root@localhost ~]# systemctl start keepalived


两台tomcat配置相同,测试页不同
安装jdk
[root@localhost ~]# tar xf jdk-8u191-linux-x64.tar.gz
[root@localhost ~]# mv jdk1.8.0_191 /usr/local/java
[root@localhost ~]# vi /etc/profile
在最后添加
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin
[root@localhost ~]# source /etc/profile
安装tomcat
[root@localhost ~]# tar xf apache-tomcat-8.5.16.tar.gz
[root@localhost ~]# mv apache-tomcat-8.5.16 /usr/local/tomcat
[root@localhost ~]# /usr/local/tomcat/bin/startup.sh
Tomcat1测试页:
[root@localhost ~]# echo “111111” > /usr/local/tomcat/webapps/ROOT/index.jsp
Tomcat2测试页:
[root@localhost ~]# echo “222222” > /usr/local/tomcat/webapps/ROOT/index.jsp

以上是关于nginx+keepalived实现高可用的主要内容,如果未能解决你的问题,请参考以下文章

Nginx+Keepalived实现站点高可用

Nginx+Keepalived实现站点高可用

Nginx+Keepalived实现站点高可用

Keepalive工作原理

Nginx+Keepalived实现Nginx高可用

keepalived + nginx实现高可用