原創(chuàng)文章,歡迎轉(zhuǎn)載。轉(zhuǎn)載請注明:轉(zhuǎn)載自IT人故事會,謝謝!
原文鏈接地址:「實戰(zhàn)篇」開源項目docker化運維部署-后端java部署(七)創(chuàng)新互聯(lián)從2013年成立,我們提供高端網(wǎng)站建設(shè)、小程序設(shè)計、電商視覺設(shè)計、手機APP定制開發(fā)及網(wǎng)絡(luò)營銷搜索優(yōu)化服務(wù),在傳統(tǒng)互聯(lián)網(wǎng)與移動互聯(lián)網(wǎng)發(fā)展的背景下,我們堅守著用標(biāo)準(zhǔn)的設(shè)計方案與技術(shù)開發(fā)實力作基礎(chǔ),以企業(yè)及品牌的互聯(lián)網(wǎng)商業(yè)目標(biāo)為核心,為客戶打造具商業(yè)價值與用戶體驗的互聯(lián)網(wǎng)+產(chǎn)品。
本節(jié)主要說說后端的部署需要注意的點,本身renren-fast這個項目就是通過springboot來進(jìn)行開發(fā)的,springboot內(nèi)置的有tomcat所以,咱們不用在容器內(nèi)安裝tomcat,直接用jar文件來進(jìn)行運行。源碼:https://github.com/limingios/netFuture/blob/master/后端/ 后端雙機熱備
https://gitee.com/renrenio/renren-fast
之前配置的redis集群,修改下單節(jié)點的吧,把所有的redis集群都放上去。
spring: # 環(huán)境 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關(guān)閉 database: 0 #host: localhost #port: 6379 #password: # 密碼(默認(rèn)為空) 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 # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制) max-wait: -1 # 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制) max-idle: 10 # 連接池中的最大空閑連接 min-idle: 5 # 連接池中的最小空閑連接
renren-fast 包含了tomcat.jar文件,準(zhǔn)確的來說是springboot的maven,pom中自帶的tomcat。所以打包成jar包可以獨立運行文件
注意:java后臺程序不使用虛擬網(wǎng)絡(luò),直接使用宿主的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
所有的負(fù)載都發(fā)送到一個jar包上,如果量比較大,tomcat最大承受500的并發(fā),Tomcat可能就掛了。
nginx
nginx 是性能非常出色的反向代理服務(wù)器,最大可以支持8萬/秒的并發(fā)訪問,之前咱們數(shù)據(jù)庫中間件和redis中間件使用了haproxy,因為haproxy對tcp這種負(fù)載均衡做的比較好,現(xiàn)在java容器內(nèi)的tomcat是支持的http的協(xié)議,http負(fù)載做的最好的是nginx,我們選擇nginx負(fù)載均衡的產(chǎn)品。
定義了一個upstream tomcat內(nèi)置的都是宿主機器的ip和端口,通過端口的映射找到對應(yīng)的容器,在server中配置好tomcat的和nginx的端口,直接訪問nginx,進(jìn)行跳轉(zhuǎn)到對應(yīng)的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;
}
}
}
創(chuàng)建nginx的指令<n1>
nginx使用宿主的主機ip。--net=host
# 容器內(nèi)的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;
}
}
}
* 創(chuàng)建nginx的指令<n2>
> nginx使用宿主的主機ip。--net=host
``` bash
# 容器內(nèi)的nginx啟動加載容器外的配置文件
docker run -it -d --name n2 -v /root/v2/nginx.conf:/etc/nginx/nginx.conf --net=host --privileged nginx
之前已經(jīng)設(shè)置了n1 和n2,都可以正常的訪問后端,但是沒有設(shè)置keepalived,他們之前無法爭搶ip,無法做到雙機熱備。這次說說雙機熱備。
進(jìn)入容器n1然后安裝keepalived
keepalived必須在n1所在的容器之內(nèi),也可以在docker倉庫里面下載一個nginx-keepalived的鏡像。這里直接在容器內(nèi)安裝keepalived。
docker exec -it n1 /bin/bash
#寫入DNS,防止apt-get update找不到服務(wù)器
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 添加下面的內(nèi)容
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
容器內(nèi)的路徑:/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 } }
容器內(nèi)啟動
service keepalived start
進(jìn)入容器n2然后安裝keepalived
keepalived必須在n2所在的容器之內(nèi),也可以在docker倉庫里面下載一個nginx-keepalived的鏡像。這里直接在容器內(nèi)安裝keepalived。
docker exec -it n2 /bin/bash
#寫入dns,防止apt-get update找不到服務(wù)器
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 添加下面的內(nèi)容
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
容器內(nèi)的路徑:/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 } }
容器內(nèi)啟動
service keepalived start
PS:到此未知后端的nginx雙負(fù)載,雙熱備方案已經(jīng)實現(xiàn)了,
當(dāng)前文章:「實戰(zhàn)篇」開源項目docker化運維部署-后端java部署(七)
文章路徑:http://muchs.cn/article48/gjsdep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、搜索引擎優(yōu)化、網(wǎng)站排名、營銷型網(wǎng)站建設(shè)、商城網(wǎng)站、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)