Nginx+Keeplived部署

57 阅读4分钟

环境:

Master:192.168.1.10 Backup:192.168.1.11 Nfs:192.168.1.12 Web1:192.168.1.128 Web2:192.168.1.129 Web3:192.168.1.130 vip:192.168.1.200

配置NFS

[root@nfs ~]# systemctl stop firewalld.service 
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable firewalld.service
[root@nfs ~]# yum install rpcbind nfs-utils -y
[root@nfs ~]# mkdir /web1
[root@nfs ~]# mkdir /web2
[root@nfs ~]# mkdir /web3
[root@nfs ~]# echo "<h1> this is Nginx1. </h1>" > /web1/index.html
[root@nfs ~]# echo "<h1> this is Nginx2. </h1>" > /web1/index.html
[root@nfs ~]# echo "<h1> this is Nginx3. </h1>" > /web1/index.html
[root@nfs ~]# vim /etc/exports

            /Web1 192.168.1.128/24(ro)
            /Web2 192.168.1.129/24(ro)
            /Web3 192.168.1.130/24(ro)
            
[root@nfs ~]# systemctl restart nfs
[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# showmount -e
Export list for nfs:
/Web3:192.168.1.130/24(ro)  *
/Web2:192.168.1.129/24(ro) *
/Web1:192.168.1.128/24(ro) *

image.png

image.png

image.png

image.png

image.png

image.png

配置web1

[root@tomcat1 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@tomcat1 ~]# tar xf nginx-1.12.2.tar.gz
[root@tomcat1 ~]# useradd -M -s /sbin/nologin nginx  #创建不带宿主,不可登录nginx用户
[root@tomcat1 ~]# cd nginx-1.12.2
[root@tomcat1 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx  --user=nginx --group=nginx
[root@tomcat1 nginx-1.12.2]# make && make install  #编译安装nginx
[root@tomcat1 nginx-1.12.2]# cd
[root@tomcat1 ~]# mount 192.168.1.12:/web1 /usr/local/nginx/html
[root@tomcat1 ~]# df -Th
[root@tomcat1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin    #命令链接,方便使用
[root@tomcat1 ~]# nginx     #开启
[root@tomcat1 ~]# netstat -anpt | grep nginx     #查看端口是否开启
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      57499/nginx: master 
[root@tomcat1 ~]# curl http://localhost
<h1> this is Nginx1. </h1>

image.png

image.png

image.png

image.png

image.png

image.png

image.png

配置web2

    [root@tomcat2 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
    [root@tomcat2 ~]# tar xf nginx-1.12.2.tar.gz
    [root@tomcat2 ~]# useradd -M -s /sbin/nologin nginx  #创建不带宿主,不可登录nginx用户
    [root@tomcat2 ~]# cd nginx-1.12.2
    [root@tomcat2 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx  --user=nginx --group=nginx
    [root@tomcat2 nginx-1.12.2]# make && make install  #编译安装nginx
    [root@tomcat2 nginx-1.12.2]# cd
    [root@tomcat2 ~]# mount 192.168.1.12:/web2 /usr/local/nginx/html
    [root@tomcat2 ~]# df -Th
    [root@tomcat2 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin    #命令链接,方便使用
    [root@tomcat2 ~]# nginx     #开启
    [root@tomcat2 ~]# netstat -anpt | grep nginx     #查看端口是否开启
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      57499/nginx: master 
    [root@tomcat2 ~]# curl http://localhost
    <h1> this is Nginx2. </h1>
    

image.png

配置web3

        [root@tomcat3 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
        [root@tomcat3 ~]# tar xf nginx-1.12.2.tar.gz
        [root@tomcat3 ~]# useradd -M -s /sbin/nologin nginx  #创建不带宿主,不可登录nginx用户
        [root@tomcat3 ~]# cd nginx-1.12.2
        [root@tomcat3 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx  --user=nginx --group=nginx
        [root@tomcat3 nginx-1.12.2]# make && make install  #编译安装nginx
        [root@tomcat3 nginx-1.12.2]# cd
        [root@tomcat3 ~]# mount 192.168.1.12:/web3 /usr/local/nginx/html
        [root@tomcat3 ~]# df -Th
        [root@tomcat3 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin    #命令链接,方便使用
        [root@tomcat3 ~]# nginx     #开启
        [root@tomcat3 ~]# netstat -anpt | grep nginx     #查看端口是否开启
        tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      57499/nginx: master 
        [root@tomcat3 ~]# curl http://localhost
        <h1> this is Nginx3. </h1>
        

image.png

配置主调度器

[root@master ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
    [root@master ~]# tar xf nginx-1.12.2.tar.gz
    [root@master ~]# useradd -M -s /sbin/nologin nginx  #创建不带宿主,不可登录nginx用户
    [root@master ~]# cd nginx-1.12.2
    [root@master nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
    [root@master nginx-1.12.2]# make && make install  #编译安装nginx
    [root@master nginx-1.12.2]# ln -s /usr/local/nginx/conf/nginx.conf /etc/
    [root@master nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
    [root@master nginx-1.12.2]# cd
    [root@master ~]# vim /usr/local/nginx/conf/nginx.conf#gzip  on;
            upstream tomcat_server {
                    server 192.168.1.128:8080 weight=1;
                    server 192.168.1.129:8080 weight=1;
                    server 192.168.1.130:8080 weight=1;
            }
        server {
            listen       80;
            server_name  localhost;
             #charset koi8-r;
           #access_log  logs/host.access.log  main;
            location / {
            #    root   html;
           #    index  index.html index.htm;
                 proxy_pass http://tomcat_server;
            }
    …
    [root@master ~]# nginx -t
    [root@master ~]# nginx
    [root@master ~]# netstat -anpt | grep 80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6393/nginx: master 
    [root@master ~]# curl http://localhost
    <h1> this is Nginx1. </h1>
    [root@master ~]# curl http://localhost
    <h1> this is Nginx2. </h1>
    [root@master ~]# curl http://localhost
    <h1> this is Nginx3. </h1>
    [root@master ~]# curl http://localhost
    <h1> this is Nginx1. </h1>   

image.png

image.png

image.png

image.png

image.png

image.png

配置backup

[root@Backup ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@Backup ~]# tar xf nginx-1.12.2.tar.gz
[root@Backup ~]# useradd -M -s /sbin/nologin nginx  #创建不带宿主,不可登录nginx用户
[root@Backup ~]# cd nginx-1.12.2
[root@Backup nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@Backup nginx-1.12.2]# make && make install  #编译安装nginx
[root@Backup nginx-1.12.2]# ln -s /usr/local/nginx/conf/nginx.conf /etc/
[root@Backup nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
[root@Backup nginx-1.12.2]# cd
[root@Backup ~]# vim /usr/local/nginx/conf/nginx.conf#gzip  on;
        upstream tomcat_server {
                server 192.168.1.128:8080 weight=1;
                server 192.168.1.129:8080 weight=1;
                server 192.168.1.130:8080 weight=1;
        }
    server {
        listen       80;
        server_name  localhost;
         #charset koi8-r;
       #access_log  logs/host.access.log  main;
        location / {
        #    root   html;
       #    index  index.html index.htm;
             proxy_pass http://tomcat_server;
        }
…
[root@Backup ~]# nginx -t
[root@Backup ~]# nginx
[root@Backup ~]# netstat -anpt | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6393/nginx: master 
[root@Backup ~]# curl http://localhost
<h1> this is Nginx1. </h1>
[root@Backup ~]# curl http://localhost
<h1> this is Nginx2. </h1>
[root@Backup ~]# curl http://localhost
<h1> this is Nginx3. </h1>
[root@Backup ~]# curl http://localhost
<h1> this is Nginx1. </h1>

image.png

master配置keepalived

[root@master ~]# yum install keepalived.x86_64 -y
[root@master init.d]#  cd /etc/keepalived/
[root@master keepalived]#  cp keepalived.conf keepalived.conf.bak
[root@master keepalived]# vim keepalived.conf
删除所有
! Configuration File for keepalived
global_defs {
     router_id master       #本服务器的名称
}
vrrp_script chk_http_port {       #vrrp脚本
script "/usr/local/src/nginx.sh"    #脚本路径 最后手动执行此脚本,以确保脚本能够正常运行
interval 2          #检测间隔时间 单位秒
 weight 2
}
vrrp_instance vi_1 {       #实例名称
state MASTER        #指定keepalived的角色,MASTER为主,BACKUP为备
interface ens33    #当前进行vrrp通讯的网络接口卡
virtual_router_id 51   #虚拟路由编号,主从要一致
priority 110            #优先级,数值越大,获取地址优先级越大
adver_int 1             #检查间隔,默认为1s (vrrp组播周期秒数)
authentication {       #认证方式
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port     #调用检测脚本
}
virtual_ipaddress {
 192.168.1.200           #定义虚拟ip(VIP)
}
}

image.png

image.png 在调度器1上配置脚本文件,并拷贝给调度器2

[root@master keepalived]# scp /usr/local/src/nginx.sh root@192.168.10.20:/usr/local/src
[root@master keepalived]# chmod +x /usr/local/src/nginx.sh
[root@master keepalived]# systemctl start keepalived.service
[root@master keepalived]# systemctl status keepalived.service

image.png

image.png

image.png

image.png

备keepalived 配置

 [root@Backup ~]# yum install keepalived.x86_64 -y
[root@Backup init.d]#  cd /etc/keepalived/
 [root@Backup keepalived]#  cp keepalived.conf keepalived.conf.bak
 [root@Backup keepalived]# vim keepalived.conf
    删除所有
! Configuration File for keepalived

global_defs {
     router_id backup       #本服务器的名称
}
vrrp_script chk_http_port {       #vrrp脚本
     script "/usr/local/src/nginx.sh"    #脚本路径 最后手动执行此脚本,以确保脚本能
够正常运行
     interval 2          #检测间隔时间 单位秒
     weight 2
}
vrrp_instance vi_1 {       #实例名称
     state BACKUP        #指定keepalived的角色,MASTER为主,BACKUP为备 
     interface ens33    #当前进行vrrp通讯的网络接口卡
     virtual_router_id 51   #虚拟路由编号,主从要一致
     priority 100            #优先级,数值越大,获取地址优先级越大
     adver_int 1             #检查间隔,默认为1s (vrrp组播周期秒数)
     authentication {       #认证方式
          auth_type PASS
          auth_pass 1111
}
track_script {
       chk_http_port     #调用检测脚本
}
virtual_ipaddress {
     192.168.1200           #定义虚拟ip(VIP)
}
}


[root@Backup keepalived]# chmod +x /usr/local/src/nginx.sh
[root@Backup keepalived]# systemctl start keepalived.service
[root@Backup keepalived]# systemctl status keepalived.service

image.png

image.png

image.png

image.png

image.png

模拟master挂了以后

master上

[root@master ~]# systemctl stop keepalived.service
[root@master ~]# ip addr

image.png backup上

[root@Backup ~]# ip addr

image.png

浏览器访问

image.png

image.png image.png