Due to its popularity in the DevOps and container orchestration communities, many organisations and developers who use tools like Kubernetes, Ansible, and Docker are likely to be using YAML on a regular basis. YAML (Yet Another Markup Language) is a human-readable data serialization format that is often used for configuration files and data exchange between different systems. It’s popular for its simplicity and ease of use compared to other formats like XML or JSON.
Kubernetes, the popular open-source container orchestration system, uses YAML to define its configuration files, known as manifests. These manifests are used to define and deploy resources such as pods, services, and deployment configurations.
YAML’s simplicity and ease of use make it a popular choice for Kubernetes configuration, as it allows developers to quickly and easily define and deploy their applications. Additionally, the hierarchical structure of YAML makes it easy to read and understand complex configurations.
YAML is important for Kubernetes because it provides a simple, human-readable format for defining and deploying complex configurations, making it easier for developers to work with the system.
Here are the top 10 Kubernetes YAML tips with examples:
Namespaces allow you to logically group resources and manage access control for different parts of your application. For example, you can create a namespace for development, testing, and production environments:
apiVersion: v1 kind: Namespace metadata: name: development
Labels and selectors allow you to easily organize and select resources based on specific criteria. For example, you can label a deployment with an app name and use selectors to find all the pods that belong to that deployment:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: selector: matchLabels: app: nginx
ConfigMaps and Secrets allow you to manage configuration data separately from your containers and pods. For example, you can store environment variables in a ConfigMap:
apiVersion: v1 kind: ConfigMap metadata: name: env-config data: ENV_VAR_1: "value1" ENV_VAR_2: "value2"
Resource limits and requests allow you to specify the amount of resources your pods and containers need. For example, you can specify CPU and memory limits for a container:
apiVersion: v1 kind: Pod metadata: name: mypod spec: containers: - name: mycontainer image: myimage resources: limits: memory: "64Mi" cpu: "500m" requests: memory: "32Mi" cpu: "250m"
InitContainers allow you to run tasks before your main container starts. For example, you can use an InitContainer to install packages before your main container starts:
apiVersion: v1 kind: Pod metadata: name: mypod spec: initContainers: - name: init-container image: busybox command: ["/bin/sh", "-c", "apt-get update && apt-get install -y mypackage"] containers: - name: mycontainer image: myimage
Readiness and liveness probes allow you to check the health of your containers and ensure that they are ready to serve traffic. For example, you can use a liveness probe to ensure that your container is running correctly:
apiVersion: v1 kind: Pod metadata: name: mypod spec: containers: - name: mycontainer image: myimage livenessProbe: httpGet: path: /healthz port: 8080 initialDelaySeconds: 15 periodSeconds: 5
Rollouts and rollbacks allow you to manage changes to your deployment and ensure that
Use rolling updates to deploy changes gradually:
apiVersion: apps/v1 kind: Deployment metadata: name: example-deployment spec: replicas: 3 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 0 selector: matchLabels: app: example-app template:
# This is a comment in a YAML file apiVersion: v1 kind: Pod metadata: name: example-pod labels: app: example-app
# Anchor definition anchors: pod_template: &pod_template apiVersion: v1 kind: Pod metadata: labels: app: example-app # Aliased resource definition apiVersion: v1 kind: Pod metadata: name: example-pod1 <<: *pod_template apiVersion: v1 kind: Pod metadata: name: example-pod2 <<: *pod_template
apiVersion: v1 kind: Pod metadata: name: example-pod spec: containers: - name: example-container image: example-image env: - name: EXAMPLE_VAR value: example-value
apiVersion: v1 kind: ConfigMap metadata: name: example-configmap data: key: value apiVersion: v1 kind: Secret metadata: name: example-secret stringData: username: user password: pass