YAML File In Kubernetes

Behdad Kardgar
2 min readMar 24, 2023

--

YAML (short for “YAML Ain’t Markup Language”) is a human-readable data serialization format that is commonly used in software development, including in Kubernetes. In Kubernetes, YAML files are used to define and configure Kubernetes objects, such as pods, services, and deployments.

Why Use YAML in Kubernetes?

Using YAML files in Kubernetes has several benefits, including:

  1. Easy to Read and Write: YAML files are human-readable and easy to write, making them a convenient way to define Kubernetes objects.
  2. Declarative Configuration: YAML files allow you to declare the desired state of your Kubernetes objects, rather than specifying the steps needed to achieve that state. This declarative approach makes it easier to manage your Kubernetes objects and ensures that they remain in the desired state.
  3. Version Control: YAML files can be stored in version control systems like Git, making it easy to track changes to your Kubernetes objects over time.
  4. Reusability: Once you have defined a Kubernetes object in a YAML file, you can reuse that file to create additional instances of the same object.

Creating a YAML File for a Kubernetes Object

To create a YAML file for a Kubernetes object, you need to define the object’s properties and their values in a specific format. Here’s an example YAML file for a simple Kubernetes pod:

apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80

In this example, the YAML file defines a Kubernetes pod named “my-pod” with a single container running the Nginx web server. The apiVersion and kind fields specify the Kubernetes API version and the type of object being defined, respectively. The metadata field provides metadata about the object, such as its name. Finally, the spec field defines the desired state of the object, including its containers and their properties.

Once you have created a YAML file for a Kubernetes object, you can create the object by running the kubectl applycommand with the filename as an argument:

kubectl apply -f my-pod.yaml

This will create the pod defined in the YAML file and ensure that it remains in the desired state.

Conclusion

YAML files are a powerful tool for defining and configuring Kubernetes objects. They provide an easy-to-read, declarative approach to managing your Kubernetes objects, making it easier to maintain the desired state of your applications. By learning how to create and use YAML files in Kubernetes, you can streamline your development workflow and ensure that your Kubernetes objects are always in the desired state.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Behdad Kardgar
Behdad Kardgar

No responses yet

Write a response