「实战篇」开源项目docker化运维部署-后端java部署(七)
原創(chuàng)文章,歡迎轉(zhuǎn)載。轉(zhuǎn)載請(qǐng)注明:轉(zhuǎn)載自IT人故事會(huì),謝謝!
原文鏈接地址:「實(shí)戰(zhàn)篇」開(kāi)源項(xiàng)目docker化運(yùn)維部署-后端java部署(七)
本節(jié)主要說(shuō)說(shuō)后端的部署需要注意的點(diǎn),本身renren-fast這個(gè)項(xiàng)目就是通過(guò)springboot來(lái)進(jìn)行開(kāi)發(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
spring boot
- 如何配置redis集群
之前配置的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 # 是否開(kāi)啟redis緩存 true開(kāi)啟 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 # 連接池中的最小空閑連接 - maven打包工程
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 - 運(yùn)行java容器部署后端項(xiàng)目 <j3的后臺(tái)> 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
設(shè)置負(fù)載均衡
所有的負(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)品。
- nginx的配置<n1>
定義了一個(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
-
創(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 - nginx的配置<n2>
端口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;
}
* 創(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后端項(xiàng)目的雙機(jī)熱備負(fù)載均衡方案
之前已經(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 #寫(xiě)入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.listsources.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源
- keepalived配置文件
容器內(nèi)的路徑:/etc/keepalived/keepalived.conf
vi /etc/keepalived/keepalived.confkeepalived.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 } }- 啟動(dòng)keeplived
容器內(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 #寫(xiě)入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.listsources.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源
- keepalived配置文件
容器內(nèi)的路徑:/etc/keepalived/keepalived.conf
vi /etc/keepalived/keepalived.confkeepalived.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 } }- 啟動(dòng)keeplived
容器內(nèi)啟動(dòng)
service keepalived startPS:到此未知后端的nginx雙負(fù)載,雙熱備方案已經(jīng)實(shí)現(xiàn)了,
轉(zhuǎn)載于:https://blog.51cto.com/12040702/2373963
總結(jié)
以上是生活随笔為你收集整理的「实战篇」开源项目docker化运维部署-后端java部署(七)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 源码剖析Django REST fram
- 下一篇: 面试官:react和vue有什么区别吗?