zoukankan      html  css  js  c++  java
  • 使用coredns 的template plugin实现一个xip 服务

    xip dns 服务在我们的实际开发中,还是挺有用的,我们可以基于dns模式方便的进行软件开发,同时
    结合nginx 的虚拟主机,可以实现灵活的软件部署,以下是基于coredns 的template plugin 实现一个
    简单,但是高效的xip 服务

    xip 格式说明

    • 参考格式
     
       10.0.0.1.xip.name  resolves to  10.0.0.1
        www.10.0.0.2.xip.name  resolves to  10.0.0.2
        foo.10.0.0.3.xip.name  resolves to  10.0.0.3
    bar.baz.10.0.0.4.xip.name  resolves to  10.0.0.4
    • 说明
      我们主要实现以下格式的
     
    foo.10.0.0.3.xip.name  resolves to  10.0.0.3

    环境准备

    • docker-compose 文件
    version: "3"
    services: 
      lb: 
        image: openresty/openresty:alpine
        networks:
         service1_net:
          ipv4_address: 192.168.1.2
        volumes:
        - "./nginx-lb.conf:/usr/local/openresty/nginx/conf/nginx.conf"
        - "./dns.log:/var/log/nginx/dns.log"
        ports:
        - "53:53/udp"
        - "80:80"
        - "53:53/tcp"
      coredns:
        image: coredns/coredns:1.7.0
        networks:
         service1_net:
          ipv4_address: 192.168.1.4
        volumes: 
        - "./Corefile:/opt/Corefile"
        command: -conf /opt/Corefile
    networks:
      service1_net:
        ipam:
          driver: default
          config:
            - subnet: 192.168.1.0/16
    • 说明
      基于nginx做dns 的proxy,基于coredns提供xip服务
    • nginx 配置
     
    worker_processes  1;
    user root;  
    events {
        worker_connections  1024;
    }
    stream {
         upstream dns_servers {
            server 192.168.1.4:53;
        }
        server {
            listen 53  udp;
            listen 53; #tcp
            proxy_pass dns_servers;
            error_log  /var/log/nginx/dns.log info;
        }
    }
    http {
        include       mime.types;
        default_type text/html;
        gzip  on;
        resolver 127.0.0.1 ipv6=off valid=30s;          
        real_ip_header     X-Forwarded-For;
        real_ip_recursive on;
        server {
            listen       80;
            charset utf-8;
            default_type text/html;
            location / {
                index index.html;
                default_type text/html;
                set $kuaidi100 "www.kuaidi100.com";
                proxy_pass https://$kuaidi100;
                proxy_redirect off;
                proxy_set_header Bloom-Request-Shard 1;
                proxy_read_timeout 10000;
                proxy_send_timeout 10000;
                proxy_buffer_size 1M; 
                proxy_buffers 8 1M; 
                proxy_busy_buffers_size 1M; 
                proxy_temp_file_write_size 1M;
                proxy_set_header   Host $kuaidi100;
                proxy_set_header   X-Real-IP $remote_addr;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Host $server_name;
                proxy_set_header   X-Forwarded-Proto $scheme;
            }
        }
    }
    • coredns 配置
      Corefile 文件
      我们基于正则以及template plugin生成xip服务,实际上很简单,我们利用了coredns 提供的变量
      以及正则分组别名,快速的实现了一个xip服务
     
    dalongrong.com:53 {
        log
        template IN A dalongrong.com {
          match ^(?P<a>w+).(?P<b>d+.d+.d+.d+).dalongrong.com.$
          answer "{{ .Name }} 60 IN A {{ .Group.b }}"
          authority  "dalongrong.com. 60 IN NS ns0.dalongrong.com."
          authority  "dalongrong.com. 60 IN NS ns1.dalongrong.com."
          additional "ns0.dalongrong.com. 60 IN A 203.0.113.8"
          additional "ns1.dalongrong.com. 60 IN A 198.51.100.8"
          fallthrough
        }
    }
    .:53 {
       log
       health
       cache 30
       loadbalance round_robin
       forward  . 8.8.8.8 8.8.4.4 114.114.114.114
    }
    • 启动
    docker-compose up -d

    测试

    • dig 命令
      我们希望..dalongrong.com 解析的dns为 就可以实现此功能了
      命令
     
    dig @127.0.0.1 openresty.192.168.0.190.dalongrong.com

    效果

    说明

    以上支持实现了部分xip服务,但是已经够我们实际使用了,具体其他模式的主要是正则的编写,有空了可以写下

    参考支持xip 的一个coredns 配置

    dalongrong.com:53 {
        log
        template IN A dalongrong.com {
          match (D*?).?(?P<b>d+.d+.d+.d+).dalongrong.com.$
          answer "{{ .Name }} 60 IN A {{ .Group.b }}"
          authority  "dalongrong.com. 60 IN NS ns0.dalongrong.com."
          authority  "dalongrong.com. 60 IN NS ns1.dalongrong.com."
          additional "ns0.dalongrong.com. 60 IN A 203.0.113.8"
          additional "ns1.dalongrong.com. 60 IN A 198.51.100.8"
          fallthrough
        }
    }
    .:53 {
       log
       health
       cache 30
       loadbalance round_robin
       forward  . 8.8.8.8 8.8.4.4 114.114.114.114
    }

    参考资料

    https://coredns.io/plugins/template/
    https://github.com/rongfengliang/nginx-coredns-consul-learning/tree/xip

  • 相关阅读:
    Go语言十六进制转十进制
    Go语言中底层数组和切片的关系以及数组扩容规则
    Golang超时机制--2秒内某个函数没被调用就认为超时
    约瑟夫环问题(猴子选大王)
    冒泡排序优化
    斐波那契数列
    Linux下使用acme.sh (Let's Encrypt) 配置https 免费证书
    git 本地分支指定对应的远程分支
    Git分支开发 -- 利用git pull命令将远程指定仓库的分支拉取到本地
    phpStorm 之 本地开发,Linux上跑项目(连接远端服务器开发)
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/13192561.html
Copyright © 2011-2022 走看看