0

I am working on running a project locally inside a Docker container with Linux-amd64 architecture. The start script runs the following command

kubectl create configmap -o yaml \
    --dry-run=true \
    --from-file ./configs/local/grafana \
    grafana-dashboards-source-config |
    kubectl apply -f -

which runs well on my MacOs but when run inside the Docker container, it fails with The connection to the server localhost:8080 was refused - did you specify the right host or port? error. On browsing resources online, many posts suggested to use these commands to fix the issue

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

but inside my Docker container I cannot find the /kubernetes directory and hence the admin.conf file. I am using COPY --from=lachlanevenson/k8s-kubectl:v1.10.3 /usr/local/bin/kubectl /usr/local/bin/kubectl command in my Dockerfile to install kubectl and kubectl version --client returns with

Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.3", GitCommit:"2bba0127d85d5a46ab4b778548be28623b32d0b0", GitTreeState:"clean", BuildDate:"2018-05-21T09:17:39Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}

I wished to know what is the correct way to install kubectl while creating a Docker image so that I can find the kubernetes/admin.conf file, or if there is a way to generate it after kubectl is installed?

scottstots
  • 155
  • 2
  • 17
  • Usually I'd run `kubectl` commands like this as part of my deployment infrastructure (maybe implicitly in a Helm chart) and not part of my container startup. Are you running this inside Kubernetes, or elsewhere? Can you run `kubectl` from the host, and if so, where did its credentials come from? Does a less-ancient version of `kubectl` do any better (1.10 is now nine releases behind)? – David Maze Sep 03 '20 at 17:12

1 Answers1

-1

If the main idea is using kubectl inside a container in the cluster, you could use a service account to reach the Kubernetes API Server. You could follow the below steps in general.

  • create a service account on your cluster with related permissions
  • set the service account to your workload (deployment, stateful set, pod, etc.)
  • set your config map to using the service account (/var/run/secrets/kubernetes.io/serviceaccount/) this link could be useful.
Emre Odabaş
  • 409
  • 3
  • 6