Kubernetes YAML Templates with kubectl

Writing YAML files for kubernetes can be tedious and annoying. However, in many cases „kubectl“ can help to quickly and easily create a template for a specific YAML file.

„kubectl“ is the central management tool for a Kubernetes cluster. Besides querying and creating cluster information via the kubernetes API, kubectl can also generate YAML of the given commands.

Currently the following resources can be created with „kubectl create“:

  • clusterrole
  • clusterrolebinding
  • configmap
  • cronjob
  • deployment
  • job
  • namespace
  • poddisruptionbudget
  • priorityclass
  • quota
  • role
  • rolebinding
  • secret
  • secret docker-registry
  • secret generic
  • secret tls
  • service
  • service clusterip
  • service externalname
  • service loadbalancer
  • service nodeport
  • serviceaccount

A simple example is to create a YAML file to generate a deployment:

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

The result then looks like this:

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

In the YAML file, further parameters can be added or adjusted accordingly. For example, the number of replicas could be increased to 3. To apply the whole thing to the kubernetes cluster, the file is called with the following command:

kubectl create -f test-deployment.yaml

After that, the pods should generate themselves accordingly in the cluster.

Hinterlasse einen Kommentar