docker+Emqx+nginx集群搭建

拉取镜像

docker pull emqx/emqx;

创建虚拟网络

docker network create -d bridge --subnet=172.18.0.0/16 emqx_bridge

启动服务

节点1

docker run -d --hostname emqx01 --name emqx01 --network emqx_bridge --ip 172.18.0.2 -p 60001:1883 -p 60004:8083 -p 60007:8883 -p 60010:8084 -p 60017:18083 -p 60013:11883 -v /etc/localtime:/etc/localtime:ro emqx/emqx:latest;

节点2

docker run -d --hostname emqx02 --name emqx02 --network emqx_bridge --ip 172.18.0.3 -p 60002:1883 -p 60005:8083 -p 60008:8883 -p 60011:8084 -p 60018:18083 -p 60014:11883 -v /etc/localtime:/etc/localtime:ro emqx/emqx:latest;

节点3

docker run -d --hostname  emqx03 --name emqx03 --network emqx_bridge --ip 172.18.0.4 -p 60003:1883 -p 60006:8083 -p 60009:8883 -p 60012:8084 -p 60019:18083 -p 60015:11883 -v /etc/localtime:/etc/localtime:ro emqx/emqx:latest

    

    

    

    

配置集群归属

节点2

docker exec -it emqx02 sh

bin/emqx_ctl  cluster join emqx01@172.18.0.2

exit

节点3

docker exec -it emqx03 sh

bin/emqx_ctl  cluster join emqx01@172.18.0.2

bin/emqx_ctl cluster status

exit

 

 负载均衡

 拉取镜像

Docker  pull  nginx;

启动项目

docker run --name nginx -p 80:80 -d nginx;

 

 本地映射配置文件

mkdir -p /data/nginx

mkdir -p /data/nginx/www

mkdir -p /data/nginx/conf

mkdir -p /data/nginx/logs

 

 复制配置文件到主机

docker cp 容器id:/etc/nginx/nginx.conf /data/nginx/

docker cp 容器id:/etc/nginx/conf.d /data/nginx/conf/

docker cp 容器id:/usr/share/nginx/html/ /data/nginx/www/

docker cp 容器id:/var/log/nginx/ /data/nginx/logs/

移除之前的服务

docker stop 容器id    docker rm  容器id

配置文件编辑

/data/nginx/conf/default.conf此处文件配置不变

/data/nginx/nginx.conf 配置文件进行修改

user  nginx;

 

worker_processes  1;

 

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

stream{

        # emqx tcp

        upstream emqxTcp {

                #hash $remote_addr consistent;

                server 192.168.100.29:60001 max_fails=3 fail_timeout=30s;

                server 192.168.100.29:60002 max_fails=3 fail_timeout=30s;

                server 192.168.100.29:60003 max_fails=3 fail_timeout=30s;

        }

        # emqx tcp server

        server {

                listen 1883;

                #proxy_timeout 180s;

                proxy_pass emqxTcp;

        }

}

 

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;

    include /etc/nginx/conf.d/*.conf; #嵌套

 

    upstream stream_backend {

          server 192.168.100.29:60017 max_fails=2 fail_timeout=30s;

          server 192.168.100.29:60018 max_fails=2 fail_timeout=30s;

          server 192.168.100.29:60019 max_fails=2 fail_timeout=30s;

      }

    server {

        listen    80;

        server_name   localhost;

        location / {

            proxy_pass  http://stream_backend;

        }

    }   

}

启动文件

docker run --name nginx -p 80:80 -p 1883:1883 --network emqx_bridge --ip 172.18.0.6   -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/www/:/usr/share/nginx/html/  -v /data/nginx/logs/:/var/log/nginx/  -v  /data/nginx/conf/:/etc/nginx/conf.d  --privileged=true -d nginx

 

页面展示

 

192.168.100.29:80

Nginx跳转随机切换emqx(目前采用的轮询策略)

posted @ 2022-04-21 20:48  大日很忧伤  阅读(665)  评论(0编辑  收藏  举报