Skip to content

Commit

Permalink
Add Consul DNS forwarding with dnsmasq
Browse files Browse the repository at this point in the history
  • Loading branch information
kencx committed Nov 1, 2023
1 parent d5e24db commit 7528872
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions ansible/inventory/group_vars/prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ consul_data_dir: "/opt/consul"
consul_tls_dir: "{{ consul_data_dir }}/tls"
consul_server_ip: "{{ server_ip }}"
consul_ttl: "168h"
consul_upstream_dns_address: ["192.168.86.49", "1.1.1.1"]
setup_consul_watches: false

# nomad
Expand Down
1 change: 1 addition & 0 deletions ansible/roles/consul/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ consul_config_dir: "/etc/consul.d"
consul_data_dir: "/opt/consul"
consul_tls_dir: "{{ consul_data_dir }}/tls"
consul_template_config_dir: "/etc/consul-template"
consul_upstream_dns_address: ["1.1.1.1"]

# server
consul_server: true
Expand Down
62 changes: 62 additions & 0 deletions ansible/roles/consul/tasks/dnsmasq.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
- name: Install dnsmasq
apt:
name:
- dnsmasq
state: present

- name: Remove resolvconf
apt:
name:
- resolvconf
- openresolv
state: absent

- name: Allow ufw port 53
ufw:
rule: "allow"
port: 53

- name: Disable systemd-resolved
systemd:
name: systemd-resolved
state: stopped
enabled: false

- name: Write new /etc/resolv.conf
copy:
content: "nameserver 127.0.0.1"
dest: "/etc/resolv.conf"
owner: root
group: root
mode: 0644

- name: Copy dnsmasq config
template:
src: "dnsmasq.conf.j2"
dest: "/etc/dnsmasq.conf"
owner: root
group: root
mode: 0644

- name: Copy Consul dnsmasq forwarding config
copy:
content: |
server=/consul/127.0.0.1#8600
rev-server=10.0.0.0/8,127.0.0.1#8600
dest: "/etc/dnsmasq.d/10-consul"
owner: root
group: root
mode: 0644

- name: Start dnsmasq
systemd:
name: dnsmasq
state: started
enabled: true

- name: Wait for port 53
wait_for:
port: 53
host: "{{ ansible_default_ipv4.address }}"
state: started
5 changes: 5 additions & 0 deletions ansible/roles/consul/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---
- name: Setup dnsmasq
become: true
import_tasks: dnsmasq.yml
when: consul_server | bool

- name: Check for both Consul server and client enabled
fail:
msg: "Cannot setup both Consul server and client on the same node."
Expand Down
4 changes: 4 additions & 0 deletions ansible/roles/consul/templates/dnsmasq.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
listen-address={{ ansible_default_ipv4.address }},127.0.0.1
{% for addr in consul_upstream_dns_address %}
server={{ addr }}
{% endfor %}
2 changes: 1 addition & 1 deletion ansible/roles/coredns/templates/Corefile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}

consul:{{ coredns_dns_port }} {
forward . 10.10.10.110:8600
forward . 10.10.10.110
log
errors
}
Expand Down
1 change: 1 addition & 0 deletions docs/src/ansible/roles/consul.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ For encryption, the role creates consul-template templates for:
| consul_data_dir | Data directory | string | `/opt/consul` |
| consul_tls_dir | TLS files directory | string | `${consul_data_dir}/tls` |
| consul_template_config_dir | consul-template configuration file | string | `/etc/consul-template` |
| consul_upstream_dns_address | List of upstream DNS servers for dnsmasq | `["1.1.1.1"]` |
| consul_server | Start Consul in server mode | bool | `true` |
| consul_bootstrap_expect | (server only) The expected number of servers in a cluster | number | `1` |
| consul_client | Start Consul in client mode | bool | `false` |
Expand Down

0 comments on commit 7528872

Please sign in to comment.