使用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM