HiveBrain v1.2.0
Get Started
← Back to all entries
snippetkubernetesMinor

How to run a daemon AND another command in a kubernetes pod

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
kubernetesanotherhowandcommandpoddaemonrun

Problem

I am trying to setup an automated influxdb instance using kubernetes, nothing too exotic I thought, but :
I need to run the "influx setup ...." command for the initial setup, and this needs to be ran after the daemon is started (obviously).

I tried multiple syntax inside the deploy yaml, here is one attempt:

spec:
      containers:
        - name: influxdb-test
          image: influxdb:2.0.7
          command:
            - sh
            - "-c"
            - |
              /bin/bash <<'EOF'
              influxd&
              influx setup --org ORG --bucket dummy --username USER --password USER --force
              EOF


But this gives me the following error :

Error: failed to determine if instance has been configured: Get "http://localhost:8086/api/v2/setup": dial tcp 127.0.0.1:8086: connect: connection refused
See 'influx setup -h' for help


Well I do not need a solution for this specific error, but more a solution about how can I have a daemon running (influxd) and run some command after this daemon (influx setup) and then keep the container up.

I first thought that adding the simple "influx setup" command would be enough, but any command given inside a yaml deploy file overwrite the container initial command (influxd in this case) so I tried to reproduce the whole stuff manually : start the daemon, then run the setup then I am done, but no luck.

Of course I am a k8s newbie.

I am looking at "init container" as this sounds close to fixing my issue ....

Solution

I finally found some way to achieve this kind of task: Creating a container only for a single and ephemeral job can be achieved through the .... Job API :)

See this doc.

So I now have a deployment for my long living task (influxdb) + a Job definition for my on-time job (influxdb initial setup).
Here is an example (I thought that could be useful for someone) :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: influxdb
  namespace: influxdb
spec:
  selector:
    matchLabels:
      app: influxdb
  replicas: 1
  template:
    metadata:
      labels:
        app: influxdb

    spec:
      volumes:
        - name: influxdb-data
          persistentVolumeClaim:
            claimName: nfs-pvc-influx

      containers:
        - name: influxdb-daemon
          image: influxdb:2.0.7
          volumeMounts:
            - mountPath: /home/influxdb/
              name: influxdb-data
          env: 
            - name: INFLUXD_CONFIG_PATH
              value: /etc/influxdb2/
          ports:       
          - containerPort: 8086
            name: pbe
            protocol: TCP


And the job :

apiVersion: batch/v1
kind: Job
metadata:
  name: influxdb-init
  namespace: influxdb
spec:
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: influxdb-init
          image: influxdb:2.0.7
          command:
            - sh
            - "-c"
            - |
              sleep 15
              influx setup --host http://influxdb-nodeport.influxdb.svc.cluster.local:8086 --org organistation1 --bucket dummy --username user --password user --token token --force && exit
  backoffLimit: 0

Code Snippets

apiVersion: apps/v1
kind: Deployment
metadata:
  name: influxdb
  namespace: influxdb
spec:
  selector:
    matchLabels:
      app: influxdb
  replicas: 1
  template:
    metadata:
      labels:
        app: influxdb

    spec:
      volumes:
        - name: influxdb-data
          persistentVolumeClaim:
            claimName: nfs-pvc-influx

      containers:
        - name: influxdb-daemon
          image: influxdb:2.0.7
          volumeMounts:
            - mountPath: /home/influxdb/
              name: influxdb-data
          env: 
            - name: INFLUXD_CONFIG_PATH
              value: /etc/influxdb2/
          ports:       
          - containerPort: 8086
            name: pbe
            protocol: TCP
apiVersion: batch/v1
kind: Job
metadata:
  name: influxdb-init
  namespace: influxdb
spec:
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: influxdb-init
          image: influxdb:2.0.7
          command:
            - sh
            - "-c"
            - |
              sleep 15
              influx setup --host http://influxdb-nodeport.influxdb.svc.cluster.local:8086 --org organistation1 --bucket dummy --username user --password user --token token --force && exit
  backoffLimit: 0

Context

StackExchange DevOps Q#14147, answer score: 2

Revisions (0)

No revisions yet.