1. 程式人生 > 其它 >Docker環境下的前後端分離部署與運維

Docker環境下的前後端分離部署與運維

《Docker環境下的前後端分離部署與運維》課程指令碼

Docker虛擬機器常用命令更多資源聯絡QQ810642693

  1. 先更新軟體包

    yum -y update
    
  2. 安裝Docker虛擬機器

    yum install -y docker
    
  3. 執行、重啟、關閉Docker虛擬機器

    service docker start
    service docker start
    service docker stop
    
  4. 搜尋映象

    docker search 映象名稱
    
  5. 下載映象

    docker pull 映象名稱
    
  6. 檢視映象

    docker images
    
  7. 刪除映象

    docker rmi 映象名稱
    
  8. 執行容器

    docker run 啟動引數  映象名稱
    
  9. 檢視容器列表

    docker ps -a
    
  10. 停止、掛起、恢復容器

docker stop 容器ID
docker pause 容器ID
docker unpase 容器ID
  1. 檢視容器資訊

    docker inspect 容器ID
    
  2. 刪除容器

    docker rm 容器ID
    
  3. 資料卷管理

    docker volume create 資料卷名稱  #建立資料卷
    docker volume rm 資料卷名稱  #刪除資料卷
    docker volume inspect 資料卷名稱  #檢視資料卷
    
  4. 網路管理

    docker network ls 檢視網路資訊
    docker network create --subnet=網段 網路名稱
    docker network rm 網路名稱
    
  5. 避免VM虛擬機器掛起恢復之後,Docker虛擬機器斷網

    vi /etc/sysctl.conf
    

    檔案中新增net.ipv4.ip_forward=1這個配置

    ​```shell

    重啟網路服務

    systemctl restart network
    ​```

安裝PXC叢集,負載均衡,雙機熱備

  1. 安裝PXC映象

    docker pull percona/percona-xtradb-cluster
    
  2. 為PXC映象改名

    docker tag percona/percona-xtradb-cluster pxc
    
  3. 建立net1網段

    docker network create --subnet=172.18.0.0/16 net1
    
  4. 建立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
    
  5. 建立備份資料卷(用於熱備份資料)

    docker volume create --name backup
    
  6. 建立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
    
  7. 安裝Haproxy映象

    docker pull haproxy
    
  8. 宿主機上編寫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
    
  9. 建立兩個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
    
  10. 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
        }
    }
    
  11. 宿主機安裝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
        }
    }
    
  12. 熱備份資料

    #進入node1容器
    docker exec -it node1 bash
    #更新軟體包
    apt-get update
    #安裝熱備工具
    apt-get install percona-xtrabackup-24
    #全量熱備
    innobackupex --user=root --password=abc123456 /data/backup/full
    
  13. 冷還原資料
    停止其餘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/
    

    重新建立其餘4個節點,元件PXC叢集

安裝Redis,配置RedisCluster叢集

  1. 安裝Redis映象

    docker pull yyyyttttwwww/redis
    
  2. 建立net2網段

    docker network create --subnet=172.19.0.0/16 net2
    
  3. 建立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
    
  4. 啟動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
    
  5. 建立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
    

打包部署後端專案

  1. 進入人人開源後端專案,執行打包(修改配置檔案,更改埠,打包三次生成三個JAR檔案)

    mvn clean install -Dmaven.test.skip=true
    
  2. 安裝Java映象

    docker pull java
    
  3. 建立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
    
  4. 安裝Nginx映象

    docker pull nginx
    
  5. 建立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
    
  6. 在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
        }
    }
    

打包部署後端專案

  1. 在前端專案路徑下執行打包指令

    npm run build
    
  2. build目錄的檔案拷貝到宿主機的/home/fn1/renren-vue、/home/fn2/renren-vue、/home/fn3/renren-vue的目錄下面

  3. 建立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
    
  4. 配置負載均衡

    宿主機/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
    
  5. 配置雙機熱備

    #進入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
        }
    }