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

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>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值