您好,登錄后才能下訂單哦!
原創文章,歡迎轉載。轉載請注明:轉載自IT人故事會,謝謝!
原文鏈接地址:「實戰篇」開源項目docker化運維部署-后端java部署(七)本節主要說說后端的部署需要注意的點,本身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容器部署后端項目 <j1的后臺>
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容器部署后端項目 <j2的后臺>
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的指令<n1>
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的指令<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,無法做到雙機熱備。這次說說雙機熱備。
進入容器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雙負載,雙熱備方案已經實現了,
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。