spring boot
以前配置的redis集羣,修改下單節點的吧,把全部的redis集羣都放上去。
spring:
profiles:
active: dev
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
database: 0
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
docker volume create j1
docker volume inspect j1
docker run -it -d name j1 -v j1:/home/soft --net=host java
docker exec -it j1 bash
nohup java -jar /home/soft/renren-fast.jar複製代碼
docker volume create j2
docker volume inspect j2
docker run -it -d name j2 -v j2:/home/soft --net=host java
docker exec -it j2 bash
nohup java -jar /home/soft/renren-fast.jar複製代碼
docker volume create j3
docker volume inspect j3
docker run -it -d name j3 -v j3:/home/soft --net=host java
docker exec -it j3 bash
nohup java -jar /home/soft/renren-fast.jar複製代碼
設置負載均衡
全部的負載都發送到一個jar包上,若是量比較大,tomcat最大承受500的併發,Tomcat可能就掛了。
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;
keepalive_timeout 65;
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使用宿主的主機ip。--net=host
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;
keepalive_timeout 65;
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使用宿主的主機ip。--net=host
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
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
}
}複製代碼
-
VI_1 名稱能夠自定義
-
state MASTER | keepalived的身份(MASTER主服務器,BACKUP備份服務器,不會搶佔虛擬機ip)。若是都是主MASTER的話,就會進行互相爭搶IP,若是搶到了就是MASTER,另外一個就是SLAVE。
-
interface網卡,定義一個虛擬IP定義到那個網卡上邊。網卡設備的名稱。eth33是宿主機是網卡。
-
virtual_router_id 51 | 虛擬路由標識,MASTER和BACKUP的虛擬路由標識必須一致。標識能夠是0-255。
-
priority 100 | 權重。MASTER權重要高於BACKUP 數字越大優選級越高。能夠根據硬件的配置來完成,權重最大的獲取搶到的級別越高。
-
advert_int 1 | 心跳檢測。MASTER與BACKUP節點間同步檢查的時間間隔,單位爲秒。主備之間必須一致。
-
authentication | 主從服務器驗證方式。主備必須使用相同的密碼才能正常通訊。進行心跳檢測須要登陸到某個主機上邊全部有帳號密碼。
-
virtual_ipaddress | 虛擬ip地址,能夠設置多個虛擬ip地址,每行一個。根據上邊配置的eth33上配置的ip。192.168.66.151 是本身定義的虛擬ip
容器內啓動
service keepalived start複製代碼
進入容器n2而後安裝keepalived
keepalived必須在n2所在的容器以內,也能夠在docker倉庫裏面下載一個nginx-keepalived的鏡像。這裏直接在容器內安裝keepalived。
docker exec -it n2 /bin/bash
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
}
}複製代碼
-
VI_1 名稱能夠自定義
-
state MASTER | keepalived的身份(MASTER主服務器,BACKUP備份服務器,不會搶佔虛擬機ip)。若是都是主MASTER的話,就會進行互相爭搶IP,若是搶到了就是MASTER,另外一個就是SLAVE。
-
interface網卡,定義一個虛擬IP定義到那個網卡上邊。網卡設備的名稱。eth33是宿主機是網卡。
-
virtual_router_id 51 | 虛擬路由標識,MASTER和BACKUP的虛擬路由標識必須一致。標識能夠是0-255。
-
priority 100 | 權重。MASTER權重要高於BACKUP 數字越大優選級越高。能夠根據硬件的配置來完成,權重最大的獲取搶到的級別越高。
-
advert_int 1 | 心跳檢測。MASTER與BACKUP節點間同步檢查的時間間隔,單位爲秒。主備之間必須一致。
-
authentication | 主從服務器驗證方式。主備必須使用相同的密碼才能正常通訊。進行心跳檢測須要登陸到某個主機上邊全部有帳號密碼。
-
virtual_ipaddress | 虛擬ip地址,能夠設置多個虛擬ip地址,每行一個。根據上邊配置的eth33上配置的ip。192.168.66.151 是本身定義的虛擬ip
容器內啓動
service keepalived start複製代碼
PS:到此未知後端的nginx雙負載,雙熱備方案已經實現了,