vlambda博客
学习文章列表

搭建Nginx+keepalived负载高可用,实现动静分离

一、部署服务器lb01

1、部署Nginx

把软件提前下载好,放到/server/tools目录里

制作一键安装Nginx脚本,把以下内容写入脚本install_nginx.sh

[root@lb01/server/scripts]#vim install_nginx.sh

#1、编译安装

#①安装依赖环境

yum install pcre pcre-devel -y

yum install openssl openssl-devel -y

#②规范软件包存放路径 

#mkdir /server/tools

cd /server/tools

#<==下载软件包,事先下载好放到tools目录里

#wget http://nginx.org/download/nginx-1.16.1.tar.gz

tar xf /server/tools/nginx-1.16.1.tar.gz

#进程使用的用户

useradd -s /sbin/nologin mifengweb -u 1002 -M 

#③配置:

#<==建软件安装目录

mkdir /application/nginx-1.16.1           

cd /server/tools/nginx-1.16.1

./configure --user=mifengweb --group=mifengweb --prefix=/application/nginx-1.16.1/ --with-http_stub_status_module --with-http_ssl_module --with-pcre

#验证是否配置成功:返回0代表正确

echo $? 

#④编译:

#<==把代码编译成二进制文件的过程

make                                

#<==把软件拷贝到指定的位置

make install                      

#<==验证是否成功

#echo $?

#<==建立软连接,开发人员使用。

ln -s /application/nginx-1.16.1/ /application/nginx    

#<==启动前检查配置文件语法

/application/nginx/sbin/nginx -t

#<==启动服务

/application/nginx/sbin/nginx   

#⑤环境变量

echo 'export PATH="/application/nginx/sbin:$PATH"'>>/etc/profile

#<==查看是否成功写入环境变量

tail -1 /etc/profile    

#生效

. /etc/profile

#<==验证是否成功

nginx -t

###设置开机启动>覆盖,>>追加

cp /etc/rc.d/rc.local /etc/rc.d/rc.local.ori

cat>>/etc/rc.d/rc.local<<EOF

/application/nginx/sbin/nginx

EOF

#分配执行权限开机才可启动

chmod +x /etc/rc.d/rc.local

2、部署keeplived

把软件提前下载好,放到/server/tools目录里

制作一键安装Nkeeplived脚本,把以下内容写入脚本install_keeplived.sh

[root@lb01/server/scripts]#vim install_keeplived.sh

#①安装依赖环境

yum install -y openssl openssl-devel

yum -y install libnl libnl-devel

#②规范软件包存放路径 

#mkdir /server/tools

cd /server/tools

#<==下载软件包,事先下载好放到tools目录里

#wget https://www.keepalived.org/software/keepalived-2.0.20.tar.gz

tar xf /server/tools/keepalived-2.0.20.tar.gz

cd /server/tools/keepalived-2.0.20

#③查看安装时参数

#./configure --help

./configure --prefix=/application/keepalived-2.0.20/ --with-pcre

#④编译:

#<==把代码编译成二进制文件的过程

make                                

#<==把软件拷贝到指定的位置

make install                      

#<==验证是否成功

#echo $?

#<==建立软连接,开发人员使用。

ln -s /application/keepalived-2.0.20/ /application/keepalived

#设置开机启动

#修改/application/keepalived/etc/sysconfig/keepalived文件如下

#vim /application/keepalived/etc/sysconfig/keepalived

#KEEPALIVED_OPTIONS="-f /application/keepalived/etc/keepalived/keepalived.conf -D -S 0"

#或

cat >/application/keepalived/etc/sysconfig/keepalived<<EOF

KEEPALIVED_OPTIONS="-f /application/keepalived/etc/keepalived/keepalived.conf -D -S 0"

EOF

systemctl start keepalived

systemctl enable keepalived

systemctl status keepalived

二、lb01服务器负载均衡配置

1、配置nginx代理负载均衡

#创建子配置文件存放目录

#mkdir -p /application/nginx/conf/extra

#cd /application/nginx/conf/extra

#touch /application/nginx/conf/extra/ssl.conf

#配置文件

cat >/application/nginx/conf/nginx.conf<<EOF

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream jpresspools {

        #server 172.16.1.7:8080;

        server 172.16.1.8:8080;

        #server 172.16.1.9:8080;

        server 172.16.1.10:8080;

        }

    server {  

       listen       80;

       server_name  blog.mifengdiandi.xyz;

       location / {

        proxy_pass http://jpresspools; 

       }

    }

}

EOF

#追加hosts解析

cat >>/etc/hosts<<EOF

172.16.1.7 blog.mifengdiandi.xyz

172.16.1.8 blog.mifengdiandi.xyz

172.16.1.9 blog.mifengdiandi.xyz

172.16.1.10 blog.mifengdiandi.xyz

EOF

2、补充设置web02,sweb02服务器

①选择web02和sweb02是因为他们的环境一样,博客都用的JPress。

##/application/nginx/sbin/nginx

##[root@sweb02/application/tomcat/bin]#./startup.sh 

②web02的Nginx.conf的配置文件修改

cat >/application/nginx/conf/nginx.conf<<EOF

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream webpools {

        server 172.16.1.8:8080;

       

    }

    server {

        listen       80;

        server_name blog.mifengdiandi.xyz; 

        location / {

            root   html;

            index  index.jsp index.html index.htm;

            proxy_pass http://webpools;

        }

    }

}

EOF

③sweb02的Nginx.conf的配置文件修改

cat >/application/nginx/conf/nginx.conf<<EOF

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream webpools {

        server 172.16.1.10:8080;

       

    }

    server {

        listen       80;

        server_name blog.mifengdiandi.xyz; 

        location / {

            root   html;

            index  index.jsp index.html index.htm;

            proxy_pass http://webpools;

        }

    }

}

EOF

三、配置keepalived配置文件

keepalived是自带监控执行脚本模块的,用户可以根据需求进行定义和监控对应的服务。

cat >/application/keepalived/etc/keepalived/keepalived.conf<<EOF

global_defs {

   router_id lb01

}

vrrp_script checknginx {

script "/server/scripts/checknginx.sh"

interval 5

weight 50

}


vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        10.10.0.3

    }

    track_script {

    checknginx

    }

}

virtual_server 10.10.0.3 80 {

    delay_loop 6

    lb_algo rr

    lb_kind NAT

    persistence_timeout 50

    protocol TCP

    real_server 192.168.198.132 80 {

        weight 3

        HTTP_GET {

            url {

              path /testurl/test.jsp

              digest 640205b7b0fc66c1ea91c463fac6334d

            }

            url {

              path /testurl2/test.jsp

              digest 640205b7b0fc66c1ea91c463fac6334d

            }

            url {

              path /testurl3/test.jsp

              digest 640205b7b0fc66c1ea91c463fac6334d

            }

            connect_timeout 3

            retry 3

            delay_before_retry 3

        }

    }

    real_server 192.168.198.143 80 {

        weight 1

        HTTP_GET {

            url {

              path /testurl/test.jsp

              digest 640205b7b0fc66c1ea91c463fac6334c

            }

            url {

              path /testurl2/test.jsp

              digest 640205b7b0fc66c1ea91c463fac6334c

            }

            connect_timeout 3

            retry 3

            delay_before_retry 3

        }

    }

}

EOF