mapping values are not allowed in this context mapping values are not allowed in this context kubernetes kubernetes

mapping values are not allowed in this context


You indicate you think the YAML format is normal, but it is not. This is a YAML error caused by the line at the end of the first document, starting with kubernetes.io/hostname being indented relative to the one before it. Because these are both key-value pair lines (i.e. item pairs of a block-style mapping) instead of

      nodeSelector: westporch-kubeminion-1        kubernetes.io/hostname: 10.0.24.52

you either need to do:

      nodeSelector: westporch-kubeminion-1      kubernetes.io/hostname: 10.0.24.52

but it might be that nodeSelector needs a mapping as argument instead of the scalar westporch-kubeminion-1 it has now.

      nodeSelector:        kubernetes.io/hostname: 10.0.24.52

This error might mask a second one in the file, depending on how lenient kubernetes is. The --- is the end-of-directives marker and since the directives are optional it can occur at the start of a YAML document. The one at the bottom of your example indicates the start of a new document. Once you address the first error, you might get a warning about single documents based on that. (The end-of-document marker consists of three dots: ... at the beginning of a line followed by whitespace.

Of course any changes you make should confirm to what kubernetes is expecting, but the above stream is clearly non-valid as YAML in itself.


In my case, the error reason for the error was an empty first line in the yaml.

When encountering this error - I strongly suggest to paste the yaml in YAML Linters (for example this one) that in some cases help to identify the problem more quickly.


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.

For example, in you case the context is "deployment" which belongs to "apiversion: extensions/v1beta1", and it expects the node selector to be like below:-

nodeSelector:  kubernetes.io/hostname: 10.0.24.52

Ideally, you should never use IP like above but a key-value pair like this:- topologyKey: failure-domain.beta.kubernetes.io/zone OR in your case it can be something like this:- kubernetes.io/hostname: zone-a-node