使用ansible 部署es集群,集群中有五台机器 安装包在 管理主机上 请给出代码示并注释
时间: 2024-05-02 15:19:25 浏览: 136
如何在CentOS上创建Kubernetes集群
Sure, I'd be happy to help you with your question! Here's an example Ansible playbook that deploys an Elasticsearch cluster on five machines:
```yaml
---
- hosts: elasticsearch
become: yes
vars:
es_version: "7.10.2"
es_heap_size: "2g"
es_data_path: "/var/lib/elasticsearch"
es_config_path: "/etc/elasticsearch"
es_plugins:
- name: "analysis-icu"
url: "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-{{ es_version }}.zip"
- name: "analysis-smartcn"
url: "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-smartcn/analysis-smartcn-{{ es_version }}.zip"
tasks:
- name: Download Elasticsearch package
get_url:
url: "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{{ es_version }}-linux-x86_64.tar.gz"
dest: "/tmp/elasticsearch-{{ es_version }}.tar.gz"
- name: Extract Elasticsearch package
unarchive:
src: "/tmp/elasticsearch-{{ es_version }}.tar.gz"
dest: "/opt/"
remote_src: yes
creates: "/opt/elasticsearch-{{ es_version }}"
- name: Create Elasticsearch user
user:
name: elasticsearch
system: yes
createhome: yes
- name: Set Elasticsearch system limits
copy:
dest: "/etc/security/limits.d/99-elasticsearch.conf"
content: |
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
- name: Copy Elasticsearch configuration files
template:
src: "templates/elasticsearch.yml.j2"
dest: "{{ es_config_path }}/elasticsearch.yml"
owner: elasticsearch
group: elasticsearch
mode: "0640"
- name: Install Elasticsearch plugins
shell: |
/opt/elasticsearch-{{ es_version }}/bin/elasticsearch-plugin install {{ item.name }} --batch
loop: "{{ es_plugins }}"
- name: Start Elasticsearch service
systemd:
name: elasticsearch
state: started
enabled: yes
```
You'll need to create a `templates/elasticsearch.yml.j2` file in the same directory as your playbook, which contains the Elasticsearch configuration. Here's an example:
```yaml
cluster.name: my-cluster
node.name: "{{ inventory_hostname }}"
path.data: "{{ es_data_path }}"
path.logs: "{{ es_data_path }}/logs"
network.host: "_site_"
discovery.seed_hosts: "{{ groups['elasticsearch'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}"
cluster.initial_master_nodes: "{{ groups['elasticsearch'][0] }}"
http.port: 9200
node.master: true
node.data: true
bootstrap.memory_lock: true
indices.memory.index_buffer_size: "40%"
```
To run the playbook, save it as `deploy_es.yml` and run:
```
ansible-playbook -i inventory.ini deploy_es.yml
```
Make sure to replace `inventory.ini` with the path to your inventory file, which should contain a `[elasticsearch]` group with the IP addresses of your five machines. Hope this helps!
阅读全文