docker镜像下载
docker pull elasticsearch:7.6.2
启动
docker run -d -p 9200:9200 -p 9300:9300 --name elasticsearch elasticsearch:7.6.2
查看日志
docker logs -f elasticsearch
启动异常解决方法
- max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
elasticsearch用户拥有的内存权限太小,至少需要262144
执行命令
sysctl -w vm.max_map_count=262144
查看
sysctl -a|grep vm.max_map_count
如果重启虚拟机将失效,在 /etc/sysctl.conf文件最后添加一行
vm.max_map_count=262144
2.ERROR: [1] bootstrap checks failed
the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
docker run -d -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 --name elasticsearch elasticsearch:7.6.2
复制并修改配置
copy容器中的文件,到宿主机上
docker cp elasticsearch:/usr/share/elasticsearch/config/ /opt/elasticsearch/
docker cp elasticsearch:/usr/share/elasticsearch/data/ /opt/elasticsearch/
docker cp elasticsearch:/usr/share/elasticsearch/logs/ /opt/elasticsearch/
docker cp elasticsearch:/usr/share/elasticsearch/plugins/ /opt/elasticsearch/
删除容器
docker rm -f elasticsearch
在elasticsearch.yml的文件末尾加上:
http.cors.enabled: true
http.cors.allow-origin: "*"
安装 IK 分词器
下载地址:
https://github.com/medcl/elasticsearch-analysis-ik/
对应的7.6.2
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip
解压到 /opt/elasticsearch/plugins/elasticsearch-analysis-ik-7.6.2 目录
配置
##修改配置,解决跨域访问问题
http.cors.enabled: true
http.cors.allow-origin: "*"
启动
docker run -d -e "discovery.type=single-node" \
-v /opt/elasticsearch/config:/usr/share/elasticsearch/config/ \
-v /opt/elasticsearch/data/:/usr/share/elasticsearch/data/ \
-v /opt/elasticsearch/logs/:/usr/share/elasticsearch/logs/ \
-v /opt/elasticsearch/plugins/:/usr/share/elasticsearch/plugins/ \
-e TZ='Asia/Shanghai' \
-e LANG="en_US.UTF-8" \
--restart=always -p 9200:9200 -p 9300:9300 --name elasticsearch elasticsearch:7.6.2
xpack 破解
进入容器:
docker exec -it elasticsearch /bin/bash
cd /usr/share/elasticsearch/modules/x-pack-core
创建两文件 XPackBuild.java 和 LicenseVerifier.java
XPackBuild.java
package org.elasticsearch.xpack.core;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.io.PathUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
public class XPackBuild {
public static final XPackBuild CURRENT;
static {
CURRENT = new XPackBuild("Unknown", "Unknown");
}
/**
* Returns path to xpack codebase path
*/
@SuppressForbidden(reason = "looks up path of xpack.jar directly")
static Path getElasticsearchCodebase() {
URL url = XPackBuild.class.getProtectionDomain().getCodeSource().getLocation();
try {
return PathUtils.get(url.toURI());
} catch (URISyntaxException bogus) {
throw new RuntimeException(bogus);
}
}
private String shortHash;
private String date;
XPackBuild(String shortHash, String date) {
this.shortHash = shortHash;
this.date = date;
}
public String shortHash() {
return shortHash;
}
public String date() {
return date;
}
}
LicenseVerifier.java
package org.elasticsearch.license;
public class LicenseVerifier {
public static boolean verifyLicense(final License license, byte[] publicKeyData) {
return true;
}
public static boolean verifyLicense(final License license) {
return true;
}
}
编译
/usr/share/elasticsearch/jdk/bin/javac -cp "/usr/share/elasticsearch/modules/x-pack-core/x-pack-core-7.6.0.jar:/usr/share/elasticsearch/lib/*" XPackBuild.java
/usr/share/elasticsearch/jdk/bin/javac -cp "/usr/share/elasticsearch/modules/x-pack-core/x-pack-core-7.6.2.jar:/usr/share/elasticsearch/lib/*" LicenseVerifier.java
生成
XPackBuild.class
LicenseVerifier.class
替换文件
查询文件位置
/usr/share/elasticsearch/jdk/bin/jar -tvf x-pack-core-7.6.2.jar | grep XPackBuild.class
文件位置:
org/elasticsearch/xpack/core/XPackBuild.class
/usr/share/elasticsearch/jdk/bin/jar -tvf x-pack-core-7.6.2.jar | grep LicenseVerifier.class
文件位置:
org/elasticsearch/license/LicenseVerifier.class
创建目录
mkdir -p org/elasticsearch/xpack/core
mkdir -p org/elasticsearch/license
把编译好的文件放入目录
mv XPackBuild.class org/elasticsearch/xpack/core
mv LicenseVerifier.class org/elasticsearch/license
替换jar中的文件
/usr/share/elasticsearch/jdk/bin/jar -uvf x-pack-core-7.6.2.jar org/elasticsearch/license/LicenseVerifier.class
/usr/share/elasticsearch/jdk/bin/jar -uvf x-pack-core-7.6.2.jar org/elasticsearch/xpack/core/XPackBuild.class
退出容器
exit
修改配置/opt/elasticsearch/config/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
transport.host: 0.0.0.0
重新启动并进入容器设置密码
重启
docker restart elasticsearch
进入容器
docker exec -it elasticsearch /bin/bash
设置密码
./bin/elasticsearch-setup-passwords interactive
[root@517c5a4ce3be elasticsearch]# ./bin/elasticsearch-setup-passwords interactive
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
访问
http://ip:9200/
输入用户密码