Run consul agent with config-dir caused not found exception

1/15/2020

In our docker-compose.yaml we have:

version: "3.5"
services:
  consul-server:
    image: consul:latest
    command: "agent -server -bootstrap -ui -enable-script-checks=true -client=0.0.0.0 -config-dir=./usr/src/app/consul.d/"
    volumes:
      - ./consul.d/:/usr/src/app/consul.d

In the consul.d folder we have statically defined our services. It works fine with docker-compose.

But when trying to run it on Kubernetes with this configmap:

ahmad@ahmad-pc:~$ kubectl describe configmap consul-config -n staging
Name:         consul-config
Namespace:    staging
Labels:       <none>
Annotations:  <none>

Data
====
trip.json:
----
... omitted for clarity ...

and consul.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    io.kompose.service: consul-server
  name: consul-server
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: consul-server
  template:
    metadata:
      labels:
        io.kompose.service: consul-server
    spec:
      containers:
      - image: quay.io/bitnami/consul:latest
        name: consul-server
        ports:
        - containerPort: 8500
        #env:
        #- name: CONSUL_CONF_DIR # Consul seems not respecting this env variable
        #  value: /consul/conf/
        volumeMounts:
        - name: config-volume
          mountPath: /consul/conf/
        command: ["agent -server -bootstrap -ui -enable-script-checks=true -client=0.0.0.0 -config-dir=/consul/conf/"]
      volumes:
        - name: config-volume
          configMap:
            name: consul-config

I got the following error:

ahmad@ahmad-pc:~$ kubectl describe pod consul-server-7489787fc7-8qzhh -n staging
...

Error: failed to start container "consul-server": Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"agent -server -bootstrap -ui -enable-script-checks=true -client=0.0.0.0 -config-dir=/consul/conf/\": 
stat agent -server -bootstrap -ui -enable-script-checks=true -client=0.0.0.0 -config-dir=/consul/conf/: 
no such file or directory": unknown

But when I run the container without command: agent... and bash into it, I can list files mounted in the right place. Why consul gives me a not found error despite that folder exists?

-- Ahmad Ahmadi
consul
kubernetes

1 Answer

1/15/2020

To execute command in the pod you have to define a command in command field and arguments for the command in args field. command field is the same as ENTRYPOINT in Docker and args field is the same as CMD.
In this case you define /bin/sh as ENTRYPOINT and "-c, "consul agent -server -bootstrap -ui -enable-script-checks=true -client=0.0.0.0 -data-dir=/bitnami/consul/data/ -config-dir=/consul/conf/" as arguments so it can execute consul agent ...:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    io.kompose.service: consul-server
  name: consul-server
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: consul-server
  template:
    metadata:
      labels:
        io.kompose.service: consul-server
    spec:
      containers:
      - image: quay.io/bitnami/consul:latest
        name: consul-server
        ports:
        - containerPort: 8500
        env:
        - name: CONSUL_CONF_DIR # Consul seems not respecting this env variable
          value: /consul/conf/  
        volumeMounts:
        - name: config-volume
          mountPath: /consul/conf/
        command: ["bin/sh"]
        args: ["-c", "consul agent -server -bootstrap -ui -enable-script-checks=true -client=0.0.0.0 -data-dir=/bitnami/consul/data/ -config-dir=/consul/conf/"] 
      volumes:
        - name: config-volume
          configMap:
            name: consul-config
-- KFC_
Source: StackOverflow