How To Write Kubernetes YAML File

Behdad Kardgar
6 min readMar 24, 2023

Kubernetes YAML files are used to define and configure Kubernetes objects such as deployments, pods, services, and more. In this article, we’ll walk through the basics of creating YAML files for Kubernetes, including best practices and examples.

Getting Started with Kubernetes YAML Files

To create a YAML file for a Kubernetes object, you will need to define the object’s properties and their values in a specific format. Here are the basic steps you should follow to create a Kubernetes YAML file:

  1. Choose the object you want to define: Decide which Kubernetes object you want to define, such as a deployment or a service.
  2. Choose the API version and kind: Determine the API version and kind of the object you want to define. The API version and kind provide Kubernetes with information about the structure of the YAML file.
  3. Define the object’s metadata: Add metadata to the YAML file, including the name of the object and any labels or annotations.
  4. Define the object’s specification: Add the specification for the object, including its desired state and any desired properties or settings.
  5. Save the YAML file: Save the YAML file to your local machine or a version control system like Git.

Now, let’s look at some examples of creating YAML files for Kubernetes objects.

Creating a YAML File for a Kubernetes Deployment

Here is an example YAML file for a Kubernetes deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80

This YAML file defines a Kubernetes deployment named “nginx-deployment” that will create three replicas of an Nginx web server. The file specifies the API version and kind, provides metadata about the deployment, and defines the desired state of the deployment.

Creating a YAML File for a Kubernetes Service

Here is an example YAML file for a Kubernetes service:

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer

This YAML file defines a Kubernetes service named “nginx-service” that exposes the Nginx deployment we defined earlier on port 80. The file specifies the API version and kind, provides metadata about the service, and defines the desired state of the service.

Best Practices for Creating Kubernetes YAML Files

Here are some best practices to keep in mind when creating YAML files for Kubernetes:

  1. Use consistent indentation: Use consistent indentation throughout your YAML file to improve readability and ensure that the file is parsed correctly.
  2. Use descriptive names: Use descriptive names for your Kubernetes objects, metadata, and labels to make it easier to identify and manage them.
  3. Use comments: Use comments in your YAML file to explain any complex or confusing sections of the file.
  4. Test your YAML files: Test your YAML files before deploying them to ensure that they are valid and will create the desired Kubernetes object.
  5. Use version control: Store your YAML files in version control systems like Git to track changes over time and make it easier to collaborate with other developers.

Extracting YAML Files from Kubernetes Objects

n addition to creating YAML files for Kubernetes objects, you may also need to extract the YAML files for existing objects. Extracting the YAML file for an existing Kubernetes object can be useful for debugging, troubleshooting, or migrating objects between clusters.

To extract the YAML file for a Kubernetes object, you can use the kubectl get command with the -o yaml flag. Here are the basic steps you should follow to extract a YAML file from a Kubernetes object:

  1. Determine the type of object: Decide which Kubernetes object you want to extract the YAML file for, such as a deployment or a service.
  2. Use the kubectl get command: Run the kubectl get command with the name of the object and the -o yaml flag to output the YAML representation of the object.
  3. Save the YAML file: Save the YAML file to your local machine or a version control system like Git.

Here is an example of how to extract the YAML file for a Kubernetes deployment:

kubectl get deployment nginx-deployment -o yaml > nginx-deployment.yaml

This command will output the YAML representation of the “nginx-deployment” deployment and save it to a file named “nginx-deployment.yaml”.

Here is an example of how to extract the YAML file for a Kubernetes service:

kubectl get service nginx-service -o yaml > nginx-service.yaml

This command will output the YAML representation of the “nginx-service” service and save it to a file named “nginx-service.yaml”.

Best Practices for Extracting Kubernetes YAML Files

Here are some best practices to keep in mind when extracting YAML files from Kubernetes objects:

  1. Use consistent formatting: Use consistent formatting in your extracted YAML files to make them easy to read and understand.
  2. Check for differences: Before making any changes to the extracted YAML files, compare them to the original files to identify any differences.
  3. Use version control: Store your extracted YAML files in version control systems like Git to track changes over time and make it easier to collaborate with other developers.

— dry-run=client -o yaml

Another way to extract the YAML file for a Kubernetes object is to use the --dry-run=client -o yaml option with the kubectl create command. This option tells Kubernetes to generate the YAML file for the object without actually creating the object.

Here are the basic steps you should follow to extract a YAML file from a Kubernetes object using the --dry-run=client -o yaml option:

  1. Determine the type of object: Decide which Kubernetes object you want to extract the YAML file for, such as a deployment or a service.
  2. Use the kubectl create command: Run the kubectl create command with the appropriate flags and options for the object you want to create, along with the --dry-run=client -o yaml option to output the YAML representation of the object.
  3. Save the YAML file: Save the YAML file to your local machine or a version control system like Git.

Here is an example of how to extract the YAML file for a Kubernetes deployment using the --dry-run=client -o yaml option:

kubectl create deployment nginx-deployment --image=nginx --replicas=3 --dry-run=client -o yaml > nginx-deployment.yaml

This command will output the YAML representation of the “nginx-deployment” deployment with three replicas and save it to a file named “nginx-deployment.yaml”.

Here is an example of how to extract the YAML file for a Kubernetes service using the --dry-run=client -o yaml option:

kubectl create service clusterip nginx-service --tcp=80:80 --dry-run=client -o yaml > nginx-service.yaml

This command will output the YAML representation of the “nginx-service” service with a ClusterIP and port 80 mapped to the container port 80, and save it to a file named “nginx-service.yaml”.

Best Practices for Extracting Kubernetes YAML Files

Here are some best practices to keep in mind when using the --dry-run=client -o yaml option to extract YAML files from Kubernetes objects:

  1. Use consistent formatting: Use consistent formatting in your extracted YAML files to make them easy to read and understand.
  2. Check for differences: Before making any changes to the extracted YAML files, compare them to the original files to identify any differences.
  3. Use version control: Store your extracted YAML files in version control systems like Git to track changes over time and make it easier to collaborate with other developers.

Conclusion

Kubernetes YAML files are a critical component of defining and configuring Kubernetes objects. By following the best practices outlined in this article and using examples like the ones provided, you can create and extract YAML files that are easy to read, modify, and manage. With the ability to create and extract YAML files, you’ll be well on your way to mastering Kubernetes.

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