Need a working Kubectl binary inside an image
Or, you can do this. It works in my context, with kubernetes on VMs, where I know where is kubeconfig
file. You would need to make the necessary changes, to make it work in your environment.
apiVersion: apps/v1kind: Deploymentmetadata: name: kubectlspec: replicas: 1 selector: matchLabels: role: kubectl template: metadata: labels: role: kubectl spec: containers: - image: viejo/kubectl name: kubelet tty: true securityContext: privileged: true volumeMounts: - name: kube-config mountPath: /root/.kube/ volumes: - name: kube-config hostPath: path: /home/$USER/.kube/ affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: node-role.kubernetes.io/master operator: Exists tolerations: - effect: NoSchedule key: node-role.kubernetes.io/master operator: Exists
This is the result:
$ kubectl get poNAME READY STATUS RESTARTS AGEkubectl-cb8bfc6dd-nv6ht 1/1 Running 0 70s$ kubectl exec kubectl-cb8bfc6dd-nv6ht -- kubectl get noNAME STATUS ROLES AGE VERSIONkubernetes-1-17-master Ready master 16h v1.17.3kubernetes-1-17-worker Ready <none> 16h v1.17.3
As Suren already explained in the comments that kubectl
is not a daemon so kubectl will run, exit and cause the container to restart.
There are a couple of workarounds for this. One of these is to use sleep
command with infinity
argument. This would keep the Pod alive, prevent it from restarting and allow you to exec into it.
Here`s an example how to do that:
spec: containers: - image: bitnami/kubectl command: - sleep - "infinity" name: kctl
Let me know if this helps.