您好,登錄后才能下訂單哦!
原創文章,歡迎轉載。轉載請注明:轉載自IT人故事會,謝謝!
原文鏈接地址:「實戰篇」開源項目docker化運維部署-前端java部署(八)本節主要說說前端的部署需要注意的點,本身renren-fast這個項目就是通過nodejs來進行開發的,nodejs編譯后生成html,css,img所以,咱們不用在容器直接用nginx就可以訪問靜態文件。源碼:https://github.com/limingios/netFuture/blob/master/前端/
https://github.com/daxiongYang/renren-fast-vue
應該修改成http://192.168.66.151:6201/renren-fast;
http://npm.taobao.org/
你可以使用我們定制的?cnpm?(gzip 壓縮支持) 命令行工具代替默認的?
npm
:
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
這個目錄上傳到nginx上。
這里的nginx并不是做負載均衡的,而是做靜態的html的靜態運行環境的。
創建容器
用宿主機的網段
docker run -it -d --name fn1 \ -v /root/fn1/nginx.conf:/etc/nginx/nginxc.conf \ -v /root/fn1/renren-vue:/home/fn1/renren-vue \ --privileged --net=host nginx
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;
server {
listen 6501;
server_name 192.168.66.100;
location / {
root /home/fn1/renren-vue;
index index.html;
}
}
}
#### renren-nginx<2>
>這里的nginx并不是做負載均衡的,而是做靜態的html的靜態運行環境的。
* 創建容器
>用宿主機的網段
``` bash
docker run -it -d --name fn2 \
-v /root/fn2/nginx.conf:/etc/nginx/nginxc.conf \
-v /root/fn2/renren-vue:/home/fn1/renren-vue \
--privileged --net=host nginx
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;
server {
listen 6502;
server_name 192.168.66.100;
location / {
root /home/fn2/renren-vue;
index index.html;
}
}
}
#### renren-nginx<3>
>這里的nginx并不是做負載均衡的,而是做靜態的html的靜態運行環境的。
* 創建容器
>用宿主機的網段
``` bash
docker run -it -d --name fn3 \
-v /root/fn3/nginx.conf:/etc/nginx/nginxc.conf \
-v /root/fn3/renren-vue:/home/fn1/renren-vue \
--privileged --net=host nginx
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;
server {
listen 6503;
server_name 192.168.66.100;
location / {
root /home/fn3/renren-vue;
index index.html;
}
}
}
###qia負載均衡
![](https://upload-images.jianshu.io/upload_images/11223715-0521406dd545ab33.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##### nginx-ff1
* ff1 容器的創建
``` bash
docker run -it -d --name ff1 \
-v /root/ff1/nginx.conf:/etc/nginx/nginx.conf \
--net=host \
--privileged --net=host nginx
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 fn {
server 192.168.66.100:6501;
server 192.168.66.100:6502;
server 192.168.66.100:6503;
}
server {
listen 6601;
server_name 192.168.66.100;
location / {
proxy_pass http://fn;
index index.html index.htm;
}
}
}
##### nginx-ff2
* ff2 容器的創建
``` bash
docker run -it -d --name ff2 \
-v /root/ff2/nginx.conf:/etc/nginx/nginx.conf \
--net=host \
--privileged --net=host nginx
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 fn {
server 192.168.66.100:6501;
server 192.168.66.100:6502;
server 192.168.66.100:6503;
}
server {
listen 6602;
server_name 192.168.66.100;
location / {
proxy_pass http://fn;
index index.html index.htm;
}
}
}
#### 前端項目的雙機熱備負載均衡方案
> 之前已經設置了ff1 和ff2,都可以正常的訪問后端,但是沒有設置keepalived,他們之前無法爭搶ip,無法做到雙機熱備。這次說說雙機熱備。
![](http://upload-images.jianshu.io/upload_images/11223715-2f22450c56914920?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
> 進入容器ff1然后安裝keepalived
> keepalived必須在ff1所在的容器之內,也可以在docker倉庫里面下載一個nginx-keepalived的鏡像。這里直接在容器內安裝keepalived。
docker exec -it ff1 /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源
apt-get clean
apt-get update
apt-get install keepalived
apt-get install vim
![](http://upload-images.jianshu.io/upload_images/11223715-19bf6ee9786cc44e?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* keepalived配置文件
> 容器內的路徑:/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.152
}
}
virtual_server 192.168.66.152 6701{
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.66.100 6601{
weight 1
}
}
1. VI_1 名稱可以自定義
2. state MASTER | keepalived的身份(MASTER主服務器,BACKUP備份服務器,不會搶占虛擬機ip)。如果都是主MASTER的話,就會進行互相爭搶IP,如果搶到了就是MASTER,另一個就是SLAVE。
3. interface網卡,定義一個虛擬IP定義到那個網卡上邊。網卡設備的名稱。eth43是宿主機是網卡。
4. virtual_router_id 51 | 虛擬路由標識,MASTER和BACKUP的虛擬路由標識必須一致。標識可以是0-255。
5. priority 100 | 權重。MASTER權重要高于BACKUP 數字越大優選級越高。可以根據硬件的配置來完成,權重最大的獲取搶到的級別越高。
6. advert_int 1 | 心跳檢測。MASTER與BACKUP節點間同步檢查的時間間隔,單位為秒。主備之間必須一致。
7. authentication | 主從服務器驗證方式。主備必須使用相同的密碼才能正常通信。進行心跳檢測需要登錄到某個主機上邊所有有賬號密碼。
8. virtual_ipaddress | 虛擬ip地址,可以設置多個虛擬ip地址,每行一個。根據上邊配置的eth43上配置的ip。192.168.66.151 是自己定義的虛擬ip
* 啟動keeplived
> 容器內啟動
service keepalived start
> 進入容器ff2然后安裝keepalived
> keepalived必須在ff2所在的容器之內,也可以在docker倉庫里面下載一個nginx-keepalived的鏡像。這里直接在容器內安裝keepalived。
docker exec -it ff2 /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
![](http://upload-images.jianshu.io/upload_images/11223715-4bf2654b4295a06c?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 更新apt源
apt-get clean
apt-get update
apt-get install keepalived
apt-get install vim
* keepalived配置文件
> 容器內的路徑:/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.152
}
}
virtual_server 192.168.66.152 6701{
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.66.100 6602{
weight 1
}
}
1. VI_1 名稱可以自定義
2. state MASTER | keepalived的身份(MASTER主服務器,BACKUP備份服務器,不會搶占虛擬機ip)。如果都是主MASTER的話,就會進行互相爭搶IP,如果搶到了就是MASTER,另一個就是SLAVE。
3. interface網卡,定義一個虛擬IP定義到那個網卡上邊。網卡設備的名稱。eth43是宿主機是網卡。
4. virtual_router_id 51 | 虛擬路由標識,MASTER和BACKUP的虛擬路由標識必須一致。標識可以是0-255。
5. priority 100 | 權重。MASTER權重要高于BACKUP 數字越大優選級越高。可以根據硬件的配置來完成,權重最大的獲取搶到的級別越高。
6. advert_int 1 | 心跳檢測。MASTER與BACKUP節點間同步檢查的時間間隔,單位為秒。主備之間必須一致。
7. authentication | 主從服務器驗證方式。主備必須使用相同的密碼才能正常通信。進行心跳檢測需要登錄到某個主機上邊所有有賬號密碼。
8. virtual_ipaddress | 虛擬ip地址,可以設置多個虛擬ip地址,每行一個。根據上邊配置的eth43上配置的ip。192.168.66.151 是自己定義的虛擬ip
* 啟動keeplived
> 容器內啟動
service keepalived start
PS:前后端部署基本是一樣的都是按照思路,先啟動多個容器,然后建立2個負載,負載內安裝keepalived做熱備。重點是想好端口。但是說實話,這是平常練習和個人項目,如果是多臺機器,就不能這么搞了,下次一起通過docker swarm的網絡方式讓多臺機器。
![](http://upload-images.jianshu.io/upload_images/11223715-3407e1c7ac8d7935?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。