CoreDNS + VScode 开发搭建(自定义插件)及生产部署
CoreDNS + VScode 开发搭建(自定义插件)及生产部署
1. 开发环境搭建
1.1 准备 Golang 环境
安装配置 golang 开发环境,这里以 Linux 为例,Windows 请自行配置就不啰嗦了。
Golang 稳定官方下载: https://golang.org/dl/#stable
cd /tmp
wget -O golang.tar.gz https://golang.org/dl/go1.17.linux-amd64.tar.gz
tar -xf golang.tar.gz
sudo mv go /usr/local/
# 配置环境变量
sudo cat <<-'EOF' >/etc/profile.d/profile-golang.sh
#/*
# * Copyright 2017 ~ 2025 the original author or authors. <wanglsir@gmail.com, 983708408@qq.com>
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# */
export GOROOT=/usr/local/go
export GOPATH=/opt/go-workspace
export PATH=$PATH:$GOROOT/bin
EOF
# 使生效
. /etc/profile.d/profile-golang.sh
# 创建工作目录
cd /opt/go-workspace/src
1.2 源码编译
cd /opt/go-workspace/src
git clone https://github.com/coredns/coredns.git
# -------------- [可选] ---------------
# 集成 dopaas-coredns 插件
# step1: 下载插件源码
git clone https://github.com/wl4g/dopaas-coredns.git
# step2: 建立软链到 coredns 的插件目录
ln -snf /opt/go-workspace/src/dopaas-coredns /opt/go-workspace/src/coredns/plugin/dopaas_coredns
# step3: 让 coredns 加载此插件,编辑 plugin.cfg 文件,在 forward:forward 之前插入 dopaas_coredns:dopaas_coredns,目的是让此插件执行顺序在 forward 插件之前。
# step4: 使用coredns自带工具生成指令(注:这一步非常重要,否则新插件就算加载了启动也报错找不到指令)
cd coredns; go run directives_generate.go
# ---------------------------------------
# vscode运行配置
sudo mkdir -p coredns/.vscode
sudo cat <<-'EOF' >coredns/.vscode/launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${fileDirname}/coredns.go",
"env": {
// https://goproxy.io/docs/getting-started.html
"GO111MODULE": "on",
"GOPROXY": "https://goproxy.io"
},
"args": []
}
]
}
EOF
# 在 vscode 导入项目(File => Open Folder)
# 编译打包
cd coredns
make
# 或手动自定义参数打包
#go build -v -a -ldflags '-s -w' -gcflags="all=-trimpath=$(pwd)" -asmflags="all=-trimpath=$(pwd)" -o $(pwd)/coredns
# 运行配置
sudo cp Corefile Corefile.bak # 备份原配置
sudo cat <<-'EOF' >Corefile
#/*
# * Copyright 2017 ~ 2025 the original author or authors. <wanglsir@gmail.com, 983708408@qq.com>
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# */
.:1053 {
# Load local /etc/hosts
hosts {
fallthrough
}
xcloud_dopaas_coredns {
address "127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:7379,127.0.0.1:7380,127.0.0.1:7381"
password "123456"
connect_timeout 5000
read_timeout 10000
write_timeout 5000
max_retries 10
pool_size 10
ttl 360
prefix _coredns:
local_cache_expire_ms 5000
}
forward . 114.114.114.114
#forward . tls://8.8.8.8 tls://8.8.4.4 {
# tls_servername dns.google
# force_tcp
# max_fails 3
# expire 10s
# health_check 5s
# policy sequential
# except www.baidu.com
#}
cache 120
reload 6s
loadbalance
log . "{local}:{port} - {>id} '{type} {class} {name} {proto} {size} {>do} {>bufsize}' {rcode} {>rflags} {rsize} {duration}"
errors
}
EOF
# 启动运行
# 方式1: 命令行运行(注:使用默认 make 编译和手动自定义编译出来的二进制文件名可能不同,不一定叫 coredns ,按实际自行修改)
coredns --conf Corefile
# 方式二: 使用 vscode 运行可进行调试 (入口在: coredns/coredns.go),就不啰嗦了。
2. 生产部署
2.1 服务配置
# 上传binary文件到以下目录
sudo mkdir -p /usr/lib/coredns-current/
# 生成 systemd 单元文件
sudo mkdir -p /mnt/disk1/log/coredns
sudo cat <<-'EOF' >/usr/lib/systemd/system/coredns.service
#/*
# * Copyright 2017 ~ 2025 the original author or authors. <wanglsir@gmail.com, 983708408@qq.com>
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# */
[Unit]
Description=CoreDNS Server Service
After=network.target
[Service]
Type=fork
User=root
Group=root
Restart=always
RestartSec=5s
ExecStart=bash -c "/usr/lib/coredns-current/coredns -conf /etc/coredns/Corefile"
ExecReload=/bin/kill -s HUP $MAINPID
StandardOutput=/mnt/disk1/log/coredns/coredns.out
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl restart coredns
sudo systemctl status coredns
# 如果发现启动失败,是因为 53 端口被占用,可能是与系统自带dns服务冲突,先停掉.
sudo systemctl stop systemd-resolved
# 查看日志
sudo tail -f /var/log/message
sudo tail -f /mnt/disk1/log/coredns/coredns.out
3. 验证
# 安装 dig 命令(dns 客户端工具)
sudo yum -y install bind-utils # Red hat/CentOS
sudo apt-get -y install dnsutils # Debian/Ubuntu
# 创建测试数据
redis-cli -c -a '123456'
127.0.0.1:6379> hset _coredns:wl4g-example.com. '@' "{\"a\":[{\"ttl\":600, \"ip\":\"10.0.0.100\"}]}"
# 使用 dig 命令是否能解析此域名
dig @127.0.0.1 wl4g-example.com
; <<>> DiG 9.16.1-Ubuntu <<>> @127.0.0.1 wl4g-example.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2404
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 25c896754ab7408f (echoed)
;; QUESTION SECTION:
;wl4g-example.com. IN A
;; ANSWER SECTION:
wl4g-example.com. 307 IN A 10.0.0.100
;; Query time: 36 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: 一 8月 30 18:32:42 CST 2021
;; MSG SIZE rcvd: 99
4. FAQ
-
若集成了
dopaas-coredns
插件,需 参见: redis cluster 快速部署 -
使用
CoreDNS
搭建多数据中心基于CNAME
智能解析服务,需依赖 ip+地理位置解析,参考: https://iplocation.com