docker安装elasticsearch伪分布式集群
0.在安装es之前,首先编辑一下宿主机的内核参数,否则配置es集群的时候极有可能启动不成功:
[[email protected] ~]# vim /etc/sysctl.conf
vm.max_map_count=655360
[[email protected] ~]# sysctl -p # 加载参数
1.搜索es的镜像,如下第一个就是官方镜像:
2.拉取镜像:
[[email protected] ~]# docker pull elasticsearch
3.启动镜像为容器:
[[email protected] ~]# docker run -d --name es1 -p 9200:9200 -p 9300:9300 elasticsearch
使用浏览器访问9200端口,看看是否正常返回如下json数据:
能返回图中的json数据代表已经启动成功,接着再启动两个节点:
[[email protected] ~]# docker run -d --name es2 -p 9201:9200 -p 9301:9300 elasticsearch
[[email protected] ~]# docker run -d --name es3 -p 9202:9200 -p 9302:9300 elasticsearch
启动成功后,此时会有三个实例正在运行:
4.由于我们需要编辑配置文件来完成es伪分布式集群的配置,但该容器默认不自带vi或vim命令,所以我们还得先进入到容器里,将所有的实例都安装上vim命令,示例如下:
[[email protected] ~]# docker exec -it 2dc233622dcb bash
[email protected]:/usr/share/elasticsearch# apt-get update
[email protected]:/usr/share/elasticsearch# apt-get install vim -y
5.我们将es1作为master节点,剩余的es2和es3作为slave节点,首先编辑es1的配置文件内容如下:
[email protected]:/usr/share/elasticsearch# vim config/elasticsearch.yml # 在文件末尾加入如下内容
# 开启跨域,为了让es-head可以访问
http.cors.enabled: true
http.cors.allow-origin: "*"
# 集群的名称
cluster.name: es
# 节点的名称
node.name: master
# 是否为主节点
node.master: true
# 是否为数据节点
node.data: false
# 允许任何ip访问
network.host: 0.0.0.0
# 通过这个ip列表进行节点发现,我这里配置的是各个容器的ip
discovery.zen.ping.unicast.hosts: ["127.0.0.1","172.17.0.2","172.17.0.4","172.17.0.5"]
[email protected]:/usr/share/elasticsearch#
es2和es3的配置文件配置的内容则如下:
-----------------------es2-----------------------
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: es
node.name: slave1
network.host: 0.0.0.0
node.master: false
node.data: true
discovery.zen.ping.unicast.hosts: ["127.0.0.1","172.17.0.2","172.17.0.4","172.17.0.5"]
-----------------------es3-----------------------
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: es
node.name: slave2
network.host: 0.0.0.0
node.master: false
node.data: true
discovery.zen.ping.unicast.hosts: ["127.0.0.1","172.17.0.2","172.17.0.4","172.17.0.5"]
6.配置好后,重启所有的es实例容器:
[[email protected] ~]# docker restart 2dc233622dcb
[[email protected] ~]# docker restart 81d5bcb5394a
[[email protected] ~]# docker restart e354f31b7785
7.重启完成之后,执行如下命令确认一下集群状况:
[[email protected] ~]# curl ‘192.168.190.129:9200/_cluster/health?pretty‘
{
"cluster_name" : "es",
"status" : "green", # 为green则代表健康没问题,如果是yellow或者red则是集群有问题
"timed_out" : false, # 是否有超时
"number_of_nodes" : 3, # 集群中的节点数量
"number_of_data_nodes" : 2, # 集群中data节点的数量
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
[[email protected] ~]#
安装es-head可视化插件
1.这一小节我们来安装es-head,同样的,先在镜像仓库中搜索相应的镜像:
2.拉取镜像:
docker pull mobz/elasticsearch-head:5
3.启动镜像为容器:
[[email protected] ~]# docker run -d --name es-head -p 9100:9100 mobz/elasticsearch-head:5
4.同样需要进入到容器里安装一下vim命令:
[[email protected] ~]# docker exec -it 85f03139f1ba bash
[email protected]:/usr/src/app# apt-get update
[email protected]:/usr/src/app# apt-get install vim -y
如果执行apt-get update时,报如下错误的话:
W: Failed to fetch http://deb.debian.org/debian/dists/jessie/main/binary-amd64/Packages Hash Sum mismatch
E: Some index files failed to download. They have been ignored, or old ones used instead.
则需要更换安装源,由于没有vi及vim命令,只能使用echo来重定向了,如下示例:
[email protected]:/usr/src/app# mv /etc/apt/sources.list /etc/apt/sources.list.bak
[email protected]:/usr/src/app# echo -e "deb http://mirrors.aliyun.com/debian wheezy main contrib non-free\ndeb-src http://mirrors.aliyun.com/debian wheezy main contrib non-free\ndeb http://mirrors.aliyun.com/debian wheezy-updates main contrib non-free\ndeb-src http://mirrors.aliyun.com/debian wheezy-updates main contrib non-free\ndeb http://mirrors.aliyun.com/debian-security wheezy/updates main contrib non-free\ndeb-src http://mirrors.aliyun.com/debian-security wheezy/updates main contrib non-free" > /etc/apt/sources.list
[email protected]:/usr/src/app#
5.es-head使用的是grunt server作为服务器,而grunt server默认监听的是localhost,所以我们需要修改Gruntfile.js文件,增加一段配置,不然外部是无法访问的,如下:
[email protected]:/usr/src/app# vim Gruntfile.js
connect: {
server: {
options: {
hostname: ‘0.0.0.0‘, # 增加这段
port: 9100,
base: ‘.‘,
keepalive: true
}
}
}
[email protected]:/usr/src/app#
6.配置好后,重启容器:
[[email protected] ~]# docker restart 85f03139f1ba
7.使用浏览器访问9100端口,访问结果如下则是正常的,可以看到es集群的状态:
原文地址:http://blog.51cto.com/zero01/2285604
时间: 2024-10-11 10:22:50