Home > Back-end >  Elasticsearch Kubernetes CrashLoopBackOff
Elasticsearch Kubernetes CrashLoopBackOff

Time:06-17

I'm trying to deployment ES to kubernetes, I did these steps before apply to kube

  1. Dockerization on my local machine
docker run -d --net devnet \
--name my-elastic \
--env ES_JAVA_OPTS="-Xms512m -Xmx512m" \
--env ES_PROTOCOL="http" \
--env discovery.type=single-node \
--env xpack.security.enabled=false \
--env ELASTIC_APM_VERIFY_SERVER_CERT=false \
--env XPACK_LICENSE_SELF_GENERATED_TYPE="basic" \
-p 9200:9200 -p 9300:9300 -it asia.gcr.io/my-project/elasticsearch:latest
  • I use JVM heap size 512m, because it's not for production
  • I disable verify cert & xpack security and use http protocol because still try non zero trust in my cluster
  • In my local that's running well
  1. I have created GCE persistent disk for data
  2. I have created volume and claimed the persistent disk volume
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-elasticsearch-pv
spec:
  storageClassName: ""
  capacity:
    storage: 10G
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  claimRef:
    namespace: default
    name: my-elasticsearch-pvc
  gcePersistentDisk:
    pdName: es-disk
    fsType: ext4
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-elasticsearch-pvc
spec:
  storageClassName: ""
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10G
  1. I have created & applied the service
apiVersion: v1
kind: Service
metadata:
  name: my-elastic
spec:
  ports:
  - name: http-port
    port: 9200
    targetPort: es-port
  selector:
    app: my-elastic
  type: ClusterIP
  1. Create deployment file and apply this setup
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-elastic
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-elastic
  template:
    metadata:
      labels:
        app: my-elastic
    spec:
      containers:
      - name: my-elastic
        resources:
            requests:
                memory: 1Gi
        image: asia.gcr.io/my-project/elasticsearch:latest
        env:
        - name: ES_JAVA_OPTS
          value: -Xms512m -Xmx512m
        - name: ES_PROTOCOL
          value: http
        - name: discovery.type
          value: single-node
        - name: XPACK_LICENSE_SELF_GENERATED_TYPE
          value: basic
        - name: xpack.security.enabled
          value: 'false'
        - name: xpack.monitoring.enabled
          value: 'false'
        ports:
        - name: es-port
          containerPort: 9200
        volumeMounts:
        - name: my-elasticsearch-data
          mountPath: /usr/share/elasticsearch/data
      volumes:
      - name: my-elasticsearch-data
        persistentVolumeClaim:
          claimName: my-elasticsearch-pvc

I got CrashLoopBackOff after applied the deployment, this is the condition of my cluster. Did I missing something on the config?

enter image description here

Here is the logs

"log.level":"ERROR", "message":"uncaught exception in thread [main]", "ecs.version": "1.2.0","service.name":"ES_ECS","event.dataset":"elasticsearch.server","process.thread.name":"main","log.logger":"org.elasticsearch.bootstrap.ElasticsearchUncaughtExceptionHandler","elasticsearch.node.name":"alodokter-emr-es-5cbd4cd95-bfvld","elasticsearch.cluster.name":"docker-cluster","error.type":"org.elasticsearch.bootstrap.StartupException","error.message":"java.lang.IllegalStateException: failed to obtain node locks, tried [/usr/share/elasticsearch/data]; maybe these locations are not writable or multiple nodes were started on the same data path?"

CodePudding user response:

Make Elasticsearch (UID: 1000) owner of the data directory /usr/share/elasticsearch/data from an initContainer.

$ chown -R 1000:1000 /usr/share/elasticsearch/data
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-elastic
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-elastic
  template:
    metadata:
      labels:
        app: my-elastic
    spec:
      initContainers:
      - name: busybox
        image: busybox:1.28
        command: ['sh', '-c', "chown -R 1000:1000 /usr/share/elasticsearch/data"]
        volumeMounts:
        - name: my-elasticsearch-data
          mountPath: /usr/share/elasticsearch/data
      containers:
      - name: my-elastic
        resources:
            requests:
                memory: 1Gi
        image: asia.gcr.io/my-project/elasticsearch:latest
        env:
        - name: ES_JAVA_OPTS
          value: -Xms512m -Xmx512m
        - name: ES_PROTOCOL
          value: http
        - name: discovery.type
          value: single-node
        - name: XPACK_LICENSE_SELF_GENERATED_TYPE
          value: basic
        - name: xpack.security.enabled
          value: 'false'
        - name: xpack.monitoring.enabled
          value: 'false'
        ports:
        - name: es-port
          containerPort: 9200
        volumeMounts:
        - name: my-elasticsearch-data
          mountPath: /usr/share/elasticsearch/data
      volumes:
      - name: my-elasticsearch-data
        persistentVolumeClaim:
          claimName: my-elasticsearch-pvc
  • Related