Docs
uk8s
Cluster Management
Manage Cluster via Kubectl
Introduction to Kubectl Command Line

Introduction to Kubectl Command Line

Since viewing cluster credentials allows direct login to the cluster, the operation of viewing cluster credentials has been classified under the add permission in user role permissions. If you need to view the cluster credentials, please ensure that the appropriate authority has been enabled in your role for UK8S.

kubectl is a command-line tool for operating Kubernetes clusters. This article will briefly introduce the syntax of kubectl and provide some common command examples. If you want to understand kubectl usage in depth, please refer to the official documentation kubectl overview, or use kubectl help command for detailed help. For installing kubectl, please check Installation and Configuration of kubectl.

kubectl Syntax

The syntax example for kubectl is as follows:

kubectl [command] [TYPE] [NAME] [flags]

command: Command refers to the operation you want to perform on some resources. Regular commands include create, get, describe, delete, etc.

TYPE: TYPE defines the resource type that command needs to operate on. TYPE is case-insensitive, insensitive to singular and plural, and supports abbreviations. The following commands are all feasible and equivalent:

kubectl get pod 
kubectl get pods
kubectl get po
kubectl get POD

NAME: NAME refers to the name of the resource and NAME is case sensitive. If you don’t specify the NAME of a resource, all resources will be displayed. A command like kubectl get pods will display all the pods under the Default namespace.

You can also get the details of multiple resources at the same time, such as getting the details of the same type of resources, different types of resource details:

kubectl get pods pod1 pod2
kubectl get pod/example-pod1 replicationcontroller/example-rc1

flags: Optional parameter. For example, you can use all-namespaces to get resource objects under all namespaces. For usage of flags in each command, please refer to kubectl command

Note: Flags specified on the command line will override default values and any corresponding environment variables.

For more details about kubectl Syntax, you can use

kubectl help

Common Commands

kubectl create - Create resources using a file or standard input.

# Create a "service" object using the exampe-service.yaml file
$ kubectl create -f example-service.yaml
 
# Create a "replication" object using the example-controller.yaml file
$ kubectl create -f example-controller.yaml

kubectl describe - Get detailed status of resources, including those being initialized.

# View the details of the node named <node-name>
$ kubectl describe nodes <node-name>
 
# View the details of the pod named <pod-name>, including the creation log of the pod
$ kubectl describe pods/<pod-name>
 
# View all pods managed by replication named <rc-name>.
# Note: Any pod created by the replication controller, the prefix of its name is the replication name.
$ kubectl describe pods <rc-name>
 
# View all pods, but not including uninitialized pods
$ kubectl describe pods --include-uninitialized=false

kubectl logs - Get logs of a pod

# Get a snapshot of a pod's logs
$ kubectl logs <pod-name>
 
# Get a real-time log stream of a pod, similar to the 'tail -f' in linux
$ kubectl logs -f <pod-name>

kubectl exec - Execute commands in a pod container

# Get output of "date" command from a pod, by default, it is from the first container in the pod.
$ kubectl exec <pod-name> date
 
# Get output of "date" command from a specified container in the pod
$ kubectl exec <pod-name> -c <container-name> date
 
# Get an interactive TTY (control terminal) from a pod, and execute /bin/bash
$ kubectl exec -ti <pod-name> /bin/bash