kubectl exec works on single commands, but I cannot enter a bash shell

Viewed 12045

I'm on macOS Catalina 10.15.4, and I'm using minikube v1.11.0 and kubernetes v1.18.3, both installed from brew. Minikube is initialized with the docker engine.

The initialization command is set up like so:

      containers:
        - name: database
          image: "mysql:5.6"
          imagePullPolicy: IfNotPresent
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: 12345
            - name: MYSQL_USER
              value: user
            - name: MYSQL_PASSWORD
              value: password
            - name: MYSQL_DATABASE
              value: db

I'm trying to get a bash script open for one of my running kubectl containers. From research online, it appears that this should be the command that will open a bash window in my terminal:

minikube kubectl exec -it --namespace=tools test-pod -- bash

However, when I run it, I get the following traceback:

Error: unknown shorthand flag: 'i' in -it See 'minikube kubectl --help' for usage.

It doesn't seem to want me using any arguments in my command. Is there something I'm missing, or am I attempting to use a command that is deprecated?

Note: I am able to run exec, but not for opening a bash script. For example, I am able to run the following command:

minikube kubectl exec test-pod -- ls /

And it outputs this following:

kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
bin
boot
dev
docker-entrypoint-initdb.d
entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

Edit: I have attempted the following command:

minikube kubectl exec --stdin --tty --namespace=tools test-pod -- sh

And I got the following traceback:

Error: unknown flag: --stdin
See 'minikube kubectl --help' for usage.

It seems like any flags at all, short or long, are failing, and I cannot figure out why they wouldn't be.

3 Answers

minikube kubectl needs the -- after the command when you want to use it with arguments:

$ minikube kubectl -- exec --stdin --tty --namespace=tools test-pod -- sh

You can also use plain kubectl

If would just make sure that your ~/.kube/config is pointing to the right minikube context/cluster. Typically, any minikube command you run from the shell will cause it to change the context to your minikube cluster. i.e minikube ssh

Then just use kubectl

$ kubectl exec --stdin --tty --namespace=tools test-pod -- sh

I'm unfamiliar with minikube but you should be able to query the command's syntax with:

minikube kubectl exec --help

It's possible that the short flags have been replaced with the long flags:

minikube kubectl exec --stdin --tty ...

It's also possible that the container doesn't include bash but another shell:

minikube kubectl exec --stdin --tty --namespace=tools test-pod -- sh
minikube kubectl exec --stdin --tty --namespace=tools test-pod -- ash

So, I figured out the solution:

With my configuration, initializing minikube with minikube start --driver=docker does not successfully initialize everything. I changed my driver to virtualbox, and minikube was able to ssh and continue without any issues.

Setup with a docker driver appears to be commonly issue-prone, as this GitHub thread shows: https://github.com/kubernetes/minikube/issues/7332

Related