20
votes

Kubectl command alway returns this error yaml: line 2: mapping values are not allowed in this context. Even when i call normal version command, config command, etc. Not sure whats causing this.

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

The only yaml file i used is

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

Whatever I try with kubectl it returns this error. What should I do to solve this?

> 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

Output of :

strace kubectl version

is here

5
You should correct your YAML. You might be using tab characters, remove those, and then check for correct indentation. Please replace the screenshots in your posts with text (so it can be indexed) and include the relevant lines of your YAML file (from the beginning to at least line 3).Anthon
how does yaml file affect "kubectl version" commandStarLord
Have you tried those URLs by hand? Which one gives something back that looks like a broken YAML file? What exactly is the content of the broken file?Anthon
It seems yaml syntax error in my case, use any online yaml linter to check for errors.Pratik
for me the reason was that i had 'replicas:1' instead of 'replicas: 1', that one space was causing error, be really careful and precise writing yaml by handurmat abdykerimov

5 Answers

12
votes

That the version command already throws an error indicates that there is some default YAML file that gets loaded.

You can use strace kubectl version to see what file was opened, hopefully this is done just before kubectl throws the error. I assume there is some global config that it reads (or alternatively a default file in your current directory).

It is of course sloppy programming in kubernetes not to catch such an error, and display the name of the file, and then re-raise the error.

8
votes

Most of the time when you get an error like this (speaking in general and meaningful terms) it is either because of :-

1). A syntax error (in your case it is not) in the yaml file.

2). Or like the error says "mapping values are not allowed in this context". It means that the keys/values you have used in the yaml, may be syntactically right but not semantically.

4
votes

Make sure you have completed the step:

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

Even as root user.

I also wasn't able to see version from

kubectl version
4
votes

Just to add... I have seen this error today after performing a copy/paste operation into my YAML file. The process brought in some whitespace characters that kubectl could not decipher.

If you are unsure, paste the YAML into a text editor first that will show all non-visible characters and make sure they are consistent with the rest of your YAML file.

3
votes

Since you're getting the error even upon running kubectl version, I'd say you've got a yaml syntax error in your kubeconfig file, located at ~/.kube/config by default.

You could validate its content using a yaml validator like this one.