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

Docker虛擬機經常使用命令

  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
     
  11. 查看容器信息
    docker inspect 容器ID
     
  12. 刪除容器
    docker rm 容器ID
     
  13. 數據卷管理
    docker volume create 數據卷名稱  #建立數據卷
    docker volume rm 數據卷名稱  #刪除數據卷
    docker volume inspect 數據卷名稱  #查看數據卷
     
  14. 網絡管理
    docker network ls 查看網絡信息
    docker network create --subnet=網段 網絡名稱
    docker network rm 網絡名稱
     
  15. 避免VM虛擬機掛起恢復以後,Docker虛擬機斷網
    vi /etc/sysctl.conf
     

    文件中添加net.ipv4.ip_forward=1這個配置html

    #重啓網絡服務
    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
     

    配置文件以下:vue

    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數據庫心跳檢測。CREATE USER 'haproxy'@'%' IDENTIFIED BY '';  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
     

    配置文件內容以下:java

    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
     

    配置文件內容以下:node

    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配置文件以下:mysql

    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的數據nginx

    #刪除數據
    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

安裝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
     
    vim /usr/redis/redis.conf 修改內容
    daemonize yes                   #之後臺進程運行
    cluster-enabled yes             #開啓集羣
    cluster-config-file nodes.conf  #集羣配置文件
    cluster-node-timeout  15000     #超時時間
    appendonly  yes                 #開啓AOF模式
     
  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配置文件內容以下:sql

    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
    Shell

    宿主機/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
       }
    }
相關文章
相關標籤/搜索