使用yum安装Elasticsearch
- 下载并安装ES的yum公钥
[root@es1 ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
- 配置Elasticsearch的yum源
[root@es1 ~]# vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
[root@es1 ~]# yum clean all
[root@es1 ~]# yum repolist
[root@es1 ~]# yum makecache
[root@es1 ~]# yum -y install elasticsearch
[root@es1 ~]# vim +54 /etc/elasticsearch/elasticsearch.yml
原为: network.host: 192.168.0.1
改为: network.host: 0.0.0.0
[root@es1 ~]# systemctl start elasticsearch.service
[root@es1 ~]# systemctl enabled elasticsearch.service
[root@es1 ~]# ss -nutlp | grep java
tcp LISTEN 0 50 :::9200 ::😗 users:((“java”,pid=910,fd=109))
tcp LISTEN 0 50 :::9300 ::😗 users:((“java”,pid=910,fd=91))
[root@es1 ~]# curl 192.168.1.11:9200
{
“name” : “Geb”,
“cluster_name” : “elasticsearch”,
“cluster_uuid” : “tSfRijFeQaaOFVRWckJ8GA”,
“version” : {
“number” : “2.4.6”,
“build_hash” : “5376dca9f70f3abef96a77f4bb22720ace8240fd”,
“build_timestamp” : “2017-07-18T12:17:44Z”,
“build_snapshot” : false,
“lucene_version” : “5.5.4”
},
“tagline” : “You Know, for Search”
浏览器中输入ip地址和端口访问试试,如果访问被拒绝,添加防火墙设置(我的阿里云服务器是需要设置的)
[root@es1 ~]# iptables -I INPUT -p tcp --dport 9200 -j ACCEPT
利用ansible-playbook批量部署elasticsearch集群
主机名 | IP | 规划角色 |
---|---|---|
es1 | 192.168.1.11 | 管理机 |
es2 | 192.168.1.12 | 集群node |
es3 | 192.168.1.13 | 集群node |
es4 | 192.168.1.14 | 集群node |
es5 | 192.168.1.15 | 集群node |
前提条件:
- 管理机安装ansible
- 创建好公钥和私钥并传给集群所有机器,能免密登入
- 确定所有机器时间同步
[root@es1 elasticsearch]# tree elasticsearch
elasticsearch
├── ansible.cfg
├── Centos-7.repo
├── elasticsearch.repo
├── elasticsearch.yml
├── essetup.retry
├── essetup.yml
├── hosts
└── myhost
[root@es1 elasticsearch]# vim ansible.cfg
[defaults]
inventory = myhost
host_key_checking = False
[root@es1 elasticsearch]# vim Centos-7.repo
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[root@es1 elasticsearch]# vim elasticsearch.repo
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
[root@es1 elasticsearch]# vim elasticsearch.yml
修改下面四行:
cluster.name: myelk
node.name: {{ansible_hostname}}
network.host: 0.0.0.0
discovery.zen.ping.unicast.hosts: [“es1”, “es2”, “es3”]
[root@es1 elasticsearch]# vim essetup.yml
---
- hosts: es
remote_user: root
tasks:
- copy:
src: "{{ item.src }}"
dest: /etc/yum.repos.d/
owner: root
group: root
mode: 0644
with_items:
- {src: Centos-7.repo}
- {src: elasticsearch.repo}
- copy:
src: /etc/hosts
dest: /etc/hosts
owner: root
group: root
mode: 0644
- name: install elasticsearch
yum:
name: java-1.8.0-openjdk,elasticsearch
state: installed
- template:
src: elasticsearch.yml
dest: /etc/elasticsearch/elasticsearch.yml
owner: root
group: root
mode: 0644
notify: reload elasticsearch
tags: esconf
- service:
name: elasticsearch
enabled: yes
handlers:
- name: reload elasticsearch
service:
name: elasticsearch
state: restarted
[root@es1 elasticsearch]# vim hosts
# ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.1.11 es1
192.168.1.12 es2
192.168.1.13 es3
192.168.1.14 es4
192.168.1.15 es5
[root@es1 elasticsearch]# vim myhost
[es]
192.168.1.[11:15]
[root@es1 elasticsearch]# ansible-playbook essetup.yml
PLAY [es] ************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [192.168.1.11]
ok: [192.168.1.15]
ok: [192.168.1.14]
ok: [192.168.1.12]
ok: [192.168.1.13]
TASK [copy] **********************************************************************************************************************************************************************************
ok: [192.168.1.11] => (item={u’src’: u’Centos-7.repo’})
changed: [192.168.1.12] => (item={u’src’: u’Centos-7.repo’})
changed: [192.168.1.13] => (item={u’src’: u’Centos-7.repo’})
changed: [192.168.1.15] => (item={u’src’: u’Centos-7.repo’})
changed: [192.168.1.14] => (item={u’src’: u’Centos-7.repo’})
ok: [192.168.1.11] => (item={u’src’: u’elasticsearch.repo’})
changed: [192.168.1.12] => (item={u’src’: u’elasticsearch.repo’})
changed: [192.168.1.13] => (item={u’src’: u’elasticsearch.repo’})
changed: [192.168.1.15] => (item={u’src’: u’elasticsearch.repo’})
changed: [192.168.1.14] => (item={u’src’: u’elasticsearch.repo’})
TASK [install elasticsearch] *****************************************************************************************************************************************************************
ok: [192.168.1.11]
changed: [192.168.1.12]
changed: [192.168.1.14]
changed: [192.168.1.15]
changed: [192.168.1.13]
TASK [template] ******************************************************************************************************************************************************************************
changed: [192.168.1.11]
changed: [192.168.1.13]
changed: [192.168.1.14]
changed: [192.168.1.12]
changed: [192.168.1.15]
TASK [service] *******************************************************************************************************************************************************************************
ok: [192.168.1.11]
changed: [192.168.1.12]
changed: [192.168.1.14]
changed: [192.168.1.13]
changed: [192.168.1.15]
RUNNING HANDLER [reload elasticsearch] *******************************************************************************************************************************************************
changed: [192.168.1.11]
changed: [192.168.1.14]
changed: [192.168.1.12]
changed: [192.168.1.13]
changed: [192.168.1.15]
PLAY RECAP ***********************************************************************************************************************************************************************************
192.168.1.11 : ok=6 changed=2 unreachable=0 failed=0
192.168.1.12 : ok=6 changed=5 unreachable=0 failed=0
192.168.1.13 : ok=6 changed=5 unreachable=0 failed=0
192.168.1.14 : ok=6 changed=5 unreachable=0 failed=0
192.168.1.15 : ok=6 changed=5 unreachable=0 failed=0
访问集群网页,确定是否部署成功
[root@elk ~]$ curl firefox http://192.168.1.11:9200/_cluster/health?pretty