Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

YAML and Its Usage in Kubernetes

DZone's Guide to

YAML and Its Usage in Kubernetes

If you've used Kubernetes before, chances are you have come into contact with YAML files. Find out more about what they do here.

· Cloud Zone ·
Free Resource

Discover a centralized approach to monitor your virtual infrastructure, on-premise IT environment, and cloud infrastructure – all on a single platform.

YAML is a human-friendly data serialization standard for all programming language.

Purpose of YAML

There are hundreds of different languages for programming, but only a handful of languages for storing and transferring data. YAML is a human-friendly data serialization standard for all programming language. Even though its potential is virtually boundless, YAML was specifically created to work well for common use cases such as:

  1. configuration files,
  2. log files,
  3. interprocess messaging,
  4. cross-language data sharing,
  5. object persistence, and
  6. complex data structures

YAML Usage in Kubernetes

The Kubernetes resources are created through a declarative way making use of YAML files. Kubernetes resources such as pods, services, and deployments are created using the YAML files.

This example helps explain the creation of the deployment resource using the YAML:

Note: This example contains both basic and advanced specifications.

apiVersion: extensions/v1beta1
kind: Deployment
metadata: 
   name: nginx-deployment
spec:
   revisionHistoryLimit: 5
   minReadySeconds: 10
   selector:
      matchLabels:
         app: nginx
         deployer: distelli
   strategy:
      type: RollingUpdate
      rollingUpdate:
         maxUnavailable: 1
         maxSurge: 1
   replicas: 3
   template:
      metadata:
         labels:
            app: nginx
            deployer: distelli
      spec:
         containers:
            - name: nginx
              image: nginx: 1.7.9

Explanation of Various Fields

  1.  replicas– Tells Kubernetes how many Pods to create during a deployment. Modifying this field is an easy way to scale a containerized application
  2.  spec.strategy.type – The Rolling Update strategy allows Kubernetes to update a service without facilitating an outage by proceeding to update Pods one at a time.
  3.  spec.strategy.rollingUpdate.maxUnavailable – It is the maximum number of pods that can be unavailable during the update.
  4.  spec.strategy.rollingUpdate.maxSurge – It is the maximum number of pods that can be scheduled above the desired number of pods.
  5.  spec.strategy.rollingUpdate.maxUnavailable and spec.strategy.rollingUpdate.maxSurge  can also be defined as a percentage of all Pods.
  6.  spec.minReadySeconds – It is an optional Integer that describes the minimum number of seconds for which a new pod should be ready without any of its containers crashing, for it to be considered available.
  7.  spec.revisionHistoryLimit – It is an optional integer attribute you can use to tell Kuberneres explicitly how many old ReplicaSets to retain at any given time.
  8.  spec.template.metadata.labels – It is to add labels to a deployment specification.
  9.  spec.selector – It is an optional object that tells the Kubernetes deployment controller to only target pods that match the specified labels. Thus, to only target pods with the labels “app” and “deployer” we can make the following modification to our deployment yaml.

So with the above information, any developer, from beginner to intermediate, would be able to write a yaml file for creation of various Kubernetes resources.

Learn how to auto-discover your containers and monitor their performance, capture Docker host and container metrics to allocate host resources, and provision containers.

Topics:
kubernetes ,yaml ,data serialization ,data language ,code snippet

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}