B i M A P

Ansible | 如何在 5 分鐘內,一次安裝大量節點 ES Cluster


大家有沒有遇到工作需要一次對多台機器執行相同的指令,或是部署相同的服務呢?如果只是三、四台機器,重複做著幾個步驟,可能沒什麼,但是如果今天有20台機器+20條指令要執行呢?總不可能一台一台ssh進去再執行各項指令吧😫

幸好有一個很棒的開源工具-Ansible,完美的解決了上述的問題,只要選定一台主控機,並安裝Ansible,再將每台機器的連線資訊設定好後,就可以一鍵控制所有機器囉!

以下會對Ansible的操作用安裝elasticsearch的方式做簡易的解說。

ansible install elasticsearch structure

Step 1. 安裝Ansible

選擇一台主控機進行安裝,被控機不需安裝

本文以Linux常用環境CentOS及Ubuntu作介紹,想要了解其他環境安裝,可以參考Ansible台灣使用者社群

  • CentOS
$ sudo yum -y install epel-release
$ sudo yum -y update
$ sudo yum -y install ansible
  • Ubuntu
$ sudo apt-get install software-properties-common -y
$ sudo apt-add-repository ppa:ansible/ansible -y
$ sudo apt-get update
$ sudo apt-get install ansible -y


Step 2. 配置inventory與ansible.cfg

  • inventory file

主要是設置被控機的連線資訊,像是ip、port、ssh_password...等,讓ansible知道該對哪些機器做事。

先建立inventory file

$ vim inventory

[群組名]
ansible_host=<IP> ansible_ssh_port=<port> ansible_connection=<連線類型> ansible_ssh_pass=<ssh密碼> ansible_user=<使用者名稱> ansible_sudo_pass=<sudo密碼>

同一台機器的配置要寫在同一行,不可任意換行。以上是常見的配置,可依需求調整,並非全部都需要寫入file。

💡tips:這邊ssh密碼若沒有設定,就需要用公鑰處理,為了安全起見,比較建議用公鑰。

如果同一群組有相同的變數,可以用下列的方式呈現。

[群組名:vars]
ansible_user=<使用者名稱>

舉例:

[elastic]
ansible_host=xxx.xxx.xx.11 
ansible_host=xxx.oo.oo.20 ansible_ssh_port=2222
ansible_host=xxx.xxx.xx.[31:40] # ip有連號,可以這樣寫 
[elastic:vars]
ansible_user=bimap
ansible_connection=ssh
ansible_sudo_pass=aaaaa # 若會擔心安全問題,建議不要放

如果有多台機器但是有不同的工作任務,也可以透過分組的方式,方便後續執行直接選擇組別即可。

  • ansible.cfg

可以用來設定inventory路徑、控制Ansible 劇本playbook的一些選項。

舉例:

[default]
inventory=/home/bimap/ansible/inventory   

ansible.cfg可以在/etc/ansible底下找到,或是自行另外建立。


Step 3. 發送公鑰

完成上述的設定後,如果沒有設定ssh的密碼,就要透過公鑰才能連線到被控機。

主控機要先產生public key

$ ssh-keygen -t rsa

透過Ansible發送出去

$ ansible -i inventory <群組名> -m authorized_key -a "user=<被控機使用者名稱> key='{{ lookup('file', '<主控機公鑰路徑/.ssh/id_rsa.pub>')}}' path='<被控機公鑰加入路徑/.ssh/authorized_keys>' manage_dir=no" --ask-pass -c paramiko


Step 4. 如何操作Ansible

Ansible可以透過直接下指令或是寫好playbook腳本再執行,如果指令不多,不一定要寫成playbook。

  • 指令:
$ ansible -i <inventory file> <群組名> -m <模組名> -a "<command>" -b -K

-i : 接inventoey file

-m : 後面接要引用的模組,更多模組可參考官方文件

-a: 傳遞模組需要的參數

-b : 用sudo 權限執行指令

-K : 如果需要輸入sudo 密碼才能使用sudo 權限,就要加-K

以下載elasticsearch安裝檔舉例:

$ ansible -i inventory elastic -m shell -a "wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.3.3-amd64.deb" -b -K

以啟動elasticsearch舉例:

$ ansible -i inventory elastic -m -service -a "name=elasticsearch state=started" -b -K


  • playbook

完整elasticsearch 8.3.3版(single-node)安裝步驟:

$ vim install_es.yml
- name: install Elasticsearch offline
  hosts: elastic # 要執行task的群組
  tasks: 
    - name: download elasticsearch # 建議將每個任務命名或簡單敘述,如有報錯才方便debug
      shell: sudo wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.3.3-amd64.deb
      become: yes # become 是否用root權限執行
    - name: install
      shell: sudo dpkg -i elasticsearch-8.3.3-amd64.deb
      become: yes
   - name: mark cluster_initial line in config file 
     lineinfile:
       destfile: /etc/elasticsearch/elasticsearch.yml
       regexp: 'cluster.initial_master_nodes:*'
       line: '#cluster.initial_master_nodes:'
       backrefs: yes
    become: yes
    - name: unmark bootstrap line in config file 
     lineinfile:
       destfile: /etc/elasticsearch/elasticsearch.yml
       regexp: '#bootstrap.memory_lock: true'
        line: 'bootstrap.memory_lock: true'
        backrefs: no
    become: yes
    - name: set discovery type in config file 
     lineinfile:
       destfile: /etc/elasticsearch/elasticsearch.yml
       regexp: 'add a line'
       line: 'discovery.type: single-node'
       backrefs: no
    become: yes
    - name: set cluster name in config file 
     lineinfile:
       destfile: /etc/elasticsearch/elasticsearch.yml
       regexp: '#cluster.name:'
        line: 'cluster.name: bimap'
        backrefs: no
    become: yes
  - name: Starting Elasticsearch
  service:
    name: elasticsearch
    state: started
  become: yes

寫好腳本後執行,就完成安裝elasticsearch囉!

$ ansible-playbook -i inventory install_es.yml -K


Logstash及Kibana可以參考之前的ELK 8.x版安裝教學+上述說明試試看吧✅

延伸閱讀
elaine的大頭照
Elaine 同學

在大數據時代,資料量大、即時又多樣,因此非常需要像是ELK、InfluxDB這些資料分析處理工具,來協助我們將暗藏的重要訊息解密,如果你也有興趣,讓我們一起進入資料處理的領域探索吧!