Skip to content

kubectl command syntax

The kubectl command syntax defines the standard structure for interacting with Kubernetes via the command-line tool. It is a wrapper around the kube-apiserver component, utilizing Restful API calls to perform operations such as creating, querying, or modifying cluster resources^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].

Syntax Structure

The general syntax for executing kubectl commands follows this specific order^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md]:

[kubectl](<./kubectl.md>) [command] [TYPE] [NAME] [flags]

Parameters

The components of the syntax are defined as follows^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md]:

  • command: Specifies the operation to be performed on one or more resources (e.g., create, get, describe, delete).
  • TYPE: Specifies the resource type. Resource types are case-insensitive and can be specified in singular, plural, or abbreviated forms (e.g., pod, pods, or po).
  • NAME: Specifies the name of the resource. Names are case-sensitive. If the name is omitted, details for all resources of the specified type are displayed (e.g., kubectl get pods).
  • flags: Specifies optional parameters, such as the -s or --server flags to define the API server address and port.

Note: Parameters specified from the command line will override default values and any corresponding environment variables^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].

Specifying Resources

When performing operations, there are several ways to target specific or multiple resources^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md]:

  • Multiple resources of the same type: Group them by type followed by names: TYPE1 name1 name2.
    • Example: kubectl get [Pod](<./pod.md>) example-pod1 example-pod2
  • Multiple resource types: Specify each pair as TYPE/name.
    • Example: kubectl get [Pod](<./pod.md>)/example-pod1 replicationcontroller/example-rc1
  • Using files: Use the -f flag to specify one or more files. YAML is generally preferred over JSON for configuration files^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    • Example: kubectl get -f ./[Pod](<./pod.md>).yaml

Common Commands

Kubernetes provides a variety of commands to manage the lifecycle of applications.

Resource Management

  • apply: Applies or updates resources based on a file or standard input^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    • Example: kubectl apply -f example-service.yaml
  • create: Creates a new resource based on a file or input^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    • Example: kubectl create -f demo-deployment.yaml
  • delete: Deletes resources, either by file or by specific resource name^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    • Example: kubectl delete -f demo-service.yaml
  • get: Retrieves information about one or more resources in the cluster^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    • Example (with wide output): kubectl get po -o wide
    • Example (all namespaces): kubectl get po --all-namespaces
  • describe: Displays detailed state of one or more resources, including uninitialized ones^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    • Example: kubectl describe pods <pod-name>

Deployment and Exposure

  • run: Creates and runs a specified container image in the cluster^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    • Example: kubectl run nginx --image=nginx:1.10 --port=80
  • expose: Creates a Service to expose a resource (like a Deployment) externally, often mapping ports^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
    • Example: kubectl expose deployment nginx --port=88 --type=NodePort
  • set: Configures specific application resources or modifies existing ones^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].

Troubleshooting and Interaction

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

Sources

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