• 生活的道路一旦选定,就要勇敢地走到底,决不回头。——左拉
  • 坚强的信心,能使平凡的人做出惊人的事业。——马尔顿
  • 人不可有傲气,但不可无傲骨。 --徐悲鸿
  • 古之立大志者,不惟有超世之才,亦必有坚韧不拔之志。 --苏轼
  • 时间像海绵里的水,只要你愿意挤,总还是有的。 --鲁迅

ansible实现elasticsearch集群并用kibana关联

技术交流 zkinogg 1年前 (2020-08-11) 164次浏览 0个评论

一.环境准备

这里三台db主机必须先把内存调至2G

主机名 ip 端口 服务
m01 10.0.0.61 22 ansible管理端
db04 10.0.0.54 9200,5601 elasticsearch,kibana
db05 10.0.0.55 9200 elasticsearch
db06 10.0.0.56 9200 elasticsearch

二. 实现步骤

# 0.发送密钥
[root@m01 ~]# ssh-keygen
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.54
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.55
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.56

# 1.准备elasticsearch的roles目录
[root@m01 ~]# mkdir ansible_elasticsearch
[root@m01 ~]# cd ansible_elasticsearch/
[root@m01 ansible_elasticsearch]# ansible-galaxy init elasticsearch

# 2.准备hosts文件
[root@m01 ansible_elasticsearch]# cat hosts 
[db_group]
db04 ansible_ssh_host=172.16.1.54
db05 ansible_ssh_host=172.16.1.55
db06 ansible_ssh_host=172.16.1.56

# 3.准备site.yml文件
[root@m01 ansible_elasticsearch]# cat site.yml 
- hosts: all
  roles:
    - { role: elasticsearch , when: ansible_fqdn is match 'db*'}

# 4.用jinjia模板准备elasticsearch的配置文件
[root@m01 elasticsearch]# cat templates/elasticsearch.yml.j2 
cluster.name: es-cluster
path.data: /service/es/data
path.logs: /service/es/logs
bootstrap.memory_lock: true
{% if ansible_fqdn == 'db04' %}
node.name: node-1
network.host: 10.0.0.54,127.0.0.1
{% elif ansible_fqdn == 'db05' %}
node.name: node-2
network.host: 10.0.0.55,127.0.0.1
{% else %}
node.name: node-3
network.host: 10.0.0.56,127.0.0.1
{% endif %}
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.54", "10.0.0.55","10.0.0.56"]
discovery.zen.minimum_master_nodes: 2

# 5.准备文件
[root@m01 elasticsearch]# ll files/
total 458224
-rw-r--r-- 1 root root 114059630 Aug  9 18:54 elasticsearch-6.6.0.rpm  # Es的rpm包
-rw-r--r-- 1 root root      1703 Aug 11 15:17 elasticsearch.service    # Es的systemd启动脚本
-rw-r--r-- 1 root root 170023183 Aug  9 18:54 jdk-8u181-linux-x64.rpm  # java环境rpm包
-rw-r--r-- 1 root root 185123116 Aug  9 18:54 kibana-6.6.0-x86_64.rpm  # kibana的rpm包
-rw-r--r-- 1 root root       190 Aug 11 15:47 kibana.yml               # kibana配置文件

[root@m01 elasticsearch]# cat files/kibana.yml 
#进程的端口
server.port: 5601
#监听地址
server.host: "10.0.0.54"
#指定ES的地址
elasticsearch.hosts: ["http://10.0.0.54:9200"]
#kibana也会创建索引
kibana.index: ".kibana"

[root@m01 elasticsearch]# cat files/elasticsearch.service 
[Unit]
Description=Elasticsearch
Documentation=http://www.elastic.co
Wants=network-online.target
After=network-online.target

[Service]
RuntimeDirectory=elasticsearch
PrivateTmp=true
Environment=ES_HOME=/usr/share/elasticsearch
Environment=ES_PATH_CONF=/etc/elasticsearch
Environment=PID_DIR=/var/run/elasticsearch
EnvironmentFile=-/etc/sysconfig/elasticsearch
LimitMEMLOCK=infinity

WorkingDirectory=/usr/share/elasticsearch

User=elasticsearch
Group=elasticsearch

ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet

# StandardOutput is configured to redirect to journalctl since
# some error messages may be logged in standard output before
# elasticsearch logging system is initialized. Elasticsearch
# stores its logs in /var/log/elasticsearch and does not use
# journalctl by default. If you also want to enable journalctl
# logging, you can simply remove the "quiet" option from ExecStart.
StandardOutput=journal
StandardError=inherit

# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=65536

# Specifies the maximum number of processes
LimitNPROC=4096

# Specifies the maximum size of virtual memory
LimitAS=infinity

# Specifies the maximum file size
LimitFSIZE=infinity

# Disable timeout logic and wait until process is stopped
TimeoutStopSec=0

# SIGTERM signal is used to stop the Java process
KillSignal=SIGTERM

# Send the signal only to the JVM rather than its control group
KillMode=process

# Java process is never killed
SendSIGKILL=no

# When a JVM receives a SIGTERM signal it exits with code 143
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

# Built for packages-6.6.0 (packages)


# 6.编写Elasticsearch的tasks
[root@m01 elasticsearch]# cat tasks/Yum_ES.yml 
- name: Yum ntpdate                      # 安装时间同步命令
  yum:
    name: ntpdate
    state: present
  when: ansible_fqdn is match 'db*'

- name: ntpdate aliyun time               # 三台机器同步阿里云时间
  shell: 'ntpdate time1.aliyun.com'
  when: ansible_fqdn is match 'db*'

- name: Push java rpm                       # 推java安装包
  copy:
    src: jdk-8u181-linux-x64.rpm
    dest: /root/jdk-8u181-linux-x64.rpm
  when: ansible_fqdn is match 'db*'

- name: Yum java rpm                        # 本地安装java
  yum:
   name: /root/jdk-8u181-linux-x64.rpm
   state: present
  when: ansible_fqdn is match 'db*'

- name: Push elasticsearch rpm              # 推elasticsearch包
  copy:
    src: elasticsearch-6.6.0.rpm
    dest: /root/elasticsearch-6.6.0.rpm
  when: ansible_fqdn is match 'db*'

- name: Yum elasticsearch rpm               # 本地安装elasticsearch
  yum:
    name: /root/elasticsearch-6.6.0.rpm
    state: present
  when: ansible_fqdn is match 'db*'

- name: reload systemd                      # 按照安装提示步骤执行命令
  shell: 'systemctl daemon-reload'
  when: ansible_fqdn is match 'db*'

- name: service start                       # 按照安装提示步骤执行命令
  service:
     name: elasticsearch.service
     state: started
     enabled: yes
  when: ansible_fqdn is match 'db*'

- name: Push elasticsearch config           # 推Es主配置文件
  template:
    src: elasticsearch.yml.j2
    dest: /etc/elasticsearch/elasticsearch.yml
  when: ansible_fqdn is match 'db*'

- name: mkdir config directory              # 根据配置文件内容创建对应目录并授权
  file:
    path: /service/es/{ item }
    owner: elasticsearch
    group: elasticsearch
    state: directory
    recurse: yes
  with_items: 
    - data
    - logs
  when: ansible_fqdn is match 'db*'

- name: Push systemd start config           # 推systemd启动脚本(添加内存锁)
  copy:
    src: elasticsearch.service
    dest: /usr/lib/systemd/system/elasticsearch.service
    mode: 0755
  when: ansible_fqdn is match 'db*'

- name: reload systemd                      # 刷新systemd守护进程
  shell: 'systemctl daemon-reload'
  when: ansible_fqdn is match 'db*'

- name: service start                       # 启动服务并添加开机自启
  service: 
     name: elasticsearch.service
     state: restarted
     enabled: yes
  when: ansible_fqdn is match 'db*'



# 7.编写kibana的tasks
[root@m01 elasticsearch]# cat tasks/Yum_kibana.yml 
- name: Push kibana rpm                     # 推kibana的安装包
  copy:
    src: kibana-6.6.0-x86_64.rpm
    dest: /root/kibana-6.6.0-x86_64.rpm
  when: ansible_fqdn == 'db04'

- name: Yum kibana rpm                      # 本地安装kibana
  yum:
    name: /root/kibana-6.6.0-x86_64.rpm
    state: present
  when: ansible_fqdn == 'db04'

- name: Push kibana config                  # 推kibana配置文件
  copy:
    src: kibana.yml
    dest: /etc/kibana/kibana.yml
  when: ansible_fqdn == 'db04'

- name: start kibana                        # 启动kibana并添加开机自启
  service:
    name: kibana.service
    state: started
    enabled: yes
  when: ansible_fqdn == 'db04'

三.执行并测试访问

[root@m01 ansible_elasticsearch]# ansible-playbook site.yml -i hosts 

aOmoGV.png

aOm8C6.png


极客公园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ansible实现elasticsearch集群并用kibana关联
喜欢 (0)
[17551054905]
分享 (0)

您必须 登录 才能发表评论!