Interacting with the cluster using kubectl.

Category : Kubernetes | Sub Category : Learn Kubernetes | By Prasad Bonam Last updated: 2023-11-22 07:30:58 Viewed : 227


Interacting with a Kubernetes cluster using kubectl involves various commands to manage resources, inspect the cluster state, and troubleshoot issues. Here is a guide to some common kubectl commands:

1. Basic Commands:

  • Get Resources:

    • List all resources of a specific type:
      bash
      kubectl get pods kubectl get services
    • Get detailed information about a resource:
      bash
      kubectl get pods <pod-name> -o yaml
  • Create and Apply:

    • Create or apply a resource from a YAML file:
      bash
      kubectl apply -f my-pod.yaml
  • Delete Resource:

    • Delete a resource by name or from a YAML file:
      bash
      kubectl delete pod <pod-name> kubectl delete -f my-pod.yaml

2. Inspecting Cluster:

  • Cluster Info:

    • Display the cluster information:
      bash
      kubectl cluster-info
  • Node Info:

    • Get information about nodes in the cluster:
      bash
      kubectl get nodes
  • Describe:

    • Describe a resource for detailed information:
      bash
      kubectl describe pod <pod-name> kubectl describe node <node-name>

3. Working with Pods:

  • Logs:

    • View the logs of a pod:
      bash
      kubectl logs <pod-name>
  • Exec into Pod:

    • Run commands inside a pod:
      bash
      kubectl exec -it <pod-name> -- /bin/bash
  • Port Forwarding:

    • Forward a local port to a port on a pod:
      bash
      kubectl port-forward <pod-name> 8080:80

4. Managing Deployments:

  • Deployments:

    • List and manage deployments:
      bash
      kubectl get deployments kubectl scale deployment <deployment-name> --replicas=3
  • Rolling Update:

    • Perform a rolling update of a deployment:
      bash
      kubectl set image deployment/<deployment-name> nginx=nginx:1.17 --record

5. Services:

  • Services:

    • List and manage services:
      bash
      kubectl get services
  • Expose Deployment:

    • Expose a deployment as a service:
      bash
      kubectl expose deployment <deployment-name> --type=NodePort --port=8080

6. Namespaces:

  • Create and Switch Namespace:

    • Create a new namespace and switch to it:
      bash
      kubectl create namespace my-namespace kubectl config set-context --current --namespace=my-namespace
  • List Resources in Namespace:

    • List resources in a specific namespace:
      bash
      kubectl get pods --namespace=my-namespace

7. Troubleshooting:

  • Events:

    • View events related to resources:
      bash
      kubectl get events
  • Describe Resource for Troubleshooting:

    • Describe a resource to troubleshoot issues:
      bash
      kubectl describe pod <pod-name>
  • Logs and Exec for Troubleshooting:

    • Check logs and exec into a pod to troubleshoot issues:
      bash
      kubectl logs <pod-name> kubectl exec -it <pod-name> -- /bin/bash

These are just a few examples of the numerous kubectl commands available. The command-line tool is powerful and flexible, allowing you to manage and interact with your Kubernetes cluster efficiently. You can explore more commands and options in the kubectl documentation.

Search
Related Articles

Leave a Comment: