zoukankan      html  css  js  c++  java
  • 【手打】coredns单台使用

    目录:

    • coredns介绍
    • coredns安装
    • corendns配置

    coredns介绍

    CoreDNS 其实就是一个 DNS 服务,而 DNS 作为一种常见的服务发现手段,所以很多开源项目以及工程师都会使用 CoreDNS 为集群提供服务发现的功能,Kubernetes 就在集群中使用 CoreDNS 解决服务发现的问题。

    作为一个加入 CNCF(Cloud Native Computing Foundation) 的服务 CoreDNS 的实现可以说的非常的简单。

    架构

    整个 CoreDNS 服务都建立在一个使用 Go 编写的 HTTP/2 Web 服务器 Caddy · GitHub 上,CoreDNS 整个项目可以作为一个 Caddy 的教科书用法。

    CoreDNS 的大多数功能都是由插件来实现的,插件和服务本身都使用了 Caddy 提供的一些功能,所以项目本身也不是特别的复杂。

    插件

    作为基于 Caddy 的 Web 服务器,CoreDNS 实现了一个插件链的架构,将很多 DNS 相关的逻辑都抽象成了一层一层的插件,包括 Kubernetes 等功能,每一个插件都是一个遵循如下协议的结构体:

    type (
    	Plugin func(Handler) Handler
    
    	Handler interface {
    		ServeDNS(context.Context, dns.ResponseWriter, *dns.Msg) (int, error)
    		Name() string
    	}
    )

    所以只需要为插件实现 ServeDNS 以及 Name 这两个接口并且写一些用于配置的代码就可以将插件集成到 CoreDNS 中。

    Corefile

    另一个 CoreDNS 的特点就是它能够通过简单易懂的 DSL 定义 DNS 服务,在 Corefile 中就可以组合多个插件对外提供服务

    coredns.io:5300 {
        file db.coredns.io
    }
    
    example.io:53 {
        log
        errors
        file db.example.io
    }
    
    example.net:53 {
        file db.example.net
    }
    
    .:53 {
        kubernetes
        proxy . 8.8.8.8
        log
        errors
        cache
    }
    

    对于以上的配置文件,CoreDNS 会根据每一个代码块前面的区和端点对外暴露两个端点提供服务:

     该配置文件对外暴露了两个 DNS 服务,其中一个监听在 5300 端口,另一个在 53 端口,请求这两个服务时会根据不同的域名选择不同区中的插件进行处理。

    原理

    CoreDNS 可以通过四种方式对外直接提供 DNS 服务,分别是 UDP、gRPC、HTTPS 和 TLS:

     转自:https://draveness.me/dns-coredns/

    coredns安装

    wget https://github.com/coredns/coredns/releases/download/v1.1.4/coredns_1.1.4_linux_amd64.tgz

    tar -zxvf coredns_1.1.4_linux_amd64.tgz

    mv coredns /usr/local/bin

    corendns配置

    mkdir /etc/coredns

    cd /etc/coredns

    vim /etc/coredns/corefile

    coredns:53 {
        errors         # show errors
        log            # enable query logs
        loadbalance round_robin
        cache 1
    }
    
    example.org:53 {
        file db.example.org
    }
    d03.com:53 {
        file db.d03.com
    }
    yiy.net:53 {
        file db.yiy.net
    }
    
    yostata.xyz:53 {
        file db.yostata.xyz
    }
    
    .:53 {
      hosts {
        # hosts 插件可以如使用 /etc/hosts 文件一样配置服务名称
        # 如果不写大括号及其中的内容,直接hosts指令,默认使用 /etc/hosts 中的服务名称
        # inline 形式
        172.16.200.245  www.y03.com 
        fallthrough
      }
      # 对于没有配置的域名,转发到114
      forward . 8.8.8.8
      cache 120
      reload 6s
      log
      errors
    }
    

      

    # 展示一个配置db文件

    vim db.example.org

    $ORIGIN example.org.
    @	3600 IN	SOA sns.dns.icann.org. noc.dns.icann.org. (
    				2017042745 ; serial
    				7200       ; refresh (2 hours)
    				3600       ; retry (1 hour)
    				1209600    ; expire (2 weeks)
    				3600       ; minimum (1 hour)
    				)
    
        3600 IN NS a.iana-servers.net.
    	3600 IN NS b.iana-servers.net.
    
    www     IN A     127.0.0.1
            IN AAAA  ::1
    
    tt      IN A     172.0.0.1
            IN AAAA  ::1
            IN TXT   HelloExampleTest
    

      coredns -conf corefile &

  • 相关阅读:
    Swift-基础语法之变量&常量&元组
    Swift
    安装MySQL
    LNMP 简介
    LNMP
    Django 定义数据模型
    Django 添加应用
    Django 创建第一个项目
    Django 安装
    Django 简介
  • 原文地址:https://www.cnblogs.com/wangshuyang/p/14618556.html
Copyright © 2011-2022 走看看