使用 CoreDNS和etcd 自建DNS 服务

文档说明:只记录关键地方;
试验环境: linux debian 11
基础软件: etcd+CoreDNS
目标:自建DNS

etcd(存储解析记录) + CoreDNS

version: "3"
services:
    coredns:
        image: coredns/coredns:1.9.3
        restart: always
        container_name: coredns
        ports:
            - "127.0.0.253:53:53/tcp"  # 127.0.0.253 默认可以不写,这么写的意义是为了解决和127.0.0.1端口冲突
            - "127.0.0.253:53:53/udp"  # 127.0.0.253 默认可以不写,这么写的意义是为了解决和127.0.0.1端口冲突
        volumes:
            - ./Corefile:/Corefile
        external_links:
            - etcd
        command:
            - "-conf"
            - "/Corefile"
    etcd:
        image: quay.io/coreos/etcd:v3.5.0
        ports:
            - "2379:2379"  #for client requests
        restart: always
        container_name: etcd
        environment:
            - "ETCD_MAX_WALS=3"
        volumes:
            - ./etcd-data:/etcd-data
        command:
            - /bin/sh
            - -c
            - |
                /usr/local/bin/etcd  \
                --name s1 \
                --data-dir /etcd-data \
                --listen-client-urls http://0.0.0.0:2379 \
                --advertise-client-urls http://0.0.0.0:2379



Corefile 配置文件

.:53 {

    cache 600
    debug
    errors
    log
    reload 5s
    health {
        lameduck 5s
    }
    # 递归查询默认就不开启了
    # forward . 223.5.5.5
    #
    hosts {
      # 自定义 服务解析
      # 因为解析的域名少我们这里直接用hosts插件即可完成需求
      # 如果有大量自定义域名解析那么建议用file插件使用 符合RFC 1035规范的DNS解析配置文件
      # 例子
      192.168.3.20 bbs
      192.168.3.21 china
      192.168.3.22 hai_service

      # ttl
      ttl 60
      # 重载hosts配置
      reload 5s
      # 继续执行
      fallthrough
    }
    etcd {
        # etcd 键值前缀(自定义的)
        path /ddns
        # 连接 etcd
        # endpoint http://192.168.3.26:2379

        # 容器内使用服务名称,不写IP。为什么这么写?请看这里: https://www.cnblogs.com/jingjingxyk/p/16818423.html

        endpoint http://etcd:2379
    }

}

启动、关闭、查看日志

# 启动
docker-compose -f docker-compose.yaml up -d 
# 关闭
docker-compose -f docker-compose.yaml down --remove-orphans
# 查看日志
docker-compose -f docker-compose.yaml logs -f 

etcdctl 设置域名解析

#!/bin/bash

set -exu
__CURRENT__=$(pwd)
__DIR__=$(
  cd "$(dirname "$0")"
  pwd
)
cd ${__DIR__}


export ETCDCTL_API=3
export ETCDCTL_COMMAND_TIMEOUT=5s

endpoints=127.0.0.1:2379


args='endpoint status'

args='member list'

# coredns etcd 参考文档 https://coredns.io/plugins/etcd/
# etcdctl 参考文档https://etcd.io/docs/v3.5/dev-guide/interacting_v3/



# SRV record 删除
args='del /ddns/com/jingjingxyk/ddns/service/srv'
# SRV record
args='put /ddns/com/jingjingxyk/ddns/service/srv {"host":"192.168.100.20","ttl":60,"priority":10,"port":65535}'


# 域名 demo 解析为 fd00::1 ttl 30秒 (ipv6)
args='put /ddns/demo {"host":"fd00::1","ttl":60}'
# 域名 demo 解析为 192.168.3.20 ttl 30 秒
args='put /ddns/demo {"host":"192.168.3.20","ttl":30}'

# 下载 etcdctl
# curl -LO https://github.com/etcd-io/etcd/releases/download/v3.5.5/etcd-v3.5.5-linux-amd64.tar.gz

${__DIR__}/etcd-v3.5.5-linux-amd64/etcdctl --endpoints $endpoints $args

# 测试解析结果
dig bbs @127.0.0.253 -p 53 +short
dig hai_service @127.0.0.253 -p 53 +short
dig demo @127.0.0.253 -p 53 +short

dig srv.service.ddns.jingjingxyk.com  @127.0.0.253 -p 53  srv +short




参考文档

  1. CoreDNS介绍
  2. CoreDNS ETCD
  3. etcd security
  4. etcd release
  5. etcdctl API
  6. etcd Libraries and tools
  7. etcd configuration
  8. DoH (DNS over HTTPS)
  9. 基于 TLS 的 DNS(DoT)
  10. DNS over HTTPS Publicly available servers
  11. 告别DNS劫持,一文读懂DoH
  12. 容器5种网络模式 与 K8S pod网络关系
  13. 自建拉取registry.k8s.io、k8s.gcr.io、gcr.io、quay.io、ghcr.io 容器镜像的服务
  14. 多播DNS ( mDNS )协议将主机名解析为不包含本地名称服务器的小型网络中的IP地址
posted @ 2022-10-23 14:47  jingjingxyk  阅读(432)  评论(0编辑  收藏  举报