Skip to content

kubectl basic commands

kubectl is the command-line tool for Kubernetes, acting as a wrapper around the kube-apiserver component to manage clusters via Restful API^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md]. Mastery of these commands facilitates better understanding of cluster concepts and aids in debugging configurations^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].

Syntax

The general syntax for executing operations follows this structure^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md]:

[kubectl](<./kubectl.md>) [command] [TYPE] [NAME] [flags]
  • command: Specifies the operation (e.g., create, get, describe, delete)^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
  • TYPE: Specifies the resource type (e.g., pod, service, deployment). This is case-insensitive and supports singular, plural, or abbreviated forms (e.g., po, pods, pod)^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
  • NAME: Specifies the name of the resource. Names are case-sensitive. If omitted, the command applies to all resources of the specified type^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
  • flags: Specifies optional parameters, such as -f for files or -s for server address^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].

Resource Management

Creating Resources

  • apply: Applies configuration changes to a resource from a file or standard input^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    [kubectl](<./kubectl.md>) apply -f example-service.yaml
    [kubectl](<./kubectl.md>) apply -f <directory>
    
  • create: Creates a new resource explicitly from a file^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    [kubectl](<./kubectl.md>) create -f demo-deployment.yaml
    
  • run: Creates and runs a specific image in the cluster^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    [kubectl](<./kubectl.md>) run nginx --replicas=3 --labels="app=example" --image=nginx:1.10 --port=80
    

Viewing Resources

  • get: Lists one or more resources^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    [kubectl](<./kubectl.md>) get pods
    [kubectl](<./kubectl.md>) get po -o wide
    
  • describe: Displays detailed state of one or more resources, including uninitialized ones^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    [kubectl](<./kubectl.md>) describe pods <pod-name>
    

Updating and Deleting Resources

  • delete: Deletes resources based on file, type, or name^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    [kubectl](<./kubectl.md>) delete -f demo-deployment.yaml
    [kubectl](<./kubectl.md>) delete [Pod](<./pod.md>) <pod-name>
    
  • set: Configures specific application resources or modifies existing ones^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
  • expose: Creates a Service to expose a resource (like a Deployment) externally^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    [kubectl](<./kubectl.md>) expose deployment nginx --port=88 --type=NodePort --target-port=80 --name=nginx-service
    

Troubleshooting and Interaction

  • logs: Prints logs for a container in a Pod^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    [kubectl](<./kubectl.md>) logs <pod-name>
    [kubectl](<./kubectl.md>) logs -f <pod-name>
    
  • exec: Executes a command against a container in a Pod^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    [kubectl](<./kubectl.md>) exec -ti <pod-name> -- /bin/bash
    

Namespaces

By default, kubectl commands target the default namespace. To interact with other namespaces, specific flags must be used^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].

[kubectl](<./kubectl.md>) get pods --all-namespaces
[kubectl](<./kubectl.md>) get namespace

Sources

^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md]