UPDATE: The code in this article has been updated to reflect changes in more recent versions of Kubernetes.)
Watch a recording of author Nick Chase in a webinar on Kubernetes Deployments using YAML.
In previous articles, we’ve been talking about how to use Kubernetes to spin up resources. So far, we’ve been working exclusively with the CLI, but there’s an easier and more useful way to do it: creating configuration files using kubernetes YAML. In this article, we’ll look at how YAML works and use it to define first a Kubernetes Pod, and then a Kubernetes Deployment.
YAML Basics
It’s difficult to escape YAML if you’re doing anything related to many software fields — particularly Kubernetes, SDN, and OpenStack. YAML, which stands for Yet Another Markup Language, or YAML Ain’t Markup Language (depending who you ask) is a human-readable text-based format for specifying configuration-type information. For example, in this article, we’ll pick apart the YAML definitions for creating first a Pod, and then a Deployment.
When defining a Kubernetes manifest, YAML gives you a number of advantages, including:
- Convenience: You’ll no longer have to add all of your parameters to the command line
- Maintenance: YAML files can be added to source control, such as a Github repository so you can track changes
- Flexibility: You’ll be able to create much more complex structures using YAML than you can on the command line
YAML is a superset of JSON, which means that any valid JSON file is also a valid YAML file. So on the one hand, if you know JSON and you’re only ever going to write your own YAML (as opposed to reading other people’s) you’re all set. On the other hand, that’s not very likely, unfortunately. Even if you’re only trying to find examples on the web, they’re most likely in (non-JSON) YAML, so we might as well get used to it. Still, there may be situations where the JSON format is more convenient, so it’s good to know that it’s available to you.
Fortunately, YAML is relatively easy to learn. There are only two types of structures you need to know about in YAML:
- Lists
- Maps
That’s it. You might have maps of lists and lists of maps, and so on, but if you’ve got those two structures down, you’re all set. That’s not to say there aren’t more complex things you can do, but in general, this is all you need to get started.
YAML Maps
Let’s start by looking at YAML maps. Maps let you associate name-value pairs, which of course is convenient when you’re trying to set up configuration information. For example, you might have a config file that starts like this:
--- apiVersion: v1 kind: Pod
The first line is a separator, and is optional unless you’re trying to define multiple structures in a single file. From there, as you can see, we have two values, v1 and Pod, mapped to two keys, apiVersion, and kind.
This kind of thing is pretty simple, of course, and you can think of it in terms of its JSON equivalent:
{ "apiVersion": "v1", "kind": "Pod" }
Notice that in our YAML version, the quotation marks are optional; the processor can tell that you’re looking at a string based on the formatting.
You can also specify more complicated structures by creating a key that maps to another map, rather than a string, as in:
--- apiVersion: v1 kind: Pod metadata: name: rss-site labels: app: web