本文通过Docker部署CoreDNS和Nacos-coredns-plugin测试Nacos 动态DNS功能
相关链接
版本约定:
Nacos:v1.0.0-RC2(Mar 22, 2019)
CoreDNS:v1.2.6
Nacos-coredns-plugin: 最新
1. 制作Docker镜像
为了兼容性考虑,我们直接采用nacos-coredns-plugin插件提供的编译脚本来构建Docker镜像。(该脚本会自动安装CoreDNS v1.2.6和相关依赖)
1.1 将Nacos-coredns-plugin下载到本地
git clone https://github.com/nacos-group/nacos-coredns-plugin.git
1.2 修改接口版本【重点】
将文件"nacos-coredns-plugin/nacos/util_and_comms.go"中的Version = "Nacos-go-client:v0.1.0"修改为Version = "Nacos-DNS:v1.0.1";
image.png插件会将该Version信息加入请求Nacos服务的request头信息中,服务端以此区分响应内容格式。
2. 编写Dockerfile
文件目录如下:
- docker
- nacos-coredns-plugin
- Dockerfile (*当前创建*)
- docker-compose.yml (*稍后创建*)
- conf (*稍后创建*)
- Corefile (*稍后创建*)
将工作目录切换到docker下,创建Dockerfile文件,加入如下内容:
FROM golang:1.12
ADD ./nacos-coredns-plugin /tmp/nacos-coredns-plugin
# 编译构建
RUN sh /tmp/nacos-coredns-plugin/bin/build.sh
# 导出默认端口,tcp和udp的都要导出
EXPOSE 53 53/udp
VOLUME ["/etc/coredns/Corefile"]
CMD ["/go/src/coredns/coredns", "-conf", "/etc/coredns/Corefile"]
然后,构建镜像
docker build -t zzw/nacos-coredns .
3. 安装Nacos
在Nacos的release页面下载需要的包,采用单机形式启动Nacos服务。
Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.sh -m standalone
Windows
启动命令:
cmd startup.cmd
或者双击startup.cmd运行文件。
启动后,在http://192.168.1.105:8848/nacos/index.html即可访问管理界面,默认账户信息:nacos#nacos。
image.png4. 注册Nacos服务
在此通过Java client向Nacos注册服务,引入maven依赖:
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.0.0-RC2</version>
</dependency>
编写注册代码:
NamingService naming = NamingFactory.createNamingService("127.0.0.1:8848");
Instance instance = new Instance();
instance.setClusterName("KanBan");
instance.setEnabled(true);
instance.setHealthy(true);
instance.setIp("192.168.0.105");
instance.setPort(8848);
instance.setServiceName("nacos.test.3");
instance.setWeight(1);
naming.registerInstance("nacos.test.3", instance);
// 保持当前线程,使服务健康检测能顺利通过
Thread.currentThread().sleep(999999999);
image.png
5. 通过docker-compose启动容器
编写docker-compose.yml文件:
version: '3'
services:
coredns:
image: zzw/nacos-coredns:latest
container_name: coredns
restart: always
network_mode: "bridge"
expose:
- '53'
- '53/udp'
ports:
- "53:53"
- "53:53/udp"
volumes:
- ./conf/Corefile:/etc/coredns/Corefile
./conf/Corefile即为CoreDNS配置文件,内容如下:
. {
nacos {
upstream /etc/resolv.conf
nacos_server 192.168.1.105
nacos_server_port 8848
}
log
errors
}
启动容器:
docker-compose up
#输出如下
Recreating coredns ...
Recreating coredns ... done
Attaching to coredns
coredns | log directory: /root/logs/nacos-go-client/
coredns | register nacos plugin
coredns | setup nacos plugin
coredns | init nacos plugin...
coredns | upstreams: [192.168.65.1:53]
coredns | init nacos client.
coredns | .:53
coredns | 2019-03-31T02:49:01.374Z [INFO] CoreDNS-1.2.6
coredns | 2019-03-31T02:49:01.374Z [INFO] linux/amd64, go1.12.1,
coredns | CoreDNS-1.2.6
coredns | linux/amd64, go1.12.1,
启动成功。
6. 测试
- 在宿主机测试是否可以正确解析“nacos.test.3”域名:
# 命令行输入
dig @127.0.0.1 nacos.test.3
# 解析结果
; <<>> DiG 9.10.6 <<>> @127.0.0.1 nacos.test.3
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 19338
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;nacos.test.3. IN A
;; ANSWER SECTION:
nacos.test.3. 1 IN A 192.168.0.105
;; ADDITIONAL SECTION:
_udp.nacos.test.3. 1 IN SRV 0 0 8848 .
;; Query time: 2 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sun Mar 31 10:49:15 CST 2019
;; MSG SIZE rcvd: 105
- 测试解析“www.baidu.com”
# 命令行输入
dig @127.0.0.1 www.baidu.com
# 解析结果
; <<>> DiG 9.10.6 <<>> @127.0.0.1 www.baidu.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40970
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.baidu.com. IN A
;; ANSWER SECTION:
www.baidu.com. 376 IN CNAME www.a.shifen.com.
www.a.shifen.com. 376 IN A 119.75.217.109
www.a.shifen.com. 376 IN A 119.75.217.26
;; Query time: 47 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sun Mar 31 10:49:59 CST 2019
;; MSG SIZE rcvd: 149
7. 常见问题
image.png2019-03-29 10:59:32.570 ERROR failed to unmarshal json: {"count":1,"doms":{"public":["nacos.test.3"]}}json: cannot unmarshal object into Go struct field AllDomNames.doms of type []string
参考本章:1.2小结即可解决
网友评论