ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

在docker容器中操作es,并给es7.6添加用户鉴权设置密码

2022-02-08 17:02:35  阅读:339  来源: 互联网

标签:容器 es7.6 log4j elasticsearch org docker 鉴权 es


1、docker启动命令:docker-compose up -d

加上 -d 表示后台运行,只是docker-compose up则是在控制台运行,会不停的打印日志。

2、从容器中拷贝文件到宿主机:sudo docker cp es01:/usr/share/elasticsearch/elastic-certificates.p12 “/mnt/test-solit/es/es138”

官方格式为:docker cp [OPTIONS] CONTAINER:SRC_PATH
DEST_PATH|-CONTAINER为容器名或者容器id这里注意:后紧跟路径,无需空格,否则拷贝不成功*

3、 停止docker中所有容器:docker stop $(docker ps -aq)

4、 进入容器:sudo docker exec -it e154e3b06d4b /bin/bash

其中e154e3b06d4b为容器id
可通过docker ps 查看容器信息

5、 es加用户鉴权:

https://www.cnblogs.com/woshimrf/p/docker-es7.html

在这里注意elasticsearch.yml为es配置文件,如果es在docker中运行,可直接从官网拉镜像文件。

自己的配置文件放在宿主机某目录下,docker-compose.yml文件可配置映射:
配置映射

这是由于docker中的容器在启动的时候也许会清空某些文件,不稳定,使用映射则有保障。

其中注意生成密钥文件后,需要查看文件权限,其他用户需要读®权限:chmod 644 elastic-certificates.p12

6、 验证es加了用户鉴权后可先测试:curl http://本服务器ip:9200

若报错表示需要用户密码连接: curl http://本服务器ip:9200 -u user:password

7、 docker中kibana设置连接es,先进入容器,进入config目录(/usr/share/kibana/config),在kibana.yml配置文件中新增

elasticsearch.username: "elastic"
elasticsearch.password: "yourpassword"

kibana分词工具配置

8、 java连接开启用户鉴权的es:

配置文件新增:spring.elasticsearch.rest
java连接已开启用户鉴权的es
同时,需要在java中新增esConfig文件:

@Configuration
public class ESConfig extends AbstractElasticsearchConfiguration {

    @Autowired
    ElasticsearchRestClientProperties elasticsearchRestClientProperties;
    @Override
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(elasticsearchRestClientProperties.getUris().get(0))
                .withBasicAuth(elasticsearchRestClientProperties.getUsername(), elasticsearchRestClientProperties.getPassword())
                .withSocketTimeout(Duration.ofSeconds(15000))
                .build();
        return RestClients.create(clientConfiguration).rest();
    }
}

9、 es启动失败,错误码为78

elasticsearch exited with code 78

分析:虚拟内存不足,需要重新设置
解决方法:sudo sysctl -w vm.max_map_count=262144 (一次性解决方法,若虚拟机重启则参数失效)

解决方法二:在 /etc/sysctl.conf文件最后一行添加配置:
vm.max_map_count=262144

10、 elasticsearch7.6.2 修复log4j漏洞:

解决方法一:es运行在docker容器中,先进入es容器,在/usr/share/elasticsearch/config/jvm.options文件中添加

-Dlog4j2.formatMsgNoLookups=true

解决方法二:升级log4j的版本,强制升级至2.16.0

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.16.0</version>
</dependency>

<!-- 辅助依赖配置 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>4.0.0.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

标签:容器,es7.6,log4j,elasticsearch,org,docker,鉴权,es
来源: https://blog.csdn.net/qq_39753413/article/details/122826488

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有