Skip to content

Commit a3dbaa4

Browse files
committed
Implement coredns service discovery
1 parent 74cabf0 commit a3dbaa4

File tree

28 files changed

+318
-29
lines changed

28 files changed

+318
-29
lines changed

group_vars/all.yml

+11-1
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ varnish_manager_ssh_key_filename: varnish/sesame
14851485

14861486
# Is varnish hosted on a separate, dedicated node?
14871487
varnish_standalone: yes
1488-
1488+
varnish_lambda_backends_update: "{{ varnish_standalone and not mageops_coredns_enabled }}"
14891489
# Shall standalone varnish instance be used as a loadbalancer (instead of AWS ELB)?
14901490
varnish_as_loadbalancer: "{{ varnish_standalone and mageops_https_termination_enable }}"
14911491

@@ -1825,3 +1825,13 @@ mageops_magento_vary_sign_enabled: no
18251825
mageops_magento_vary_sign_secret: ""
18261826
magento_vary_sign: "{{ mageops_magento_vary_sign_enabled }}"
18271827
magento_vary_secret: "{{ mageops_magento_vary_sign_secret }}"
1828+
1829+
# ---------------------------
1830+
# -------- CoreDNS --------
1831+
# ---------------------------
1832+
mageops_coredns_enabled: no
1833+
mageops_coredns_dynamic_http_port: 2673
1834+
# Required to be set
1835+
# mageops_coredns_dynamic_secret:
1836+
mageops_dynamic_node_enabled: "{{ mageops_coredns_enabled }}"
1837+
varnish_dns_service_discovery: "{{ mageops_dynamic_node_enabled }}"

roles/cs.aws-security-group/tasks/main.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
- proto: tcp
4141
ports: ["{{ goaccess_vhost_port }}"]
4242
cidr_ip: "{{ mageops_trusted_cidr_blocks }}"
43+
- proto: tcp
44+
ports: [ "{{ mageops_coredns_dynamic_http_port }}" ]
45+
group_name: "{{ aws_security_group_app_name }}"
4346
vpc_id: "{{ aws_vpc_id }}"
4447
tags: "{{ aws_tags_default | combine(ec2_sg_tags) }}"
4548
vars:
@@ -214,7 +217,3 @@
214217
vars:
215218
ec2_sg_tags:
216219
Name: "{{ aws_security_group_varnish_name }}"
217-
218-
219-
220-

roles/cs.coredns/defaults/main.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
coredns_dynamic_enabled: no
2+
coredns_dynamic_domain: dynamic.internal
3+
# coredns_dynamic_http_port:
4+
# coredns_dynamic_secret:
5+
coredns_dynamic_timeout: 2m
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Unit]
2+
Description=Coredns serivce discovery and dns server
3+
4+
[Service]
5+
Type=simple
6+
ExecStart=/usr/bin/coredns -conf /etc/coredns/Corefile
7+
WorkingDirectory=/etc/coredns
8+
9+
[Install]
10+
WantedBy=multi-user.target

roles/cs.coredns/meta/main.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dependencies:
2+
- name: cs.repo-mageops

roles/cs.coredns/tasks/disable.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
- name: Update dhclient config
2+
template:
3+
dest: /etc/dhcp/dhclient.conf
4+
src: dhclient.conf.j2
5+
register: _update_dhclient
6+
7+
- name: Restart network
8+
service:
9+
name: network
10+
state: restarted
11+
when: _update_dhclient is changed
12+
13+
- name: Disable coredns service
14+
service:
15+
name: coredns
16+
state: stopped
17+
enabled: no
18+
19+
- name: Remove coredns config
20+
file:
21+
path: /etc/coredns/Corefile
22+
state: absent
23+
24+
- name: Remove coredns service file
25+
file:
26+
path: /etc/systemd/system/coredns.service
27+
state: absent
28+
29+
- name: Uninstall coredns
30+
yum:
31+
name: coredns
32+
state: absent

roles/cs.coredns/tasks/enable.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- name: Install coredns
2+
yum:
3+
name: coredns
4+
state: latest
5+
6+
- name: Install service file
7+
copy:
8+
dest: /etc/systemd/system/coredns.service
9+
src: coredns.service
10+
11+
- name: Create coredns config directory
12+
file:
13+
name: /etc/coredns
14+
state: directory
15+
16+
- name: Configure coredns
17+
template:
18+
dest: /etc/coredns/Corefile
19+
src: Corefile.j2
20+
register: _corefile
21+
22+
- name: Restart coredns service
23+
service:
24+
name: coredns
25+
state: restarted
26+
enabled: yes
27+
when: _corefile is changed
28+
29+
- name: Enable coredns service
30+
service:
31+
name: coredns
32+
state: started
33+
enabled: yes
34+
when: _corefile is not changed
35+
36+
- name: Update dhclient config
37+
template:
38+
dest: /etc/dhcp/dhclient.conf
39+
src: dhclient.conf.j2
40+
register: _update_dhclient
41+
42+
- name: Restart network
43+
service:
44+
name: network
45+
state: restarted
46+
when: _update_dhclient is changed

roles/cs.coredns/tasks/main.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- name: Enable coredns
2+
include_tasks: enable.yml
3+
when: coredns_dynamic_enabled
4+
- name: Disable coredns
5+
include_tasks: disable.yml
6+
when: not coredns_dynamic_enabled
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% if coredns_dynamic_enabled %}
2+
{{ coredns_dynamic_domain }}:53 {
3+
bind 127.0.0.53
4+
dynamic {
5+
addr :{{ coredns_dynamic_http_port }}
6+
secret {{ coredns_dynamic_secret }}
7+
host_timeout {{ coredns_dynamic_timeout }}
8+
}
9+
}
10+
{% endif %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% if coredns_dynamic_enabled %}
2+
prepend domain-name-servers 127.0.0.53;
3+
{% endif %}
4+
timeout 300;
5+
retry 60;
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Set node name to register
2+
# dynamic_node_backend_name:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[Unit]
2+
Description=Register node as dynamic backend named %i
3+
4+
[Service]
5+
Type=oneshot
6+
ExecStart=/usr/local/bin/mageopscli register_dynamic_bakcend %i
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Unit]
2+
Description=Timer to keep dynamic backend named %i registered
3+
4+
[Timer]
5+
OnUnitActiveSec=30sec
6+
AccuracySec=30sec
7+
OnActiveSec=10sec
8+
9+
[Install]
10+
WantedBy=timers.target

roles/cs.dynamic-node/meta/main.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dependencies:
2+
- cs.mageops-cli

roles/cs.dynamic-node/tasks/main.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- name: Install dynamic node service
2+
copy:
3+
dest: "/etc/systemd/system/[email protected]"
4+
5+
6+
- name: Install dynamic node timer
7+
copy:
8+
dest: "/etc/systemd/system/[email protected]"
9+
10+
11+
- name: Enable dynamic node timer
12+
service:
13+
name: "dynamic_node@{{ dynamic_node_backend_name }}.timer"
14+
state: started
15+
enabled: yes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
dynamicnode::register_node() {
5+
local node_name=$1
6+
curl -Lsf -H "secret: ${config__dynamicnode_secret}" -H "backend: ${node_name}" "${config__dynamicnode_endpoint_addr}/register"
7+
}

roles/cs.mageops-cli/files/mageopscli

+67-19
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,73 @@
22
set -euo pipefail
33

44
source "$(dirname "${BASH_SOURCE[0]}")/../lib/mageops/config.bash"
5-
source "$(dirname "${BASH_SOURCE[0]}")/../lib/mageops/libaws.bash"
6-
source "$(dirname "${BASH_SOURCE[0]}")/../lib/mageops/libmageops.bash"
7-
source "$(dirname "${BASH_SOURCE[0]}")/../lib/mageops/libfeatures.bash"
5+
if [ "${config__aws_enabled}" = "yes" ];then
6+
source "$(dirname "${BASH_SOURCE[0]}")/../lib/mageops/libaws.bash"
7+
source "$(dirname "${BASH_SOURCE[0]}")/../lib/mageops/libmageops.bash"
8+
source "$(dirname "${BASH_SOURCE[0]}")/../lib/mageops/libfeatures.bash"
9+
fi
10+
source "$(dirname "${BASH_SOURCE[0]}")/../lib/mageops/libdynamicnode.bash"
811

912
main::main() {
1013
local cmd=${1:-}
1114

1215
case $cmd in
1316
is_cron_node)
17+
main::aws_only
1418
if [ $# != 1 ];then main::help; fi
1519
main::is_cron_node || exit 1
1620
;;
1721
is_tag_present)
22+
main::aws_only
1823
if [ $# != 2 ];then main::help; fi
1924
local tag=$2
2025
main::is_tag_present "$tag" || exit 1
2126
;;
2227
get_tag_value)
28+
main::aws_only
2329
if [ $# != 2 ];then main::help; fi
2430
local tag=$2
2531
main::get_tag_value "$tag" || exit 1
2632
;;
2733
current_instance_id)
34+
main::aws_only
2835
if [ $# != 1 ];then main::help; fi
2936
main::current_instance_id || exit 1
3037
;;
3138
current_region)
39+
main::aws_only
3240
if [ $# != 1 ];then main::help; fi
3341
main::current_region || exit 1
3442
;;
3543
set_feature_flag)
44+
main::aws_only
3645
if [ $# != 3 ];then main::help; fi
3746
local feature=$2
3847
local value=$3
3948
main::set_feature_flag "$feature" "$value"
4049
;;
4150
read_feature_flag)
51+
main::aws_only
4252
if [ $# != 2 ];then main::help; fi
4353
local feature=$2
4454
main::read_feature_flag "$feature"
4555
;;
4656
is_feature_flag_set)
57+
main::aws_only
4758
if [ $# != 2 ];then main::help; fi
4859
local feature=$2
4960
main::is_feature_flag_set "$feature" || exit 1
5061
;;
5162
apply_features)
52-
if [ $# != 1 ];then main::help; fi
63+
main::aws_only
64+
if [ $# != 1 ];then main::help; fi
5365
main::apply_features
5466
;;
67+
register_dynamic_bakcend)
68+
if [ $# != 2 ];then main::help; fi
69+
local backend_name=$2
70+
main::register_dynamic_backend "$backend_name" || exit 1
71+
;;
5572
*) main::help ;;
5673
esac
5774
}
@@ -125,27 +142,58 @@ main::apply_features() {
125142
features::update_host_state
126143
}
127144

145+
main::register_dynamic_backend() {
146+
local backend_name=$1
147+
148+
dynamicnode::register_node "$backend_name" || main::panic "Failed to register this node as dynamic backend"
149+
}
150+
151+
main::aws_only() {
152+
if [ "${config__aws_enabled}" != "yes" ];then
153+
main::panic "This command is only available on aws environment"
154+
fi
155+
}
156+
157+
main::dynamic_node_only() {
158+
if [ "${config__dynamicnode_enabled}" != "yes" ];then
159+
main::panic "This command is only available on nodes with dynamic node feature configured"
160+
fi
161+
}
162+
128163
main::help() {
164+
local contains_commands=0
129165
main::eprintln " $0 <cmd>"
130166
main::eprintln ""
131167
main::eprintln "Where cmd is one of:"
132-
main::eprintln " is_cron_node Checks if there is tag 'Cron' with value 'yes'"
133-
main::eprintln " status code 0 means tag present, 1 means missing tag or"
134-
main::eprintln " other error"
135-
main::eprintln " is_tag_present <tag_name> Checks if specified tag name is set"
136-
main::eprintln " status code 0 means tag present, 1 means missing tag or"
137-
main::eprintln " other error"
138-
main::eprintln " get_tag_value <tag_name> Reads value assigned to tag"
139-
main::eprintln " current_instance_id Reads current ec2 instance id"
140-
main::eprintln " current_region Reads current ec2 region name"
141-
main::eprintln " set_feature_flag <feature> <value> Update feature flag value"
142-
main::eprintln " read_feature_flag <feature> Reads current feature flag value"
143-
main::eprintln " is_feature_flag_set <feature> Checks if there is any value set for feature flag"
144-
main::eprintln " status code 0 means flag is set, 1 otherwise"
145-
main::eprintln " apply_features Apply feature updates to this host"
168+
if [ "${config__aws_enabled}" = "yes" ];then
169+
contains_commands=1
170+
main::eprintln " is_cron_node Checks if there is tag 'Cron' with value 'yes'"
171+
main::eprintln " status code 0 means tag present, 1 means missing tag or"
172+
main::eprintln " other error"
173+
main::eprintln " is_tag_present <tag_name> Checks if specified tag name is set"
174+
main::eprintln " status code 0 means tag present, 1 means missing tag or"
175+
main::eprintln " other error"
176+
main::eprintln " get_tag_value <tag_name> Reads value assigned to tag"
177+
main::eprintln " current_instance_id Reads current ec2 instance id"
178+
main::eprintln " current_region Reads current ec2 region name"
179+
main::eprintln " set_feature_flag <feature> <value> Update feature flag value"
180+
main::eprintln " read_feature_flag <feature> Reads current feature flag value"
181+
main::eprintln " is_feature_flag_set <feature> Checks if there is any value set for feature flag"
182+
main::eprintln " status code 0 means flag is set, 1 otherwise"
183+
main::eprintln " apply_features Apply feature updates to this host"
184+
fi
185+
if [ "${config__dynamicnode_enabled}" = "yes" ];then
186+
contains_commands=1
187+
main::eprintln " register_dynamic_bakcend <name> Register this node as dynamic backend with <name>."
188+
main::eprintln " This command need to be repeated constandly to keep"
189+
main::eprintln " this node in list."
190+
fi
191+
if [ "${contains_commands}" = "0" ];then
192+
main::eprintln " Sorry but this node does not contain any usable commands"
193+
fi
146194
main::eprintln ""
147195
main::eprintln " Mageops cli tools"
148-
main::eprintln " (c) Creativestyle 2020"
196+
main::eprintln " (c) Creativestyle 2020-2021"
149197
exit 1
150198
}
151199

roles/cs.mageops-cli/tasks/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- { src: "libaws.bash", dest: "/usr/local/lib/mageops/libaws.bash", mode: "0644" }
1717
- { src: "libmageops.bash", dest: "/usr/local/lib/mageops/libmageops.bash", mode: "0644" }
1818
- { src: "libfeatures.bash", dest: "/usr/local/lib/mageops/libfeatures.bash", mode: "0644" }
19+
- { src: "libdynamicnode.bash", dest: "/usr/local/lib/mageops/libdynamicnode.bash", mode: "0644" }
1920
- { src: "mageopscli", dest: "/usr/local/bin/mageopscli", mode: "0755" }
2021
- { src: "features/example_feature.bash", dest: "/usr/local/lib/mageops/features/example_feature.bash", mode: "0644" }
2122
- { src: "mageops-update-features.service", dest: "/etc/systemd/system/mageops-update-features.service", mode: "0644" }

roles/cs.mageops-cli/templates/config.bash

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
config__features_s3_bucket="{{ aws_s3_secret_bucket }}"
44
config__features_s3_config_path="s3://${config__features_s3_bucket}/features.json"
55
config__features_modules_path="{{ mageops_cli_features_dir }}"
6+
config__dynamicnode_endpoint_addr="http://{{ mageops_varnish_host }}:{{ mageops_coredns_dynamic_http_port }}/"
7+
config__dynamicnode_secret="{{ mageops_coredns_dynamic_secret }}"
8+
config__aws_enabled="{{ aws_use | ternary('yes', 'no') }}"
9+
config__dynamicnode_enabled="{{ mageops_dynamic_node_enabled | ternary('yes', 'no') }}"

roles/cs.varnish/defaults/main.yml

+6
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,9 @@ varnish_301_cache_time: 3h
172172
# If you are on varnish_trusted_ips list you will
173173
# still receive those headers
174174
varnish_do_not_expose_caching: no
175+
176+
varnish_dns_service_discovery: no
177+
varnish_dns_backend_domain: magento.dynamic.internal
178+
varnish_dns_extra_backend_domain: magento_extra.dynamic.internal
179+
varnish_dns_acl:
180+
- 10/8 # Default aws network

roles/cs.varnish/meta/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
allow_duplicates: no
22
dependencies:
3+
- cs.repo-mageops
34
- cs.ansible-plugins
45
- cs.repo-varnish
56

0 commit comments

Comments
 (0)