Elasticsearch rolling restart with Ansible

By | 2020년 7월 9일
Table of Content

Elasticsearch rolling restart with Ansible

Ansible 을 이용한 무중단 ES 클러스터 재시작을 설명합니다.

파일 생성

mkdir inventory
vi inventory/hosts

[myelasticsearch]
ES01 ansible_ssh_host=127.0.0.1 ansible_user=elk
ES02 ansible_ssh_host=127.0.0.2 ansible_user=elk
ES03 ansible_ssh_host=127.0.0.3 ansible_user=elk

[myelasticsearch:vars]
ansible_python_interpreter=/usr/bin/python3
mkdir playbooks
vi playbooks/task.yml

---
- name: do rolling restart of ES nodes
  hosts: ES01
  serial: 1
  tasks:
    - name: disable cluster routing
      shell: "curl -XPUT localhost:9200/_cluster/settings -H 'Content-Type: application/json' -d '{\"transient\" : {\"cluster.routing.allocation.enable\" : \"none\" }}'"
      args:
        warn: false
      register: result
      until: result.stdout.find('"acknowledged"') != -1
      retries: 200
      delay: 3
      changed_when: result.stdout.find('"acknowledged":true') != -1

    - name: do restart
      become: yes
      service: name=elasticsearch state=restarted

    - name: wait for node to restart
      shell: curl -I -s -m 2 http://localhost:9200 | head -n 1
      args:
        warn: false
      register: result
      until: result.stdout == "HTTP/1.1 200 OK"
      retries: 200
      delay: 3

    - name: enable cluster routing
      shell: "curl -XPUT localhost:9200/_cluster/settings -H 'Content-Type: application/json' -d '{\"transient\" : {\"cluster.routing.allocation.enable\" : \"all\" }}'"
      args:
        warn: false
      register: result
      until: result.stdout.find('"acknowledged"') != -1
      retries: 200
      delay: 3
      changed_when: result.stdout.find('"acknowledged":true') != -1

    - name: wait for cluster to stabilize
      shell: curl -s -m 2 localhost:9200/_cat/health | cut -d ' ' -f 4
      args:
        warn: false
      register: result
      until: result.stdout.find("green") != -1
      retries: 200
      delay: 30

실행

접속해야 하는 elk 계정은 sudo 이용권한이 있어야 하고 또 비밀번호 입력없이 sudo 를 이용할 수 있어야 합니다.

ansible-playbook playbooks/task.yml -i ./inventory/hosts

답글 남기기