Connection to elasticsearch on kubernetes fails

Viewed 8

I want to setup a simple single-node elasticsearch pod on kubernetes that I can connect to via my backend.

Here is the config for my service and statefulset:

apiVersion: v1
kind: Service
metadata:
  name: elasticsearch
spec:
  type: ClusterIP
  clusterIP: None
  selector:
    app: elasticsearch
  ports:
    - port: 9200 # To get at the elasticsearch container, just hit the service on 9200
      targetPort: 9200 # routes to the exposed port on elasticsearch
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: elasticsearch # name of stateful
  namespace: default
spec:
  serviceName: elasticsearch
  replicas: 1
  selector:
    matchLabels:
      app: elasticsearch # should match service > spec.slector.app.
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:
      volumes:
        - name: elasticsearch-pvc
          persistentVolumeClaim:
            claimName: elasticsearch-volume-claim
      containers:
        - name: elasticsearch
          image: docker.elastic.co/elasticsearch/elasticsearch:8.2.3
          resources:
            limits:
              cpu: 100m
            requests:
              cpu: 100m
          ports:
            - containerPort: 9200
              name: rest
              protocol: TCP
            - containerPort: 9300
              name: inter-node
              protocol: TCP
          volumeMounts:
            - name: elasticsearch-pvc
              mountPath: /usr/share/elasticsearch/data
          env:
            - name: cluster.name
              value: search
            - name: node.name
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: discovery.type
              value: single-node
            - name: ES_JAVA_OPTS
              value: "-Xms512m -Xmx512m"
            - name: xpack.security.enabled
              value: "false"
      initContainers:
        - name: fix-permissions
          image: busybox
          command:
            ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data"]
          securityContext:
            privileged: true
          volumeMounts:
            - name: elasticsearch-pvc
              mountPath: /usr/share/elasticsearch/data
        - name: increase-vm-max-map
          image: busybox
          command: ["sysctl", "-w", "vm.max_map_count=262144"]
          securityContext:
            privileged: true
        - name: increase-fd-ulimit
          image: busybox
          command: ["sh", "-c", "ulimit -n 65536"]
          securityContext:
            privileged: true

I'm connecting via the javascript client ("@elastic/elasticsearch": "^8.2.1") like so:

import { Client, HttpConnection } from '@elastic/elasticsearch'
import config from '../../config'

export const client = new Client({
  node: config.elasticSearch.host,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json'
  },
  Connection: HttpConnection
 
})

Where config.elasticSearch.host = http://elasticsearch:9200

However when I run my initial seed script I get the following error:

/app/node_modules/@elastic/transport/lib/Transport.js:525
                            : new errors_1.ConnectionError(error.message, result);
                              ^

ConnectionError: connect ECONNREFUSED 10.244.0.112:9200

I'm not entirely sure why the connection is being refused since the service should be directing the request to my elasticsearch stateful set.

0 Answers
Related