idetcd

原始碼 首頁

啟用方式
idetcd:github.com/jiachengxu/idetcd

idetcd - 基於 etcd 的 CoreDNS 外掛程式,用於識別叢集中的節點,而不會發生網域名稱衝突。

描述

idetcd 用於識別叢集中的節點,而不會發生網域名稱衝突。基本概念很簡單:當您要啟動叢集時,在每個節點上設定 CoreDNS 伺服器,並且節點會透過在 etcd 中取得空閒網域名稱來公開自身。

語法

idetcd {
	endpoint ENDPOINT...
	limit LIMIT
	pattern PATTERN
}
  • endpoint 定義 etcd 端點。預設值為 “http://localhost:2379”。
  • limit 定義叢集中節點數量的最大限制,如果叢集中節點數量達到此限制後,有其他節點要公開自身,則會失敗。
  • pattern 定義叢集中每個節點遵循的網域名稱模式。這裡我們使用 golang 範本來定義模式。

範例

在以下範例中,我們將啟動一個包含 5 個節點的叢集,在每個節點上,我們可以透過以下方式取得這個專案:

$ go get -u github.com/jiachengxu/idetcd

在您進行下一步之前,請確保您已經設定好 etcd 實例,並且不要忘記寫下端點。

然後您需要新增一個 Corefile,其中指定了 CoreDNS 伺服器的設定,它與 main.go 位於同一目錄。以下是一個簡單的 Corefile 範例,請前往 CoreDNS GitHub 儲存庫 以取得更多詳細資訊。

. {
    idetcd {
        endpoint ETCDENDPOINTS
        limit 5
        pattern worker{{.ID}}.tf.local.
    }
}

然後您可以使用以下方式產生二進制檔案:

$ go build -v -o coredns

或者,如果您已安裝 docker,也可以執行以下命令來建置:

$ docker run --rm -i -t -v $PWD:/go/src/github.com/jiachengxu/idetcd \
      -w /go/src/github.com/jiachengxu/idetcd golang:1.10 go build -v -o coredns

然後執行它:

$ ./coredns

之後,叢集中的所有節點都會嘗試在 etcd 中找到空閒位置來公開自身。一旦成功,您就可以在同一個叢集中的每個節點上,透過以下方式取得每個節點的網域名稱:

$ dig +short worker4.tf.local @localhost

也支援 IPv6

$ dig +short worker4.tf.local AAAA @localhost

與 AWS 整合

使用 CoreDNS 和 idetcd 外掛程式來設定叢集是一個一次性的過程,與一般的設定過程不同。例如,如果您想在 AWS 上設定一個包含多個執行個體的叢集,您可以為每個執行個體使用相同的設定,並讓所有執行個體在 init 過程中公開自身。這可以使用 cloud-inituser data 中實現。以下是一個在 AWS 執行個體啟動時執行的 bash 腳本範例:

#!/bin/bash
set -x
## Install docker.
yum install -y docker
echo
chkconfig docker on
service docker start
echo
## Install git.
yum install -y git
git clone https://github.com/jiachengxu/idetcd.git /home/ec2-user/idetcd
cd /home/ec2-user/idetcd
## Using docker to build the binary file of CoreDns with idetcd plugin specified.
docker run --rm -v $PWD:/go/src/github.com/jiachengxu/idetcd -w /go/src/github.com/jiachengxu/idetcd golang:1.10 go build -v -o coredns
## Create a Corefile for specifying the configuration of CoreDNS.(Don't forget to replace the ETCDENDPOINTS and NUMBER with your own etcd endpoints and limit of node in the cluster!)
cat > Corefile << EOF
. {
    idetcd {
        endpoint ETCDENDPOINTS
        limit NUMBER
        pattern worker{{.ID}}.tf.local.
    }
}
EOF
./coredns