Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of highly available Master-Slave configuration in nginx+keepalived

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this "nginx+keepalived High availability Master-Slave configuration method" article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "nginx+keepalived High availability Master-Slave configuration method" article.

I. system environment and software version

Centos 6.6 x64

Keepalived-1.2.18.tar.gz

Nginx-1.6.2.tar.gz

Master server: 192.168.38.64

Slave server: 192.168.38.66

Vip: 192.168.38.100

2. Nginx installation (master-slave installation is consistent)

1. Installation depends on the environment

The copy code is as follows:

Yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-developenssl openssl-devel

two。 Upload nginx to opt directory

3. Decompression and installation

# tar-zxvf nginx-1.6.2.tar.gz # cd nginx-1.6.2 #. / configure-- prefix=/opt/nginx (prefix=/opt/nginx is specified as the nginx directory) # make & & make install

4. Modify nginx listening port and index.html

# vi / opt/nginx/conf/nginx.conf

Vi / opt/nginx/html/index.html

5. Nginx startup and common commands

Configuration test: / opt/nginx/sbin/nginx-t shows the following interface, indicating that the configuration is fine.

Start: / opt/nginx/sbin/nginx

Restart: / opt/nginx/sbin/nginx-s reload

Stop: / opt/nginx/sbin/nginx-s stop

6. Boot and start nginx

Vi / etc/rc.local

Join: / opt/nginx/sbin/nginx

7. Modify firewall open port

Vi / etc/sysconfig/iptables

Add:-an input-p tcp-m state-- state new-m tcp-- dport 8888-j accept

Restart the firewall: service iptables restart

8. problem

Problems encountered in starting nginx

Vi / etc/ld.so.conf

Add: / opt/nginx/lib/

Load balancing of 9.nginx

The load balancing of nginx is mainly completed by the module upstream.

Modify the configuration file of nginx

Vi / data/nginx/conf/nginx.conf

Add the following: (the name of web_pools is variable)

Upstream web_pools {server 10.0.6.108 server 7080 weightweights 1; server 10.0.0.85 VR 8980 weightweights 1;}

Configure the proxy_pass in the location node under the server node to: http:// + upstream name

The results are as follows:

Where weight is the weight backup is the standby server, and the standby server will only start when other servers are down.

III. Keepalived installation

Upload 1.keepalived to the opt directory

two。 Decompression and installation

Tar-zxvf keepalived-1.2.18.tar.gz cd keepalived-1.2.18. / configure-- prefix=/opt/keepalived make & & make install

3. Install keepalived as a linux service

Cp / opt/keepalived/etc/rc.d/init.d/keepalived / etc/init.d/ cp / opt/keepalived/etc/sysconfig/keepalived / etc/sysconfig/ ln-s / opt/sbin/keepalived / usr/sbin/ ln-s / opt/keepalived/sbin/keepalived / sbin/

4. Set the keepalived service to boot

Chkconfig keepalived on

5. Modify keepalived configuration file

Vi / etc/keepalived/keepalived.conf

! Configuration file for keepalived (!, Global_defs {# Global configuration notification_email {acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc} notification_email_from alexandre.cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id lvs_01 # this configuration should be unique} vrrp_script chk_nginx { Script "/ etc/keepalived/nginx_check.sh" # # script path to detect nginx status interval 2 # # Detection interval weight-20 # # if the condition holds Weight-20} vrrp_instance vi_1 {# instance vi_1 name is optional but it is not recommended to modify the state master # master server master slave server backup interface em1 # em1 network card virtual_router_id 51 # virtual_router_id master and standby consistent priority 100 # the higher the priority number, the higher the value of the priority master server Greater than the slave server advert_int 1 # sets the time interval for synchronization checks between master and backup load balancers Unit: authentication {# master-slave communication authentication type and password auth_type pass # set vrrp authentication type, mainly pass and ah auth_pass 1111 # set vrrp authentication password under the same vrrp_instance Master and backup must use the same password to communicate properly} # # add track_script blocks to instance configuration block track_script {chk_nginx # # Services for nginx monitoring} virtual_ipaddress {192.168.38.100 vip 24 # vrrp ha virtual address if there is more than one vip, continue to fill in line feeds}}

6. Write a script for nginx state detection

Vi / etc/keepalived/nginx_check.sh

The contents are as follows:

#! / bin/bash a = `ps-c nginx-no-header | wc-l` if [$a-eq 0]; then / opt/nginx/sbin/nginx sleep 2 if [`ps-c nginx-- no-header | wc-l`-eq 0]; then killall keepalived fi fi

After saving, assign execution permission to the script: chmod + x/etc/keepalived/nginx_check.sh

7. Note: differences between keepalived master and slave configuration files

A.router_id inconsistency

The b.state master server is master and the slave server is backup

The c.priority master server is larger than the slave server.

8.keepalived command

Launch: servicekeepalived start

Stop: servicekeepalived stop

Restart: servicekeepalived restart

9. Problems to pay attention to

A.vip failed to bind successfully

Solution: ip addr looks at the name of the network card where the local ip is located, and then modifies the configuration file

Vi / etc/keepalived/keepalived.conf

After saving, servicekeepalived restart can restart the keepalived service.

10. test

Start the master-slave nginx and keepalived services

Master / slave server: ip add | grep 192.168.38.100

Can be seen in 192.168.38.64

At the same time, at 192.168.38.66

When killing the keepalived on the master server, the slave server

When you start keepalived on the master server again, it turns out that there are results on the master server and no results on the slave server.

When nginx is killed, keepalived automatically starts the nginx service

11. Keepalived brain fissure (ip add | grep 192.168.38.100 has results in both master and slave)

Solution: firewall issu

Iptables-iinput4-pvrrp-jaccept service iptables saveservice iptables restart

The above is about the content of this article on "nginx+keepalived High availability Master-Slave configuration method". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report