如何获取在机器上启动的单元的IP地址,并在另一个机组中使用它? [英] How can I get the IP address of a unit started on a machine and use it in another unit with fleet?

查看:211
本文介绍了如何获取在机器上启动的单元的IP地址,并在另一个机组中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的CoreOS和Docker,我正面临着车队的问题。



我有一个标准的单位启动一个POSTGRES容器,我想了解启动本机的机器的IP地址
我实际上是一组3台机器,POSTGRES单元并不总是在同一台机器上启动(这意味着IP不是静态的)。



当我开始另一个单元(见下文)需要POSTGRES时,需要它。



我正在使用单位参数称为BindsTo:

  [单位] 
描述=测试
之后= docker.service
After=postgres@1.service

要求= docker.service
Requires = postgres@1.service

BindsTo=postgres@1.service

[服务]
TimeoutStartSec = 0
ExecStartPre = - / usr / bin / docker kill test%i
ExecStartPre = - / usr / bin / docker rm test%i
ExecStart = / usr / bin / docker run -rm --name test%i -e HOST =HereThePostgresIPsryther / test
ExecStop = / usr / bin / docker stop test%i

NB :该单位不需要在与POSTGRES单元相同的机器上启动。



我尝试使用%H变量,但是它返回主机名,而不是机器的IP地址。



Flannel也用于我的集群。



谢谢






我发现了一种解决方法:

  [单位] 
描述=测试
后= docker.service

要求= docker.service

[服务]
TimeoutStartSec = 0
ExecStartPre = - / usr / bin / docker kill test%i
ExecStartPre = - / usr / bin / docker rm test%i

ExecStart = / usr / bin / docker run -rm --name test%i --link postgres1:postgres sryther / test
ExecStop = / usr / bin / docker stop test%i

并且IP地址在容器环境中设置为 POSTGRES_PORT_5432_TCP_ADDR

解决方案

根据Greg我使用了SkyDNS和注册人:




我正在使用注册商在我的车队环境中发现。当我启动我的车队时,我修改云配置文件,还包括注册商(以及etcd,法兰绒,车队等)。登记员在码头营地营地,并识别何时新的集装箱启动/停止/停止。注册管理员管理已发现的容器的数据库,它具有多种口味(如领事,skydns)。我配置我的运行在skydns模式。


下面你可以找到我的工作云配置,请记住,我正在使用CoreOS 607和我的测试在VMware上执行。我的目标是设置3台机器,使一个小型集群在每台机器上运行,如这个



我设法找到了这个解决方案,感谢Greg关于这个话题和与Vagrant一​​起使用的云配置文件



我希望它会帮助:)






  cloud-config 

coreos:
etcd:
发现:https://discovery.etcd.io/4c8c3f9a67048e76e9075c97d9d63c0c
addr:192.168.1.13:4001
peer-addr:192.168.1.13:7001
fleet:
public-ip:192.168.1.13
units:
- name:etcd.service
command:start
- 名称:flanneld.service
drop-ins:
- 名称:50-network-config.conf
content:|
[服务]
ExecStartPre = / usr / bin / etcdctl set /coreos.com/network/config'{网络:172.16.0.0/16}'
命令:start
- 名称:fleet.service
命令:start
- 名称:skydns.service
enable:false
content:|
[单位]
描述= Skydns
After = docker.service
After = fleet.service
[服务]
ExecStartPre = / usr / bin / docker pull skynetservices / skydns
ExecStart = / usr / bin / env bash -c'/ usr / bin / docker run -p 53:53 --name skydns -e ETCD_MACHINES =http:// $(ifconfig docker0 | awk \'/ \< inet\> / {print $ 2} \'):4001skynetservices / skydns -nameservers =8.8.8.8:53--addr =0.0.0.0:53 '
ExecStop = / usr / bin / docker stop skydns
Restart = always

[X-Fleet]
Global = true

[Install]
WantedBy = multiuser.target
- name:registrator.service
enable:false
content:|
[单位]
描述=注册人
After = docker.service
After = fleet.service

[服务]
ExecStartPre = usr / bin / docker pull gliderlabs / registrator
ExecStart = / usr / bin / env bash -c'/ usr / bin / docker run --net = host -p 8080:8080 -p 8443:8443 -v / var / run / docker.sock:/tmp/docker.sock --name注册器gliderlabs / registrator skydns2:// $(ifconfig docker0 | awk \'/ \< inet\> / {print $ 2} \\ \\'):4001 / skydns.local'
ExecStop = / usr / bin / docker stop registrator
重新启动=总是

[X-Fleet]
全局= true
- name:primordial.service
命令:start
content:|
[单位]
说明=加载和启动车队服务
后= fleet.service
要求= fleet.service
后= etcd.service
要求= etcd.service
After = flanneld.service
Requires = flanneld.service

[服务]
Type = oneshot
RemainAfterExit = yes
ExecStartPre = / usr / bin / systemctl stop docker.service
ExecStartPre = / usr / bin / ifconfig docker0 down
ExecStartPre = / usr / bin / brctl delbr docker0
ExecStartPre = / usr / bin / systemctl start docker.service
ExecStart = / usr / bin / fleetctl start /etc/systemd/system/skydns.service
ExecStart = / usr / bin / fleetctl start / etc / systemd / system / registrator.service
ssh_authorized_keys:
- ssh-rsa AAAA ....


I'm new to CoreOS and Docker and I'm facing with a problem with fleet.

I have a standard unit launching a POSTGRES container and I would like to know the IP address of the machine where this unit is started. I have actually a cluster of 3 machines and the POSTGRES unit isn't always started on the same machine (which means the IP is not static).

I need it when I start another unit (see below), which needs a POSTGRES.

I'm using at the moment the Unit Parameter called BindsTo :

[Unit]
Description=Test
After=docker.service
After=postgres@1.service

Requires=docker.service
Requires=postgres@1.service

BindsTo=postgres@1.service

[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill test%i
ExecStartPre=-/usr/bin/docker rm test%i
ExecStart=/usr/bin/docker run -rm --name test%i -e HOST="HereThePostgresIP" sryther/test
ExecStop=/usr/bin/docker stop test%i

N.B. : This unit doesn't need to be started on the same machine as the POSTGRES unit.

I tried with %H variable but it returns the hostname and not the IP address of the machine.

Flannel is also used in my cluster.

Thanks!


I found a workaround :

[Unit]
Description=Test
After=docker.service

Requires=docker.service

[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill test%i
ExecStartPre=-/usr/bin/docker rm test%i

ExecStart=/usr/bin/docker run -rm --name test%i --link postgres1:postgres sryther/test
ExecStop=/usr/bin/docker stop test%i

And the IP address is set in the container environment as POSTGRES_PORT_5432_TCP_ADDR.

解决方案

According to Greg I used SkyDNS and Registrator :

I am using registrator for discovery in my fleet environment. When I start up my fleet I modify the cloud-config file to also include registrator (along with etcd, flannel, fleet, etc). Registrator camps on docker and recognizes when new containers are started/killed/stop. Registrator manages a database of discovered containers, it does so in several flavors (like consul, skydns). I configure mine to run in skydns mode.

Below you can find my working cloud-config, keep in mind that I'm using CoreOS 607 and my tests are performed on VMware. My goal was to set up 3 machines to make a small cluster with one etcd running on each machine like this.

I managed to found this solution thanks to Greg on this topic and with this cloud-config file used with Vagrant.

I hope it'll help :)


#cloud-config

coreos:
  etcd:
    discovery: https://discovery.etcd.io/4c8c3f9a67048e76e9075c97d9d63c0c
    addr: 192.168.1.13:4001
    peer-addr: 192.168.1.13:7001
  fleet:
    public-ip: 192.168.1.13
  units:
    - name: etcd.service
      command: start
    - name: flanneld.service
      drop-ins:
        - name: 50-network-config.conf
          content: |
            [Service]
            ExecStartPre=/usr/bin/etcdctl set /coreos.com/network/config '{ "Network": "172.16.0.0/16" }'
      command: start
    - name: fleet.service
      command: start
    - name: skydns.service
      enable: false
      content: |
        [Unit]
        Description=Skydns
        After=docker.service
        After=fleet.service
        [Service]
        ExecStartPre=/usr/bin/docker pull skynetservices/skydns
        ExecStart=/usr/bin/env bash -c '/usr/bin/docker run -p 53:53 --name skydns -e ETCD_MACHINES="http://$(ifconfig docker0 | awk \'/\<inet\>/ { print $2}\'):4001" skynetservices/skydns -nameservers="8.8.8.8:53" --addr="0.0.0.0:53"'
        ExecStop=/usr/bin/docker stop skydns
        Restart=always

        [X-Fleet]
        Global=true

        [Install]
        WantedBy=multi-user.target
    - name: registrator.service
      enable: false
      content: |
        [Unit]
        Description=Registrator
        After=docker.service
        After=fleet.service

        [Service]
        ExecStartPre=/usr/bin/docker pull gliderlabs/registrator
        ExecStart=/usr/bin/env bash -c '/usr/bin/docker run --net=host -p 8080:8080 -p 8443:8443 -v /var/run/docker.sock:/tmp/docker.sock --name registrator gliderlabs/registrator skydns2://$(ifconfig docker0 | awk \'/\<inet\>/ { print $2}\'):4001/skydns.local'
        ExecStop=/usr/bin/docker stop registrator
        Restart=always

        [X-Fleet]
        Global=true
    - name: primordial.service
      command: start
      content: |
        [Unit]
        Description=Load and start fleet services
        After=fleet.service
        Requires=fleet.service
        After=etcd.service
        Requires=etcd.service
        After=flanneld.service
        Requires=flanneld.service

        [Service]
        Type=oneshot
        RemainAfterExit=yes
        ExecStartPre=/usr/bin/systemctl stop docker.service
        ExecStartPre=/usr/bin/ifconfig docker0 down
        ExecStartPre=/usr/bin/brctl delbr docker0
        ExecStartPre=/usr/bin/systemctl start docker.service
        ExecStart=/usr/bin/fleetctl start /etc/systemd/system/skydns.service
        ExecStart=/usr/bin/fleetctl start /etc/systemd/system/registrator.service
ssh_authorized_keys:
  - ssh-rsa AAAA....

这篇关于如何获取在机器上启动的单元的IP地址,并在另一个机组中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆