您好,登錄后才能下訂單哦!
原創(chuàng)文章,歡迎轉(zhuǎn)載。轉(zhuǎn)載請(qǐng)注明:轉(zhuǎn)載自IT人故事會(huì),謝謝!
原文鏈接地址:「實(shí)戰(zhàn)篇」開源項(xiàng)目docker化運(yùn)維部署-后端java部署(七)本節(jié)主要說(shuō)說(shuō)后端的部署需要注意的點(diǎn),本身renren-fast這個(gè)項(xiàng)目就是通過(guò)springboot來(lái)進(jìn)行開發(fā)的,springboot內(nèi)置的有tomcat所以,咱們不用在容器內(nèi)安裝tomcat,直接用jar文件來(lái)進(jìn)行運(yùn)行。源碼:https://github.com/limingios/netFuture/blob/master/后端/ 后端雙機(jī)熱備
https://gitee.com/renrenio/renren-fast
之前配置的redis集群,修改下單節(jié)點(diǎn)的吧,把所有的redis集群都放上去。
spring: # 環(huán)境 dev|test|prod profiles: active: dev # jackson時(shí)間格式化 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 # 連接超時(shí)時(shí)長(zhǎng)(毫秒) 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ù)值表示沒(méi)有限制) max-wait: -1 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制) max-idle: 10 # 連接池中的最大空閑連接 min-idle: 5 # 連接池中的最小空閑連接
renren-fast 包含了tomcat.jar文件,準(zhǔn)確的來(lái)說(shuō)是springboot的maven,pom中自帶的tomcat。所以打包成jar包可以獨(dú)立運(yùn)行文件
注意:java后臺(tái)程序不使用虛擬網(wǎng)絡(luò),直接使用宿主的ip端口。--net=host
運(yùn)行java容器部署后端項(xiàng)目 <j1的后臺(tái)>
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拷貝到宿主機(jī)上j1所在的目錄下 nohubp 就是后臺(tái)掛機(jī)項(xiàng)目
nohup java -jar /home/soft/renren-fast.jar
運(yùn)行java容器部署后端項(xiàng)目 <j2的后臺(tái)>
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拷貝到宿主機(jī)上j2所在的目錄下 nohubp 就是后臺(tái)掛機(jī)項(xiàng)目
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拷貝到宿主機(jī)上j3所在的目錄下 nohubp 就是后臺(tái)掛機(jī)項(xiàng)目
nohup java -jar /home/soft/renren-fast.jar
所有的負(fù)載都發(fā)送到一個(gè)jar包上,如果量比較大,tomcat最大承受500的并發(fā),Tomcat可能就掛了。
nginx
nginx 是性能非常出色的反向代理服務(wù)器,最大可以支持8萬(wàn)/秒的并發(fā)訪問(wèn),之前咱們數(shù)據(jù)庫(kù)中間件和redis中間件使用了haproxy,因?yàn)閔aproxy對(duì)tcp這種負(fù)載均衡做的比較好,現(xiàn)在java容器內(nèi)的tomcat是支持的http的協(xié)議,http負(fù)載做的最好的是nginx,我們選擇nginx負(fù)載均衡的產(chǎn)品。
定義了一個(gè)upstream tomcat內(nèi)置的都是宿主機(jī)器的ip和端口,通過(guò)端口的映射找到對(duì)應(yīng)的容器,在server中配置好tomcat的和nginx的端口,直接訪問(wèn)nginx,進(jìn)行跳轉(zhuǎn)到對(duì)應(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使用宿主的主機(jī)ip。--net=host
# 容器內(nèi)的nginx啟動(dòng)加載容器外的配置文件 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使用宿主的主機(jī)ip。--net=host
``` bash
# 容器內(nèi)的nginx啟動(dòng)加載容器外的配置文件
docker run -it -d --name n2 -v /root/v2/nginx.conf:/etc/nginx/nginx.conf --net=host --privileged nginx
之前已經(jīng)設(shè)置了n1 和n2,都可以正常的訪問(wèn)后端,但是沒(méi)有設(shè)置keepalived,他們之前無(wú)法爭(zhēng)搶ip,無(wú)法做到雙機(jī)熱備。這次說(shuō)說(shuō)雙機(jī)熱備。
進(jìn)入容器n1然后安裝keepalived
keepalived必須在n1所在的容器之內(nèi),也可以在docker倉(cāng)庫(kù)里面下載一個(gè)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)啟動(dòng)
service keepalived start
進(jìn)入容器n2然后安裝keepalived
keepalived必須在n2所在的容器之內(nèi),也可以在docker倉(cāng)庫(kù)里面下載一個(gè)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)啟動(dòng)
service keepalived start
PS:到此未知后端的nginx雙負(fù)載,雙熱備方案已經(jīng)實(shí)現(xiàn)了,
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。