因项目需求需要在鲲鹏麒麟服务器上安装ElasticSearch7.8.0,考虑Docker方式安装比较简单,因此使用Docker方式安装
环境信息
操作系统:Kylin Linux Advanced Server release V10 (Tercel)
Docker:18.09.0
[root@server ~]# uname -a
Linux server 4.19.148+ #1 SMP Mon Oct 5 22:04:46 EDT 2020 aarch64 aarch64 aarch64 GNU/Linux
[root@server ~]#
[root@server ~]# cat /etc/kylin-release
Kylin Linux Advanced Server release V10 (Tercel)
[root@server ~]#
[root@server ~]# docker info
Containers: 9
Running: 8
Paused: 0
Stopped: 1
Images: 11
Server Version: 18.09.0
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Hugetlb Pagesize: 2MB
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: commit:93cdc66ff6636a3f8755b614978c5699a8532d20.m
runc version: 8f8796634d4feb9d8a4914d6c090ceaa869cf7b0
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 4.19.148+
Operating System: Kylin Linux Advanced Server V10 (Tercel)
OSType: linux
Architecture: aarch64
CPUs: 32
Total Memory: 62.6GiB
Name: server
ID: 6CY7:ABTW:G2C2:IH3P:GSMU:UOOZ:K6IE:YP7E:DV4F:ST6P:C7FC:EGDW
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://y5ep8mo8.mirror.aliyuncs.com/
Live Restore Enabled: true
WARNING: API is accessible on http://0.0.0.0:2375 without encryption.
Access to the remote API is equivalent to root access on the host. Refer
to the 'Docker daemon attack surface' section in the documentation for
more information: https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
[root@server ~]#
安装包
访问Docker官网https://hub.docker.com/r/amd64/elasticsearch/tags?name=7.8,拉取镜像
docker pull amd64/elasticsearch:7.8.0@sha256:4a65567332214e36b3ad7fcb0c4f00d87f16edba57d0c4f2c7938b6014041ca3
[root@hxh2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
arm64v8/elasticsearch 7.8.0 9522ad0a773f 4 years ago 1.33GB
保存镜像信息到服务器本地
docker save -o elasticsearch-7.8.0-arm64v8.tar arm64v8/elasticsearch:7.8.0
然后下载elasticsearch-7.8.0-arm64v8.tar并传到服务器上
部署
加载镜像
[root@server ~]# docker load -i elasticsearch-7.8.0-arm64v8.tar
[root@server ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
moby/buildkit buildx-stable-1 d3fee711a716 9 days ago 209MB
arm64v8/nginx 1.27.3 e845dff95154 2 weeks ago 204MB
mongo 4.4.29 80d502872ebd 9 months ago 408MB
postgres 13.13-bullseye f17c1bab0868 12 months ago 353MB
arm64v8/mysql latest bdfb0ec4d54a 16 months ago 599MB
nacos/nacos-server v2.1.2-slim e669772aae32 21 months ago 300MB
arm64v8/redis 5.0.14 91a41e726017 2 years ago 104MB
openjdk 8u342-jre 726b45a38636 2 years ago 266MB
seataio/seata-server 1.3.0 9522c77abba9 4 years ago 151MB
arm64v8/elasticsearch 7.8.0 9522ad0a773f 4 years ago 1.33GB
biarms/mysql 5.7.30-linux-arm64v8 4f00dec165f1 4 years ago 266MB
创建实例
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" arm64v8/elasticsearch:7.8.0
检查实例运行情况
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ebdcfb045150 arm64v8/elasticsearch:7.8.0 "/tini -- /usr/local…" 2 hours ago Up 2 hours 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch
eda69cbec370 arm64v8/mysql:latest "docker-entrypoint.s…" 2 days ago Up 2 days 0.0.0.0:3306->3306/tcp, 33060/tcp mysql8
ad62cee77b99 moby/buildkit:buildx-stable-1 "buildkitd" 6 days ago Up 6 days buildx_buildkit_default
a4a5cd6e4fac postgres:13.13-bullseye "docker-entrypoint.s…" 7 days ago Up 7 days 0.0.0.0:5432->5432/tcp postgres
4cef99e68c8c arm64v8/redis:5.0.14 "docker-entrypoint.s…" 7 days ago Up 7 days 0.0.0.0:6379->6379/tcp redis
ec6b852b5ccd arm64v8/nginx:1.27.3 "/docker-entrypoint.…" 8 days ago Up 12 hours 0.0.0.0:80->80/tcp, 0.0.0.0:8080->8080/tcp nginx
c4ef3a8ab738 nacos/nacos-server:v2.1.2-slim "bin/docker-startup.…" 9 days ago Up 9 days 0.0.0.0:8848->8848/tcp nacos
93b30390a04f mongo:4.4.29 "docker-entrypoint.s…" 9 days ago Up 9 days 0.0.0.0:27017->27017/tcp mongo
开放访问
firewall-cmd --zone=public --add-port=9200/tcp --permanent && firewall-cmd --reload
代码测试
增加依赖
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
测试代码
package com.example.es01;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import java.io.IOException;
public class DataTests {
@Test
public void test() throws IOException {
// 创建Elasticsearch客户端
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("10.16.39.121", 9200, "http")));
// 插入数据
String indexName = "test_index";
String typeName = "_doc";
String id = "14";
String jsonString = "{" +
"\"name\":\"John Doe\"," +
"\"age\":30," +
"\"city\":\"China\"" +
"}";
IndexRequest request = new IndexRequest(indexName, typeName, id)
.source(jsonString, XContentType.JSON);
try {
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println("Insert data success: " + response.toString());
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
// 搜索数据
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
SearchRequest searchRequest = new SearchRequest(indexName);
searchRequest.source(sourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("Search data success: " + searchResponse.toString());
} catch (IOException e) {
e.printStackTrace();
}
// 关闭客户端
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行结果
Insert data success: IndexResponse[index=test_index,type=_doc,id=14,version=1,result=created,seqNo=21,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
Search data success: {"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":14,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"test_index","_type":"_doc","_id":"7","_score":1.0,"_source":{"name":"John Doe","age":30,"city":"New York"}},{"_index":"test_index","_type":"_doc","_id":"8","_score":1.0,"_source":{"name":"John Doe","age":30,"city":"China"}},{"_index":"test_index","_type":"_doc","_id":"9","_score":1.0,"_source":{"name":"John Doe","age":30,"city":"China"}},{"_index":"test_index","_type":"_doc","_id":"10","_score":1.0,"_source":{"name":"John Doe","age":30,"city":"China"}},{"_index":"test_index","_type":"_doc","_id":"11","_score":1.0,"_source":{"name":"John Doe","age":30,"city":"China"}},{"_index":"test_index","_type":"_doc","_id":"12","_score":1.0,"_source":{"name":"John Doe","age":30,"city":"China"}},{"_index":"test_index","_type":"_doc","_id":"1","_score":1.0,"_source":{"name":"John Doe","age":30,"city":"New York"}},{"_index":"test_index","_type":"_doc","_id":"2","_score":1.0,"_source":{"name":"John Doe","age":30,"city":"New York"}},{"_index":"test_index","_type":"_doc","_id":"3","_score":1.0,"_source":{"name":"John Doe","age":30,"city":"New York"}},{"_index":"test_index","_type":"_doc","_id":"4","_score":1.0,"_source":{"name":"John Doe","age":30,"city":"New York"}}]}}