Skip to content

Commit 1b5248b

Browse files
committed
Rewrite etcd role for proper lifecycle
Previously, docker could not be restarted on a node in a cluster and have v2plugin remaining working due to a conflict because the container was never stopped, the container name killed now matches that start name for the container. Previously, docker could not be shut down on multiple nodes without getting etcd in a state where it could not start because the start/stop script was dynamically managing the cluster membership as part of service start/stop state. Etcd is a HA database, starting or stopping should not depend on a single node, the old code checked the first node for peers and used that to determine if the cluster was being initialized or was already active. Instead, just configure etcd statically with all the member nodes in configuration. To improve readability, much of the old etcd start/stop scripts template to produce urls for etcd configuration was jinja macros, redid that as role default variables using composition instead. Introduced some naming for the etcd cluster that drives systemd service naming etcd member names are by default based on the hostname instead of numbers that can be out of order with the machines. The systemd unit was updated to handle: * docker stop * docker kill * systemctl stop docker * systemctl restart docker When stopping docker, etcd will be stopped first, and when docker starts, it will always trigger etcd to start. Also, slow down restarts a little to avoid getting blocked by systemd for restarting too many times and requiring manual intervention v2plugin updated to be wantedby instead of PartOf so it starts when docker is "start"ed and not just restarted Drive-by: * Add names to some plays and tasks to improve output * Removed etcd peer port 7001 as it is not used Signed-off-by: Chris Plock [email protected]
1 parent bd69c42 commit 1b5248b

File tree

8 files changed

+148
-275
lines changed

8 files changed

+148
-275
lines changed

group_vars/all

+2
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ service_vip: "{{ netmaster_ip }}"
3030
# netmaster_ip:
3131

3232
host_capability: "can-run-user-containers, storage"
33+
34+
etcd_peer_advertise_interface: "{{ control_interface }}"
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[Unit]
22
Description=enable v2plugin after etcd
33
After=docker.service etcd.service
4-
PartOf=etcd.service
54

65
[Service]
76
Type=oneshot
87
ExecStart=/usr/bin/v2plugin.sh
98
StandardOutput=journal
9+
1010
[Install]
11-
WantedBy=multi-user.target
11+
WantedBy=multi-user.target etcd.service

roles/etcd/defaults/main.yml

+47-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,56 @@
11
---
22
# role variable for the etcd service
33

4+
etcd_version: v2.3.8
5+
etcd_cluster_name: contiv
6+
7+
etcd_proxy: "{{ etcd_proxies_group in groups
8+
and inventory_hostname in groups[etcd_proxies_group] }}"
9+
etcd_member: "{{ etcd_members_group in groups
10+
and inventory_hostname in groups[etcd_members_group] }}"
11+
etcd_service_name: "etcd{{ etcd_proxy|bool|ternary('-proxy','') }}-{{
12+
etcd_cluster_name}}"
13+
etcd_docker_name: "{{ etcd_service_name }}"
14+
15+
etcd_node_name: "{{ inventory_hostname_short }}"
416
etcd_client_port1: 2379
517
etcd_client_port2: 4001
18+
619
etcd_peer_port1: 2380
7-
etcd_peer_port2: 7001
8-
etcd_peers_group: "service-master"
9-
etcd_rule_comment: "contiv_etcd traffic"
10-
etcd_version: "v2.3.8"
20+
21+
etcd_members_group: netplugin-master
22+
etcd_proxies_group: netplugin-worker
23+
etcd_initial_cluster_token: "etcd-{{ etcd_cluster_name }}"
24+
25+
etcd_rule_comment: "{{ etcd_cluster_name }}_etcd traffic"
26+
1127
etcd_heartbeat_interval: 1000
1228
etcd_election_timeout: 10000
13-
etcd_data_dir: /var/lib/etcd
29+
30+
etcd_data_dir: /var/lib/etcd/data
31+
etcd_wal_dir: /var/lib/etcd/wal
32+
1433
etcd_reset_state: false
34+
35+
# Etcd has "advertise" urls for the other nodes to use if it's behind a proxy
36+
# Also, it needs to "listen" on IPs and ports for peers to talk to
37+
etcd_peer_advertise_interface: "{{ ansible_default_ipv4.interface }}"
38+
etcd_peer_advertise_address: "{{
39+
hostvars[inventory_hostname]['ansible_' + etcd_peer_advertise_interface]['ipv4']['address'] }}"
40+
etcd_peer_listen_address: "{{ etcd_peer_advertise_address }}"
41+
etcd_peer_advertise_urls:
42+
- "http://{{ etcd_peer_advertise_address }}:{{ etcd_peer_port1 }}"
43+
etcd_peer_listen_urls:
44+
- "http://{{ etcd_peer_listen_address }}:{{ etcd_peer_port1 }}"
45+
46+
etcd_client_advertise_address: "{{ etcd_peer_advertise_address }}"
47+
etcd_client_listen_address: 0.0.0.0
48+
etcd_client_advertise_urls:
49+
- "http://{{ etcd_client_advertise_address }}:{{ etcd_client_port1 }}"
50+
- "http://{{ etcd_client_advertise_address }}:{{ etcd_client_port2 }}"
51+
etcd_client_listen_urls:
52+
- "http://{{ etcd_client_listen_address }}:{{ etcd_client_port1 }}"
53+
- "http://{{ etcd_client_listen_address }}:{{ etcd_client_port2 }}"
54+
55+
etcd_systemd_restart_delay_sec: 2
56+
etcd_systemd_restart: always

roles/etcd/files/etcd.service

-12
This file was deleted.

roles/etcd/tasks/main.yml

+53-40
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,56 @@
11
---
22
# This role contains tasks for configuring and starting etcd service
33

4-
- name: download etcdctl {{ etcd_version }}
5-
get_url:
6-
validate_certs: "{{ validate_certs }}"
7-
url: https://github.com/coreos/etcd/releases/download/{{ etcd_version }}/etcd-{{ etcd_version }}-linux-amd64.tar.gz
8-
dest: /tmp/etcd-{{ etcd_version }}-linux-amd64.tar.gz
9-
tags:
10-
- prebake-for-dev
11-
12-
- name: install etcdctl
13-
shell: >
14-
tar vxzf /tmp/etcd-{{ etcd_version }}-linux-amd64.tar.gz && \
15-
mv etcd-{{ etcd_version }}-linux-amd64/etcd* /usr/bin
16-
tags:
17-
- prebake-for-dev
18-
19-
- name: install etcd {{ etcd_version }}
20-
shell: docker pull quay.io/coreos/etcd:{{ etcd_version }}
21-
tags:
22-
- prebake-for-dev
23-
24-
- name: setup iptables for etcd
25-
shell: >
26-
( iptables -L INPUT | grep "{{ etcd_rule_comment }} ({{ item }})" ) || \
27-
iptables -I INPUT 1 -p tcp --dport {{ item }} -j ACCEPT -m comment --comment "{{ etcd_rule_comment }} ({{ item }})"
28-
become: true
29-
with_items:
30-
- "{{ etcd_client_port1 }}"
31-
- "{{ etcd_client_port2 }}"
32-
- "{{ etcd_peer_port1 }}"
33-
- "{{ etcd_peer_port2 }}"
34-
35-
- name: copy the etcd start/stop script
36-
template: src=etcd.j2 dest=/usr/bin/etcd.sh mode=u=rwx,g=rx,o=rx
37-
38-
- name: copy systemd units for etcd
39-
copy: src=etcd.service dest=/etc/systemd/system/etcd.service
40-
41-
- name: start etcd
42-
systemd: name=etcd daemon_reload=yes state=started enabled=yes
43-
4+
- block:
5+
- name: download etcdctl {{ etcd_version }}
6+
get_url:
7+
validate_certs: "{{ validate_certs }}"
8+
url: https://github.com/coreos/etcd/releases/download/{{ etcd_version }}/etcd-{{ etcd_version }}-linux-amd64.tar.gz
9+
dest: /tmp/etcd-{{ etcd_version }}-linux-amd64.tar.gz
10+
tags:
11+
- prebake-for-dev
12+
13+
- name: install etcdctl binaries
14+
shell: >
15+
tar vxzf /tmp/etcd-{{ etcd_version }}-linux-amd64.tar.gz &&
16+
mv etcd-{{ etcd_version }}-linux-amd64/etcd* /usr/bin
17+
tags:
18+
- prebake-for-dev
19+
20+
- name: pull etcd container {{ etcd_version }}
21+
shell: docker pull quay.io/coreos/etcd:{{ etcd_version }}
22+
tags:
23+
- prebake-for-dev
24+
25+
- name: setup iptables for etcd
26+
shell: >
27+
( iptables -L INPUT | grep "{{ etcd_rule_comment }} ({{ item }})" ) || \
28+
iptables -I INPUT 1 -p tcp --dport {{ item }} -j ACCEPT -m comment --comment "{{ etcd_rule_comment }} ({{ item }})"
29+
become: true
30+
with_items:
31+
- "{{ etcd_client_port1 }}"
32+
- "{{ etcd_client_port2 }}"
33+
- "{{ etcd_peer_port1 }}"
34+
35+
- name: Set facts from role defaults for sharing with other hosts
36+
set_fact:
37+
etcd_node_name: "{{ etcd_node_name }}"
38+
etcd_peer_advertise_urls: "{{ etcd_peer_advertise_urls }}"
39+
40+
- name: template etcd docker environment file
41+
template:
42+
src: etcd_env_file.j2
43+
dest: "/etc/{{ etcd_service_name }}.env"
44+
45+
- name: template systemd units for etcd
46+
template:
47+
src: etcd.service.j2
48+
dest: "/etc/systemd/system/{{ etcd_service_name }}.service"
49+
50+
- name: start etcd server or proxy
51+
systemd:
52+
name: "{{ etcd_service_name }}"
53+
daemon_reload: yes
54+
state: started
55+
enabled: yes
56+
when: etcd_member|bool or etcd_proxy|bool

0 commit comments

Comments
 (0)