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
内存飙升!记一次nginx拦截爬虫内存飙升!记一次nginx拦截爬虫Mar 30, 2023 pm 04:35 PM

本篇文章给大家带来了关于nginx的相关知识,其中主要介绍了nginx拦截爬虫相关的,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

nginx限流模块源码分析nginx限流模块源码分析May 11, 2023 pm 06:16 PM

高并发系统有三把利器:缓存、降级和限流;限流的目的是通过对并发访问/请求进行限速来保护系统,一旦达到限制速率则可以拒绝服务(定向到错误页)、排队等待(秒杀)、降级(返回兜底数据或默认数据);高并发系统常见的限流有:限制总并发数(数据库连接池)、限制瞬时并发数(如nginx的limit_conn模块,用来限制瞬时并发连接数)、限制时间窗口内的平均速率(nginx的limit_req模块,用来限制每秒的平均速率);另外还可以根据网络连接数、网络流量、cpu或内存负载等来限流。1.限流算法最简单粗暴的

nginx+rsync+inotify怎么配置实现负载均衡nginx+rsync+inotify怎么配置实现负载均衡May 11, 2023 pm 03:37 PM

实验环境前端nginx:ip192.168.6.242,对后端的wordpress网站做反向代理实现复杂均衡后端nginx:ip192.168.6.36,192.168.6.205都部署wordpress,并使用相同的数据库1、在后端的两个wordpress上配置rsync+inotify,两服务器都开启rsync服务,并且通过inotify分别向对方同步数据下面配置192.168.6.205这台服务器vim/etc/rsyncd.confuid=nginxgid=nginxport=873ho

nginx php403错误怎么解决nginx php403错误怎么解决Nov 23, 2022 am 09:59 AM

nginx php403错误的解决办法:1、修改文件权限或开启selinux;2、修改php-fpm.conf,加入需要的文件扩展名;3、修改php.ini内容为“cgi.fix_pathinfo = 0”;4、重启php-fpm即可。

如何解决跨域?常见解决方案浅析如何解决跨域?常见解决方案浅析Apr 25, 2023 pm 07:57 PM

跨域是开发中经常会遇到的一个场景,也是面试中经常会讨论的一个问题。掌握常见的跨域解决方案及其背后的原理,不仅可以提高我们的开发效率,还能在面试中表现的更加

nginx部署react刷新404怎么办nginx部署react刷新404怎么办Jan 03, 2023 pm 01:41 PM

nginx部署react刷新404的解决办法:1、修改Nginx配置为“server {listen 80;server_name https://www.xxx.com;location / {root xxx;index index.html index.htm;...}”;2、刷新路由,按当前路径去nginx加载页面即可。

Linux系统下如何为Nginx安装多版本PHPLinux系统下如何为Nginx安装多版本PHPMay 11, 2023 pm 07:34 PM

linux版本:64位centos6.4nginx版本:nginx1.8.0php版本:php5.5.28&php5.4.44注意假如php5.5是主版本已经安装在/usr/local/php目录下,那么再安装其他版本的php再指定不同安装目录即可。安装php#wgethttp://cn2.php.net/get/php-5.4.44.tar.gz/from/this/mirror#tarzxvfphp-5.4.44.tar.gz#cdphp-5.4.44#./configure--pr

nginx怎么禁止访问phpnginx怎么禁止访问phpNov 22, 2022 am 09:52 AM

nginx禁止访问php的方法:1、配置nginx,禁止解析指定目录下的指定程序;2、将“location ~^/images/.*\.(php|php5|sh|pl|py)${deny all...}”语句放置在server标签内即可。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools