diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40dbd6f --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +dns.env +.terraform +terraform.tfstate* +terraform.tfvars + diff --git a/README.md b/README.md new file mode 100644 index 0000000..d4e339a --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ +# bind-dns + +Use packer and terraform to deploy a pair of Bind DNS servers in an AWS VPC to resolve DNS name for K8s API server (or whatever you wish). + +## Usage + +1. Clone this repo. +``` + $ git clone git@github.com:lander2k2/bind-dns.git + $ cd kube-cluster +``` + +2. Export your AWS keys and preferred region. +``` + $ export AWS_ACCESS_KEY_ID="accesskey" + $ export AWS_SECRET_ACCESS_KEY="secretkey" + $ export AWS_DEFAULT_REGION="us-east-2" +``` + +3. Build a CentOS-based image for your DNS servers. Not the AMI ID for adding to the tfvars. +``` + $ cd images + $ packer build ns_template_centos.json +``` + +4. Edit terraform variables. Add the appropriate values for your environment. +``` + $ cd ../ + $ cp terraform.tfvars.example terraform.tfvars + $ vi terraform.tfvars +``` + +5. Deploy the name servers. The 2 deployed servers will be identical. Arbitrarily assign one as the master and the other as the slave. +``` + $ terraform init infra + $ terraform plan infra + $ terraform apply infra +``` + +6. Set the variables to configure your name servers. +``` + $ cp dns.env.example dns.env + $ vi dns.env +``` + +7. Copy the env vars to your name servers. +``` + $ scp dns.env centos@[master ip] + $ scp dns.env centos@[slave ip] +``` + +8. Connect to the master name server and configure. +``` + $ ssh centos@[master ip] + $ sudo su + # source dns.env + # configure_master.sh +``` + +9. Repeat for the slave name server. +``` + $ ssh centos@[slave ip] + $ sudo su + # source dns.env + # configure_slave.sh +``` + +10. Add the two name server IPs to your jump box resolv.conf + diff --git a/dns.env.example b/dns.env.example new file mode 100644 index 0000000..01cbc4a --- /dev/null +++ b/dns.env.example @@ -0,0 +1,19 @@ +# e.g. example.com +export DOMAIN_NAME="" + +# private IP for master DNS server +export MASTER_IP="" + +# private IP for slave DNS server +export SLAVE_IP="" + +# subdomain you will use for k8s API +export SUBDOMAIN="" + +# DNS name for K8s API load balancer +export API_ELB="" + +# IP of jump box from which DNS queries will be sent +# will also accept a CIDR if preferred +export JUMP_IP="" + diff --git a/images/config_master.sh b/images/config_master.sh new file mode 100755 index 0000000..01ab4d2 --- /dev/null +++ b/images/config_master.sh @@ -0,0 +1,129 @@ +#!/bin/bash + +set -e + +: "${DOMAIN_NAME:?Env variable DOMAIN_NAME must be set and not empty}" +: "${SLAVE_IP:?Env variable SLAVE_IP must be set and not empty}" +: "${SUBDOMAIN:?Env variable SUBDOMAIN must be set and not empty}" +: "${API_ELB:?Env variable API_ELB must be set and not empty}" +: "${JUMP_IP:?Env variable JUMP_IP must be set and not empty}" +: "${FORWARDER:?Env variable FORWARDER must be set and not empty}" + +PRIVATE_IP=$(ip addr show eth0 | grep -Po 'inet \K[\d.]+') + +grep -qF "${PRIVATE_IP} ns1.${DOMAIN_NAME} ns1" /etc/hosts || echo "${PRIVATE_IP} ns1.${DOMAIN_NAME} ns1" >> /etc/hosts + +echo "ns1" > /etc/hostname +hostname -F /etc/hostname + +cat > /etc/named.conf < /etc/named/named.conf.local < /etc/named/zones/db.${DOMAIN_NAME} <> /etc/hosts + +echo "ns2" > /etc/hostname +hostname -F /etc/hostname + +cat > /etc/named.conf < /etc/named/named.conf.local <