Kubernetes 06 - YAML Generators

In Kubernetes, managing resources efficiently is key to maintaining an environment that is both scalable and reliable. One powerful feature is the use of YAML generators, which streamline the creation and management of resources.

Resource Generators

The dry-run feature in kubectl is a powerful tool that allows users to simulate a command without actually creating the resource. By using --dry-run=client -o yaml, you can generate and view the YAML configuration that would be applied, providing a safe way to review and tweak your configurations.

kubectl create deployment sample --image nginx --dry-run=client -o yaml

The above command simulates the creation of a deployment without actually deploying it, outputting the YAML configuration instead.

Generator Examples

Using the dry-run option with YAML output, we can inspect the default templates generated by Kubernetes for various resources.

Creating a Deployment

To create a deployment named test with an Nginx image, you can use the following command:

kubectl create deployment test --image nginx --dry-run=client -o yaml

This generates the following YAML configuration:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: test
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: test
    spec:
      containers:
        - image: nginx
          name: nginx
          resources: {}
status: {}

Creating a Job

A Job is used for running a pod to completion, unlike a Deployment which manages a set of replica pods. To create a Job named test:

kubectl create job test --image nginx --dry-run=client -o yaml

This generates the following YAML configuration:

apiVersion: batch/v1
kind: Job
metadata:
  creationTimestamp: null
  name: test
spec:
  template:
    metadata:
      creationTimestamp: null
    spec:
      containers:
        - image: nginx
          name: test
          resources: {}
      restartPolicy: Never
status: {}

Exposing a Deployment as a Service

To expose the test deployment on port 80, you can use the following command:

kubectl expose deployment/test --port 80 --dry-run=client -o yaml

If the deployment doesn’t exist, you’ll encounter an error. First, create the deployment:

kubectl create deployment test --image nginx

Then expose it:

kubectl expose deployment/test --port 80 --dry-run=client -o yaml

This generates the following YAML configuration for a Service:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: test
  name: test
spec:
  ports:
    - port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: test
status:
  loadBalancer: {}

Cleaning Up

You can delete the resources created using the following commands:

kubectl delete deployment test



    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • Dependency Injection
  • CPU Cache
  • Understanding Linear Blended Skinning in 3D Animation
  • Starvation in Operating Systems
  • Virtual Memory