20 Kubernetes Commands and Templates Every DevOps Engineer Must Know

20 Kubernetes Commands and Templates Every DevOps Engineer Must Know

Kubernetes, also known as K8s, is a popular open-source container orchestration platform that has revolutionized the way organizations deploy, manage, and scale their applications. As a DevOps engineer, mastering Kubernetes commands and templates is essential to manage Kubernetes clusters efficiently. In this article, we will explore the top 20 Kubernetes commands and templates that every DevOps engineer must know.

Table of Contents

  1. Introduction to Kubernetes

  2. Why DevOps Engineers Should Know Kubernetes Commands and Templates

  3. Top 20 Kubernetes Commands and Templates

    1. kubectl apply

    2. kubectl get

    3. kubectl describe

    4. kubectl logs

    5. kubectl delete

    6. kubectl rollout

    7. kubectl exec

    8. kubectl port-forward

    9. kubectl scale

    10. kubectl expose

    11. kubectl label

    12. kubectl annotate

    13. kubectl patch

    14. kubectl create secret

    15. kubectl create configmap

    16. kubectl apply deployment template

    17. kubectl apply service template

    18. kubectl apply ingress template

    19. kubectl apply statefulset template

    20. kubectl apply persistent volume template

  4. Conclusion

Introduction to Kubernetes

Kubernetes is an open-source container orchestration platform developed by Google. It automates the deployment, scaling, and management of containerized applications. Kubernetes provides a container-centric management environment and a powerful set of APIs to manage containerized applications across different cloud providers and on-premise infrastructure.

Why DevOps Engineers Should Know Kubernetes Commands and Templates

As a DevOps engineer, your job involves deploying, managing, and scaling applications. Kubernetes commands and templates help automate these tasks and make them more efficient. Knowing these commands and templates enables you to manage Kubernetes clusters more effectively and helps you save time and effort.

Top 20 Kubernetes Commands and Templates

1. kubectl apply

The kubectl apply command is used to apply a configuration to a Kubernetes cluster. It creates new resources or updates existing ones in the cluster.

2. kubectl get

The kubectl get command is used to list resources in a Kubernetes cluster. You can use this command to get information about pods, deployments, services, and other resources in the cluster.

3. kubectl describe

The kubectl describe command is used to get detailed information about a resource in a Kubernetes cluster. You can use this command to get information about pods, deployments, services, and other resources in the cluster.

4. kubectl logs

The kubectl logs command is used to get the logs of a container in a pod. You can use this command to troubleshoot issues with your applications.

5. kubectl delete

The kubectl delete command is used to delete a resource in a Kubernetes cluster. You can use this command to delete pods, deployments, services, and other resources in the cluster.

6. kubectl rollout

The kubectl rollout command is used to manage the rollout of a deployment in a Kubernetes cluster. You can use this command to roll out a new version of your application and roll back to a previous version if needed.

7. kubectl exec

The kubectl exec command is used to execute a command inside a container in a pod. You can use this command to debug your application or run maintenance tasks.

8. kubectl port-forward

The kubectl port-forward command is used to forward a local port to a port on a pod. You can use this command to access a service running inside a pod.

9. kubectl scale

The kubectl scale command is used to scale a deployment in a Kubernetes cluster. You can use this command to increase or decrease the number of replicas of your application.

10. kubectl expose

The kubectl expose command is used to create a new service in a Kubernetes cluster. You can use this command to expose a deployment or a pod to the outside world.

11. kubectl label

The kubectl label command is used to add or remove labels from a resource in a Kubernetes cluster. You can use this command to organize your resources and apply policies.

12. kubectl annotate

The kubectl annotate command is used to add or remove annotations from a resource in a Kubernetes cluster. You can use this command to add metadata to your resources.

13. kubectl patch

The kubectl patch command is used to update a resource in a Kubernetes cluster. You can use this command to modify the configuration of your resources.

14. kubectl create secret

The kubectl create secret command is used to create a new secret in a Kubernetes cluster. You can use this command to store sensitive information like passwords, keys, and certificates.

15. kubectl create configmap

The kubectl create configmap command is used to create a new configmap in a Kubernetes cluster. You can use this command to store configuration data for your applications.

16. kubectl apply deployment template

The kubectl apply deployment template command is used to create a new deployment in a Kubernetes cluster from a YAML template file.

Here is an example of a Kubernetes deployment template that creates a deployment with a single container:

yamlCopy codeapiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

In this example, the template creates a deployment named "nginx-deployment" with 3 replicas. The deployment runs a single container with the nginx image and exposes port 80.

You can apply this template to a Kubernetes cluster using the kubectl apply command:

Copy codekubectl apply -f nginx-deployment.yaml

This will create the deployment in your cluster based on the configuration defined in the template.

17. kubectl apply service template

The kubectl apply service template command is used to create a new service in a Kubernetes cluster from a YAML template file.

Service Template:

yamlCopy codeapiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - name: http
      port: 80
      targetPort: 8080
  type: ClusterIP

In this example, the Service template creates a Service named my-service that exposes port 80 and targets port 8080 on Pods with the app=my-app label. The Service type is ClusterIP, which creates a virtual IP address that is used to communicate with the Pods.

18. kubectl apply ingress template

The kubectl apply ingress template command is used to create a new ingress in a Kubernetes cluster from a YAML template file.

Ingress Template:

yamlCopy codeapiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: my.domain.com
      http:
        paths:
          - path: /app
            pathType: Prefix
            backend:
              service:
                name: my-service
                port:
                  name: http

In this example, the Ingress template creates an Ingress named my-ingress that maps the path /app on the host my.domain.com to the Service named my-service. The annotation nginx.ingress.kubernetes.io/rewrite-target rewrites the request URI to / before sending it to the Service.

19. kubectl apply statefulset template

The kubectl apply statefulset template command is used to create a new statefulset in a Kubernetes cluster from a YAML template file.

StatefulSet Template:

yamlCopy codeapiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-statefulset
spec:
  selector:
    matchLabels:
      app: my-app
  serviceName: my-service
  replicas: 3
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts:
        - name: my-volume
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: my-volume
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

In this example, the StatefulSet template creates a StatefulSet named my-statefulset with 3 replicas. The StatefulSet runs a single container named my-container with the nginx image and exposes port 80. The StatefulSet also creates a Persistent Volume Claim with a request for 1Gi of storage that is mounted to the container at /data.

20. kubectl apply persistent volume template

The kubectl apply persistent volume template command is used to create a new persistent volume in a Kubernetes cluster from a YAML template file.

Persistent Volume Template:

yamlCopy codeapiVersion: v1
kind: PersistentVolume
metadata:
  name: my-volume
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /mnt/data

In this example, the Persistent Volume template creates a Persistent Volume named my-volume with a capacity of 1Gi and an access mode of ReadWriteOnce. The Persistent Volume is created using the hostPath storage type, which mounts a directory on the host at /mnt/data as a volume in the Pod.

Conclusion

Kubernetes commands and templates are essential for DevOps engineers to manage Kubernetes clusters efficiently. In this article, we have explored the top 20 Kubernetes commands and templates that every DevOps engineer must know. By mastering these commands and templates, you can automate your tasks and save time and effort.