zoukankan      html  css  js  c++  java
  • godns 简单dnsmasq 的dns 替换方案

    godns 可以用来替换dnsmasq,同时包含了一些新的特性

    • 数据可以在hosts 文件以及redis
    • 当hosts 文件变动的时候,自动reload
    • 包含了一个相对比较全的ui(支持添加以及删除操作,dnsmasq可以基于jpillora/webproc实现一个ui的包装,但是一般)
      以下是一个测试使用

    环境准备

    • docker-compose 文件
      包含了redis,godns以及joke,说明对于godns 以及joke 的构建都是基于源码的(使用了go mod)
     
    version: "3"
    services: 
      redis:
         image: redis
         ports: 
         - "6379:6379"
      joke: 
         image: dalongrong/godns:joke
         build: 
          context: ./
          dockerfile: ./Dockerfile-joke
         ports: 
         - "1223:1223"
      godns: 
         image: dalongrong/godns
         build: 
          context: ./
          dockerfile: ./Dockerfile-godns
         ports: 
         - "53:53/udp"
         - "53:53/tcp"
    • godns dockerfile
      基于golang 的supervisord进行管理
     
    FROM golang:1.13-alpine AS build-env
    WORKDIR /go/src/app
    RUN  /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
    ENV  GO111MODULE=on
    ENV  GOPROXY=https://goproxy.cn
    COPY code/godns/ .
    RUN apk update && apk add git 
        && go build
    FROM alpine:latest
    WORKDIR /app
    RUN  /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
    RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
    COPY --from=ochinchina/supervisord:latest /usr/local/bin/supervisord /usr/local/bin/supervisord
    COPY --from=build-env /go/src/app/godns /app/godns
    COPY supervisor-godns.conf /etc/supervisord.conf
    COPY godns.conf /etc/godns.conf
    EXPOSE 53/udp 53 9001
    CMD ["/usr/local/bin/supervisord"]
    • joke dockerfile
    FROM golang:1.13-alpine AS build-env
    WORKDIR /go/src/app
    RUN  /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
    ENV  GO111MODULE=on
    ENV  GOPROXY=https://goproxy.cn
    COPY code/joke/ .
    RUN apk update && apk add git 
        && go build
    FROM alpine:latest
    WORKDIR /app
    RUN  /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
    RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
    COPY --from=ochinchina/supervisord:latest /usr/local/bin/supervisord /usr/local/bin/supervisord
    COPY --from=build-env /go/src/app/joke /app/joke
    COPY --from=build-env /go/src/app/static /app/static
    COPY --from=build-env /go/src/app/views /app/views
    COPY supervisor-joke.conf /etc/supervisord.conf
    COPY joke.conf /etc/joke.conf
    EXPOSE 1223 9001
    CMD ["/usr/local/bin/supervisord"]
    • godns 配置
      修改了一些添加了redis 的配置,默认是没有直接支持redis的
     
    #Toml config file
    Title = "GODNS"
    Version = "0.1.2"
    Author = "kenshin"
    Debug = false
    [server]
    host = "0.0.0.0"
    port = 53
    [resolv]
    # Domain-specific nameservers configuration, formatting keep compatible with Dnsmasq
    # Semicolon separate multiple files.
    #server-list-file = "./etc/apple.china.conf;./etc/google.china.conf"
    resolv-file = "/etc/resolv.conf"
    timeout = 5  # 5 seconds
    # The concurrency interval request upstream recursive server
    # Match the PR15, https://github.com/kenshinx/godns/pull/15
    interval = 200 # 200 milliseconds
    setedns0 = false #Support for larger UDP DNS responses
    [redis]
    enable = true
    host = "redis"
    port = 6379
    db = 0
    password =""
    [memcache]
    servers = ["127.0.0.1:11211"]
    [log]
    stdout = true
    level = "INFO"  #DEBUG | INFO |NOTICE | WARN | ERROR  
    [cache]
    # backend option [memory|memcache|redis]  
    backend = "redis"  
    expire = 600  # 10 minutes
    maxcount = 0 #If set zero. The Sum of cache itmes will be unlimit.
    [hosts]
    #If set false, will not query hosts file and redis hosts record
    enable = true
    host-file = "/etc/hosts"
    redis-enable = true
    redis-key = "godns:hosts"
    ttl = 600
    refresh-interval = 5 # 5 seconds
    • joke 配置
    #[beego]
    appname = Joke
    httpaddr = "0.0.0.0"
    httpport = 1223
    runmode = "dev"
    autorender = true
    autorecover = true
    viewspath = "views"
    #[auth]
    #username:password.
    #basic_auth = "joke:hello"
    #[redis]
    redisaddr = "redis:6379"
    redisdb = 0
    redispassword = ""
    bindkey = "godns:hosts"
    #[log]
    stdout = true
    logfile = "logs/joke.log"
    logrorate = true
    • supervidord 配置
      基本都一样可以参考
     
    [program:godns]
    command =/app/godns -c /etc/godns.conf
    [inet_http_server]
    port = :9001
    • 启动
    docker-compose up -d
    • 添加redis key
      这个数据程序的一个设计问题,没有自动添加hash key
     
    hset godns:hosts demo.rong.com 192.168.0.109

    测试试用

    • ui 效果

    • dig 测试
    dig @127.0.0.1 demo.rong.com

    效果

    说明

    godns 还是一个不错的dns 选择(测试),如果我们集成了coredns 会更加强大(rewrite,log,trace,metrics。。。),同时相关docker
    image 已经push 到docker hub 了,参考dalongrong/godns dalongrong/godns:joke

    参考资料

    https://github.com/rongfengliang/godns-joke-learning
    https://github.com/kenshinx/godns
    https://github.com/kenshinx/joke
    https://www.cnblogs.com/rongfengliang/p/11498598.html
    https://github.com/coredns/coredns

  • 相关阅读:
    [数据结构]图的DFS和BFS的两种实现方式
    [算法]两个栈实现一个队列
    [数据结构]手动实现队列
    [数据结构]手动实现栈
    [数据结构]手动实现单链表
    Hive分组取Top K数据
    HBase解决海量图片存储方案
    非结构化数据存储方案
    头条面试题之实现两个线程轮流打印字符串
    [算法]最大连续子数组和,最长重复子串,最长无重复字符子串
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/13200197.html
Copyright © 2011-2022 走看看