「实战篇」开源项目docker化运维部署-后端java部署(七)

发布时间:2020-04-07 11:43:21 作者:IT人故事
来源:网络 阅读:392

原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!
原文链接地址:「实战篇」开源项目docker化运维部署-后端java部署(七)

本节主要说说后端的部署需要注意的点,本身renren-fast这个项目就是通过springboot来进行开发的,springboot内置的有tomcat所以,咱们不用在容器内安装tomcat,直接用jar文件来进行运行。源码:https://github.com/limingios/netFuture/blob/master/后端/ 后端双机热备
https://gitee.com/renrenio/renren-fast

「实战篇」开源项目docker化运维部署-后端java部署(七)

spring boot

「实战篇」开源项目docker化运维部署-后端java部署(七)

「实战篇」开源项目docker化运维部署-后端java部署(七)

注意:java后台程序不使用虚拟网络,直接使用宿主的ip端口。--net=host

设置负载均衡

所有的负载都发送到一个jar包上,如果量比较大,tomcat最大承受500的并发,Tomcat可能就挂了。

「实战篇」开源项目docker化运维部署-后端java部署(七)

「实战篇」开源项目docker化运维部署-后端java部署(七)


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    proxy_redirect          off;
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size    10m;
    client_body_buffer_size   128k;
    proxy_connect_timeout   5s;
    proxy_send_timeout      5s;
    proxy_read_timeout      5s;
    proxy_buffer_size        4k;
    proxy_buffers           4 32k;
    proxy_busy_buffers_size  64k;
    proxy_temp_file_write_size 64k;

    upstream tomcat {
        server 192.168.66.100:6001;
        server 192.168.66.100:6002;
        server 192.168.66.100:6003;
    }
    server {
        listen       6101;
        server_name  192.168.66.100; 
        location / {  
            proxy_pass   http://tomcat;
            index  index.html index.htm;  
        }  

    }
}

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;

keepalive_timeout  65;

#gzip  on;

proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size   128k;
proxy_connect_timeout   5s;
proxy_send_timeout      5s;
proxy_read_timeout      5s;
proxy_buffer_size        4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size  64k;
proxy_temp_file_write_size 64k;

upstream tomcat {
    server 192.168.66.100:6001;
    server 192.168.66.100:6002;
    server 192.168.66.100:6003;
}
server {
    listen       6102;
    server_name  192.168.66.100; 
    location / {  
        proxy_pass   http://tomcat;
        index  index.html index.htm;  
    }  

}

}


* 创建nginx的指令<n2>
> nginx使用宿主的主机ip。--net=host
``` bash
# 容器内的nginx启动加载容器外的配置文件
docker run -it -d --name n2 -v /root/v2/nginx.conf:/etc/nginx/nginx.conf --net=host --privileged nginx
后端项目的双机热备负载均衡方案

之前已经设置了n1 和n2,都可以正常的访问后端,但是没有设置keepalived,他们之前无法争抢ip,无法做到双机热备。这次说说双机热备。

「实战篇」开源项目docker化运维部署-后端java部署(七)

进入容器n1然后安装keepalived

keepalived必须在n1所在的容器之内,也可以在docker仓库里面下载一个nginx-keepalived的镜像。这里直接在容器内安装keepalived。

docker exec -it n1 /bin/bash
#写入dns,防止apt-get update找不到服务器
echo "nameserver 8.8.8.8" | tee /etc/resolv.conf > /dev/null 
apt-get clean
apt-get update
apt-get install vim
vi /etc/apt/sources.list 

sources.list 添加下面的内容

deb http://mirrors.163.com/ubuntu/ precise main universe restricted multiverse 
deb-src http://mirrors.163.com/ubuntu/ precise main universe restricted multiverse 
deb http://mirrors.163.com/ubuntu/ precise-security universe main multiverse restricted 
deb-src http://mirrors.163.com/ubuntu/ precise-security universe main multiverse restricted 
deb http://mirrors.163.com/ubuntu/ precise-updates universe main multiverse restricted 
deb http://mirrors.163.com/ubuntu/ precise-proposed universe main multiverse restricted 
deb-src http://mirrors.163.com/ubuntu/ precise-proposed universe main multiverse restricted 
deb http://mirrors.163.com/ubuntu/ precise-backports universe main multiverse restricted 
deb-src http://mirrors.163.com/ubuntu/ precise-backports universe main multiverse restricted 
deb-src http://mirrors.163.com/ubuntu/ precise-updates universe main multiverse restricted

「实战篇」开源项目docker化运维部署-后端java部署(七)

apt-get clean
apt-get update
apt-get install keepalived
apt-get install vim

「实战篇」开源项目docker化运维部署-后端java部署(七)

容器内的路径:/etc/keepalived/keepalived.conf

vi /etc/keepalived/keepalived.conf

keepalived.conf

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.66.151
}
}
virtual_server 192.168.66.151 6201 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.66.100 6101 {
weight 1
}
}
  1. VI_1 名称可以自定义
  2. state MASTER | keepalived的身份(MASTER主服务器,BACKUP备份服务器,不会抢占虚拟机ip)。如果都是主MASTER的话,就会进行互相争抢IP,如果抢到了就是MASTER,另一个就是SLAVE。
  3. interface网卡,定义一个虚拟IP定义到那个网卡上边。网卡设备的名称。eth43是宿主机是网卡。
  4. virtual_router_id 51 | 虚拟路由标识,MASTER和BACKUP的虚拟路由标识必须一致。标识可以是0-255。
  5. priority 100 | 权重。MASTER权重要高于BACKUP 数字越大优选级越高。可以根据硬件的配置来完成,权重最大的获取抢到的级别越高。
  6. advert_int 1 | 心跳检测。MASTER与BACKUP节点间同步检查的时间间隔,单位为秒。主备之间必须一致。
  7. authentication | 主从服务器验证方式。主备必须使用相同的密码才能正常通信。进行心跳检测需要登录到某个主机上边所有有账号密码。
  8. virtual_ipaddress | 虚拟ip地址,可以设置多个虚拟ip地址,每行一个。根据上边配置的eth43上配置的ip。192.168.66.151 是自己定义的虚拟ip

容器内启动

service keepalived start

「实战篇」开源项目docker化运维部署-后端java部署(七)

进入容器n2然后安装keepalived

keepalived必须在n2所在的容器之内,也可以在docker仓库里面下载一个nginx-keepalived的镜像。这里直接在容器内安装keepalived。

docker exec -it n2 /bin/bash
#写入dns,防止apt-get update找不到服务器
echo "nameserver 8.8.8.8" | tee /etc/resolv.conf > /dev/null 
apt-get clean
apt-get update
apt-get install vim
vi /etc/apt/sources.list 

sources.list 添加下面的内容

deb http://mirrors.163.com/ubuntu/ precise main universe restricted multiverse 
deb-src http://mirrors.163.com/ubuntu/ precise main universe restricted multiverse 
deb http://mirrors.163.com/ubuntu/ precise-security universe main multiverse restricted 
deb-src http://mirrors.163.com/ubuntu/ precise-security universe main multiverse restricted 
deb http://mirrors.163.com/ubuntu/ precise-updates universe main multiverse restricted 
deb http://mirrors.163.com/ubuntu/ precise-proposed universe main multiverse restricted 
deb-src http://mirrors.163.com/ubuntu/ precise-proposed universe main multiverse restricted 
deb http://mirrors.163.com/ubuntu/ precise-backports universe main multiverse restricted 
deb-src http://mirrors.163.com/ubuntu/ precise-backports universe main multiverse restricted 
deb-src http://mirrors.163.com/ubuntu/ precise-updates universe main multiverse restricted

「实战篇」开源项目docker化运维部署-后端java部署(七)

apt-get clean
apt-get update
apt-get install keepalived
apt-get install vim

「实战篇」开源项目docker化运维部署-后端java部署(七)

容器内的路径:/etc/keepalived/keepalived.conf

vi /etc/keepalived/keepalived.conf

keepalived.conf

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.66.151
}
}
virtual_server 192.168.66.151 6201 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.66.100 6101 {
weight 1
}
}
  1. VI_1 名称可以自定义
  2. state MASTER | keepalived的身份(MASTER主服务器,BACKUP备份服务器,不会抢占虚拟机ip)。如果都是主MASTER的话,就会进行互相争抢IP,如果抢到了就是MASTER,另一个就是SLAVE。
  3. interface网卡,定义一个虚拟IP定义到那个网卡上边。网卡设备的名称。eth43是宿主机是网卡。
  4. virtual_router_id 51 | 虚拟路由标识,MASTER和BACKUP的虚拟路由标识必须一致。标识可以是0-255。
  5. priority 100 | 权重。MASTER权重要高于BACKUP 数字越大优选级越高。可以根据硬件的配置来完成,权重最大的获取抢到的级别越高。
  6. advert_int 1 | 心跳检测。MASTER与BACKUP节点间同步检查的时间间隔,单位为秒。主备之间必须一致。
  7. authentication | 主从服务器验证方式。主备必须使用相同的密码才能正常通信。进行心跳检测需要登录到某个主机上边所有有账号密码。
  8. virtual_ipaddress | 虚拟ip地址,可以设置多个虚拟ip地址,每行一个。根据上边配置的eth43上配置的ip。192.168.66.151 是自己定义的虚拟ip

容器内启动

service keepalived start

「实战篇」开源项目docker化运维部署-后端java部署(七)

PS:到此未知后端的nginx双负载,双热备方案已经实现了,

「实战篇」开源项目docker化运维部署-后端java部署(七)

推荐阅读:
  1. Kubernetes运维之部署主流JAVA应用
  2. Saltstack(自动化运维环境部署)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

docker 云计算 nginx

上一篇:使用香港云服务器是不是更安全?

下一篇:react-router实现原理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》