ansible部署elasticsearch

创建四台虚拟机并分别命名为ansible,elk1,elk2,elk3,并上传centos7.9镜像到ansible节点

hostnamectl set-hostname ansible
hostnamectl set-hostname elk-1
hostnamectl set-hostname elk-2
hostnamectl set-hostname elk-3

配置anisble主机映射

[root@ansible ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.33.131 ansible
192.168.33.28 node1
192.168.33.31 node2
192.168.33.130 node3

配置免密登录,并复制域名解析到所有节点

# 配置免密登录
[root@ansible ~]# ssh-keygen
[root@ansible ~]# ssh-copy-id elk-1
[root@ansible ~]# ssh-copy-id elk-2
[root@ansible ~]# ssh-copy-id elk-3
# 复制域名解析文件到所有节点
[root@ansible ~]# scp /etc/hosts elk-1:/etc/
[root@ansible ~]# scp /etc/hosts elk-2:/etc/
[root@ansible ~]# scp /etc/hosts elk-3:/etc/

 关闭ansible节点的防火墙和selinux配置

[root@ansible ~]# systemctl stop firewalld
[root@ansible ~]# setenforce 0

下载相关文件至ansible节点,并将rpm包拷贝到节点

[root@ansible ~]# ll
total 220520
-rw-r--r-- 1 root root  20569762 Apr 17 10:10 ansible.tar.gz
-rw-r--r-- 1 root root  27970243 Apr 18 11:15 elasticsearch-6.0.0.rpm
-rw-r--r-- 1 root root  63979183 Apr 18 11:16 kibana-6.0.0-x86_64.rpm
-rw-r--r-- 1 root root 113288712 Apr 18 11:17 logstash-6.0.0.rpm
[root@ansible ~]# scp elasticsearch-6.0.0.rpm elk-1:/root/
[root@ansible ~]# scp elasticsearch-6.0.0.rpm elk-2:/root/
[root@ansible ~]# scp elasticsearch-6.0.0.rpm elk-3:/root/
[root@ansible ~]# scp kibana-6.0.0-x86_64.rpm elk-1:/root/
[root@ansible ~]# scp kibana-6.0.0-x86_64.rpm elk-2:/root/
[root@ansible ~]# scp kibana-6.0.0-x86_64.rpm elk-3:/root/
[root@ansible ~]# scp logstash-6.0.0.rpm elk-1:/root/
[root@ansible ~]# scp logstash-6.0.0.rpm elk-2:/root/
[root@ansible ~]# scp logstash-6.0.0.rpm elk-3:/root/

配置ansible源和centos源

[root@ansible ~]# tar -zxvf ansible.tar.gz -C /opt
[root@ansible ~]# umount CentOS-7-x86_64-DVD-2009.iso /mnt/
[root@ansible ~]# mkdir /opt/centos-repo && cp -frv /mnt/* /opt/centos-repo
[root@ansible ~]# vim /etc/yum.repos.d/local.repo
[ansible]
name=ansible
baseurl=file:///opt/ansible
gpgcheck=0
enalbed=1
[centos]
name=centos
baseurl=file:///opt/centos-repo
gpgcheck=0
enabled=1

安装ansible并配置主机映射

[root@ansible ~]# vim /etc/ansible/hosts
# 在文档的末尾加上
[elk-1]
192.168.33.28
[elk-2]
192.168.33.31
[elk-3]
192.168.33.130

安装vsftpd服务

[root@ansible ~]# yum install -y vsftpd
[root@ansible ~]#vim /etc/vsftpd/vsftpd.conf
# 在文档末尾加上
anon_root=/opt
[root@ansible ~]# systemctl restart vsftpd && systemctl enable vsftpd

安装elasticsearch并编写三个节点的文档

[root@ansible ~]# rpm -ivh /root/elasticsearch-6.0.0.rpm 
[root@ansible ~]# mkdir elasticsearch && cd elasticsearch
[root@ansible example]# cp -rf /etc/elasticsearch/elasticsearch.yml elk-1.yml
[root@ansible example]# cp -rf /etc/elasticsearch/elasticsearch.yml elk-2.yml
[root@ansible example]# cp -rf /etc/elasticsearch/elasticsearch.yml elk-3.yml
[root@ansible example]# vim elk-1.yml
node.name: elk-1         
node.master: true
node.data: false
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 192.168.33.131
http.port: 9200
discovery.zen.ping.unicast.hosts: ["node1", "node2","node3"]
[root@ansible example]# vim elk-2.yml
node.name: elk-2         
node.master: false
node.data: true
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 192.168.33.31
http.port: 9200
discovery.zen.ping.unicast.hosts: ["node1", "node2","node3"]
[root@ansible example]# vim elk-3.yml
node.name: elk-3         
node.master: false
node.data: true
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 192.168.33.130
http.port: 9200
discovery.zen.ping.unicast.hosts: ["node1", "node2","node3"]

安装kibana并编写相关文件

[root@ansible example]# rpm -ivh /root/kibana-6.0.0-x86_64.rpm 
[root@ansible example]# cp -rf /etc/kibana/kibana.yml .
[root@ansible example]# cat kibana.yml | grep -v ^#
server.port: 5601
server.host: "192.168.33.28"
elasticsearch.url: "http://192.168.33.28:9200"

安装logstash并编写相关文档

[root@ansible example]# rpm -ivh /root/logstash-6.0.0.rpm 
[root@ansible example]# cp -rf /etc/logstash/logstash.yml .
[root@ansible example]# vim logstash.yaml
http.host: "192.168.33.31"

编写日志输出文件

[root@ansible example]# vim syslog.conf 
inout{
file{
path => "/var/log/messages"
type => "systemlog"
start_position => "beginning"
stat_intrerval => "3"
}
}
output{
if[type] == "systemlog"{
elasticsearch{
hosts => ["192.168.33.28:9200"]
index => "system-log-%{+YYYY.MM.dd}"
}}
}

编写剧本文件

[root@ansible example]# vim cscc_install.yaml 
- hosts: all
  remote_user: root
  tasks:
   - name: rm repo
     shell: rm -rf /etc/yum.repos.d/*
   - name: copy repo
     copy: src=local.repo dest=/etc/yum.repos.d/
   - name: install java
     yum: name=java-1.8.0-*
   - name: install elk
     shell: rpm -e elasticsearch &&  rpm -ivh /root/elasticsearch-6.0.0.rpm
- hosts: elk-1
  remote_user: root
  tasks:
   - name: copy config
     copy: src=elk-1.yml dest=/etc/elasticsearch/elasticsearch.yml
   - name: daemon-reload
     shell: systemctl daemon-reload
   - name: start elk
     shell: systemctl start elasticsearch && systemctl enable elasticsearch
   - name: install kibana
     shell: rpm -e kibana &&  rpm -ivh kibana-6.0.0-x86_64.rpm
   - name: copy config
     template: src=kibana.yml dest=/etc/kibana/kibana.yml
   - name: start kibana
     shell: systemctl start kibana && systemctl enable kibana
- hosts: elk-2
  remote_user: root
  tasks:
   - name: copy config
     copy: src=elk-2.yml dest=/etc/elasticsearch/elasticsearch.yml
   - name: daemon-reload
     shell: systemctl daemon-reload
   - name: start elk
     shell: systemctl start elasticsearch && systemctl enable elasticsearch
   - name: install logstash
     shell: rpm -ivh logstash-6.0.0.rpm
   - name: copy config
     copy: src=logstash.yml dest=/etc/logstash/logstash.yml
   - name: copy config
     copy: src=syslog.conf dest=/etc/logstash/conf.d/syslog.conf
- hosts: elk-3
  remote_user: root
  tasks:
   - name: copy config
     copy: src=elk-3.yml dest=/etc/elasticsearch/elasticsearch.yml
   - name: daemon-reload
     shell: systemctl daemon-reload
   - name: start elk
     shell: systemctl start elasticsearch && systemctl enable elasticsearch

执行文件完成部署

[root@ansible example]# ansible-playbook cscc_install.yaml

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Ansible 部署 ES 集群的示例代码: 1. 创建一个名为 es_cluster 的目录,用于存放 Ansible 相关文件。 2. 在 es_cluster 目录下创建一个名为 hosts 的文件,用于指定要部署 ES 集群的机器。 [es_nodes] node1 ansible_host=192.168.1.1 node2 ansible_host=192.168.1.2 node3 ansible_host=192.168.1.3 node4 ansible_host=192.168.1.4 node5 ansible_host=192.168.1.5 3. 在 es_cluster 目录下创建一个名为 es.yml 的文件,用于定义 Ansible playbook。 --- - hosts: es_nodes become: true tasks: - name: Install Java apt: name: openjdk-8-jdk state: present tags: java - name: Install Elasticsearch apt: deb: https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.2-amd64.deb state: present tags: elasticsearch - name: Configure Elasticsearch copy: src: elasticsearch.yml dest: /etc/elasticsearch/elasticsearch.yml owner: elasticsearch group: elasticsearch mode: 0644 tags: elasticsearch 4. 在 es_cluster 目录下创建一个名为 elasticsearch.yml 的文件,用于定义 ES 配置文件。 cluster.name: my_cluster node.name: "{{ inventory_hostname }}" network.host: 0.0.0.0 discovery.seed_hosts: ["node1", "node2", "node3", "node4", "node5"] cluster.initial_master_nodes: ["node1"] 5. 使用以下命令运行 Ansible playbook: ansible-playbook es.yml -i hosts --tags java,elasticsearch 这将安装 Java 和 Elasticsearch,并将 Elasticsearch 配置文件复制到每个节点上。 希望这个示例能够帮助到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值