keepalived+nginx+tomcat高可用负载均衡

前提-配置java环境变量

准备centos服务器或者虚拟机,以及JAVA环境,这个是必须的前提啊,我相信你可以的。

tomcat

# 下载
curl "http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.tar.gz" -o apache-tomcat-8.5.31.tar.gz

# 解压
tar -xvf apache-tomcat-8.5.31.tar.gz 

# 启动
./apache-tomcat-8.5.31/bin/startup.sh

nginx

下载及解压 相关的模块到/u01目录

curl "http://nginx.org/download/nginx-1.14.0.tar.gz" -o nginx-1.14.0.tar.gz
tar -xvf nginx-1.14.0.tar.gz

curl "http://www.zlib.net/zlib-1.2.11.tar.gz" -o zlib-1.2.11.tar.gz
tar -xvf zlib-1.2.11.tar.gz

curl "https://jaist.dl.sourceforge.net/project/pcre/pcre/8.41/pcre-8.41.tar.gz" -o pcre-8.41.tar.gz
tar -xvf pcre-8.41.tar.gz

curl "https://www.openssl.org/source/openssl-1.0.2o.tar.gz" -o openssl-1.0.2o.tar.gz
tar -xvf openssl-1.0.2o.tar.gz

编译安装nginx

cd nginx-1.14.0

./configure  --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre=../pcre-8.41 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.2o

make 

make install
如果有异常:./configure: error: C compiler cc is not found,则安装gcc等软件

yum -y install gcc gcc-c++ autoconf automake make

配置并启动

# 最精简的nginx负载均衡配置,仅15行配置
events {
    # 并发连接数
    worker_connections  1024;
}
http {
    # Tomcat服务器集群
    upstream tomcat_servers {
        server 192.168.100.1:8081;
        server 192.168.100.1:8080;
    }
    server {
        # 监听80端口
        listen    80;
        
        # 将所有请求交给Tomcat集群去处理
        location / {
            proxy_pass http://tomcat_servers;
        }
    }
}

# 执行命令 /usr/local/nginx/sbin/nginx 就可以启动啦

接下来就是通过keepalived实现nginx的高可用了

学习感言

这是我在学习下面这些高并发分布式技术时的笔记,不知道怎么发附件,需要配置文件的话我后续补上,或者你可以私信我。
keepalived+nginx+tomcat高可用负载均衡_第1张图片

keepalived

下载安装相关相关的组件

yum -y install openssl-devel 
yum -y install libnl libnl-devel
yum install -y libnfnetlink-devel

下载安装keepalived

# 下载
curl "http://www.keepalived.org/software/keepalived-1.4.4.tar.gz" -o keepalived-1.4.4.tar.gz
tar -xvf keepalived-1.4.4.tar.gz

cd keepalived-1.4.4  

# 安装到/usr/local/keepalived目录
./configure --prefix=/usr/local/keepalived --sysconf=/etc  
make && make install

配置文件存放地址

配置放在/etc/keepalived/,三份配置文件(一个nginx_monitor监控脚本,主备各一份keepalived配置)

nginx监控shell脚本 nginx_monitor.sh 文件

# 创建nginx monitor 脚本,并赋予可执行权限
chmod +x /etc/keepalived/nginx_monitor.sh
# 测试一下脚本能不能执行
执行命令:/etc/keepalived/nginx_monitor.sh 
没报错即表示为成功

keepalived配置

#  - master主机
keepalived-nginx-master.conf
#  - backup备机
keepalived-nginx-backup.conf

启动keepalived

# 启动master主机
/usr/local/keepalived/sbin/keepalived -f /etc/keepalived/keepalived-nginx-master.conf
# 启动backup备机
/usr/local/keepalived/sbin/keepalived -f /etc/keepalived/keepalived-nginx-backup.conf

停止

ps -ef | grep keepalived
kill -9  关闭相关的进程

测试高可用

1. 关掉备机,功能完全不受影响。
2. 关掉主机,虚拟IP漂移到备机,备机开始工作。
3. 关掉主机nginx,主机监控到无nginx后,自动切换

你可能感兴趣的:(java,nginx,keepalived,高可用,分布式)