oLeiShen 2020-06-25
xip dns 服务在我们的实际开发中,还是挺有用的,我们可以基于dns模式方便的进行软件开发,同时
结合nginx 的虚拟主机,可以实现灵活的软件部署,以下是基于coredns 的template plugin 实现一个
简单,但是高效的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
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
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; } } }
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 @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