DEV Community

Cover image for 🐳 Kubernetes Commands: From Beginner to Advanced for DevOps Engineers

🐳 Kubernetes Commands: From Beginner to Advanced for DevOps Engineers

Introduction

Kubernetes, also known as K8s, is a powerful open-source system for automating the deployment, scaling, and management of containerized applications. Whether you are new to Kubernetes or an experienced user, mastering its commands is crucial for efficiently managing your clusters. This article provides a comprehensive guide to Kubernetes commands, organized from beginner to advanced levels.

🎯 Key Concepts

Before we dive into the commands, let's review some fundamental Kubernetes concepts:

  • Pod: The smallest and simplest Kubernetes object, representing a single instance of a running process in your cluster.
  • Node: A worker machine in Kubernetes, which can be a virtual or a physical machine.
  • Namespace: A way to divide cluster resources between multiple users.
  • Deployment: A higher-level abstraction that manages a replicated application, ensuring that a specified number of replicas are running.
  • Service: An abstraction that defines a logical set of pods and a policy by which to access them.
  • ConfigMap and Secret: Mechanisms to inject configuration data into your applications.

🏁 Beginner Commands

1. Cluster Information

Get Cluster Info



kubectl cluster-info


Enter fullscreen mode Exit fullscreen mode

Displays the address of the Kubernetes master and services running in the cluster.

Get Nodes



kubectl get nodes


Enter fullscreen mode Exit fullscreen mode

Lists all nodes in the cluster.

2. Namespace Management

List Namespaces



kubectl get namespaces


Enter fullscreen mode Exit fullscreen mode

Displays all namespaces in the cluster.

Create Namespace



kubectl create namespace my-namespace


Enter fullscreen mode Exit fullscreen mode

Creates a new namespace.

Delete Namespace



kubectl delete namespace my-namespace


Enter fullscreen mode Exit fullscreen mode

Deletes a specified namespace.

3. Pod Management

List Pods



kubectl get pods
kubectl get pods -n my-namespace


Enter fullscreen mode Exit fullscreen mode

Lists all pods in the default namespace or a specified namespace.

Describe Pod



kubectl describe pod my-pod


Enter fullscreen mode Exit fullscreen mode

Displays detailed information about a specific pod.

Create Pod



kubectl run my-pod --image=nginx


Enter fullscreen mode Exit fullscreen mode

Creates a new pod running the specified container image.

Delete Pod



kubectl delete pod my-pod


Enter fullscreen mode Exit fullscreen mode

Deletes a specified pod.

4. Deployment Management

List Deployments



kubectl get deployments
kubectl get deployments -n my-namespace


Enter fullscreen mode Exit fullscreen mode

Lists all deployments in the default namespace or a specified namespace.

Create Deployment



kubectl create deployment my-deployment --image=nginx


Enter fullscreen mode Exit fullscreen mode

Creates a new deployment with the specified container image.

Delete Deployment



kubectl delete deployment my-deployment


Enter fullscreen mode Exit fullscreen mode

Deletes a specified deployment.

5. Service Management

List Services



kubectl get services
kubectl get services -n my-namespace


Enter fullscreen mode Exit fullscreen mode

Lists all services in the default namespace or a specified namespace.

Create Service



kubectl expose deployment my-deployment --type=LoadBalancer --name=my-service


Enter fullscreen mode Exit fullscreen mode

Creates a new service to expose a deployment.

Delete Service



kubectl delete service my-service


Enter fullscreen mode Exit fullscreen mode

Deletes a specified service.

🚀 Intermediate Commands

1. ConfigMap and Secret Management

List ConfigMaps



kubectl get configmaps
kubectl get configmaps -n my-namespace


Enter fullscreen mode Exit fullscreen mode

Lists all ConfigMaps in the default namespace or a specified namespace.

Create ConfigMap



kubectl create configmap my-config --from-literal=key1=value1 --from-literal=key2=value2


Enter fullscreen mode Exit fullscreen mode

Creates a new ConfigMap from literal values.

List Secrets



kubectl get secrets
kubectl get secrets -n my-namespace


Enter fullscreen mode Exit fullscreen mode

Lists all secrets in the default namespace or a specified namespace.

Create Secret



kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=secret


Enter fullscreen mode Exit fullscreen mode

Creates a new secret from literal values.

2. Scaling Applications

Scale Deployment



kubectl scale deployment my-deployment --replicas=3


Enter fullscreen mode Exit fullscreen mode

Scales a deployment to the specified number of replicas.

3. Updating Applications

Update Deployment



kubectl set image deployment/my-deployment nginx=nginx:1.19.1


Enter fullscreen mode Exit fullscreen mode

Updates the container image in a deployment.

Rollout Status



kubectl rollout status deployment/my-deployment


Enter fullscreen mode Exit fullscreen mode

Displays the rollout status of a deployment.

Rollback Deployment



kubectl rollout undo deployment/my-deployment


Enter fullscreen mode Exit fullscreen mode

Rolls back a deployment to the previous revision.

4. Monitoring and Logging

View Pod Logs



kubectl logs my-pod


Enter fullscreen mode Exit fullscreen mode

Displays the logs of a specified pod.

View Previous Pod Logs



kubectl logs my-pod --previous


Enter fullscreen mode Exit fullscreen mode

Displays the logs of a previous instance of a specified pod.

Get Events



kubectl get events
kubectl get events -n my-namespace


Enter fullscreen mode Exit fullscreen mode

Lists all events in the default namespace or a specified namespace.

5. Resource Management

Describe Node



kubectl describe node my-node


Enter fullscreen mode Exit fullscreen mode

Displays detailed information about a specified node.

Label Nodes



kubectl label nodes my-node disktype=ssd


Enter fullscreen mode Exit fullscreen mode

Adds a label to a node.

Annotate Resources



kubectl annotate pod my-pod description="my example pod"


Enter fullscreen mode Exit fullscreen mode

Adds an annotation to a resource.

6. Network Policies

Create Network Policy



apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-nginx
  namespace: my-namespace
spec:
  podSelector:
    matchLabels:
      app: nginx
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: frontend


Enter fullscreen mode Exit fullscreen mode

Apply the network policy:



kubectl apply -f network-policy.yaml


Enter fullscreen mode Exit fullscreen mode

🧠 Advanced Commands

1. Advanced Pod Management

Debug Pod



kubectl debug my-pod --image=busybox --target=my-container


Enter fullscreen mode Exit fullscreen mode

Debugs a running pod by creating a new debugging container in the pod.

Port Forwarding



kubectl port-forward my-pod 8080:80


Enter fullscreen mode Exit fullscreen mode

Forwards a local port to a port on a pod.

2. Advanced Node Management

Cordon Node



kubectl cordon my-node


Enter fullscreen mode Exit fullscreen mode

Marks a node as unschedulable.

Drain Node



kubectl drain my-node --ignore-daemonsets


Enter fullscreen mode Exit fullscreen mode

Safely evicts all pods from a node before maintenance.

Uncordon Node



kubectl uncordon my-node


Enter fullscreen mode Exit fullscreen mode

Marks a node as schedulable.

3. Resource Quotas and Limits

Create Resource Quota



apiVersion: v1
kind: ResourceQuota
metadata:
  name: compute-resources
  namespace: my-namespace
spec:
  hard:
    pods: "10"
    requests.cpu: "4"
    requests.memory: 16Gi
    limits.cpu: "10"
    limits.memory: 32Gi


Enter fullscreen mode Exit fullscreen mode

Apply the resource quota:



kubectl apply -f resource-quota.yaml


Enter fullscreen mode Exit fullscreen mode

4. Custom Resources and CRDs

Create Custom Resource Definition



apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: crontabs.stable.example.com
spec:
  group: stable.example.com
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                cronSpec:
                  type: string
                image:
                  type: string
                replicas:
                  type: integer
  scope: Namespaced
  names:
    plural: crontabs
    singular: crontab
    kind: CronTab
    shortNames:
    - ct


Enter fullscreen mode Exit fullscreen mode

Apply the CRD:



kubectl apply -f crd.yaml


Enter fullscreen mode Exit fullscreen mode

Create Custom Resource



apiVersion: stable.example.com/v1
kind: CronTab
metadata:
  name: my-new-cron-object
  namespace: my-namespace
spec:
  cronSpec: "* * * * */5"
  image: my-cron-image
  replicas: 3


Enter fullscreen mode Exit fullscreen mode

Apply the custom resource:



kubectl apply -f custom-resource.yaml

Enter fullscreen mode Exit fullscreen mode



  1. Helm for Kubernetes Package Management

Install Helm

Follow the installation instructions for Helm from the official Helm website.

Add Helm Repository



helm repo add stable https://charts.helm.sh/stable

Enter fullscreen mode Exit fullscreen mode




Install Helm Chart




helm install my-release stable/nginx

Enter fullscreen mode Exit fullscreen mode




List Helm Releases




helm list

Enter fullscreen mode Exit fullscreen mode




Upgrade Helm Release




helm upgrade my-release stable/nginx

Enter fullscreen mode Exit fullscreen mode




Uninstall Helm Release




helm uninstall my-release

Enter fullscreen mode Exit fullscreen mode




📊 Best Practices

Use Namespaces

for Isolation

  • Organize resources by namespaces for better management and access control.

Label Resources

  • Use labels to organize and select resources efficiently.

Monitor and Log

  • Continuously monitor your cluster and collect logs for troubleshooting and performance analysis.

Automate with Scripts

  • Use scripts to automate repetitive tasks and ensure consistency.

Secure Your Cluster

  • Implement RBAC, network policies, and secure access controls to protect your cluster.

🚀 Conclusion

Mastering Kubernetes commands, from beginner to advanced levels, is essential for DevOps engineers to manage and troubleshoot clusters effectively. This comprehensive guide serves as a valuable reference for navigating your Kubernetes environment. By following best practices and leveraging these commands, you can ensure a robust and efficient Kubernetes setup.

Happy Clustering!🎉


Thank you for reading my blog …:)

© Copyrights: ProDevOpsGuy

Image description
Join Our Telegram Community || Follow me for more DevOps & Cloud Content

Top comments (0)