From the course: Learning Kubernetes

Reading and writing YAML - Kubernetes Tutorial

From the course: Learning Kubernetes

Reading and writing YAML

- [Instructor] Once you start working with Cloud Native Tech, you'll notice the terms Infrastructure as Code and GitOps all over the place. These two approaches are often paired with Kubernetes. In Kubernetes, setting up your infrastructure with code and preparing yourself to deploy changes through a GitOps workflow require that you can save the desired state of your cluster in a set of files that you manage with a source control system like Git. In Kubernetes, the most commonly used format for these files are YAML manifests. A quick note on the terms Infrastructure as Code and GitOps. We won't go in depth on these terms in this course, but when you're ready to learn more, I recommend you check out the course called GitOps Foundations in the LinkedIn Learning Library. YAML is a data serialization language often used for configuration files. Data serialization languages provide a standard syntax for communicating information so that devices, operating systems and programming languages can easily share data with one another. Examples of other data serialization languages are json and xml. Using a data serialization language makes your data portable. Remember, we used that word to talk about containers earlier in the course. Like a suitcase, YAML lets you package and pick up your data and take it to another location and unload it without requiring anything extra from the developer. YAML is an acronym for YAML aint markup language, which is a little strange because the first letter of the acronym is the acronym itself. Self-Referential acronyms aside, YAML is designed to be read and understood by humans as opposed to byte code or an assembly language, that provides instructions to a CPU and makes little sense to a person when they look at the code. If you know a handful of facts about YAML syntax, it will help you when you're working with Kubernetes manifests, and I'll show you a few of these things with the help of an example YAML file. Here it is. In YAML, three horizontal dashes on a line of its own means that it is the beginning of a document. You can have multiple documents in one file, and each document starts with the three dashes. A line that begins with a hash or number sign is a comment. In our example YAML file, our comment says "an instructor record." This is a note for the human reading the file and will be ignored by the program pulling data from the file. YAML stores key value pairs with a colon in between them. For example, in this file, the first key is name and the value is Kim Schlesinger. In YAML a list or array of items is called a sequence, and each item is proceeded by a dash and has its own line. In this example file, we have a sequence listing the two LinkedIn learning courses I've created. You can use YAML to create a map of nested key value pairs. On the first line, you have a key with a colon at the end. Then on the next line, you indent and add the next key value pair. In our example file, I have the jobs collection. Then a list of my past few jobs with the number of years I worked at each company, I even added a list of titles I had at Galvanize. There are a couple things about YAML that I want you to watch out for. First, YAML files can either have the dot yaml or dot yml extension. In this course I use dot yaml, but it's up to you or your engineering team on which extension you use. Second, it's really easy to mess up indentation in yaml, so if you're having issues, don't beat yourself up. Instead, I recommend you run your files through a validator like yamlchecker.com. Let's validate our example dot yaml file. We'll copy and paste it into the YAML checker. Yay, it looks like the way it's written is valid YAML, but let's change the indentation on the jobs map and we get an error. The validator tells us there's an issue with the map, and it points us to the line where the error is occurring. Let's fix that and get back to valid YAML syntax. In this video, we had a crash course on YAML, the data serialization language commonly used to create Kubernetes objects. YAML enables us to declare what we want to be true about our cluster and save those files for Infrastructure as Code and a GitOps workflow. Next, we're going to create a name space and deploy an application in our cluster, and we'll use YAML Manifest to make the magic happen.

Contents