'Error from server (NotFound): pods "$(kubectl" not found

In the minikube hyper-v machine I did deployment of sawtooth-0, using sawtooth config file.

Now when I am checking running pods it seems to have some default ones but while trying to connect with the kubernetes shell container.

C:\Users\Debo>kubectl get --all-namespaces
You must specify the type of resource to get. Use "kubectl api-resources" for a complete list of supported resources.

error: Required resource not specified.
Use "kubectl explain <resource>" for a detailed description of that resource (e.g. kubectl explain pods).
See 'kubectl get -h' for help and examples

C:\Users\Debo>kubectl get pods --all-namespaces
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
default                sawtooth-0-65d547498c-mfrsb                  7/7     Running   1          120m
kube-system            coredns-6955765f44-b684l                     1/1     Running   0          122m
kube-system            coredns-6955765f44-tc4vg                     1/1     Running   0          122m
kube-system            etcd-minikube                                1/1     Running   0          121m
kube-system            kube-addon-manager-minikube                  1/1     Running   0          121m
kube-system            kube-apiserver-minikube                      1/1     Running   0          121m
kube-system            kube-controller-manager-minikube             1/1     Running   0          121m
kube-system            kube-proxy-t7nhs                             1/1     Running   0          122m
kube-system            kube-scheduler-minikube                      1/1     Running   0          121m
kube-system            storage-provisioner                          1/1     Running   0          122m
kubernetes-dashboard   dashboard-metrics-scraper-7b64584c5c-b8t7s   1/1     Running   0          122m
kubernetes-dashboard   kubernetes-dashboard-79d9cd965-pn94z         1/1     Running   0          122m

C:\Users\Debo>kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
sawtooth-0-65d547498c-mfrsb   7/7     Running   1          120m

C:\Users\Debo>kubectl exec -it $(kubectl get pods | awk "/sawtooth-0/ {print $1}") --container sawtooth-shell -- bash
awk: /sawtooth-0/ {print $1})
awk:                        ^ syntax error
errcount: 1
Error from server (NotFound): pods "$(kubectl" not found

C:\Users\Debo>


Solution 1:[1]

If i understand your use case you are trying to filter the sawtooth-0 pod from list of give pods in a namespace and then open a interactive session toward container running inside it where you know the container name.

So to solve above i took the sawtooth config file attached in question and started two instance of it sawtooth-0 and sawtooth-1 alongside few of dummy pods then i use this command on windows power shell to filter and connect to container on sawtooth-0 pod.

All below was done assuming you need kubectl running on windows powershell

PS C:\Users\winuser> kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
busybox                       1/1     Running   0          4m18s
nginx                         1/1     Running   0          10m
sawtooth-0-7f584587cf-8jfwk   7/7     Running   0          19m
sawtooth-1-6ccd4494-kq24n     7/7     Running   0          18m

Connecting to sawtooth-0

PS C:\Users\winuser> kubectl exec -it $(kubectl get pods --no-headers -o custom-columns=":metadata.name" | findstr -i sawtooth-0) --container sawtooth-shell -- bash
root@sawtooth-0-7f584587cf-8jfwk:/# whoami
root
root@sawtooth-0-7f584587cf-8jfwk:/# date
Thu Jan 16 11:57:22 UTC 2020
root@sawtooth-0-7f584587cf-8jfwk:/# exit
exit
command terminated with exit code 130

Connecting to sawtooth-1

PS C:\Users\winuser> kubectl exec -it $(kubectl get pods --no-headers -o custom-columns=":metadata.name" | findstr -i sawtooth-1) --container sawtooth-shell -- bash
root@sawtooth-1-6ccd4494-kq24n:/# date
Thu Jan 16 12:17:33 UTC 2020

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DT.