k8s集群部署elasticsearch集群和elasticsearch集群设置用户密码

这篇具有很好参考价值的文章主要介绍了k8s集群部署elasticsearch集群和elasticsearch集群设置用户密码。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

一、背景:

二、部署elasticsearch集群:

1、部署elasticsearch集群:

2、验证elasticsearch集群是否正常:

 三、部署elasticsearch集群并设置用户密码

1、生产elastic集群所需的证书:

2、重新建构elasticsearch镜像:

3、部署elasticsearch集群:

4、设置elasticsearch集群的密码:

5、测试验证elasticsearch集群的认证:

总结:

一、背景:

在paas云平台运维维护过程中有遇见项目需要部署一套elasticsearch集群,用于业务系统的使用。根据业务需求,在k8s集群环境中部署一套elasticsearch集群,操作记录参考改文章!!!

二、部署elasticsearch集群:

默认的elasticsearch集群是没有密码的。

1、部署elasticsearch集群:

apiVersion: v1
kind: Service
metadata:
  name: elasticsearch
  namespace: sit
spec:
  selector:
    app: elasticsearch
  type: ClusterIP
  ports:
  - port: 9200
    name: es-9200
    targetPort: 9200
  - port: 9300
    name: es-9300
    targetPort: 9300
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: es7-cluster
  namespace: sit
spec:
  serviceName: elasticsearch
  replicas: 3
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:
      containers:
      - name: elasticsearch
        image: dockerhub.jiang.com/jiang-public/elasticsearch:7.9.3
        resources:
            limits:
              cpu: 1000m
            requests:
              cpu: 100m
        ports:
        - containerPort: 9200
          name: rest
          protocol: TCP
        - containerPort: 9300
          name: inter-node
          protocol: TCP
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data
        env:
          - name: cluster.name
            value: k8s-logs
          - name: node.name
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: discovery.zen.minimum_master_nodes
            value: "2"
          - name: discovery.seed_hosts
            value: "es7-cluster-0.elasticsearch,es7-cluster-1.elasticsearch,es7-cluster-2.elasticsearch"
          - name: cluster.initial_master_nodes
            value: "es7-cluster-0,es7-cluster-1,es7-cluster-2"
          - name: ES_JAVA_OPTS
            value: "-Xms1g -Xmx1g"
      initContainers:
      - name: fix-permissions
        image: dockerhub.jiang.com/system_containers/busybox:latest
        command: ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data"]
        securityContext:
          privileged: true
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data
      - name: increase-vm-max-map
        image: dockerhub.jiang.com/system_containers/busybox:latest
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
        securityContext:
          privileged: true
      - name: increase-fd-ulimit
        image: dockerhub.jiang.com/system_containers/busybox:latest
        command: ["sh", "-c", "ulimit -n 65536"]
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "huawei-san" #根据实际情况而定
      resources:
        requests:
          storage: 1Gi

执行yaml文件:

[root@master-01 xhj]# kubectl apply -f elasticsearch.yaml 
service/elasticsearch created
statefulset.apps/es7-cluster created

验证结果:

[root@master-01 xhj]# k get svc
NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)             AGE
elasticsearch   ClusterIP   172.44.255.234   <none>        9200/TCP,9300/TCP   43s
mysql           ClusterIP   None             <none>        3306/TCP,9125/TCP   48d
[root@master-01 xhj]# k get sts
NAME          READY   AGE
es7-cluster   1/3     47s
[root@master-01 xhj]# k get sts
NAME          READY   AGE
es7-cluster   3/3     2m12s

2、验证elasticsearch集群是否正常:

[root@master-01 xhj]# curl http://172.44.255.234:9200/
{
  "name" : "es7-cluster-2",
  "cluster_name" : "k8s-logs",
  "cluster_uuid" : "VeyRUKdwTHu5lySgV3XJVw",
  "version" : {
    "number" : "7.9.3",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "c4138e51121ef06a6404866cddc601906fe5c868",
    "build_date" : "2020-10-16T10:36:16.141335Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

到此就部署好了一个elasticsearch集群服务!!!!

 三、部署elasticsearch集群并设置用户密码

1、生产elastic集群所需的证书:

使用docker 运行elasticsearch容器

[root@master-01 ~]# docker run -it -d --name es-tls  dockerhub.jiang.com/jxstjh-public/elasticsearch:7.9.3 /bin/bash
3dff79f1de6f7e3ba83db4b1af2d079e20589d57b028156473cba1b3346f6bbb

进入容器内生成证书

[root@master-01 ~]# docker exec -it es-tls /bin/bash
[root@3dff79f1de6f elasticsearch]# pwd
/usr/share/elasticsearch
[root@3dff79f1de6f elasticsearch]# ./bin/elasticsearch-certutil cert -out config/elastic-certificates.p12 -pass ""

 执行结果显示如下:

This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.

The 'cert' mode generates X.509 certificate and private keys.
    * By default, this generates a single certificate and key for use
       on a single instance.
    * The '-multiple' option will prompt you to enter details for multiple
       instances and will generate a certificate and key for each one
    * The '-in' option allows for the certificate generation to be automated by describing
       the details of each instance in a YAML file

    * An instance is any piece of the Elastic Stack that requires an SSL certificate.
      Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
      may all require a certificate and private key.
    * The minimum required value for each instance is a name. This can simply be the
      hostname, which will be used as the Common Name of the certificate. A full
      distinguished name may also be used.
    * A filename value may be required for each instance. This is necessary when the
      name would result in an invalid file or directory name. The name provided here
      is used as the directory name (within the zip) and the prefix for the key and
      certificate files. The filename is required if you are prompted and the name
      is not displayed in the prompt.
    * IP addresses and DNS names are optional. Multiple values can be specified as a
      comma separated string. If no IP addresses or DNS names are provided, you may
      disable hostname verification in your SSL configuration.

    * All certificates generated by this tool will be signed by a certificate authority (CA).
    * The tool can automatically generate a new CA for you, or you can provide your own with the
         -ca or -ca-cert command line options.

By default the 'cert' mode produces a single PKCS#12 output file which holds:
    * The instance certificate
    * The private key for the instance certificate
    * The CA certificate

If you specify any of the following options:
    * -pem (PEM formatted output)
    * -keep-ca-key (retain generated CA key)
    * -multiple (generate multiple certificates)
    * -in (generate certificates from an input file)
then the output will be be a zip file containing individual certificate/key files


Certificates written to /usr/share/elasticsearch/config/elastic-certificates.p12

This file should be properly secured as it contains the private key for 
your instance.

This file is a self contained file and can be copied and used 'as is'
For each Elastic product that you wish to configure, you should copy
this '.p12' file to the relevant configuration directory
and then follow the SSL configuration instructions in the product guide.

将证书文件复制到本地:

[root@master-01 ~]# docker cp es-tls:/usr/share/elasticsearch/config/elastic-certificates.p12 /xhj/elastic-certificates.p12

2、重新建构elasticsearch镜像:

使用证书通过Dockerfile重新封装镜像

FROM dockerhub.jiang.com/jxstjh-public/elasticsearch:7.9.3
MAINTAINER jiang
LABEL maintainer="jiang"
COPY elastic-certificates.p12 /usr/share/elasticsearch/config/
RUN  chown 1000:0 /usr/share/elasticsearch/config/elastic-certificates.p12
EXPOSE 9200 9300
CMD ["eswrapper"]

编译镜像

[root@master-01 xhj]# docker build -t dockerhub.jiang.com/jxstjh-public/elasticsearch:7.9.3-p12 .
Sending build context to Docker daemon  127.5MB
Step 1/7 : FROM dockerhub.jiang.com/jiang-public/elasticsearch:7.9.3
 ---> 1ab13f928dc8
Step 2/7 : MAINTAINER jiang
 ---> Running in beba0ca606a4
Removing intermediate container beba0ca606a4
 ---> 4a4003bf74c9
Step 3/7 : LABEL maintainer="jiang"
 ---> Running in b1e0c1168982
Removing intermediate container b1e0c1168982
 ---> c5eba7158904
Step 4/7 : COPY elastic-certificates.p12 /usr/share/elasticsearch/config/
 ---> 7b4dab67c080
Step 5/7 : RUN  chown 1000:0 /usr/share/elasticsearch/config/elastic-certificates.p12
 ---> Running in cb8f9383fa70
Removing intermediate container cb8f9383fa70
 ---> 071eb2ce173e
Step 6/7 : EXPOSE 9200 9300
 ---> Running in 5ca429e2b39e
Removing intermediate container 5ca429e2b39e
 ---> cfc9b0b05386
Step 7/7 : CMD ["eswrapper"]
 ---> Running in abbf96c90588
Removing intermediate container abbf96c90588
 ---> 9b6304124b9b
Successfully built 9b6304124b9b
Successfully tagged dockerhub.jiang.com/jiang-public/elasticsearch:7.9.3-p12

3、部署elasticsearch集群:

在k8s上部署elasticsearch集群,使用了statefulset控制器部署的。yaml文件参考如下:

apiVersion: v1
kind: Service
metadata:
  name: elastic-svc #这里根据名称而定
  namespace: sit #根据所在命名空间
  labels:
    app: elastic-cluster
spec:
  selector:
    app: elastic-cluster
  type: ClusterIP
  ports:
  - name: rest-api
    port: 9200
    protocol: TCP
    targetPort: 9200
  - name: inter-node
    port: 9300
    protocol: TCP
    targetPort: 9300
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: elastic-cluster #根据实际情况调整
  namespace: sit #根据所在命名空间
  labels:
    app: elastic-cluster
spec:
  serviceName: elastic-svc #要跟上吗的service的名称对齐
  replicas: 3
  selector:
    matchLabels:
      app: elastic-cluster
      kubernetes.io/cluster-service: "true"
  template:
    metadata:
      labels:
        app: elastic-cluster
        kubernetes.io/cluster-service: "true"
    spec:
      initContainers:
      - name: fix-permissions
        image: dockerhub.jiang.com/system_containers/busybox:latest #根据镜像所在位置进行调整
        imagePullPolicy: IfNotPresent
        command: ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data"]
        securityContext:
          privileged: true
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data
        - name: localtime
          readOnly: true
          mountPath: /etc/localtime
      - name: increase-vm-max-map
        image: dockerhub.jiang.com/system_containers/busybox:latest #根据镜像所在位置进行调整
        imagePullPolicy: IfNotPresent
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
        securityContext:
          privileged: true
      - name: increase-fd-ulimit
        image: dockerhub.jiang.com/system_containers/busybox:latest #根据镜像所在位置进行调整
        imagePullPolicy: IfNotPresent
        command: ["sh", "-c", "ulimit -n 65536"]
      volumes:
      - name: localtime
        hostPath:
          path: /etc/localtime
          type: ''
      containers:
      - name: elasticsearch
        image: dockerhub.jiang.com/jiang-public/elasticsearch:7.9.3-p12 #根据镜像所在位置进行调整
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9200
          name: rest-api
          protocol: TCP
        - containerPort: 9300
          name: inter-node
          protocol: TCP
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data
        - name: localtime
          readOnly: true
          mountPath: /etc/localtime
        env:
        - name: node.name
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.name
        - name: discovery.zen.minimum_master_nodes
          value: "2"
        - name: discovery.seed_hosts
          value: "elastic-svc" #要跟上吗的service名称对齐
        - name: cluster.initial_master_nodes
          value: "elastic-cluster-0,elastic-cluster-1,elastic-cluster-2" #根据集群名称进行调整
        - name: ES_JAVA_OPTS
          value: "-Xms1024m -Xmx1024m" #这里根据业务需求而定,要求是要一样的配置大小
        - name: xpack.security.enabled
          value: "true"
        - name: xpack.security.transport.ssl.enabled
          value: "true"
        - name: xpack.security.transport.ssl.verification_mode
          value: "certificate"
        - name: xpack.security.transport.ssl.keystore.path
          value: "elastic-certificates.p12"
        - name: xpack.security.transport.ssl.truststore.path
          value: "elastic-certificates.p12"
  volumeClaimTemplates:   
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "huawei-san" #根据实际情况调整
      resources:
        requests:
          storage: 2Gi

部署Elasticsearch集群

[root@master-01 xhj]# kubectl apply -f elasticsearch-p12.yaml 
service/elastic-svc created
statefulset.apps/elastic-cluster created

验证elasticsearch集群运行情况:

[root@master-01 xhj]# kubectl get svc
NAME          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)             AGE
elastic-svc   ClusterIP   172.41.9.197   <none>        9200/TCP,9300/TCP   36s
mysql         ClusterIP   None           <none>        3306/TCP,9125/TCP   48d
[root@master-01 xhj]# kubectl get sts
NAME              READY   AGE
elastic-cluster   3/3     4m6s

4、设置elasticsearch集群的密码:

进入到任意pod容器操作如下:

# kubectl exec -it -n sit pods/elastic-cluster-0  -- /bin/bash

执行如下的命令 

./bin/elasticsearch-setup-passwords interactive
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,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 #输入yes


Enter password for [elastic]: #输入密码,这里选择的elasticsearch
Reenter password for [elastic]: #再次输入密码,这里选择的elasticsearch
Enter password for [apm_system]: #输入密码,这里选择的elasticsearch
Reenter password for [apm_system]: #再次输入密码,这里选择的elasticsearch
Enter password for [kibana_system]: #输入密码,这里选择的elasticsearch
Reenter password for [kibana_system]: #再次输入密码,这里选择的elasticsearch
Enter password for [logstash_system]: #输入密码,这里选择的elasticsearch
Reenter password for [logstash_system]: #再次输入密码,这里选择的elasticsearch
Enter password for [beats_system]: #输入密码,这里选择的elasticsearch
Reenter password for [beats_system]: #再次输入密码,这里选择的elasticsearch
Enter password for [remote_monitoring_user]: #输入密码,这里选择的elasticsearch
Reenter password for [remote_monitoring_user]: #再次输入密码,这里选择的elasticsearch
Changed password for user [apm_system]
Changed password for user [kibana_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]

5、测试验证elasticsearch集群的认证:

[root@master-01 ~]# k get svc
NAME          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)             AGE
elastic-svc   ClusterIP   172.34.44.86   <none>        9200/TCP,9300/TCP   16m
mysql         ClusterIP   None           <none>        3306/TCP,9125/TCP   48d
[root@master-01 ~]# curl http://172.34.44.86:9200/
{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}[root@stcs-master-01 ~]#

发现通过无密码的方式访问提示报错,需要进行密码认证。

[root@master-01 ~]# curl --user elastic  http://172.34.44.86:9200/
Enter host password for user 'elastic':
{
  "name" : "elastic-cluster-1",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "O-M9L7gfQBagxQxIBMjXIQ",
  "version" : {
    "number" : "7.9.3",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "c4138e51121ef06a6404866cddc601906fe5c868",
    "build_date" : "2020-10-16T10:36:16.141335Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

通过密码认证的方式,就可以正常访问elasticsearch集群。

总结:

在paas云平台的运维维护中,需要经常自定义部署elasticsearch集群的,要求不高的,基本以无密码的elasticsearch集群为准,要求高的,都是以密码认证的elasticsearch集群为准的。希望可以帮助到大家!!!文章来源地址https://www.toymoban.com/news/detail-850215.html

到了这里,关于k8s集群部署elasticsearch集群和elasticsearch集群设置用户密码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 使用kubekey部署k8s集群和kubesphere、在已有k8s集群上部署kubesphere

    环境: centos 7.6、k8s 1.22.17、kubesphere v3.3.0 本篇以kubesphere v3.3.0版本讲解。 kubesphere 愿景是打造一个以 kubernetes 为内核的云原生分布式操作系统,它的架构可以非常方便地使第三方应用与云原生生态组件进行即插即用(plug-and-play)的集成,支持云原生应用在多云与多集群的统一

    2024年02月04日
    浏览(14)
  • 2.k8s账号密码登录设置

    2.k8s账号密码登录设置

    前面已经搭建好了k8s集群,现在设置下账号密码登录,k8s默认使用token登录,很麻烦,这里修改下配置。 由于重启会导致所有的进程丢失,这里写了个集群重启脚本,需要hadoop1、hadoop2、hadoop3三台集群机器可以相互ssh命令想通才可以执行脚本。这里我在hadoop1上建立脚本如下

    2024年02月09日
    浏览(5)
  • 部署K8S集群

    目录 一、环境搭建 1、准备环境 2、安装master节点 3、安装k8s-master上的node 4、安装配置k8s-node1节点 5、安装k8s-node2节点 6、为所有node节点配置flannel网络 7、配置docker开启加载防火墙规则允许转发数据 二、k8s常用资源管理 1、创建一个pod 2、pod管理 1、准备环境 计算机说明,建议

    2024年02月13日
    浏览(8)
  • K8S—集群部署

    K8S—集群部署

            K8s是容器集群管理系统,是一个开源的平台,可以实现容器集群的自动化部署、自动扩展容维护等功能,k8s的原名是kubernetes。 1.1、K8s的功能: 有大量跨主机的容器需要管理 快速部署应用快速扩展应用 无缝对接新的应用功能 节省资源,优化硬件资源的使用情况

    2024年02月12日
    浏览(8)
  • K8s 集群部署

    K8s 集群部署

    学习了黑马K8s,首先跟着视频部署K8s,写下笔记 转至 原文链接 整合黑马老师笔记 目前生产部署Kubernetes集群主要有两种方式: 一 kubeadm Kubeadm是一个K8s部署工具,提供kubeadm init和kubeadm join,用于快速部署Kubernetes集群。 二 进制包 从github下载发行版的二进制包,手动部署每个

    2024年02月04日
    浏览(6)
  • 25-k8s集群中-RBAC用户角色资源权限

    25-k8s集群中-RBAC用户角色资源权限

            我们通过k8s各组件架构,指导各个组件之间是使用https进行数据加密及交互的,那么同理,我们作为“使用”k8s的各种资源,也是通过https进行数据加密的;         k8s通过我们家目录下的证书来判断我们是谁?通过证书内容,认定我们的权限; 用户证书的位置 [r

    2024年02月22日
    浏览(8)
  • K8S二进制部署详解,一文教会你部署高可用K8S集群

    Pod网段: 10.0.0.0/16 Service网段: 10.255.0.0/16 集群角色 ip 主机名 安装组件 控制节点 10.10.0.10 master01 apiserver、controller-manager、scheduler、etcd、docker、keepalived、nginx 控制节点 10.10.0.11 master02 apiserver、controller-manager、scheduler、etcd、docker、keepalived、nginx 控制节点 10.10.0.12 master03 apiser

    2024年04月28日
    浏览(14)
  • K8s 部署 CNI 网络组件+k8s 多master集群部署+负载均衡

    K8s 部署 CNI 网络组件+k8s 多master集群部署+负载均衡

    ------------------------------ 部署 CNI 网络组件 ------------------------------ ---------- 部署 flannel ---------- K8S 中 Pod 网络通信: ●Pod 内容器与容器之间的通信 在同一个 Pod 内的容器(Pod 内的容器是不会跨宿主机的)共享同一个网络命名空间,相当于它们在同一台机器上一样,可以用 lo

    2024年02月08日
    浏览(10)
  • 安装部署k8s集群

    安装部署k8s集群

    系统: CentOS Linux release 7.9.2009 (Core) 准备3台主机 192.168.44.148 k8s-master 92.168.44.154 k8s-worker01 192.168.44.155 k8s-worker02 3台主机准备工作 关闭防火墙和selinux 关闭swap分区(swap分区会降低性能,所以选择关闭) 参考如下链接: 设置node的主机名,并配置/etc/hosts (这样可以方面看到pod调

    2024年02月19日
    浏览(11)
  • 单机部署K8S集群

    单机部署K8S集群

    1 系统准备 操作系统: Kubernetes 支持多种Linux发行版,包括但不限于 CentOS、Ubuntu、RHEL等。通常建议使用稳定版本,并且是 64位系统 。我这里使用的是CentOS 8.1版本  硬件配置: 内存(RAM): 每台机器至少需要2GB或更多 ,具体需求根据集群规模和应用程序负载来定。 CPU核心数

    2024年02月20日
    浏览(8)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包