Get Started Free

How to Write YAML for Kubernetes

write yaml

Kubernetes users must have a good understanding of YAML when dealing with the popular orchestration framework as it is used across the board for all the configurations. These configurations range from pod creations, deployments, secrets to network configurations. In this post, we will see how to use YAML for pods and deployments in K8s.

Introduction to YAML

YAML, Yet Another Markup Language, is a data serialization language that is often used for creating configuration files. Its goal is to provide a more human-readable, easily understandable, and complete information model than other options such as XML or JSON.

YAML uses a similar indentation to Python with whitespaces and without tabs or format symbols like braces, quotation marks, tags, etc. The basic structure of a YAML file can consist of key-value pairs, arrays/lists, and dictionaries/maps. These YAML files can be defined either using the .yml or .yaml extension and easily moved between programming languages. This flexibility allows YAML to be used in most kinds of confirmations and managed directly via a source control system.

In Kubernetes, a typical YAML file will consist of the following fields to define the necessary resources.

  • apiVersion – The Kubernetes API version used to create the object
  • kind – The kind of the object
  • metadata – Data to identify the object which can include names, UID, labels, etc…
  • spec – The state (configuration) of the object. The format of this field can significantly vary from object to object.

How to Use YAML for Pods

A Pod is the most basic deployable object in K8s. It is a single instance of a running process and can contain multiple containers. Users can utilize the YAML file to define a Pod within a Kubernetes cluster. They can create pods by defining a Pod object in a YAML configuration and using the “kubectl apply” command.

The following configuration shows a simple YAML Pod configuration for an Nginx server container.

apiVersion: v1
# K8s Object
kind: Pod
metadata:
  # Pod Name
  name: nginx-server
  labels:
    app: test-web-app
spec:
  containers:
    # Container Information
    - name: web
      image: nginx
      # Resource Limits
      resources:
        requests:
          cpu: 250m
          memory: 256Mi
        limits:
          cpu: 500m
          memory: 512Mi
      # Network Configuration
      ports:
        - name: web
          containerPort: 80
          protocol: TCP

Note – It is highly encouraged to define resource limits even though it is not mandatory, so that the cluster can efficiently manage and allocate the available resources.

RESULT

Kubectl get pods

How to Use YAML for Deployments

A Deployment in Kubernetes refers to a configuration where a user can define the desired state for an application (Pod). Kubernetes will deploy and manage the required number of Pods (Replicas) for a deployment. These Pods are managed through the Kubernetes Deployment Controller using a Pod template that contains the Pod specifications to create the required Pods. Any changes to this configuration will be automatically reflected in the running Pods, and it will also remove and recreate the Pods if necessary.

Why is using Deployments Better than Deploying Pods?

Let’s consider the following simple example. Assume that you need to create three Pods with identical configurations to provide a highly available application within a Kubernetes cluster. If we are using Pod configuration, we will have to create three different YAML configurations and apply them to create the required number of Pods. When it comes to managing these Pods, the users will have to individually change each YAML configuration and redeploy the Pods if there is a need for a configuration change.

However, a Deployment allows users to use a single YAML configuration with a replica count of 3 to create three identical Pods. For any change, the user can simply edit this deployment and apply it so that Kubernetes will automatically make the necessary modifications to the Pods. Even if there is an error in a Pod making it unresponsive, the deployment controller will automatically spin up new Pods and ensure that the defined number of Pods are always active.

This way, Deployments offer far greater flexibility when managing applications (Pods) within K8s. A direct YAML Pod configuration might be a good solution for creating a single Pod or testing an application, yet a more flexible solution is required when managing applications at scale. Thus, Deployments are the go-to solution when dealing with stateless applications in Kubernetes.

Let’s look at the following YAML deployment configuration that shows how to use a deployment to create three Pods with Nginx containers.

apiVersion: apps/v1
# K8s Object
kind: Deployment
metadata:
  # Deployment Name
  name: nginx-server-deployment
spec: 
  # No of Pods
  replicas: 3
  selector:
    matchLabels:
      app: nginx-server
  strategy:
    # Update Strategy
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  # Pod Template
  template:
    metadata:
      labels:
        app: nginx-server
    # Pod Specifications
    spec:
      containers:
      # Container Information
      - name: nginx-server
        image: nginx
        # Resource Limits
        resources:
          requests:
            cpu: 250m
            memory: 256Mi
          limits:
            cpu: 500m
            memory: 512Mi
        # Network Configuration
        ports:
        - name: web
          containerPort: 80
          protocol: TCP

RESULT

Kubectl get pods-2

Now let’s have a look at the following deployment-specific fields in the YAML file.

  • kind – It defines this YAML as a deployment configuration with Deployment as the value.
  • replicas – The number of pods to be created and maintained by this deployment.
  • selector.matchLabels – Inform the deployment controller to only select Pods containing specific labels, which is the “app” label in this instance.
  • strategy.type – The update strategy to be used when updating the Pods. For instance, the rolling update strategy ensures that there will be no service interruptions while updating.
  • strategy.rollingupdate.maxUnaviable – Maximum number of Pods that will be unavailable during an update.
  • strategy.rollingupdate.maxSurge – Maximum number of Pods that can be scheduled above the desired number.
  • template – The configuration template for each Pod
  • template.metadata.lables – Labels for the template which can be used by a selector to target specific pods.

All the other configurations which define the container images, network config, configuration name (deployment/pod name), and resource limits are the same as a Pod.

Conclusion

YAML is the cornerstone for creating configurations, yet it can quickly become complex when managing large applications with different resources. With this increased complexity, more and more resources are required to properly manage all these YAML configurations and keep the cluster functioning properly. Therefore, YAML configurations should become a part of the development process with proper tools to configure, validate and deploy these files on K8s clusters.

Platforms like CloudPlex eliminate the need to search for valid parameters and supported values and manually create YAML configurations. Instead, they can directly configure services using a single interface. The platform will then perform validations and generate all the required files, which can also be downloaded and applied to any K8s cluster.

The following screenshot is a visual interface used by the developer to create the same application as above. As you can see there, the developer can create the application simply by dragging and dropping containers from a palette to the canvass and configure the containers visually using the Config Panel:

Using CloudPlex

Spend more time coding and less time managing YAML, and start using CloudPlex for free.

Start building app

Start building your cloud native application

166530cookie-checkHow to Write YAML for Kubernetes