search
HomeOperation and MaintenanceNginxnginx+keepalived high availability master-slave configuration method

1. 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. Install dependent environment

Copy code The code is as follows:

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

##2.Upload nginx to the opt directory

3. Unzip and install

  # tar -zxvf nginx-1.6.2.tar.gz

  # cd nginx-1.6.2
  # ./configure --prefix=/opt/nginx (prefix=/opt/nginx 这个指定的是 nginx目录)
  # make && make install

4. Modify the nginx listening port and index.html

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

nginx+keepalived high availability master-slave configuration method ##vi /opt/nginx/html/index.html

nginx+keepalived high availability master-slave configuration method

5. nginx startup and common commands

Configuration test: /opt/nginx/sbin/nginx -t The following interface appears, indicating that the configuration is OK

nginx+keepalived high availability master-slave configuration methodStart: /opt/nginx/sbin/nginx

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

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

6. Start nginx

vi /etc/rc.local

Join: /opt/nginx/sbin/nginx

7. Modify the firewall open port

vi /etc/sysconfig/iptables

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

Restart the firewall:

service iptables restart

8. Problem

Problems encountered when starting nginx

##vi / etc/ld.so.confnginx+keepalived high availability master-slave configuration method

Add: /opt/nginx/lib/

9.nginx load balancing

nginx load balancing Mainly done by the upstream module

Modify the nginx configuration file

vi /data/nginx/conf/nginx.conf

Add the following content: (the name web_pools Variable)

  upstream web_pools { 
  server 10.0.6.108:7080weight=1;
   server 10.0.0.85:8980weight=1;
  }

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

The result is as follows:

where weight is the weight and backup is the backup server. The backup server will start only after other servers are down. nginx+keepalived high availability master-slave configuration method

3. Keepalived installation

1. Upload keepalived to the opt directory

2. Unzip and install

  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 start at boot

chkconfig keepalived on

5.

Modify the keepalived configuration file

vi /etc/keepalived/ keepalived.conf

  ! configuration file for keepalived (!、#都是注释)

  global_defs { #全局配置
  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 #这个配置要唯一
  } 

  vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
    interval 2 ## 检测时间间隔
    weight -20 ## 如果条件成立,权重-20
  } 

  vrrp_instance vi_1 { #实例 vi_1 名字可以随意 但是不建议修改
    state master # 主服务器master 从服务器 backup
    interface em1 # em1 网卡
    virtual_router_id 51 #virtual_router_id 主备要一致
    priority 100   # 优先级 数字越大 优先级越高 priority 的值 主服务器要大于 从服务器
    advert_int 1  #设定master与backup负载均衡器之间同步检查的时间间隔,单位是秒
    authentication { # 主从通信 验证类型及密码 
      auth_type pass  #设置vrrp验证类型,主要有pass和ah两种
      auth_pass 1111  #设置vrrp验证密码,在同一个vrrp_instance下,master与backup必须使用相同的密码才能正常通信
    } 

    ## 将 track_script 块加入 instance 配置块
    track_script {
      chk_nginx ## 执行 nginx 监控的服务
    }

    virtual_ipaddress {
    192.168.38.100/24 #vrrp ha 虚拟地址 如果有多个vip,继续换行填写
    }
  } 

6. Write nginx status detection script

vi /etc/keepalived/nginx_check.sh

The content is 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, grant execution permissions to the script:

chmod x/etc/keepalived/nginx_check.sh

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

a.router_id is inconsistent

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

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

8.keepalived command

Start: servicekeepalived start

Stop: servicekeepalived stop

Restart: servicekeepalived restart

9. Issues to note

a.vip was not bound successfully

Solution: ip addr Check the name of the network card where the local ip is located, and then modify the configuration file

##vi /etc/keepalived/keepalived.conf

nginx+keepalived high availability master-slave configuration method

After saving, servicekeepalived restart can restart the keepalived service

nginx+keepalived high availability master-slave configuration method10. Test

Start the master-slave nginx and keepalived services

Master From the server respectively: ip add | grep 192.168.38.100

can be seen on 192.168.38.64

## and at the same time on 192.168.38.66

nginx+keepalived high availability master-slave configuration method

When you kill keepalived on the master server, the slave server

nginx+keepalived high availability master-slave configuration method

When you start keepalived on the master server again, there will be results on the master server. , no results from the server.

当杀死 nginx后,keepalived则会自动启动 nginx服务

11. keepalived脑裂 (ip add | grep 192.168.38.100 在主从都有结果)

解决方案:防火墙问题

iptables-iinput4-pvrrp-jaccept  

service iptables save

service iptables restart

nginx+keepalived high availability master-slave configuration method

The above is the detailed content of nginx+keepalived high availability master-slave configuration method. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
The Advantages of NGINX: Speed, Efficiency, and ControlThe Advantages of NGINX: Speed, Efficiency, and ControlMay 12, 2025 am 12:13 AM

The reason why NGINX is popular is its advantages in speed, efficiency and control. 1) Speed: Adopt asynchronous and non-blocking processing, supports high concurrent connections, and has strong static file service capabilities. 2) Efficiency: Low memory usage and powerful load balancing function. 3) Control: Through flexible configuration file management behavior, modular design facilitates expansion.

NGINX vs. Apache: Community, Support, and ResourcesNGINX vs. Apache: Community, Support, and ResourcesMay 11, 2025 am 12:19 AM

The differences between NGINX and Apache in terms of community, support and resources are as follows: 1. Although the NGINX community is small, it is active and professional, and official support provides advanced features and professional services through NGINXPlus. 2.Apache has a huge and active community, and official support is mainly provided through rich documentation and community resources.

NGINX Unit: An Introduction to the Application ServerNGINX Unit: An Introduction to the Application ServerMay 10, 2025 am 12:17 AM

NGINXUnit is an open source application server that supports a variety of programming languages ​​and frameworks, such as Python, PHP, Java, Go, etc. 1. It supports dynamic configuration and can adjust application configuration without restarting the server. 2.NGINXUnit supports multi-language applications, simplifying the management of multi-language environments. 3. With configuration files, you can easily deploy and manage applications, such as running Python and PHP applications. 4. It also supports advanced configurations such as routing and load balancing to help manage and scale applications.

Using NGINX: Optimizing Website Performance and ReliabilityUsing NGINX: Optimizing Website Performance and ReliabilityMay 09, 2025 am 12:19 AM

NGINX can improve website performance and reliability by: 1. Process static content as a web server; 2. forward requests as a reverse proxy server; 3. allocate requests as a load balancer; 4. Reduce backend pressure as a cache server. NGINX can significantly improve website performance through configuration optimizations such as enabling Gzip compression and adjusting connection pooling.

NGINX's Purpose: Serving Web Content and MoreNGINX's Purpose: Serving Web Content and MoreMay 08, 2025 am 12:07 AM

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

NGINX Unit: Streamlining Application DeploymentNGINX Unit: Streamlining Application DeploymentMay 07, 2025 am 12:08 AM

NGINXUnit simplifies application deployment with dynamic configuration and multilingual support. 1) Dynamic configuration can be modified without restarting the server. 2) Supports multiple programming languages, such as Python, PHP, and Java. 3) Adopt asynchronous non-blocking I/O model to improve high concurrency processing performance.

NGINX's Impact: Web Servers and BeyondNGINX's Impact: Web Servers and BeyondMay 06, 2025 am 12:05 AM

NGINX initially solved the C10K problem and has now developed into an all-rounder who handles load balancing, reverse proxying and API gateways. 1) It is well-known for event-driven and non-blocking architectures and is suitable for high concurrency. 2) NGINX can be used as an HTTP and reverse proxy server, supporting IMAP/POP3. 3) Its working principle is based on event-driven and asynchronous I/O models, improving performance. 4) Basic usage includes configuring virtual hosts and load balancing, and advanced usage involves complex load balancing and caching strategies. 5) Common errors include configuration syntax errors and permission issues, and debugging skills include using nginx-t command and stub_status module. 6) Performance optimization suggestions include adjusting worker parameters, using gzip compression and

Nginx Troubleshooting: Diagnosing and Resolving Common ErrorsNginx Troubleshooting: Diagnosing and Resolving Common ErrorsMay 05, 2025 am 12:09 AM

Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use