大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!
原文链接地址:「实战篇」开源项目docker化运维部署-后端java部署(七)创新互联从2013年成立,我们提供高端网站建设、小程序设计、电商视觉设计、手机APP定制开发及网络营销搜索优化服务,在传统互联网与移动互联网发展的背景下,我们坚守着用标准的设计方案与技术开发实力作基础,以企业及品牌的互联网商业目标为核心,为客户打造具商业价值与用户体验的互联网+产品。
本节主要说说后端的部署需要注意的点,本身renren-fast这个项目就是通过springboot来进行开发的,springboot内置的有tomcat所以,咱们不用在容器内安装tomcat,直接用jar文件来进行运行。源码:https://github.com/limingios/netFuture/blob/master/后端/ 后端双机热备
https://gitee.com/renrenio/renren-fast
之前配置的redis集群,修改下单节点的吧,把所有的redis集群都放上去。
spring: # 环境 dev|test|prod profiles: active: dev # jackson时间格式化 jackson: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss http: multipart: max-file-size: 100MB max-request-size: 100MB enabled: true redis: open: false # 是否开启redis缓存 true开启 false关闭 database: 0 #host: localhost #port: 6379 #password: # 密码(默认为空) timeout: 6000 # 连接超时时长(毫秒) cluster: nodes: - 172.19.0.2:6379 - 172.19.0.3:6379 - 172.19.0.4:6379 - 172.19.0.5:6379 - 172.19.0.6:6379 - 172.19.0.7:6379 pool: max-active: 1000 # 连接池最大连接数(使用负值表示没有限制) max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) max-idle: 10 # 连接池中的最大空闲连接 min-idle: 5 # 连接池中的最小空闲连接
renren-fast 包含了tomcat.jar文件,准确的来说是springboot的maven,pom中自带的tomcat。所以打包成jar包可以独立运行文件
注意:java后台程序不使用虚拟网络,直接使用宿主的ip端口。--net=host
运行java容器部署后端项目
docker volume create j1
#查看j1所在的路径,方便jar包上传
docker volume inspect j1
docker run -it -d name j1 -v j1:/home/soft --net=host java
docker exec -it j1 bash
#将编译好的jar拷贝到宿主机上j1所在的目录下 nohubp 就是后台挂机项目
nohup java -jar /home/soft/renren-fast.jar
运行java容器部署后端项目
docker volume create j2
#查看j2所在的路径,方便jar包上传
docker volume inspect j2
docker run -it -d name j2 -v j2:/home/soft --net=host java
docker exec -it j2 bash
#将编译好的jar拷贝到宿主机上j2所在的目录下 nohubp 就是后台挂机项目
nohup java -jar /home/soft/renren-fast.jar
docker volume create j3
#查看j3所在的路径,方便jar包上传
docker volume inspect j3
docker run -it -d name j3 -v j3:/home/soft --net=host java
docker exec -it j3 bash
#将编译好的jar拷贝到宿主机上j3所在的目录下 nohubp 就是后台挂机项目
nohup java -jar /home/soft/renren-fast.jar
所有的负载都发送到一个jar包上,如果量比较大,tomcat最大承受500的并发,Tomcat可能就挂了。
nginx
nginx 是性能非常出色的反向代理服务器,最大可以支持8万/秒的并发访问,之前咱们数据库中间件和redis中间件使用了haproxy,因为haproxy对tcp这种负载均衡做的比较好,现在java容器内的tomcat是支持的http的协议,http负载做的最好的是nginx,我们选择nginx负载均衡的产品。
定义了一个upstream tomcat内置的都是宿主机器的ip和端口,通过端口的映射找到对应的容器,在server中配置好tomcat的和nginx的端口,直接访问nginx,进行跳转到对应的java容器上。端口6101
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;
}
}
}
创建nginx的指令
nginx使用宿主的主机ip。--net=host
# 容器内的nginx启动加载容器外的配置文件 docker run -it -d --name n1 -v /root/v1/nginx.conf:/etc/nginx/nginx.conf --net=host --privileged nginx
端口6102
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的指令
> 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,无法做到双机热备。这次说说双机热备。
进入容器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
apt-get clean
apt-get update
apt-get install keepalived
apt-get install vim
容器内的路径:/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 } }
容器内启动
service keepalived start
进入容器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
apt-get clean
apt-get update
apt-get install keepalived
apt-get install vim
容器内的路径:/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 } }
容器内启动
service keepalived start
PS:到此未知后端的nginx双负载,双热备方案已经实现了,