Skip to content

Instantly share code, notes, and snippets.

@v1k0d3n
Created September 8, 2019 15:59
Show Gist options
  • Save v1k0d3n/f63c43dd99ffb9056cdb900c105c5714 to your computer and use it in GitHub Desktop.
Save v1k0d3n/f63c43dd99ffb9056cdb900c105c5714 to your computer and use it in GitHub Desktop.
Local CoreDNS setup. For production we used CoreDNS extensions for proxy/forwarding to Route53, but this was used to test a local CoreDNS instance + etcd SRV auto-discovery for local platform development.

CoreDNS Proposal

Proof of Concept

#!/bin/bash
## Prepare any variables used for this script:
export network_endpoints_dns_coredir="$(pwd)/scripts/deployments/coredns"
export network_endpoints_dns_fqdn="jinkit.com"
export network_endpoints_dns_forewarder="8.8.8.8"
export network_endpoints_dns_kubernetes_api="kubernetes"
export node_bootstrap_addr=("192.168.3.21")
export network_endpoints_dns_bootstrap_name="kubetcd01"
export node_master_addr0=("192.168.3.21")
export node_master_addr1=("192.168.3.22")
export node_master_addr2=("192.168.3.23")
export node_master_addr3=("192.168.3.24")
export node_master_addr4=("192.168.3.25")
export node_master_dns_name0=("fs-etcd01")
export node_master_dns_name1=("fs-etcd02")
export node_master_dns_name2=("fs-etcd03")
export node_master_dns_name3=("fs-etcd04")
export node_master_dns_name4=("fs-etcd05")

# Prepare directories for Coredns Corefile and custom domain files:
mkdir -p ${network_endpoints_dns_coredir}

# Write out CoreDNS Domain file:
rm -rf ${network_endpoints_dns_coredir}/Corefile
cat << EOF | sudo tee -a ${network_endpoints_dns_coredir}/Corefile
${network_endpoints_dns_fqdn}:53 {
    log stdout
    file /data/${network_endpoints_dns_fqdn}
}
.:53 {
    proxy . ${network_endpoints_dns_forewarder}:53
    log stdout
}
EOF

# Write out CoreDNS Domain file:
rm -rf ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
cat << EOF | tee -a ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
\$TTL    1M
\$ORIGIN ${network_endpoints_dns_fqdn}.

${network_endpoints_dns_fqdn}.		    IN	SOA	sns.dns.icann.org. noc.dns.icann.org. 2015082541 7200 3600 1209600 3600
${network_endpoints_dns_fqdn}.		    IN	NS	b.iana-servers.net.
${network_endpoints_dns_fqdn}.		    IN	NS	a.iana-servers.net.
${network_endpoints_dns_fqdn}.		    IN	A	127.0.0.1

; Flagship: Test A Record
test.${network_endpoints_dns_fqdn}.	    IN	A	${node_bootstrap_addr}

; Flagship: Test TXT Record
text.${network_endpoints_dns_fqdn}.	    IN	TXT	"This is a test text record"

; Flagship: Test CNAME Record
cname.${network_endpoints_dns_fqdn}.	IN	CNAME	www.jinkit.net.

; Flagship: Test SRV Record
service.${network_endpoints_dns_fqdn}.	IN	SRV	8080 10 10 ${network_endpoints_dns_fqdn}.

; Flagship: Kubernetes ETCD Server SRV Records
_etcd-server._tcp.${network_endpoints_dns_fqdn}.   300     IN      SRV 0 0 2380    ${network_endpoints_dns_bootstrap_name}.${network_endpoints_dns_fqdn}.

; Flagship: Kubernetes ETCD Client SRV Records
_etcd-client._tcp.${network_endpoints_dns_fqdn}.   300     IN      SRV 0 0 2379    ${network_endpoints_dns_bootstrap_name}.${network_endpoints_dns_fqdn}.

; Flagship: ETCD Member A Records
${network_endpoints_dns_bootstrap_name}             IN      A       ${node_bootstrap_addr}

; Flagship: Kubernetes Member A Records
${network_endpoints_dns_kubernetes_api}             IN      A       ${node_bootstrap_addr}

; Flagship: Kubernetes/ETCD Member A Records
*.apps                  IN      CNAME   master

; Flagship: Custom User Provided Entries
openshift	        	IN	    A	    192.168.1.40
master	        		IN	    A   	192.168.1.40
node1	        		IN	    A   	192.168.1.41
node2		        	IN	    A   	192.168.1.42
quay                    IN      A       172.29.248.34
kubenode01              IN      A       192.168.3.21
kubenode02              IN      A       192.168.3.22
kubenode03              IN      A       192.168.3.23
kubenode04              IN      A       192.168.3.24
kubenode05              IN      A       192.168.3.25
EOF


# Run docker command:
docker stop flagship_coredns && docker rm flagship_coredns
docker run -d \
  --restart=always \
  --name flagship_coredns \
  --privileged \
  -v ${network_endpoints_dns_coredir}:/data:ro \
  -p "53:53/udp" -p "53:53/tcp" -p "9153:9153/tcp" \
  --cap-drop=all --cap-add=net_bind_service \
  coredns/coredns -conf /data/Corefile

## Testing:
dig ${network_endpoints_dns_kubernetes_api}.${network_endpoints_dns_fqdn} @127.0.0.1
dig ${node_master_addr3} @127.0.0.1
dig srv _etcd-server._tcp.${network_endpoints_dns_fqdn}. @127.0.0.1

## Changes:
### A Records:
sed -i 's/.*Flagship: Kubernetes Member A Records.*/&\n'${node_master_dns_name0}'             IN      A       '${node_master_addr0}'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes Member A Records.*/&\n'${node_master_dns_name1}'             IN      A       '${node_master_addr1}'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes Member A Records.*/&\n'${node_master_dns_name2}'             IN      A       '${node_master_addr2}'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes Member A Records.*/&\n'${node_master_dns_name3}'             IN      A       '${node_master_addr3}'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes Member A Records.*/&\n'${node_master_dns_name4}'             IN      A       '${node_master_addr4}'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}

### SRV Server Records:
sed -i 's/.*Flagship: Kubernetes ETCD Server SRV Records.*/&\n_etcd-server._tcp.'${network_endpoints_dns_fqdn}'.   300     IN      SRV 0 0 2380    '${node_master_dns_name0}'.'${network_endpoints_dns_fqdn}.'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes ETCD Server SRV Records.*/&\n_etcd-server._tcp.'${network_endpoints_dns_fqdn}'.   300     IN      SRV 0 0 2380    '${node_master_dns_name1}'.'${network_endpoints_dns_fqdn}.'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes ETCD Server SRV Records.*/&\n_etcd-server._tcp.'${network_endpoints_dns_fqdn}'.   300     IN      SRV 0 0 2380    '${node_master_dns_name2}'.'${network_endpoints_dns_fqdn}.'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes ETCD Server SRV Records.*/&\n_etcd-server._tcp.'${network_endpoints_dns_fqdn}'.   300     IN      SRV 0 0 2380    '${node_master_dns_name3}'.'${network_endpoints_dns_fqdn}.'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes ETCD Server SRV Records.*/&\n_etcd-server._tcp.'${network_endpoints_dns_fqdn}'.   300     IN      SRV 0 0 2380    '${node_master_dns_name4}'.'${network_endpoints_dns_fqdn}.'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}

### SRV Client Records:
sed -i 's/.*Flagship: Kubernetes ETCD Client SRV Records.*/&\n_etcd-server._tcp.'${network_endpoints_dns_fqdn}'.   300     IN      SRV 0 0 2379    '${node_master_dns_name0}'.'${network_endpoints_dns_fqdn}.'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes ETCD Client SRV Records.*/&\n_etcd-server._tcp.'${network_endpoints_dns_fqdn}'.   300     IN      SRV 0 0 2379    '${node_master_dns_name1}'.'${network_endpoints_dns_fqdn}.'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes ETCD Client SRV Records.*/&\n_etcd-server._tcp.'${network_endpoints_dns_fqdn}'.   300     IN      SRV 0 0 2379    '${node_master_dns_name2}'.'${network_endpoints_dns_fqdn}.'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes ETCD Client SRV Records.*/&\n_etcd-server._tcp.'${network_endpoints_dns_fqdn}'.   300     IN      SRV 0 0 2379    '${node_master_dns_name3}'.'${network_endpoints_dns_fqdn}.'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}
sed -i 's/.*Flagship: Kubernetes ETCD Client SRV Records.*/&\n_etcd-server._tcp.'${network_endpoints_dns_fqdn}'.   300     IN      SRV 0 0 2379    '${node_master_dns_name4}'.'${network_endpoints_dns_fqdn}.'/' ${network_endpoints_dns_coredir}/${network_endpoints_dns_fqdn}

## Now restart the container:
docker restart flagship_coredns

## Testing:
dig ${network_endpoints_dns_kubernetes_api}.${network_endpoints_dns_fqdn} @127.0.0.1
dig ${node_master_addr3} @127.0.0.1
dig srv _etcd-server._tcp.${network_endpoints_dns_fqdn}. @127.0.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment