diff --git a/files/haproxy-sample.cfg b/files/haproxy-sample.cfg new file mode 100644 index 0000000..9480661 --- /dev/null +++ b/files/haproxy-sample.cfg @@ -0,0 +1,32 @@ +global + log fd@2 local2 + chroot /var/lib/haproxy + pidfile /var/run/haproxy.pid + maxconn 4000 + user haproxy + group haproxy + stats socket /var/lib/haproxy/stats expose-fd listeners + master-worker + +resolvers docker + nameserver dns1 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold other 10s + hold refused 10s + hold nx 10s + hold timeout 10s + hold valid 10s + hold obsolete 10s + +frontend fe_web + bind *:80 + use_backend stat if { path -i /my-stats } + +backend stat + stats enable + stats uri /my-stats + stats refresh 15s + stats show-legends + stats show-node diff --git a/tasks/haproxy-docker-service.yml b/tasks/haproxy-docker-service.yml index ebf435c..aa042bf 100644 --- a/tasks/haproxy-docker-service.yml +++ b/tasks/haproxy-docker-service.yml @@ -2,17 +2,23 @@ - name: Manage the composition of haproxy as a docker swarm service block: - name: Create the destination directory of the haproxy docker compose file - file: dest={{ haproxy_docker_compose_dir }} state=directory + file: dest={{ haproxy_docker_compose_dir }} state=directory mode='0755' - name: Install the docker compose file - template: src=haproxy-docker-compose.yml.j2 dest={{ haproxy_docker_compose_dir }}/docker-compose.yml + template: src=haproxy-docker-compose.yml.j2 dest={{ haproxy_docker_compose_dir }}/docker-compose.yml mode='0644' + + - name: Create the local haproxy configuration directory + file: dest=/etc/haproxy state=directory mode='0755' + + - name: Install a bare haproxy configuration if there's none + copy: src=haproxy-sample.cfg dest=/etc/haproxy/haproxy.cfg mode='0644' force=no - name: Run the docker compose file to start the service - docker_compose: - project_src: '{{ haproxy_docker_compose_dir }}' + docker_stack: + name: haproxy state: present - pull: yes - recreate: smart + compose: + - '{{ haproxy_docker_compose_dir }}/docker-compose.yml' run_once: True when: docker_swarm_manager_main_node is defined and docker_swarm_manager_main_node | bool diff --git a/templates/haproxy-docker-compose.yml.j2 b/templates/haproxy-docker-compose.yml.j2 index 5f8a1ae..9244cfc 100644 --- a/templates/haproxy-docker-compose.yml.j2 +++ b/templates/haproxy-docker-compose.yml.j2 @@ -1,11 +1,11 @@ -version: '3.8' +version: '3.6' services: haproxy: image: haproxytech/haproxy-debian:{{ haproxy_version }} volumes: - {{ haproxy_cert_dir }}:{{ haproxy_cert_dir }}:ro - - /etc/haproxy:/etc/haproxy:ro + - /etc/haproxy:/usr/local/etc/haproxy:ro ports: - target: {{ haproxy_default_port }} published: {{ haproxy_default_port }} @@ -15,27 +15,16 @@ services: published: {{ haproxy_ssl_port }} protocol: tcp mode: host -{% if docker_swarm_haproxy_networks is defined %} - networks: -{% for net in docker_swarm_haproxy_networks %} - - {{ net }} -{% endfor %} -{% endif %} -{% if docker_swarm_haproxy_additional_services is defined %} -{% for net in docker_swarm_haproxy_additional_services %} - - {{ net.service_overlay_network }} -{% endfor %} -{% endif %} + dns: [127.0.0.11] deploy: mode: replicated replicas: 1 - endpoint_mode: dnsrr placement: constraints: [node.role == manager] restart_policy: - condition: unless-stopped - delay: 5s - max_attempts: 3 + condition: on-failure + delay: 20s + max_attempts: 5 window: 120s resources: limits: @@ -45,5 +34,6 @@ services: cpus: '1.0' memory: 384M {% if docker_log_to_journal %} - log_driver: 'journald' + logging: + driver: 'journald' {% endif %}