Loading

在docker中使用nginx部署前端项目

前言

部署了三个nginx用于前端项目, 并使用keepalived部署好热备, 所以总共有5nginx

创建好nginx的文件和配置

根据上面的指令创建好目录

mkdir /home/web/nginx01
mkdir /home/web/nginx02
mkdir /home/web/nginx03
  • nginx.conf配置文件

这里我配置了三个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.0.200;
		location / {
	        root   /home/web/nginx/renren-vue;
	        index  index.html index.htm;
    	}
	}
}

前面的差别不大, 就后面这一点不太一样

server {
	listen 6501;
	server_name  192.168.0.200;
	location / {
        root   /home/web/nginx/renren-vue;
        index  index.html index.htm;
   	}
}
server {
	listen 6502;
	server_name  192.168.0.200;
	location / {
        root   /home/web/nginx/renren-vue;
        index  index.html index.htm;
   	}
}
server {
	listen 6503;
	server_name  192.168.0.200;
	location / {
        root   /home/web/nginx/renren-vue;
        index  index.html index.htm;
   	}
}

docker配置指令

docker run -it -d --name=web01 --net=host \
-v /home/web/nginx01/nginx.conf:/etc/nginx/nginx.conf \
-v /home/web/nginx01:/home/web/nginx/renren-vue \
--user=root --privileged=true nginx

docker run -it -d --name=web02 --net=host \
-v /home/web/nginx02/nginx.conf:/etc/nginx/nginx.conf \
-v /home/web/nginx02:/home/web/nginx/renren-vue \
--user=root --privileged=true nginx

docker run -it -d --name=web03 --net=host \
-v /home/web/nginx03/nginx.conf:/etc/nginx/nginx.conf \
-v /home/web/nginx03:/home/web/nginx/renren-vue \
--user=root --privileged=true nginx

利用keepalived配置热备方案

配置主机keepalived

  • 进入容器
    docker exec -it bn01 bash
  • 更新
    apt update
  • 安装vim
    apt install vim
  • 安装keepalived
    apt install keepalived
  • 修改配置文件
    vi /etc/keepalived/keepalived.conf
  • 写入配置文件
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.0.160
    }
}
virtual_server 192.168.0.160 6701 {
    delay_loop 3
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
    real_server 192.168.0.200 6601 {
        weight 1
    }
}
  • 启动
    service keepalived start
  • 验证
    docker top bn01
    看下keepalived进程是否运行, 如果运行再访问
    http://192.168.0.160:6701/#/home
    看下是否能够访问

配置备机

  • 进入容器
    docker exec -it bn02 bash
  • 更新
    apt update
  • 安装vim
    apt install vim
  • 安装keepalived
    apt install keepalived
  • 修改配置文件
    vi /etc/keepalived/keepalived.conf
  • 写入配置文件
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 52
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.0.160
    }
}
virtual_server 192.168.0.160 6701 {
    delay_loop 3
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
    real_server 192.168.0.200 6602 {
        weight 1
    }
}
  • 启动
    service keepalived start
  • 验证
    docker top bn02
    看下keepalived进程是否存在, 如果存在则
    http://192.168.0.160:6701/#/home
    看下是否能够访问

现在停止掉主机, 然后稍等一会儿再看看是否能够运行
exit 退出docker容器
执行docker stop bn01停止掉主机

可能需要判断下, docker logs bn01docker logs bn02 看下最近一次访问是否, 我这里发现是备机在运行, 所以实际上我执行的是docker stop bn02

发现访问http://192.168.0.160:6701/#/home网址还是没问题

这里需要等待几分钟, 可改等待时间

成功部署热备

posted @ 2020-07-17 15:11  bangiao  阅读(2769)  评论(0编辑  收藏  举报