使用ansible自动化部署elk单机并添加x-pack认证
时间: 2025-01-06 15:50:56 浏览: 15
使用Ansible自动化部署ELK(Elasticsearch、Logstash、Kibana)单机并添加X-Pack认证可以通过以下步骤实现:
### 1. 准备工作
确保你的控制节点(运行Ansible的机器)和目标节点(部署ELK的机器)已经安装并配置好SSH连接,并且目标节点上已经安装了Python。
### 2. 安装Ansible
在你的控制节点上安装Ansible。你可以使用pip进行安装:
```bash
pip install ansible
```
### 3. 创建Ansible Playbook
创建一个YAML文件,例如`deploy_elk.yml`,并添加以下内容:
```yaml
---
- name: Deploy ELK Stack with X-Pack
hosts: elk_servers
become: yes
vars:
elastic_version: 7.x
kibana_version: 7.x
logstash_version: 7.x
xpack_security_enabled: true
tasks:
- name: Install Java
apt:
name: openjdk-11-jdk
state: present
- name: Import Elasticsearch GPG key
apt_key:
url: https://artifacts.elastic.co/GPG-KEY-elasticsearch
state: present
- name: Add Elasticsearch repository
apt_repository:
repo: "deb https://artifacts.elastic.co/packages/{{ elastic_version }}/apt stable main"
state: present
filename: 'elasticsearch'
- name: Install Elasticsearch
apt:
name: elasticsearch={{ elastic_version }}
state: present
- name: Configure Elasticsearch
template:
src: templates/elasticsearch.yml.j2
dest: /etc/elasticsearch/elasticsearch.yml
notify:
- Restart Elasticsearch
- name: Enable and start Elasticsearch
systemd:
name: elasticsearch
enabled: yes
state: started
- name: Import Kibana GPG key
apt_key:
url: https://artifacts.elastic.co/GPG-KEY-elasticsearch
state: present
- name: Add Kibana repository
apt_repository:
repo: "deb https://artifacts.elastic.co/packages/{{ kibana_version }}/apt stable main"
state: present
filename: 'kibana'
- name: Install Kibana
apt:
name: kibana={{ kibana_version }}
state: present
- name: Configure Kibana
template:
src: templates/kibana.yml.j2
dest: /etc/kibana/kibana.yml
notify:
- Restart Kibana
- name: Enable and start Kibana
systemd:
name: kibana
enabled: yes
state: started
- name: Import Logstash GPG key
apt_key:
url: https://artifacts.elastic.co/GPG-KEY-elasticsearch
state: present
- name: Add Logstash repository
apt_repository:
repo: "deb https://artifacts.elastic.co/packages/{{ logstash_version }}/apt stable main"
state: present
filename: 'logstash'
- name: Install Logstash
apt:
name: logstash={{ logstash_version }}
state: present
- name: Configure Logstash
template:
src: templates/logstash.yml.j2
dest: /etc/logstash/logstash.yml
notify:
- Restart Logstash
- name: Enable and start Logstash
systemd:
name: logstash
enabled: yes
state: started
- name: Enable X-Pack Security
lineinfile:
path: /etc/{{ item }}/{{ item }}.yml
regexp: '^xpack.security.enabled'
line: 'xpack.security.enabled: true'
with_items:
- elasticsearch
- kibana
- logstash
notify:
- Restart Elasticsearch
- Restart Kibana
- Restart Logstash
handlers:
- name: Restart Elasticsearch
systemd:
name: elasticsearch
state: restarted
- name: Restart Kibana
systemd:
name: kibana
state: restarted
- name: Restart Logstash
systemd:
name: logstash
state: restarted
```
### 4. 创建模板文件
在`templates`目录下创建以下模板文件:
- `elasticsearch.yml.j2`
- `kibana.yml.j2`
- `logstash.yml.j2`
根据你的需求配置这些文件。例如,`elasticsearch.yml.j2`可以包含以下内容:
```yaml
cluster.name: my-cluster
node.name: "{{ ansible_hostname }}"
network.host: "{{ ansible_default_ipv4.address }}"
http.port: 9200
discovery.seed_hosts: ["127.0.0.1"]
cluster.initial_master_nodes: ["{{ ansible_hostname }}"]
xpack.security.enabled: true
```
### 5. 运行Ansible Playbook
在控制节点上运行以下命令来部署ELK并启用X-Pack认证:
```bash
ansible-playbook -i hosts deploy_elk.yml
```
### 6. 配置X-Pack认证
在部署完成后,你需要在Elasticsearch和Kibana中配置用户和密码。可以使用以下命令创建初始用户:
```bash
/usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive
```
### 7. 配置Kibana
在`kibana.yml`中配置Elasticsearch的用户名和密码:
```yaml
elasticsearch.username: "kibana_system"
elasticsearch.password: "your_password"
```
### 8. 重启服务
重启所有相关服务以应用配置更改:
```bash
systemctl restart elasticsearch
systemctl restart kibana
systemctl restart logstash
```
通过以上步骤,你就可以使用Ansible自动化部署ELK并添加X-Pack认证了。
阅读全文