可以使用IP连接到elasticsearch pod但不使用pod名称

时间:2017-10-16 15:54:06

标签: elasticsearch kubernetes minikube

我在通过IP(hardcoding address:port)在elasticsearch.yaml文件中指定主机之前已经成功了但是我被告知这是不好的做法。我正在尝试切换到仅使用我的ES群集的pod名称,现在这些pod未被发现​​/用作主服务器。我有一个elasticsearch.yml configMap用于我安装的所有3个pod,它具有以下规范:

cluster.name: elasticsearch-logs
    node.name: ${HOSTNAME}
    node.master: true
    node.data: true
    network.host: _local_
    transport.tcp.port: 9300
    http.port: 9200 
    bootstrap.memory_lock: false
    xpack.security.enabled: false
    discovery.zen.minimum_master_nodes: 2
    discovery.zen.ping.unicast.hosts: ["es-0:9300", "es-1:9300", "es-2:9300"]

除此之外我还有2项服务。一个是无头服务,另一个是ClusterIP。

apiVersion: v1
kind: Service
metadata:
  name: elasticsearch-svc
  labels:
    component: elasticsearch
    role: master
spec:
  selector:
    component: elasticsearch
    role: master
  ports:
  - name: transport
    port: 9300
    targetPort: 9300
  clusterIP: None


apiVersion: v1
kind: Service
metadata:
  name: elasticsearch-discovery
  labels:
    component: elasticsearch
    role: master
spec:
  selector:
    component: elasticsearch
    role: master
  ports:
  - name: transport
    port: 9300
    protocol: TCP

在创建ES pod的主StatefulSet文件中,我有端口规范:

ports:
        - containerPort: 9200
          name: db
          protocol: TCP
        - containerPort: 9300
          name: transport
          protocol: TCP

我试图让所有3个pod充当主(和数据/客户端)。当我在创建服务/状态集后查看其中一个pod日志(此处为es-0)时,我看到以下重复错误:

[2017-10-16T15:31:29,078][WARN ][o.e.d.z.UnicastZenPing   ] [es-0] timed out after [5s] resolving host [es-1:9300]
[2017-10-16T15:31:29,079][WARN ][o.e.d.z.UnicastZenPing   ] [es-0] timed out after [5s] resolving host [es-2:9300]
[2017-10-16T15:31:32,080][WARN ][o.e.d.z.ZenDiscovery     ] [es-0] not enough master nodes discovered during pinging (found [[Candidate{node={es-0}{TUE-h8SNR6q7WbWUl2Pm-A}{XrTrBg3ATqSvlB3hTlezpg}{172.17.0.3}{172.17.0.3:9300}{ml.max_open_jobs=10, ml.enabled=true}, clusterStateVersion=-1}]], but needed [2]), pinging again
[2017-10-16T15:31:36,111][WARN ][o.e.d.z.UnicastZenPing   ] [es-0] failed to resolve host [es-1:9300]
java.net.UnknownHostException: es-1
    at java.net.InetAddress.getAllByName0(InetAddress.java:1280) ~[?:1.8.0_141]
    at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[?:1.8.0_141]
    at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[?:1.8.0_141]
    at org.elasticsearch.transport.TcpTransport.parse(TcpTransport.java:908) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.transport.TcpTransport.addressesFromString(TcpTransport.java:863) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.transport.TransportService.addressesFromString(TransportService.java:691) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.discovery.zen.UnicastZenPing.lambda$null$0(UnicastZenPing.java:212) ~[elasticsearch-5.6.3.jar:5.6.3]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[?:1.8.0_141]
    at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:569) [elasticsearch-5.6.3.jar:5.6.3]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_141]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_141]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_141]
[2017-10-16T15:31:36,116][WARN ][o.e.d.z.UnicastZenPing   ] [es-0] failed to resolve host [es-2:9300]
java.net.UnknownHostException: es-2
    at java.net.InetAddress.getAllByName0(InetAddress.java:1280) ~[?:1.8.0_141]
    at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[?:1.8.0_141]
    at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[?:1.8.0_141]
    at org.elasticsearch.transport.TcpTransport.parse(TcpTransport.java:908) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.transport.TcpTransport.addressesFromString(TcpTransport.java:863) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.transport.TransportService.addressesFromString(TransportService.java:691) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.discovery.zen.UnicastZenPing.lambda$null$0(UnicastZenPing.java:212) ~[elasticsearch-5.6.3.jar:5.6.3]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[?:1.8.0_141]
    at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:569) [elasticsearch-5.6.3.jar:5.6.3]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_141]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_141]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_141]
[2017-10-16T15:31:39,120][WARN ][o.e.d.z.ZenDiscovery     ] [es-0] not enough master nodes discovered during pinging (found [[Candidate{node={es-0}{TUE-h8SNR6q7WbWUl2Pm-A}{XrTrBg3ATqSvlB3hTlezpg}{172.17.0.3}{172.17.0.3:9300}{ml.max_open_jobs=10, ml.enabled=true}, clusterStateVersion=-1}]], but needed [2]), pinging again

我仍然可以通过node-ip:node-port的浏览器访问elasticsearch,但是一旦我尝试/_cluster/state

我就会收到503错误

我相信我在端口的“网络”方面有错误,但我不确定到底在哪里。我该怎么看?谢谢!

StatefulSet

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: es
  labels:
    component: elasticsearch
    role: master
spec:
  serviceName: elasticsearch
  replicas: 3
  template:
    metadata:
      labels:
        component: elasticsearch
        role: master
      annotations:
       pod.alpha.kubernetes.io/init-containers: '[
        {
          "name": "init-sysctl",
          "image": "alpine:3.4",
          "imagePullPolicy": "IfNotPresent",
          "command": ["sysctl", "-w", "vm.max_map_count=262144"],
          "securityContext": {
            "privileged": true
           }
         }
       ]'
    spec:
      subdomain: elasticsearch-svc
      containers:
      - name: es-master
        securityContext:
          privileged: true
          capabilities:
            add:
              - IPC_LOCK
        image: docker.elastic.co/elasticsearch/elasticsearch:5.6.3
        imagePullPolicy: Always
        env:
        - name: "ES_JAVA_OPTS"
          value: "-Xms512m -Xmx512m"
        ports:
        - containerPort: 9200
          name: http
          protocol: TCP
        - containerPort: 9300
          name: transport
          protocol: TCP
        volumeMounts:
        - name: storage
          mountPath: /data
        - name: config-volume
          mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
          subPath: elasticsearch.yml 
      volumes:
       - name: config-volume
         configMap:
          name: elasticsearch-config
  volumeClaimTemplates:
  - metadata:
      name: storage
      annotations:
        volume.beta.kubernetes.io/storage-class: standard
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 12Gi

1 个答案:

答案 0 :(得分:1)

您需要使用完整的DNS名称进行连接:

es-0.elasticsearch-internal:9300