Optimize reconfiguration for haproxy

Co-Authored-By: caoyuan <cao.yuan@99cloud.net>
Change-Id: Iddde03760ff85af5263868ebc47b8b9438e92e8e
Partially-implements: blueprint better-reconfigure
This commit is contained in:
Mauricio Lima 2017-01-23 11:39:01 -03:00 committed by caoyuan
parent 88eb3a9696
commit a836033e14
8 changed files with 155 additions and 133 deletions

View File

@ -1,6 +1,29 @@
--- ---
project_name: "haproxy" project_name: "haproxy"
haproxy_services:
haproxy:
container_name: haproxy
group: haproxy
enabled: true
image: "{{ haproxy_image_full }}"
privileged: True
volumes:
- "{{ node_config_directory }}/haproxy/:{{ container_config_directory }}/:ro"
- "/etc/localtime:/etc/localtime:ro"
- "haproxy_socket:/var/lib/kolla/haproxy/"
keepalived:
container_name: keepalived
group: haproxy
enabled: true
image: "{{ keepalived_image_full }}"
privileged: True
volumes:
- "{{ node_config_directory }}/keepalived/:{{ container_config_directory }}/:ro"
- "/etc/localtime:/etc/localtime:ro"
- "/lib/modules:/lib/modules:ro"
- "haproxy_socket:/var/lib/kolla/haproxy/"
#################### ####################
# Docker # Docker

View File

@ -0,0 +1,61 @@
---
- name: Restart haproxy container
vars:
service_name: "haproxy"
service: "{{ haproxy_services[service_name] }}"
config_json: "{{ haproxy_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
haproxy_container: "{{ check_haproxy_containers.results|selectattr('item.key', 'equalto', service_name)|first }}"
kolla_docker:
action: "recreate_or_restart_container"
common_options: "{{ docker_common_options }}"
name: "{{ service.container_name }}"
image: "{{ service.image }}"
privileged: "{{ service.privileged | default(False) }}"
volumes: "{{ service.volumes }}"
when:
- action != "config"
- inventory_hostname in groups[service.group]
- service.enabled | bool
- config_json.changed | bool
or haproxy_cfg.changed | bool
or haproxy_pem.changed | bool
or haproxy_container.changed | bool
notify:
- Waiting for virtual IP to appear
- name: Restart keepalived container
vars:
service_name: "keepalived"
service: "{{ haproxy_services[service_name] }}"
config_json: "{{ haproxy_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
keepalived_container: "{{ check_haproxy_containers.results|selectattr('item.key', 'equalto', service_name)|first }}"
kolla_docker:
action: "recreate_or_restart_container"
common_options: "{{ docker_common_options }}"
name: "{{ service.container_name }}"
image: "{{ service.image }}"
privileged: "{{ service.privileged | default(False) }}"
volumes: "{{ service.volumes }}"
when:
- action != "config"
- inventory_hostname in groups[service.group]
- service.enabled | bool
- config_json.changed | bool
or keepalived_conf.changed | bool
or keepalived_container.changed | bool
notify:
- Waiting for virtual IP to appear
- name: Ensuring latest haproxy config is used
command: docker exec haproxy /usr/local/bin/kolla_ensure_haproxy_latest_config
register: status
changed_when: status.stdout.find('changed') != -1
when:
- haproxy_config_jsons.changed | bool
or haproxy_cfg.changed | bool
or haproxy_pem.changed | bool
- name: Waiting for virtual IP to appear
wait_for:
host: "{{ kolla_internal_vip_address }}"
port: "{{ database_port }}"

View File

@ -8,43 +8,91 @@
- name: Ensuring config directories exist - name: Ensuring config directories exist
file: file:
path: "{{ node_config_directory }}/{{ item }}" path: "{{ node_config_directory }}/{{ item.key }}"
state: "directory" state: "directory"
recurse: yes recurse: yes
with_items: when:
- "keepalived" - inventory_hostname in groups[item.value.group]
- "haproxy" - item.value.enabled | bool
with_dict: "{{ haproxy_services }}"
- name: Copying over config.json files for services - name: Copying over config.json files for services
template: template:
src: "{{ item }}.json.j2" src: "{{ item.key }}.json.j2"
dest: "{{ node_config_directory }}/{{ item }}/config.json" dest: "{{ node_config_directory }}/{{ item.key }}/config.json"
with_items: register: haproxy_config_jsons
- "keepalived" when:
- "haproxy" - inventory_hostname in groups[item.value.group]
- item.value.enabled | bool
with_dict: "{{ haproxy_services }}"
notify:
- "Restart {{ item.key }} container"
- Ensuring latest haproxy config is used
- name: Copying over haproxy.cfg - name: Copying over haproxy.cfg
vars:
service: "{{ haproxy_services['haproxy'] }}"
template: template:
src: "{{ item }}" src: "{{ item }}"
dest: "{{ node_config_directory }}/haproxy/haproxy.cfg" dest: "{{ node_config_directory }}/haproxy/haproxy.cfg"
register: haproxy_cfg
when:
- inventory_hostname in groups[service.group]
- service.enabled | bool
with_first_found: with_first_found:
- "{{ node_custom_config }}/haproxy/{{ inventory_hostname }}/haproxy.cfg" - "{{ node_custom_config }}/haproxy/{{ inventory_hostname }}/haproxy.cfg"
- "{{ node_custom_config }}/haproxy/haproxy.cfg" - "{{ node_custom_config }}/haproxy/haproxy.cfg"
- "haproxy.cfg.j2" - "haproxy.cfg.j2"
notify:
- Restart haproxy container
- Ensuring latest haproxy config is used
- name: Copying over keepalived.conf - name: Copying over keepalived.conf
vars:
service: "{{ haproxy_services['keepalived'] }}"
template: template:
src: "{{ item }}" src: "{{ item }}"
dest: "{{ node_config_directory }}/keepalived/keepalived.conf" dest: "{{ node_config_directory }}/keepalived/keepalived.conf"
register: keepalived_conf
when:
- inventory_hostname in groups[service.group]
- service.enabled | bool
with_first_found: with_first_found:
- "{{ node_custom_config }}/keepalived/{{ inventory_hostname }}/keepalived.conf" - "{{ node_custom_config }}/keepalived/{{ inventory_hostname }}/keepalived.conf"
- "{{ node_custom_config }}/keepalived/keepalived.conf" - "{{ node_custom_config }}/keepalived/keepalived.conf"
- "keepalived.conf.j2" - "keepalived.conf.j2"
notify:
- Restart keepalived container
- name: Copying over haproxy.pem - name: Copying over haproxy.pem
vars:
service: "{{ haproxy_services['haproxy'] }}"
copy: copy:
src: "{{ kolla_external_fqdn_cert }}" src: "{{ kolla_external_fqdn_cert }}"
dest: "{{ node_config_directory }}/haproxy/{{ item }}" dest: "{{ node_config_directory }}/haproxy/{{ item }}"
register: haproxy_pem
when:
- kolla_enable_tls_external | bool
- inventory_hostname in groups[service.group]
- service.enabled | bool
with_items: with_items:
- "haproxy.pem" - "haproxy.pem"
when: kolla_enable_tls_external | bool notify:
- Restart haproxy container
- Ensuring latest haproxy config is used
- name: Check haproxy containers
kolla_docker:
action: "compare_container"
common_options: "{{ docker_common_options }}"
name: "{{ item.value.container_name }}"
image: "{{ item.value.image }}"
volumes: "{{ item.value.volumes }}"
register: check_haproxy_containers
when:
- action != "config"
- inventory_hostname in groups[item.value.group]
- item.value.enabled | bool
with_dict: "{{ haproxy_services }}"
notify:
- "Restart {{ item.key }} container"

View File

@ -1,6 +1,5 @@
--- ---
- include: config.yml - include: config.yml
when: inventory_hostname in groups['haproxy']
- include: start.yml - name: Flush handlers
when: inventory_hostname in groups['haproxy'] meta: flush_handlers

View File

@ -1,14 +1,10 @@
--- ---
- name: Pulling keepalived image - name: Pulling haproxy images
kolla_docker: kolla_docker:
action: "pull_image" action: "pull_image"
common_options: "{{ docker_common_options }}" common_options: "{{ docker_common_options }}"
image: "{{ keepalived_image_full }}" image: "{{ item.value.image }}"
when: inventory_hostname in groups['haproxy'] when:
- inventory_hostname in groups[item.value.group]
- name: Pulling haproxy image - item.value.enabled | bool
kolla_docker: with_dict: "{{ haproxy_services }}"
action: "pull_image"
common_options: "{{ docker_common_options }}"
image: "{{ haproxy_image_full }}"
when: inventory_hostname in groups['haproxy']

View File

@ -1,74 +1,2 @@
--- ---
- name: Ensuring the containers up - include: deploy.yml
kolla_docker:
name: "{{ item.name }}"
action: "get_container_state"
register: container_state
failed_when: container_state.Running == false
when: inventory_hostname in groups[item.group]
with_items:
- { name: haproxy, group: haproxy }
- { name: keepalived, group: haproxy }
- include: config.yml
- name: Check the configs
command: docker exec {{ item.name }} /usr/local/bin/kolla_set_configs --check
changed_when: false
failed_when: false
register: check_results
when: inventory_hostname in groups[item.group]
with_items:
- { name: haproxy, group: haproxy }
- { name: keepalived, group: haproxy }
- name: Containers config strategy
kolla_docker:
name: "{{ item.name }}"
action: "get_container_env"
register: container_envs
when: inventory_hostname in groups[item.group]
with_items:
- { name: haproxy, group: haproxy }
- { name: keepalived, group: haproxy }
- name: Remove the containers
kolla_docker:
name: "{{ item[0]['name'] }}"
action: "remove_container"
register: remove_containers
when:
- config_strategy == "COPY_ONCE" or item[1]['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE'
- item[2]['rc'] == 1
- inventory_hostname in groups[item[0]['group']]
with_together:
- [{ name: haproxy, group: haproxy },
{ name: keepalived, group: haproxy }]
- "{{ container_envs.results }}"
- "{{ check_results.results }}"
- include: start.yml
when: remove_containers.changed
# container_envs.results is a list of two elements, first corresponds to
# haproxy container result and second to keepalived container result and the
# same applicable for check_results.results
- name: Ensuring latest haproxy config is used
command: docker exec haproxy /usr/local/bin/kolla_ensure_haproxy_latest_config
register: status
changed_when: status.stdout.find('changed') != -1
when:
- config_strategy == 'COPY_ALWAYS'
- container_envs.results[0]['KOLLA_CONFIG_STRATEGY'] != 'COPY_ONCE'
- check_results.results[0]['rc'] == 1
- inventory_hostname in groups['haproxy']
- name: Restart keepalived container
kolla_docker:
name: "keepalived"
action: "restart_container"
when:
- config_strategy == 'COPY_ALWAYS'
- container_envs.results[1]['KOLLA_CONFIG_STRATEGY'] != 'COPY_ONCE'
- check_results.results[1]['rc'] == 1
- inventory_hostname in groups['haproxy']

View File

@ -1,35 +0,0 @@
---
- name: Starting haproxy container
kolla_docker:
action: "start_container"
common_options: "{{ docker_common_options }}"
image: "{{ haproxy_image_full }}"
name: "haproxy"
privileged: True
volumes:
- "{{ node_config_directory }}/haproxy/:{{ container_config_directory }}/:ro"
- "/etc/localtime:/etc/localtime:ro"
- "haproxy_socket:/var/lib/kolla/haproxy/"
- name: Starting keepalived container
kolla_docker:
action: "start_container"
common_options: "{{ docker_common_options }}"
image: "{{ keepalived_image_full }}"
name: "keepalived"
privileged: True
volumes:
- "{{ node_config_directory }}/keepalived/:{{ container_config_directory }}/:ro"
- "/etc/localtime:/etc/localtime:ro"
- "/lib/modules:/lib/modules:ro"
- "haproxy_socket:/var/lib/kolla/haproxy/"
- name: Ensuring latest haproxy config is used
command: docker exec haproxy /usr/local/bin/kolla_ensure_haproxy_latest_config
register: status
changed_when: status.stdout.find('changed') != -1
- name: Waiting for virtual IP to appear
wait_for:
host: "{{ kolla_internal_vip_address }}"
port: "{{ database_port }}"

View File

@ -11,9 +11,11 @@
when: kolla_internal_vip_address not in secondary_addresses when: kolla_internal_vip_address not in secondary_addresses
# Upgrading master keepalived and haproxy # Upgrading master keepalived and haproxy
- include: start.yml - name: Flush handlers
meta: flush_handlers
when: kolla_internal_vip_address in secondary_addresses when: kolla_internal_vip_address in secondary_addresses
# Upgrading slave keepalived and haproxy # Upgrading slave keepalived and haproxy
- include: start.yml - name: Flush handlers
meta: flush_handlers
when: kolla_internal_vip_address not in secondary_addresses when: kolla_internal_vip_address not in secondary_addresses