【问题标题】:Error from server (NotFound): pods "$(kubectl" not found来自服务器的错误 (NotFound): 未找到 pod "$(kubectl"
【发布时间】:2019-12-29 15:56:49
【问题描述】:

在 minikube hyper-v 机器上,我使用 sawtooth config file 部署了锯齿形 0。

现在,当我检查正在运行的 pod 时,它似乎有一些默认的,但在尝试连接 kubernetes shell 容器时。

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>

【问题讨论】:

  • 尝试使用单引号中的 awk 命令 - awk '/sawtooth-0/ {print $1}' 或执行 awk "/sawtooth-0/ {print \$1}"
  • ``` C:\Users\Debo>kubectl exec -it $(kubectl get pods | awk '/sawtooth-0/ {print $1}') --container sawtooth-shell -- bash awk: '/sawtooth-0/ awk: ^ invalid char ''' in expression Error from server (NotFound): pods "$(kubectl" not found C:\Users\Debo>kubectl exec -it $(kubectl get pods | awk "/sawtooth-0/ {print \$1}") --container sawtooth-shell -- bash awk: /sawtooth-0/ {print \$1}) awk: ^ 反斜杠不是最后一个字符来自服务器的错误(未找到): pods "$(kubectl" not found ``` 不工作兄弟
  • 这看起来像 Windows 命令提示符,但 Unix Bourne shell 语法。您实际使用的是什么 shell,它是否支持 Bourne shell $(command) 替换语法?
  • (kubectl get pods -o name 可能比awk 调用更健壮;还可以考虑按标签kubectl get pods -l app=sawtooth 或类似方法进行过滤,以避免需要awk/grep/sed 步骤。 )
  • 您到底想达到什么目标? Kubernetes 有许多内置命令。如果您想从锯齿 pod 连接到特定容器,您可以使用 kubectl exec -ti sawtooth-0-65d547498c-mfrsb -c &lt;container_name&gt; /bin/bash

标签: awk kubernetes hyperledger minikube hyperledger-sawtooth


【解决方案1】:

如果我了解您的用例,您正在尝试从命名空间中的给定 pod 列表中过滤锯齿 0 pod,然后打开一个交互式会话,在其中运行您知道容器名称的容器。强>

所以为了解决上面的问题,我使用了有问题的锯齿配置文件并启动了它的两个实例,锯齿 0 和锯齿 1 以及几个虚拟 pod,然后我在 Windows 电源外壳上使用这个命令来过滤并连接到容器锯齿 0 豆荚。

假设您需要在 windows powershell 上运行 kubectl,以下所有内容都已完成

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

连接到锯齿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

连接到锯齿 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

【讨论】:

  • 只是一个题外话 - kubectl exec 似乎需要容器名称(无论我们是否指定)。有没有办法只连接 pod(而不是容器)?
猜你喜欢
  • 2020-07-30
  • 2020-06-03
  • 2020-06-08
  • 1970-01-01
  • 2020-10-23
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 2018-12-13
相关资源
最近更新 更多