docker swarm mode --endpoint-mode dnsrr 如何对外访问

Swarm背景

现实中我们的应用可能会有很多,应用本身也可能很复杂,单个Docker Engine所能提供的资源未必能够满足要求。而且应用本身也会有可靠性的要求,希望避免单点故障,这样的话势必需要分布在多个Docker Engine。在这样一个大背景下,Docker社区就产生了Swarm项目。

Swarm是什么

Swarm这个项目名称特别贴切。在Wiki的解释中,Swarm behavior是指动物的群集行为。比如我们常见的蜂群,鱼群,秋天往南飞的雁群都可以称作Swarm behavior。
Swarm4.JPG

Swarm项目正是这样,通过把多个Docker Engine聚集在一起,形成一个大的docker-engine,对外提供容器的集群服务。同时这个集群对外提供Swarm API,用户可以像使用Docker Engine一样使用Docker集群。

Swarm 特点

  • 对外以Docker API接口呈现,这样带来的好处是,如果现有系统使用Docker Engine,则可以平滑将Docker Engine切到Swarm上,无需改动现有系统。
  • Swarm对用户来说,之前使用Docker的经验可以继承过来。非常容易上手,学习成本和二次开发成本都比较低。同时Swarm本身专注于Docker集群管理,非常轻量,占用资源也非常少。 *“Batteries included but swappable”,简单说,就是插件化机制,Swarm中的各个模块都抽象出了API,可以根据自己一些特点进行定制实现。
  • Swarm自身对Docker命令参数支持的比较完善,Swarm目前与Docker是同步发布的。Docker的新功能,都会第一时间在Swarm中体现。

正题,假如设置了--endpoint-mode dnsrr,如何对外呢?

   --endpoint-mode 有两种模式,一种是vip(默认),另一种是dnsrr,dnsrr模式下 -p参数是无效的,就是说其服务器是无法直接通过端口映射被外边访问的,如果想要被外边访问有两种方式:一种是使用浏览器代理,可参考https://5socks.net/Manual/browser_en.html ;另一种是使用类似nginx转发功能。
   
  本文采用的是nginx模式,为了内容的完整性,我们从创建网络开始说起

  # add current user to docker group so that it can execute docker commands

  sudo usermod -aG docker $USER

  # install bash-completion to support bash completion (optional)

  yum -y install bash-completion

  # init a docker swarm cluster and listens on localhost

  docker swarm init --advertise-addr 127.0.0.1

  # create an overlay network

  docker network create --driver overlay swarm-net


  我们创建了名字叫 swarm-net network环境,接着我们创建nginx转发路由服务:

  docker service create \
--replicas 1 \
--name proxy_docker \
--network swarm-net \
--publish published=8088,target=8088 \
--publish published=50070,target=50070 \

centos7.3:latest /usr/sbin/init /bin/bash

我们来看看官方的说明:

Use the --publish flag to publish a port when you create a service. target is used to specify the port inside the container, and published is used to specify the port to bind on the routing mesh. If you leave off the published port, a random high-numbered port is bound for each service task. You need to inspect the task to determine the port.

$ docker service create \
  --name <SERVICE-NAME> \
  --publish published=<PUBLISHED-PORT>,target=<CONTAINER-PORT> \
  <IMAGE>

Note: The older form of this syntax is a colon-separated string, where the published port is first and the target port is second, such as -p 8088:8088. The new syntax is preferred because it is easier to read and allows more flexibility.

The <PUBLISHED-PORT> is the port where the swarm makes the service available. If you omit it, a random high-numbered port is bound. The <CONTAINER-PORT> is the port where the container listens. This parameter is required.

效果跟-p参数是一样的,接着我们进去服务器里边,egg:


通过ps参数我们知道了我们刚刚创建的proxy_docker容器信息(329a7d31bcff),现在正是进去

docker exec -it 329a7d31bcff /bin/sh


安装nginx服务

1、添加源

默认情况Centos7中无Nginx的源,最近发现Nginx官网提供了Centos的源地址。因此可以如下执行命令添加源:
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2、安装Nginx
通过yum search nginx看看是否已经添加源成功。如果成功则执行下列命令安装Nginx。
yum install -y nginx

3、设置docker环境下的运行许可

nginx 默认允许在daemon模式下,在执行docker run -d nginx /usr/bin/nginx 的时候会自动退出,

在nginx的nginx.conf文件最上面一行加入。

vi /etc/nginx/nginx.conf

daemon off;

添加转发配置,在/etc/nginx/conf.d/目录下添加:

upstream hadoop {
    server hadoop-master:50070;
}
server {
    listen       50070;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        proxy_pass   http://hadoop;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
server {
    listen       8088;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        proxy_pass   http://hadoop_hdfs;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
启动
/usr/sbin/nginx &



如果映射的端口无法连通
解决办法:
# vi /etc/sysctl.conf
或者
# vi /usr/lib/sysctl.d/00-system.conf
添加如下代码:
net.ipv4.ip_forward=1


重启network服务
# systemctl restart network

查看是否修改成功

# sysctl net.ipv4.ip_forward


截图:


宿机下执行 netstat -tlnp 发现端口映射有ok了,然后浏览器访问:





都是ok的


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值