備注:
coredns 默認已經安裝了一些插件,比如大家用的多的kubernetes etcd ... 但是我們可以自己編譯插件,構建我們自己的
coredns 版本,方便集成使用
1. 項目結構
├── Corefile
├── coredns.go
2. 參考代碼
a. main.go
package main
import (
_ "github.com/coredns/example"
"github.com/coredns/coredns/coremain"
_ "github.com/coredns/coredns/plugin/auto"
_ "github.com/coredns/coredns/plugin/autopath"
_ "github.com/coredns/coredns/plugin/bind"
_ "github.com/coredns/coredns/plugin/cache"
_ "github.com/coredns/coredns/plugin/chaos"
_ "github.com/coredns/coredns/plugin/debug"
_ "github.com/coredns/coredns/plugin/dnssec"
_ "github.com/coredns/coredns/plugin/dnstap"
_ "github.com/coredns/coredns/plugin/erratic"
_ "github.com/coredns/coredns/plugin/errors"
_ "github.com/coredns/coredns/plugin/etcd"
_ "github.com/coredns/coredns/plugin/federation"
_ "github.com/coredns/coredns/plugin/file"
_ "github.com/coredns/coredns/plugin/health"
_ "github.com/coredns/coredns/plugin/hosts"
_ "github.com/coredns/coredns/plugin/kubernetes"
_ "github.com/coredns/coredns/plugin/loadbalance"
_ "github.com/coredns/coredns/plugin/log"
_ "github.com/coredns/coredns/plugin/metrics"
_ "github.com/coredns/coredns/plugin/nsid"
_ "github.com/coredns/coredns/plugin/pprof"
_ "github.com/coredns/coredns/plugin/proxy"
_ "github.com/coredns/coredns/plugin/reload"
_ "github.com/coredns/coredns/plugin/reverse"
_ "github.com/coredns/coredns/plugin/rewrite"
_ "github.com/coredns/coredns/plugin/root"
_ "github.com/coredns/coredns/plugin/route53"
_ "github.com/coredns/coredns/plugin/secondary"
_ "github.com/coredns/coredns/plugin/template"
_ "github.com/coredns/coredns/plugin/tls"
_ "github.com/coredns/coredns/plugin/trace"
_ "github.com/coredns/coredns/plugin/whoami"
_ "github.com/coredns/example"
_ "github.com/coredns/forward"
_ "github.com/mholt/caddy/onevent"
_ "github.com/mholt/caddy/startupshutdown"
_ "github.com/arvancloud/redis"
"github.com/coredns/coredns/core/dnsserver"
)
var directives = []string{
"example",
"tls",
"reload",
"nsid",
"root",
"bind",
"debug",
"trace",
"health",
"pprof",
"prometheus",
"errors",
"log",
"dnstap",
"chaos",
"loadbalance",
"cache",
"rewrite",
"dnssec",
"autopath",
"reverse",
"template",
"hosts",
"route53",
"federation",
"kubernetes",
"file",
"auto",
"secondary",
"etcd",
"redis",
"forward",
"proxy",
"erratic",
"whoami",
"on",
"startup",
"shutdown",
}
func init() {
dnsserver.Directives = directives
}
func main() {
coremain.Run()
}
b. Corefile
example.com {
proxy . 8.8.8.8:53
log
example
}
3. 代碼說明
a. main.go
為集成全部插件,我使用了 coredns 源碼中的plugins 配置代碼里面包含了默認的全部,同時我添加了
官方的example 以及一個redis 插件
b. Corefile
配置文件使用已有的插件 example
4. 構建
a. 依賴包安裝
簡答的方式是使用源碼包中的make 腳本,對於沒有的使用go get 進行安裝
b. 構建
go build
c. 查看編譯的插件
./main -plugins
Password:
Server types:
dns
Caddyfile loaders:
flag
default
Other plugins:
dns.auto
dns.autopath
dns.bind
dns.cache
dns.chaos
dns.debug
dns.dnssec
dns.dnstap
dns.erratic
dns.errors
dns.etcd
dns.example
dns.federation
dns.file
dns.forward
dns.health
dns.hosts
dns.kubernetes
dns.loadbalance
dns.log
dns.nsid
dns.pprof
dns.prometheus
dns.proxy
dns.redis
dns.reload
dns.reverse
dns.rewrite
dns.root
dns.route53
dns.secondary
dns.template
dns.tls
dns.trace
dns.whoami
on
shutdown
startup
5. 運行
sudo ./main -conf Corefile
參考日志:
example.com.:53
2018/02/02 22:32:12 [INFO] CoreDNS-1.0.5
2018/02/02 22:32:12 [INFO] darwin/amd64, go1.9.3,
CoreDNS-1.0.5
darwin/amd64, go1.9.3,
2018/02/02 22:32:12 [INFO] "A IN connpm.gj.qq.com." - No such zone at dns://:53 (Remote: 192.168.31.161:59752)
2018/02/02 22:32:41 [INFO] "A IN www.thoughtworks.com." - No such zone at dns://:53 (Remote: 192.168.31.161:52965)
2018/02/02 22:32:45 [INFO] "A IN static.thoughtworks.com." - No such zone at dns://:53 (Remote: 192.168.31.161:54903)
2018/02/02 22:32:45 [INFO] "A IN s7.addthis.com." - No such zone at dns://:53 (Remote: 192.168.31.161:63212)
2018/02/02 22:32:45 [INFO] "A IN qzonestyle.gtimg.cn." - No such zone at dns://:53 (Remote: 192.168.31.161:54212)
2018/02/02 22:32:45 [INFO] "A IN app-e.marketo.com." - No such zone at dns://:53 (Remote: 192.168.31.161:62861)
2018/02/02 22:32:45 [INFO] "A IN dynamic.thoughtworks.com." - No such zone at dns://:53 (Remote: 192.168.31.161:54761)
2018/02/02 22:3
。。。。。
6. 參考資料
https://github.com/coredns/example
https://github.com/arvancloud/redis
https://github.com/coredns/coredns
https://coredns.io/2017/07/25/compile-time-enabling-or-disabling-plugins/