Docker基本命令
更新軟件包
yum -y update
安裝Docker虛擬機
yum install -y docker
運行、重啟、關閉Docker虛擬機
service docker start
service docker stop
搜索鏡像
docker search 鏡像名稱
下載鏡像
docker pull 鏡像名稱
查看鏡像
docker images
刪除鏡像
docker rmi 鏡像名稱
運行容器
docker run 啟動參數 鏡像名稱
查看容器列表
docker ps -a
停止、掛起、恢復容器
docker stop 容器ID
docker pause 容器ID
docker unpase 容器ID
查看容器信息
docker inspect 容器ID
刪除容器
docker rm 容器ID
數據卷管理
docker volume create 數據卷名稱 #創建數據卷
docker volume rm 數據卷名稱 #刪除數據卷
docker volume inspect 數據卷名稱 #查看數據卷
網絡管理
docker network ls 查看網絡信息
docker network create --subnet=網段 網絡名稱
docker network rm 網絡名稱
避免VM虛擬機掛起恢復之后,Docker虛擬機斷網
vi /etc/sysctl.conf
文件中添加`net.ipv4.ip_forward=1`這個配置
#重啟網絡服務
systemctl restart network
開源前后端分離項目人人網,用Docker部署案例
前端部署方案:
后端部署方案
數據庫集群部署方案:
redis集群部署方案
安裝PXC集群,負載均衡,雙機熱備
安裝PXC鏡像
docker pull percona/percona-xtradb-cluster
為PXC鏡像改名
docker tag percona/percona-xtradb-cluster pxc
創建net1網段
docker network create --subnet=172.18.0.0/16 net1
創建5個數據卷
docker volume create --name v1
docker volume create --name v2
docker volume create --name v3
docker volume create --name v4
docker volume create --name v5
創建備份數據卷(用于熱備份數據)
docker volume create --name backup
創建5節點的PXC集群
注意,每個MySQL容器創建之后,因為要執行PXC的初始化和加入集群等工作,耐心等待1分鐘左右再用客戶端連接MySQL。另外,必須第1個MySQL節點啟動成功,用MySQL客戶端能連接上之后,再去創建其他MySQL節點。
#創建第1個MySQL節點
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSword=abc123456
-e CLUSTER_NAME=PXC
-e XTRABACKUP_PASSWORD=abc123456
-v v1:/var/lib/mysql -v backup:/data --privileged
--name=node1
--net=net1
--ip 172.18.0.2 pxc
#創建第2個MySQL節點
docker run -d -p 3307:3306
-e MYSQL_ROOT_PASSWORD=abc123456
-e CLUSTER_NAME=PXC
-e XTRABACKUP_PASSWORD=abc123456
-e CLUSTER_JOIN=node1
-v v2:/var/lib/mysql -v backup:/data --privileged
--name=node2
--net=net1
--ip 172.18.0.3 pxc
#創建第3個MySQL節點
docker run -d -p 3308:3306
-e MYSQL_ROOT_PASSWORD=abc123456
-e CLUSTER_NAME=PXC
-e XTRABACKUP_PASSWORD=abc123456
-e CLUSTER_JOIN=node1
-v v3:/var/lib/mysql --privileged
--name=node3
--net=net1
--ip 172.18.0.4 pxc
#創建第4個MySQL節點
docker run -d -p 3309:3306
-e MYSQL_ROOT_PASSWORD=abc123456
-e CLUSTER_NAME=PXC
-e XTRABACKUP_PASSWORD=abc123456
-e CLUSTER_JOIN=node1
-v v4:/var/lib/mysql --privileged
--name=node4
--net=net1
--ip 172.18.0.5 pxc
#創建第5個MySQL節點
docker run -d -p 3310:3306
-e MYSQL_ROOT_PASSWORD=abc123456
-e CLUSTER_NAME=PXC
-e XTRABACKUP_PASSWORD=abc123456
-e CLUSTER_JOIN=node1
-v v5:/var/lib/mysql -v backup:/data --privileged
--name=node5
--net=net1
--ip 172.18.0.6 pxc
安裝Haproxy鏡像
docker pull haproxy
宿主機上編寫Haproxy配置文件
vi /home/soft/haproxy.cfg
配置文件如下:
global
#工作目錄
chroot /usr/local/etc/haproxy
#日志文件,使用rsyslog服務中local5日志設備(/var/log/local5),等級info
log 127.0.0.1 local5 info
#守護進程運行
daemon
defaults
log global
mode http
#日志格式
option httplog
#日志中不記錄負載均衡的心跳檢測記錄
option dontlognull
#連接超時(毫秒)
timeout connect 5000
#客戶端超時(毫秒)
timeout client 50000
#服務器超時(毫秒)
timeout server 50000
#監控界面
listen admin_stats
#監控界面的訪問的IP和端口
bind 0.0.0.0:8888
#訪問協議
mode http
#URI相對地址
stats uri /dbs
#統計報告格式
stats realm Global statistics
#登陸帳戶信息
stats auth admin:abc123456
#數據庫負載均衡
listen proxy-mysql
#訪問的IP和端口
bind 0.0.0.0:3306
#網絡協議
mode tcp
#負載均衡算法(輪詢算法)
#輪詢算法:roundrobin
#權重算法:static-rr
#最少連接算法:leastconn
#請求源IP算法:source
balance roundrobin
#日志格式
option tcplog
#在MySQL中創建一個沒有權限的haproxy用戶,密碼為空。
#Haproxy使用這個賬戶對MySQL數據庫心跳檢測
option mysql-check user haproxy
server MySQL_1 172.18.0.2:3306 check weight 1 maxconn 2000
server MySQL_2 172.18.0.3:3306 check weight 1 maxconn 2000
server MySQL_3 172.18.0.4:3306 check weight 1 maxconn 2000
server MySQL_4 172.18.0.5:3306 check weight 1 maxconn 2000
server MySQL_5 172.18.0.6:3306 check weight 1 maxconn 2000
#使用keepalive檢測死鏈
option tcpka
創建兩個Haproxy容器
#創建第1個Haproxy負載均衡服務器
docker run -it -d -p 4001:8888 -p 4002:3306
-v /home/soft/haproxy:/usr/local/etc/haproxy
--name h1 --privileged --net=net1 --ip 172.18.0.7 haproxy
#進入h1容器,啟動Haproxy
docker exec -it h1 bash
haproxy -f /usr/local/etc/haproxy/haproxy.cfg
#創建第2個Haproxy負載均衡服務器
docker run -it -d -p 4003:8888 -p 4004:3306
-v /home/soft/haproxy:/usr/local/etc/haproxy
--name h2 --privileged --net=net1 --ip 172.18.0.8 haproxy
#進入h2容器,啟動Haproxy
docker exec -it h2 bash
haproxy -f /usr/local/etc/haproxy/haproxy.cfg
Haproxy容器內安裝Keepalived,設置虛擬IP
#進入h1容器
docker exec -it h1 bash
#更新軟件包
apt-get update
#安裝VIM
apt-get install vim
#安裝Keepalived
apt-get install keepalived
#編輯Keepalived配置文件(參考下方配置文件)
vim /etc/keepalived/keepalived.conf
#啟動Keepalived
service keepalived start
#宿主機執行ping命令
ping 172.18.0.201
配置文件內容如下:
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
172.18.0.201
}
}
#進入h2容器
docker exec -it h2 bash
#更新軟件包
apt-get update
#安裝VIM
apt-get install vim
#安裝Keepalived
apt-get install keepalived
#編輯Keepalived配置文件
vim /etc/keepalived/keepalived.conf
#啟動Keepalived
service keepalived start
#宿主機執行ping命令
ping 172.18.0.201
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
172.18.0.201
}
}
宿主機安裝Keepalived,實現雙擊熱備
#宿主機執行安裝Keepalived
yum -y install keepalived
#修改Keepalived配置文件
vi /etc/keepalived/keepalived.conf
#啟動Keepalived
service keepalived start
Keepalived配置文件如下:
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.99.150
}
}
virtual_server 192.168.99.150 8888 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 172.18.0.201 8888 {
weight 1
}
}
virtual_server 192.168.99.150 3306 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 172.18.0.201 3306 {
weight 1
}
}
熱備份數據
#進入node1容器
docker exec -it node1 bash
#更新軟件包
apt-get update
#安裝熱備工具
apt-get install percona-xtrabackup-24
#全量熱備
innobackupex --user=root --password=abc123456 /data/backup/full
冷還原數據 停止其余4個節點,并刪除節點
docker stop node2
docker stop node3
docker stop node4
docker stop node5
docker rm node2
docker rm node3
docker rm node4
docker rm node5
node1容器中刪除MySQL的數據
#刪除數據
rm -rf /var/lib/mysql/*
#清空事務
innobackupex --user=root --password=abc123456
--Apply-back /data/backup/full/2018-04-15_05-09-07/
#還原數據
innobackupex --user=root --password=abc123456
--copy-back /data/backup/full/2018-04-15_05-09-07/
安裝Redis,配置RedisCluster集群
安裝Redis鏡像
docker pull yyyyttttwwww/redis
創建net2網段
docker network create --subnet=172.19.0.0/16 net2
創建6節點Redis容器
docker run -it -d --name r1 -p 5001:6379 --net=net2 --ip 172.19.0.2 redis bash
docker run -it -d --name r2 -p 5002:6379 --net=net2 --ip 172.19.0.3 redis bash
docker run -it -d --name r3 -p 5003:6379 --net=net2 --ip 172.19.0.4 redis bash
docker run -it -d --name r4 -p 5004:6379 --net=net2 --ip 172.19.0.5 redis bash
docker run -it -d --name r5 -p 5005:6379 --net=net2 --ip 172.19.0.6 redis bash
啟動6節點Redis服務器
#進入r1節點
docker exec -it r1 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf
#進入r2節點
docker exec -it r2 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf
#進入r3節點
docker exec -it r3 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf
#進入r4節點
docker exec -it r4 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf
#進入r5節點
docker exec -it r5 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf
#進入r6節點
docker exec -it r6 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf
創建Cluster集群
#在r1節點上執行下面的指令
cd /usr/redis/src
mkdir -p ../cluster
cp redis-trib.rb ../cluster/
cd ../cluster
#創建Cluster集群
./redis-trib.rb create
--replicas 1
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
打包部署后端項目
進入人人開源后端項目,執行打包(修改配置文件,更改端口,打包三次生成三個JAR文件)
mvn clean install -Dmaven.test.skip=true
安裝JAVA鏡像
docker pull java
創建3節點Java容器
#創建數據卷,上傳JAR文件
docker volume create j1
#啟動容器
docker run -it -d --name j1 -v j1:/home/soft --net=host java
#進入j1容器
docker exec -it j1 bash
#啟動Java項目
nohup java -jar /home/soft/renren-fast.jar
#創建數據卷,上傳JAR文件
docker volume create j2
#啟動容器
docker run -it -d --name j2 -v j2:/home/soft --net=host java
#進入j1容器
docker exec -it j2 bash
#啟動Java項目
nohup java -jar /home/soft/renren-fast.jar
#創建數據卷,上傳JAR文件
docker volume create j3
#啟動容器
docker run -it -d --name j3 -v j3:/home/soft --net=host java
#進入j1容器
docker exec -it j3 bash
#啟動Java項目
nohup java -jar /home/soft/renren-fast.jar
安裝Nginx鏡像
docker pull nginx
創建Nginx容器,配置負載均衡
宿主機上/home/n1/nginx.conf配置文件內容如下:
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.99.104:6001;
server 192.168.99.104:6002;
server 192.168.99.104:6003;
}
server {
listen 6101;
server_name 192.168.99.104;
location / {
proxy_pass http://tomcat;
index index.html index.htm;
}
}
}
創建第1個Nginx節點
docker run -it -d --name n1
-v /home/n1/nginx.conf:/etc/nginx/nginx.conf
--net=host --privileged nginx
宿主機上/home/n2/nginx.conf配置文件內容如下:
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.99.104:6001;
server 192.168.99.104:6002;
server 192.168.99.104:6003;
}
server {
listen 6102;
server_name 192.168.99.104;
location / {
proxy_pass http://tomcat;
index index.html index.htm;
}
}
}
創建第2個Nginx節點
docker run -it -d --name n2
-v /home/n2/nginx.conf:/etc/nginx/nginx.conf
--net=host --privileged nginx
在Nginx容器安裝Keepalived
#進入n1節點
docker exec -it n1 bash
#更新軟件包
apt-get update
#安裝VIM
apt-get install vim
#安裝Keepalived
apt-get install keepalived
#編輯Keepalived配置文件(如下)
vim /etc/keepalived/keepalived.conf
#啟動Keepalived
service keepalived start
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.99.151
}
}
virtual_server 192.168.99.151 6201 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.99.104 6101 {
weight 1
}
}
#進入n1節點
docker exec -it n2 bash
#更新軟件包
apt-get update
#安裝VIM
apt-get install vim
#安裝Keepalived
apt-get install keepalived
#編輯Keepalived配置文件(如下)
vim /etc/keepalived/keepalived.conf
#啟動Keepalived
service keepalived start
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.99.151
}
}
virtual_server 192.168.99.151 6201 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.99.104 6102 {
weight 1
}
}
打包部署后端項目
在前端項目路徑下執行打包指令
npm run build
build目錄的文件拷貝到宿主機的/home/fn1/renren-vue、/home/fn2/renren-vue、/home/fn3/renren-vue的目錄下面
創建3節點的Nginx,部署前端項目
宿主機/home/fn1/nginx.conf的配置文件
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.99.104;
location / {
root /home/fn1/renren-vue;
index index.html;
}
}
}
#啟動第fn1節點
docker run -it -d --name fn1
-v /home/fn1/nginx.conf:/etc/nginx/nginx.conf
-v /home/fn1/renren-vue:/home/fn1/renren-vue --privileged
--net=host nginx
宿主機/home/fn2/nginx.conf的配置文件
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.99.104;
location / {
root /home/fn2/renren-vue;
index index.html;
}
}
}
#啟動第fn2節點
docker run -it -d --name fn2
-v /home/fn2/nginx.conf:/etc/nginx/nginx.conf
-v /home/fn2/renren-vue:/home/fn2/renren-vue --privileged
--net=host nginx
宿主機/home/fn3/nginx.conf的配置文件
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.99.104;
location / {
root /home/fn3/renren-vue;
index index.html;
}
}
}
啟動fn3節點
#啟動第fn3節點
docker run -it -d --name fn3
-v /home/fn3/nginx.conf:/etc/nginx/nginx.conf
-v /home/fn3/renren-vue:/home/fn3/renren-vue --privileged
--net=host nginx
配置負載均衡
宿主機/home/ff1/nginx.conf配置文件
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.99.104:6501;
server 192.168.99.104:6502;
server 192.168.99.104:6503;
}
server {
listen 6601;
server_name 192.168.99.104;
location / {
proxy_pass http://fn;
index index.html index.htm;
}
}
}
#啟動ff1節點
docker run -it -d --name ff1
-v /home/ff1/nginx.conf:/etc/nginx/nginx.conf
--net=host --privileged nginx
宿主機/home/ff2/nginx.conf配置文件
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.99.104:6501;
server 192.168.99.104:6502;
server 192.168.99.104:6503;
}
server {
listen 6602;
server_name 192.168.99.104;
location / {
proxy_pass http://fn;
index index.html index.htm;
}
}
}
#啟動ff2節點
docker run -it -d --name ff2
-v /home/ff2/nginx.conf:/etc/nginx/nginx.conf
--net=host --privileged nginx
配置雙機熱備
#進入ff1節點
docker exec -it ff1 bash
#更新軟件包
apt-get update
#安裝VIM
apt-get install vim
#安裝Keepalived
apt-get install keepalived
#編輯Keepalived配置文件(如下)
vim /etc/keepalived/keepalived.conf
#啟動Keepalived
service keepalived start
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.99.152
}
}
virtual_server 192.168.99.151 6701 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.99.104 6601 {
weight 1
}
}
#進入ff1節點
docker exec -it ff2 bash
#更新軟件包
apt-get update
#安裝VIM
apt-get install vim
#安裝Keepalived
apt-get install keepalived
#編輯Keepalived配置文件(如下)
vim /etc/keepalived/keepalived.conf
#啟動Keepalived
service keepalived start
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.99.152
}
}
virtual_server 192.168.99.151 6701 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.99.104 6602 {
weight 1
}
}