From 8924f489ee2f00e48030366a3950137ce37e6df4 Mon Sep 17 00:00:00 2001 From: Yousef Alam Date: Thu, 12 Jan 2017 15:27:47 +0000 Subject: [PATCH] Add HAProxy and Consul template to mesos slaves for internal service discovery --- roles/mesos-slave/files/haproxy.ctmpl | 36 +++++++++++++++++++ roles/mesos-slave/handlers/main.yml | 5 +++ roles/mesos-slave/meta/main.yml | 4 +++ roles/mesos-slave/tasks/dependencies.yml | 16 +++++++++ roles/mesos-slave/tasks/main.yml | 34 ++++++++++++++++++ roles/mesos-slave/templates/consul.json.j2 | 7 ++++ .../templates/consul_template_config.j2 | 7 ++++ 7 files changed, 109 insertions(+) create mode 100644 roles/mesos-slave/files/haproxy.ctmpl create mode 100644 roles/mesos-slave/meta/main.yml create mode 100644 roles/mesos-slave/templates/consul_template_config.j2 diff --git a/roles/mesos-slave/files/haproxy.ctmpl b/roles/mesos-slave/files/haproxy.ctmpl new file mode 100644 index 0000000..47d019a --- /dev/null +++ b/roles/mesos-slave/files/haproxy.ctmpl @@ -0,0 +1,36 @@ +global + log /dev/log local0 + log /dev/log local1 notice + chroot /var/lib/haproxy + maxconn 4000 + stats socket /run/haproxy/admin.sock mode 660 level admin + stats timeout 30s + user haproxy + group haproxy + daemon + +defaults + log global + mode tcp + timeout connect 5000 + timeout client 500000 + timeout server 500000 + +{{- $aws_env := env "AWS_ENV" }} +{{ range services -}} +{{- if .Tags.Contains $aws_env }} +{{- $service_port_key := (print "services/" .Name "/" $aws_env "/port") -}} +{{- $service_port := (key_or_default $service_port_key "") -}} +{{- if $service_port }} +listen {{ .Name }} + bind 0.0.0.0:{{ $service_port }} + mode tcp + option clitcpka + balance roundrobin + {{- $env_services := (print $aws_env "." .Name) }} + {{ range service $env_services }} + server {{.ID}} {{ .NodeAddress }}:{{ .Port }} check + {{- end }} +{{- end }} +{{- end }} +{{- end }} diff --git a/roles/mesos-slave/handlers/main.yml b/roles/mesos-slave/handlers/main.yml index 1f1f322..e46a1a0 100644 --- a/roles/mesos-slave/handlers/main.yml +++ b/roles/mesos-slave/handlers/main.yml @@ -18,3 +18,8 @@ name: consul enabled: yes state: restarted + +- name: restart consul-template + service: + name: consul-template + state: restarted diff --git a/roles/mesos-slave/meta/main.yml b/roles/mesos-slave/meta/main.yml new file mode 100644 index 0000000..7ba1a2a --- /dev/null +++ b/roles/mesos-slave/meta/main.yml @@ -0,0 +1,4 @@ +--- + +dependencies: + - { role: consul-template } diff --git a/roles/mesos-slave/tasks/dependencies.yml b/roles/mesos-slave/tasks/dependencies.yml index 8ab2fc8..6f710f3 100644 --- a/roles/mesos-slave/tasks/dependencies.yml +++ b/roles/mesos-slave/tasks/dependencies.yml @@ -79,3 +79,19 @@ state: present tags: - mesos + +- name: Enable haproxy ppa + apt_repository: + repo: "ppa:vbernat/haproxy-1.6" + state: present + tags: + - haproxy + - consul-template + +- name: Install haproxy + apt: + name: "haproxy" + state: present + tags: + - haproxy + - consul-template diff --git a/roles/mesos-slave/tasks/main.yml b/roles/mesos-slave/tasks/main.yml index 561ec60..d0538c1 100644 --- a/roles/mesos-slave/tasks/main.yml +++ b/roles/mesos-slave/tasks/main.yml @@ -104,6 +104,39 @@ tags: - docker +- name: Make sure haproxy folder exists + file: + path: /var/run/haproxy + state: directory + tags: + - haproxy + +- name: Add consul-template configuration + template: + src: consul_template_config.j2 + dest: /etc/consul-template.d/config + owner: root + group: root + mode: 0644 + notify: + - restart consul-template + tags: + - haproxy + - consul-template + +- name: Add haproxy config template + copy: + src: haproxy.ctmpl + dest: /etc/consul-template.d/templates/haproxy.ctmpl + owner: root + group: root + mode: 0644 + notify: + - restart consul-template + tags: + - haproxy + - consul-template + - name: Setup consul service and check definitions template: src: consul.json.j2 @@ -115,3 +148,4 @@ - restart consul tags: - consul + - haproxy diff --git a/roles/mesos-slave/templates/consul.json.j2 b/roles/mesos-slave/templates/consul.json.j2 index 5d43df9..371f156 100644 --- a/roles/mesos-slave/templates/consul.json.j2 +++ b/roles/mesos-slave/templates/consul.json.j2 @@ -15,5 +15,12 @@ "timeout": "4s" } ] + }, + { + "name": "haproxy-internal", + "id": "haproxy-internal-{{ ec2_id }}", + "tags": ["haproxy-internal", "{{ ec2_tag_env }}"], + "port": 1936, + "enableTagOverride": false } ]} diff --git a/roles/mesos-slave/templates/consul_template_config.j2 b/roles/mesos-slave/templates/consul_template_config.j2 new file mode 100644 index 0000000..d1a1d04 --- /dev/null +++ b/roles/mesos-slave/templates/consul_template_config.j2 @@ -0,0 +1,7 @@ +consul = "{{ ansible_default_ipv4['address'] }}:8500" + +template { + source = "/etc/consul-template.d/templates/haproxy.ctmpl" + destination = "/etc/haproxy/haproxy.cfg" + command = "service haproxy restart" +}