最近想学一手elasticsearch,但是如果在本机上搭建环境就太麻烦了,要用的时候得启动3个服务,不用的时候又特别占内存,所以想利用最近购买的云服务器搭建一个学习环境。
最终目的是将elasticsearch,kibana以及elasticsearch-head通过docker部署在云服务器上,然后开放三个端口到公网上用来访问kibana,elasticsearch和elasticsearch-head。
阿里云服务器1核2G(宿舍左右室8人集资35元巨款买来运行脚本填学校的晨午晚检,“忘记填写还要被通报批评”)
阿里云安全组开放9200,9100以及5601端口,这些端口分别是elasticsearch,elasticsearch-head以及kibana的服务端口。
当然你有自己的映射规则也行,这样更安全。
如果linux有防火墙,记得要开放通行这些端口。
kibana和elasticsearch的版本必须对应,所以我使用最新版8.1.2,虽然8.1.3版已经出来了,但是好像官方还没有推出docker版。
docker network create somenetwork
# 创建一个docker网络,用于elasticsearch集群和kibanna之间的通信,如果没有指定子网和网关,docker会给你分配的,下面是我的somenetwork网络
#注意somenetwork名字可以是别的
[root@iZwz95y0pmhiy0e077sn8uZ elasticsearch-head]# docker network inspect somenetwork
[
{
"Name": "somenetwork",
"Id": "35ce74cd24fbf884bd4ea5979f81b656a090b7f32b527600476745c231828f04",
"Created": "2022-04-21T21:17:33.039072147+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.19.0.0/16",
"Gateway": "172.19.0.1" //网关
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"2900c35039641428fe9c888b328438967bcd550d33beda91c06e3e9b9323347b": {
"Name": "elasticsearch",
"EndpointID": "41097f98474703d7dab26babab9b13533df2cd2ad3e2ad757ff4ec493bbd2868",
"MacAddress": "02:42:ac:13:00:02",
"IPv4Address": "172.19.0.2/16", //这个是elasticsearch服务的ip
"IPv6Address": ""
},
"bbc29999009c9caad1fd5f4987f979b3c8bbe63ee37e7e9a98764165deb7451f": {
"Name": "kibana",
"EndpointID": "fbaf12946f8f0867ed6d7efab8f94a61384d09ec137329f74d2799992bde3cda",
"MacAddress": "02:42:ac:13:00:03", //这个是kibana的服务ip
"IPv4Address": "172.19.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
docker run -d --name MD --net somenetwork -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:8.1.2
#tag这里得用8.1.2,注意官方没有设置latest标签,所以你直接docker pull elasticsearch会报错,必须指定tag
#Elasticsearch非常消耗内存,ES_JAVA_OPTS用于指定JVM的配置,初始分配堆内存64MB,最大可分配堆内存512MB,不然直接启动服务器可能就寄了
#运行完上面的命令后需要配置一下elasticsearch.yml文件
docker exec -it 容器MD的id /bin/bash
cd /usr/share/elasticsearch/config
vi elasticsearch.yml
#添加跨域访问,以及允许非https请求
#将这个的值修改为flase,以允许非https请求,注意分号后有个空格
xpack.security.enabled: false
#再在文件末尾添加啊
http.cors.enabled: true
http.cors.allow-origin: "*"
#这里我允许了任何来源的ip访问,这里这么配置就行了
#重新启动elasticsearch
docker restart 容器MD的id
[root@iZwz95y0pmhiy0e077sn8uZ elasticsearch-head]# curl localhost:9200
{
"name" : "2900c3503964",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "pF32VtsnRfGXUJfEQU18wA",
"version" : {
"number" : "8.1.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "31df9689e80bad366ac20176aa7f2371ea5eb4c1",
"build_date" : "2022-03-29T21:18:59.991429448Z",
"build_snapshot" : false,
"lucene_version" : "9.0.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
[root@iZwz95y0pmhiy0e077sn8uZ elasticsearch-head]# curl 172.19.0.2:9200
{
"name" : "2900c3503964",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "pF32VtsnRfGXUJfEQU18wA",
"version" : {
"number" : "8.1.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "31df9689e80bad366ac20176aa7f2371ea5eb4c1",
"build_date" : "2022-03-29T21:18:59.991429448Z",
"build_snapshot" : false,
"lucene_version" : "9.0.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
#注意这里请求172.19.0.2会查询路由表,然后匹配第四条,0.0.0.0网关地址代表本机地址,求情会转给br-35ce74cd24fb网卡,这块网卡是通过veth-pair技术创建的虚拟网卡,对应的一块接收网卡就是172.19.0.1,通过这个docker维护的网关就可以联系到somenetwork网络。
[root@iZwz95y0pmhiy0e077sn8uZ elasticsearch-head]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default _gateway 0.0.0.0 UG 100 0 0 eth0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-957a8182791f
172.19.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-35ce74cd24fb
172.28.96.0 0.0.0.0 255.255.240.0 U 0 0 0 br-aa63fc7683b0
172.28.96.0 0.0.0.0 255.255.240.0 U 100 0 0 eth0
192.168.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-1453f1839b5d
[root@iZwz95y0pmhiy0e077sn8uZ elasticsearch-head]# ip addr
229: veth70a2786@if228: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-35ce74cd24fb state UP group default
link/ether 26:4e:3e:4e:76:64 brd ff:ff:ff:ff:ff:ff link-netnsid 3
inet6 fe80::244e:3eff:fe4e:7664/64 scope link
valid_lft forever preferred_lft forever
#还有一个注意点,就是通过docker network create创建网络并且自己来指定子网和网关,这时候要注意所创建的网络是否会与主机的网络发生冲突,这种冲突是可能发生的,通过route命令可以检查路由表来确定是否发生冲突。
docker run -d --net somenetwork -e ELASTICSEARCH_URL=http://172.19.0.2:9200 --name kibana --restart=always -p 5601:5601 kibana:8.1.2 #ELASTICSEARCH_URL用于指定elasticsearch的IP地址
#通过公网等录kibana需要配置一些东西,这里忘记截图了,首先要你输入enrollment-token,这个先不管,点check config(好像是这个),然后输入es在somenetwork中的网址,我这里是http://172.19.0.2:9200,注意不能用https,然后会提示你去kibana中运行一个脚本,这个脚本是bat,肯定不是linux的,截取.bat前面的命令就行,如下:
docker exec -it -u root kibana /bin/bash
./bin/xxxxxx(忘记了)
#这时候会出现一个验证码,把这个验证码通过网页提交就行了
docker run -d --net somenetwork -v /usr/src/app/_site:/usr/src/app/_site -p 9100:9100 --name eh5 mobz/elasticsearch-head:5
#这里我将容器_site目录挂载到本机对应目录上,因为要做一些修改
docker stop eh5
cd /usr/src/app/_site
#编辑vendor.js 共有两处
#6886行 contentType: "application/x-www-form-urlencoded,改成:contentType: "application/json;charset=UTF-8"
#7574行 var inspectData = s.contentType === "application/x-www-form-urlencoded" &&,改成:var inspectData = s.contentType === "application/json;charset=UTF-8" &&
#使用vim,在命令模式输入数字,再按两次g键就可以跳转到对应行
docker start eh5