codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☸️

Kubernetes

5 / 82 topics
5Using kubectl Command Line Tool6Understanding Pods7Deploying Applications with Deployments8Using Services in Kubernetes
Tutorials/Kubernetes/Using kubectl Command Line Tool
☸️Kubernetes

Using kubectl Command Line Tool

Updated 2026-04-20
2 min read

Using kubectl Command Line Tool

kubectl is the command-line tool for interacting with Kubernetes clusters. It allows you to deploy applications, inspect and manage cluster resources, view logs, and perform administrative tasks. This tutorial will guide you through the fundamentals of using kubectl, including installation, basic commands, resource management, and advanced features.

Prerequisites

Before you begin, ensure that you have:

  • A Kubernetes cluster running.
  • Access to the cluster with appropriate permissions.
  • kubectl installed on your local machine or in your development environment.

Installation

Installing kubectl on Linux

  1. Download the latest release:

    curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    
  2. Make the binary executable:

    chmod +x ./kubectl
    
  3. Move the binary to a directory in your PATH:

    sudo mv ./kubectl /usr/local/bin/kubectl
    
  4. Verify the installation:

    kubectl version --client
    

Installing kubectl on macOS

  1. Use Homebrew (recommended):

    brew install kubectl
    
  2. Verify the installation:

    kubectl version --client
    

Installing kubectl on Windows

  1. Use Chocolatey (recommended):

    choco install kubernetes-cli
    
  2. Verify the installation:

    kubectl version --client
    

Basic Commands

Getting Help

To get help with kubectl, use:

kubectl help

For more specific commands, you can use:

kubectl <command> --help

Viewing Cluster Information

  1. Get cluster details:

    kubectl cluster-info
    
  2. List all nodes in the cluster:

    kubectl get nodes
    
  3. Describe a node:

    kubectl describe node <node-name>
    

Managing Pods

  1. Create a Pod from a YAML file:

    # pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: example-pod
    spec:
      containers:
      - name: nginx
        image: nginx:latest
    

    Apply the configuration:

    kubectl apply -f pod.yaml
    
  2. List all Pods in a namespace (default):

    kubectl get pods
    
  3. Describe a Pod:

    kubectl describe pod <pod-name>
    
  4. Delete a Pod:

    kubectl delete pod <pod-name>
    

Managing Deployments

  1. Create a Deployment from a YAML file:

    # deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: example-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:latest
    

    Apply the configuration:

    kubectl apply -f deployment.yaml
    
  2. List all Deployments:

    kubectl get deployments
    
  3. Describe a Deployment:

    kubectl describe deployment <deployment-name>
    
  4. Scale a Deployment:

    kubectl scale deployment <deployment-name> --replicas=5
    

Managing Services

  1. Create a Service from a YAML file:

    # service.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: example-service
    spec:
      selector:
        app: nginx
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      type: ClusterIP
    

    Apply the configuration:

    kubectl apply -f service.yaml
    
  2. List all Services:

    kubectl get services
    
  3. Describe a Service:

    kubectl describe service <service-name>
    

Advanced Features

Executing Commands in Pods

You can execute commands inside running Pods:

kubectl exec -it <pod-name> -- /bin/sh

Port Forwarding

Port forwarding allows you to access a Pod's port directly from your local machine:

kubectl port-forward pod/<pod-name> 8080:80

Now, you can access the application running in the Pod at http://localhost:8080.

Logging

View logs for a specific container within a Pod:

kubectl logs <pod-name>

To follow logs in real-time:

kubectl logs -f <pod-name>

Best Practices

  1. Use YAML files for configuration management: This allows version control and easier collaboration.
  2. Namespace your resources: Use namespaces to organize and isolate your applications.
  3. Automate with scripts: Create shell or bash scripts to automate repetitive tasks.
  4. Monitor and troubleshoot: Regularly check the status of your Pods, Deployments, and Services for any issues.

Conclusion

kubectl is a powerful tool for managing Kubernetes clusters. By mastering its basic commands and advanced features, you can effectively deploy, manage, and maintain applications in a Kubernetes environment. Always refer to the official Kubernetes documentation for the latest information and best practices.


PreviousKubernetes TerminologyNext Understanding Pods

Recommended Gear

Kubernetes TerminologyUnderstanding Pods