Nginx host prepares (Keepalived implemented)

Foreword
first introduce Keepalived, it is a high-performance server availability or hot standby solution, originally designed for LVS load-balancing software design, Keepalived main server to prevent single point of failure problem, by which the Nginx with the high availability of web server.

Keepalived basis for the realization of the agreement with VRRP, VRRP is Virtual Router Redundancy Protocol (Virtual Router Redundancy Protocol) acronym, the VRRP virtual two or more routers device into a device, provide external virtual router IP (one or more) .

When the purpose is to solve VRRP appear single point of failure static routing, it can ensure that when an individual node goes down, the entire network can run uninterrupted.

Here we introduce install nginx keepalived deploy high-availability solutions.

Environmental ready
on both hosts prepared as compressed files:

2.0.20.tar.gz-keepalived
nginx-1.16.1.tar.gz
virtual real IP nginx IP port master from
192.168.124.20 192.168.124.13 80 MASTER
192.168.124.20 192.168.124.14 80 the BACKUP
install nginx
File create a new user:

useradd tianyan
determine the installation directory, I have here is the installation directory: /home/tianyan/tianyan_soft/nginx.install.

In this directory are two directories for new installation and nginx keepalived, extract the two compressed.

The installation command:

./configure --prefix=/home/tianyan/tianyan_soft/nginx.install
--sbin-path=/home/tianyan/tianyan_soft/nginx.install/sbin/nginx --conf-path=/home/tianyan/tianyan_soft/nginx.install/conf/nginx.conf
--error-log-path=/home/tianyan/tianyan_soft/nginx.install/error.log
--http-log-path=/home/tianyan/tianyan_soft/nginx.install/access.log
--pid-path=/home/tianyan/tianyan_soft/nginx.install/nginx.pid
--lock-path=/home/tianyan/tianyan_soft/nginx.install/nginx.lock
--user=tianyan --group=tianyan
--with-http_stub_status_module
--with-http_gzip_static_module
--with-http_ssl_module
--with-http_realip_module
--with-threads
--with-pcre
--http-client-body-temp-path=/home/tianyan/tianyan_soft/nginx.install/client/
--http-proxy-temp-path=/home/tianyan/tianyan_soft/nginx.install/proxy/
--http-fastcgi-temp-path=/home/tianyan/tianyan_soft/nginx.install/fcgi/
--http-uwsgi-temp-path=/home/tianyan/tianyan_soft/nginx.install/uwsgi
--http-scgi-temp-path=/home/tianyan/tianyan_soft/nginx.install/scgi
如果报错,记得安装相关依赖:

the install GCC GCC C-yum ++
Nginx: [emerg] the bind () failed to 0.0.0.0:80 (13 is: the Permission denied).
Note: The non-root privileges to start, there will be nginx: [emerg] bind () to 0.0.0.0:80 failed (13: Permission denied) error.

The reason: Linux only the root user can use the port at 1024

Solution:

1. Start accordance with root privileges

2. The 80-port /usr/local/nginx/conf/nginx.conf file was changed to 1024 or more.

Installation keepalived
./configure --prefix = / usr / local / keepalived
above commands continue after the implementation of:

make && make install
After installation, the directory looks like this:

file

Copy the configuration file to the corresponding directory system

mkdir / etc / keepalived
CP /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
CP / usr / local / keepalived / etc / sysconfig / keepalived / etc / sysconfig / keepalived
edit master node the keepalived.conf
Vim /etc/keepalived/keepalived.conf
contents refer to the following:

! Configuration File for keepalived

{global_defs
# a name can not duplicate
the router_id of the hyq_slave
}

ng is running

vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}

{VI_1 vrrp_instance
State BACKUP # required, may be MASTER or BACKUP

interface ens33
virtual_router_id 101
priority 90
advert_int 1

# 如果两节点的上联交换机禁用了组播,则采用vrrp单播通告的方式
# 本机ip
unicast_src_ip 192.168.124.14
unicast_peer {
    # 其他机器ip
    192.168.124.13
}
# 设置nopreempt防止抢占资源
nopreempt

authentication {
    auth_type PASS
    auth_pass 1111
}

# 与上方nginx运行状况检测呼应
track_script {
    chk_nginx
}
virtual_ipaddress {
    192.168.124.20
}

}
Edit slave node keepalived.conf
vim /etc/keepalived/keepalived.conf
! Keepalived for the Configuration File

{global_defs
# a name can not duplicate
the router_id of the hyq_slave
}

ng is running

vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}

{VI_1 vrrp_instance
State BACKUP # required, may be MASTER or BACKUP

interface ens33
virtual_router_id 101
priority 90
advert_int 1

# 如果两节点的上联交换机禁用了组播,则采用vrrp单播通告的方式
# 本机ip
unicast_src_ip 192.168.124.14
unicast_peer {
    # 其他机器ip
    192.168.124.13
}
# 设置nopreempt防止抢占资源
nopreempt

authentication {
    auth_type PASS
    auth_pass 1111
}

# 与上方nginx运行状况检测呼应
track_script {
    chk_nginx
}
virtual_ipaddress {
    192.168.124.20
}

}
Write nginx_check.sh script
in the / etc / keepalived directory new script nginx_check.sh

touch nginx_check.sh
edit its content:

!/bin/sh

= A ps -C nginx --no-header |wc -l
IF [$ A -eq 0]
the then
/ usr / sbin / nginx
SLEEP 1
A2 = ps -C nginx --no-header |wc -l
IF [$ A2 -eq 0]
the then
systemctl STOP keepalived
fi
fi
implication is: if nginx stops running, try to start, but if you can not start , then kill keepalived process of the machine, keepalied virtual ip will be bound to the BACKUP machine. Note: / usr / sbin / nginx nginx is the start command, if you install to a different directory, then the corresponding replacement.

Keepalived log
Keepalived log default location is at / var / log / messages directory. We will modify it.

Because the system is centos7, modify location: /lib/systemd/system/keepalived.service

Original content:

EnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalived
ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS
修改为:

file

After modification reload service

systemctl daemon-reload
create a soft link command:

ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
执行:

-D -f /etc/keepalived/keepalived.conf keepalived
-D logs to the log message, the default message log also
-f specified configuration file
modified / etc / sysconfig / keepalived

The KEEPALIVED_OPTIONS = "- D" is modified: KEEPALIVED_OPTIONS = "- D -d -S 0"

file

Add at the end of /etc/rsyslog.conf

local0.* /var/log/keepalived.log
file

Finally, execute the command:

service rsyslog restart
after the restart keepalived can see the log in /var/log/keepalived.log down.

VIP test to verify
when the keepalived and nginx are started, we have to test.

First visit three addresses in the browser

http://192.168.124.20 (vip)
http://192.168.124.13 (Master)
http://192.168.124.14 (Slave)
I modified a bit of nginx index.html, you can see the current vip point is the master 13 node:

file

We then manually stop nginx on 13, visit http://192.168.124.20 again.

file

The installation was successful.

Which can be observed through the card file change ip address command

Experiment here, we've had keepalived + nginx deployed from the main installation configuration.

Thinking: How to open the main dual mode
What is a dual master mode?

Two configurations are brief

1, Nginx + keepalived master-slave configuration

file

This solution is introduced above, using a vip address, using the front end of two machines, a master, one prepared to do, but only one working machine, the machine is not another backup failure occurs in the host's time, always in the state of waste, only for disaster recovery, usually dormant for the.

2, Nginx + keepalived main configuration bis

This scheme, using two vip addresses, using the front end of the machine 2, mutual backup, two machines working simultaneously, when one of the machines fails, the request is transferred to the two machines is a machine load, as shown below :

file

[Actual] elasticsearch write speed upgrade of Case Studies

Do a micro-channel group can make money with java chat robot (PC protocol)

Mysql a mega efficient data import Redis

Visual interface parameters generated online JVM

Fault Analysis java online tuning +

ELK practice Raiders

Guess you like

Origin www.cnblogs.com/hyq0823/p/12571781.html