SkyDNS 编译、部署、使用(备忘)

SkyDNS

SkyDNS 是一个域名解析服务器。使用 etcd 作为后端存储

Github 地址: https://github.com/skynetservices/skydns

Kubernets 的 kube-dns 是基于 SkyDNS 的实现

Kubernets 当前默认的 DNS 服务 CoreDNS ,是 SkyDNS 的继任者,支持 SkyDNS 的所有特性

熟悉下 SkyDNS 有助于理解 Kubernets 的服务发现机制

编译 SkyDNS

官网介绍使用 go get ,现在基本上是编译不过的

go get github.com/skynetservices/skydns
cd $GOPATH/src/github.com/skynetservices/skydns
go build -v

  
 
  • 1
  • 2
  • 3

可以自己做下 go mod 来编译:

添加 go.mod 文件,如下:

module github.com/skynetservices/skydns

go 1.16

replace github.com/coreos/bbolt => go.etcd.io/bbolt v1.3.5

replace go.etcd.io/bbolt => github.com/coreos/bbolt v1.3.5

require (
	github.com/coreos/bbolt v0.0.0-00010101000000-000000000000 // indirect
	github.com/coreos/etcd v3.3.25+incompatible
	github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
	github.com/miekg/dns v1.1.41
	github.com/prometheus/client_golang v1.10.0
	golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4
)

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

再执行:

go mod tidy
go build

  
 
  • 1
  • 2

运行 SkyDNS

1. 安装 Etcd

这里直接用软件安装:

sudo apt install etcd

  
 
  • 1

修改服务启动参数:

fananchong@vm-ubuntu20:~$ cat /lib/systemd/system/etcd.service
[Unit]
Description=etcd - highly-available key value store
Documentation=https://github.com/coreos/etcd
Documentation=man:etcd
After=network.target
Wants=network-online.target

[Service]
Environment=DAEMON_ARGS="--listen-client-urls=http://0.0.0.0:2379 --advertise-client-urls=http://0.0.0.0:2379 --listen-peer-urls=http://0.0.0.0:2380"
Environment=ETCD_NAME=%H
Environment=ETCD_DATA_DIR=/var/lib/etcd/default
EnvironmentFile=-/etc/default/%p
Type=notify
User=etcd
PermissionsStartOnly=true
#ExecStart=/bin/sh -c "GOMAXPROCS=$(nproc) /usr/bin/etcd $DAEMON_ARGS"
ExecStart=/usr/bin/etcd $DAEMON_ARGS
Restart=on-abnormal
#RestartSec=10s
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
Alias=etcd2.service

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

主要对 Environment=DAEMON_ARGS="--listen-client-urls=http://0.0.0.0:2379 --advertise-client-urls=http://0.0.0.0:2379 --listen-peer-urls=http://0.0.0.0:2380" 这行填充启动参数

2. 启动 SkyDNS

执行以下命令:

fananchong@vm-ubuntu20:~/skydns$ sudo ./skydns -machines=http://127.0.0.1:2379 -etcd3=true
[sudo] password for fananchong: 
2021-03-21 12:40:46.897207 I | skydns: metrics enabled on :/metrics
2021-03-21 12:40:46.897248 I | skydns: ready for queries on skydns.local. for tcp://127.0.0.1:53 [rcache 0]
2021-03-21 12:40:46.897263 I | skydns: ready for queries on skydns.local. for udp://127.0.0.1:53 [rcache 0]

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

使用 SkyDNS

1. 先往 Etcd 插入些 DNS 记录

执行以下命令:

export ETCDCTL_API=3
etcdctl put /skydns/local/skydns/east/production/rails/1 '{"host":"service1.example.com","port":8080}'
etcdctl put /skydns/local/skydns/west/production/rails/2 '{"host":"service2.example.com","port":8080}'
etcdctl put /skydns/local/skydns/east/staging/rails/4 '{"host":"10.0.1.125","port":8080}'
etcdctl put /skydns/local/skydns/east/staging/rails/6 '{"host":"2003::8:1","port":8080}'

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

etcd 中会有以下记录:
在这里插入图片描述

2. 从 SkyDNS 中查询

比如执行以下命令:

fananchong@vm-ubuntu20:~/skydns$ dig @localhost staging.east.skydns.local. SRV

; <<>> DiG 9.16.1-Ubuntu <<>> @localhost staging.east.skydns.local. SRV
; (1 server found)
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23559
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 2

;; QUESTION SECTION:
;staging.east.skydns.local. IN SRV

;; ANSWER SECTION:
staging.east.skydns.local. 3600 IN SRV 10 50 8080 4.rails.staging.east.skydns.local.
staging.east.skydns.local. 3600 IN SRV 10 50 8080 6.rails.staging.east.skydns.local.

;; ADDITIONAL SECTION:
4.rails.staging.east.skydns.local. 3600 IN A 10.0.1.125
6.rails.staging.east.skydns.local. 3600 IN AAAA 2003::8:1

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sun Mar 21 12:44:38 UTC 2021
;; MSG SIZE  rcvd: 193

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

可见,通过 dig 查询 staging.east.skydns.local ,可以得到 2 个 IP

4.rails.staging.east.skydns.local. 3600 IN A 10.0.1.125
6.rails.staging.east.skydns.local. 3600 IN AAAA 2003::8:1

  
 
  • 1
  • 2

其他

Etcd UI 客户端, etcdkeepr 安装:

docker run --name=etcdkeeper -d --restart=always -p 8080:8080 evildecay/etcdkeeper

  
 
  • 1

文章来源: blog.csdn.net,作者:fananchong2,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/u013272009/article/details/115055039

(完)