etcd+registrator+confd 服务发现

(金庆的专栏 2018.6)

因为k8s使用 etcd, 所以选 etcd 作为服务发现的 DB.

registrator 可以为 docker 方式运行的服务自动注册到 etcd.

confd 读取 etcd, 生成配置文件。

先运行一个etcd用于测试:

docker run -d \
  -p 12379:2379 \
  --name jinqing-etcd \
  quay.io/coreos/etcd \
   /usr/local/bin/etcd \
  --listen-client-urls http://0.0.0.0:2379 \
  --advertise-client-urls http://0.0.0.0:12379 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

再运行 registrator:

docker run -d --rm \
    --name=jinqing-registrator \
    --net=host \
    --volume=/var/run/docker.sock:/tmp/docker.sock \
    gliderlabs/registrator:latest \
      -ip="192.168.93.183" \
      etcd://127.0.0.1:12379/registrator
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

好像只能使用本机的 etcd. 一般需要用-ip参数指定本机IP。注册到 registrator 目录。

用 etcdkeeper 可以查看自动注册的服务。registrator 不支持 etcd v3.

这里写图片描述

然后配置 confd

mkdir -p /etc/confd/{conf.d,templates}
 
 
  • 1

/etc/confd/conf.d/myconfig.toml

[template]
src = "myconfig.toml.tmpl"
dest = "/tmp/myconfig.toml"
keys = [
    "/registrator",
]
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

/etc/confd/templates/myconfig.toml.tmpl

[config]
{{- range lsdir "/registrator"}}
{{-     $serviceName := . }}
{{-     $<span class="hljs-variable">serviceDir</span> := <span class="hljs-variable">printf</span> <span class="hljs-string">"/registrator/%s/*"</span> $serviceName }}

    [config.{{ $serviceName }}]
    # {{ $serviceDir }}

{{-     range gets $serviceDir }}
    {{ base .Key }} = {{ .Value }}
{{-     end }}

{{- end}}





# End of [config].
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

用 lsdir 列出所有服务目录,然后用 gets 取服务目录下的键值对。

执行 confd:

~/go/bin/confd -onetime -backend etcd -node http://127.0.0.1:12379
 
 
  • 1
[jinqing@localhost confd]$ cat /tmp/myconfig.toml
[config]

[config<span class="hljs-preprocessor">.etcd</span>-<span class="hljs-number">2379</span>]
<span class="hljs-preprocessor"># /registrator/etcd-2379/*</span>
localhost<span class="hljs-preprocessor">.tech</span>:jinqing-etcd:<span class="hljs-number">2379</span> = <span class="hljs-number">192.168</span><span class="hljs-number">.93</span><span class="hljs-number">.183</span>:<span class="hljs-number">12379</span>

[config<span class="hljs-preprocessor">.nginx</span>]
<span class="hljs-preprocessor"># /registrator/nginx/*</span>
localhost<span class="hljs-preprocessor">.tech</span>:jinqing-nginx:<span class="hljs-number">80</span> = <span class="hljs-number">192.168</span><span class="hljs-number">.93</span><span class="hljs-number">.183</span>:<span class="hljs-number">1024</span>

[config<span class="hljs-preprocessor">.registry</span>]
<span class="hljs-preprocessor"># /registrator/registry/*</span>
localhost<span class="hljs-preprocessor">.tech</span>:registry:<span class="hljs-number">5000</span> = <span class="hljs-number">192.168</span><span class="hljs-number">.93</span><span class="hljs-number">.183</span>:<span class="hljs-number">5000</span>

# End of [config].

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16





阅读更多

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐