Kubectl always returns a error: yaml: mapping values are not allowed in this context

Kubectl always returns a error: yaml: mapping values are not allowed in this context

Kubectl 命令总是 returns 此错误 yaml:第 2 行:此上下文中不允许映射值。即使当我调用普通版本命令、配置命令等时。不确定是什么原因造成的。

tessact@tessact-sys-1:~$ kubectl version
Client Version: version.Info{Major:"1", Minor:"4",
GitVersion:"v1.4.4",
GitCommit:"3b417cc4ccd1b8f38ff9ec96bb50a81ca0ea9d56",
GitTreeState:"clean", BuildDate:"2016-10-21T02:48:38Z",
GoVersion:"go1.6.3", Compiler:"gc", Platform:"linux/amd64"}
error: yaml: line 2: mapping values are not allowed in this context


tessact@tessact-sys-1:~/[some path]$ kubectl create -f kubernetes_configs/frontend.yaml
error: yaml: line 2: mapping values are not allowed in this context

我使用的唯一 yaml 文件是

apiVersion: v1
kind: ReplicationController
metadata:
  name: frontend
  labels:
    name: frontend
spec:
  replicas: 3
  template:
    metadata:
      labels:
        name: frontend
    spec:
      containers:
      - name: trigger
        # Replace  with your project ID or use `make template`
        image: asia.gcr.io/trigger-backend/trigger-backend

        # This setting makes nodes pull the docker image every time before
        # starting the pod. This is useful when debugging, but should be turned
        # off in production.
        imagePullPolicy: Always
        ports:
        - containerPort: 8080


apiVersion: v1
kind: Service
metadata:
  name: frontend
  labels:
    name: frontend
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 8080
  selector:
    name: frontend

无论我用 kubectl 尝试什么,它都会 returns 这个错误。我应该怎么做才能解决这个问题?

> tessact@tessact-sys-1:~/developer/trigger-backend-dev/trigger-backend$
> kubectl get service error: yaml: line 2: mapping values are not
> allowed in this context

输出:

strace kubectl version

here

版本命令已经抛出错误表明有一些默认的 YAML 文件被加载。

您可以使用 strace kubectl version 查看打开了什么文件,希望在 kubectl 抛出错误之前完成。我假设它读取了一些全局配置(或者当前目录中的默认文件)。

在kubernetes中当然是马虎编程没有捕捉到这样的错误,并显示文件名,然后重新引发错误。

因为即使在 运行 kubectl version 时你也收到错误,我会说你的 kubeconfig 文件中有一个 yaml 语法错误,位于 ~/.kube/config默认。

您可以使用像 this one 这样的 yaml 验证器来验证它的内容。

大多数情况下,当您遇到这样的错误(笼统且有意义的术语)时,要么是因为 :-

1). yaml 文件中的语法错误(在您的情况下不是)。

2).或者像错误所说的 "mapping values are not allowed in this context"。这意味着您在 yaml 中使用的 keys/values 在句法上可能是正确的,但在语义上可能不正确。

确保您已完成以下步骤:

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

即使是 root 用户。

我也无法看到来自

kubectl version

的版本

补充一下... 在对我的 YAML 文件执行 copy/paste 操作后,我今天看到了这个错误。该过程引入了一些 kubectl 无法破译的空白字符。

如果您不确定,请先将 YAML 粘贴到文本编辑器中,这将显示所有不可见字符并确保它们与 YAML 文件的其余部分一致。

我在执行一些 Kubernetes 基本命令时遇到了同样的问题。 kubectl get ns , kubectl get pods, 等等...

解决方案:

尝试停止 minikube 集群然后重新启动