How to install/upgrade elasticsearch cluster via ansible?

First, we have a playbook:

es.yml
1
2
3
4
5
- hosts: es
become: yes
roles:
- elasticsearch
serial: "50%"

serial: "50%" means ansbile will run tasks in 50% of hosts (or less).

And, we should add a rule elasticsearch:

roles/elasticsearch/tasks/main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
---
- name: disable shard allocation for the cluster
uri: url=http://localhost:9200/_cluster/settings method=PUT body='{{ es_allocation.disable }}' body_format=json
  ignore_errors: yes

- name: download ealsticsearch deb pacakge
local_action:
module: get_url
url: "{{ elasticsearch_url }}"
dest: "cache/elasticsearch-{{ elasticsearch_version }}.deb"
checksum: "sha256:{{ elasticsearch_sha256 }}"
become: no

- name: check if elasticsearch is installed
command: dpkg-query --showformat='${Version}' -W elasticsearch
register: elasticsearch_version_check
failed_when: elasticsearch_version_check.rc > 1
changed_when: elasticsearch_version_check.rc == 1

- block:
- name: copy elasticsearch package
copy: src="cache/elasticsearch-{{ elasticsearch_version }}.deb" dest=/tmp/elasticsearch.deb
- name: install elasticsearch
apt: deb=/tmp/elasticsearch.deb
always:
- name: delete elasticsearch package
file: path=/tmp/elasticsearch.deb state=absent
when: elasticsearch_version_check.stdout != "{{ elasticsearch_version }}"

# configure elasticsearch
# install plugins

- name: restart elasticsearch
service: name=elasticsearch state=restarted

- name: wait for elasticsearch node to come back up
wait_for: port=9200 delay=15

- name: enable shard allocation for the cluster
uri: url=http://localhost:9200/_cluster/settings method=PUT body='{{ es_allocation.enable }}' body_format=json

- name: wait for cluster health to return to green
uri: url=http://localhost:9200/_cluster/health method=GET
register: response
until: "response.json.status == 'green'"
retries: 50
delay: 5
  1. we try to disable allocation for cluster upgrade
  2. download elasticsearch’s deb package to local cache
  3. install and configure, restart elasticsearch
  4. wait 9200 port is available, enable allocation, wait for cluster become green.

some vars:

roles/elasticsearch/vars/main.yml
1
2
3
4
5
6
7
8
---
elasticsearch_version: 2.3.2
elasticsearch_url: "https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/{{ elasticsearch_version }}/elasticsearch-{{ elasticsearch_version }}.deb"
elasticsearch_sha256: 3d474b0123ec8ad4ebfa089f8cde607033e6cbef28a6a0df318bdc3d2a546cd8

es_allocation:
disable: '{"transient":{"cluster.routing.allocation.enable":"none"}}'
enable: '{"transient":{"cluster.routing.allocation.enable": "all"}}'

something to notice:

  • Do not forget configure unique cluster.name in elasticsearch.yml.

  • configure node.name to ${HOSTNAME} in elasticsearch.yml, needn’t to generate nodename.

  • generate host list from inventory list:

    1
    2
    3
    4
    discovery.zen.ping.unicast.hosts:
    {% for host in groups['es'] %}
    - {{ host }}
    {% endfor -%}
  • When master is upgrading, master electing will cause cluster unavailable for write.

How to install/upgrade elasticsearch cluster via ansible?

https://robberphex.com/how-to-installupgrade-elasticsearch-cluster-via-ansible/

作者

Robert Lu

发布于

2016-05-25

许可协议

评论