Kubernetes: kubectl

kubectl lets you control the Kubernetes Cluster Manager.



Quick tips

create – creates resources

get – get resources

apply – applies resources


Common Commands

See also the kubectl Cheat Sheet: https://kubernetes.io/docs/reference/kubectl/cheatsheet/


Show config: k config view


To switch to a non-VT cluster (e.g. minikube), need to:

kubectl config get-contexts

k config use-context minikube


And view cluster-info or nodes (e.g. to check) with:

Cluster info: k cluster-info

List nodes: k get nodes

Show deployments: k get deployments

List contexts: k config get-contexts




Get all pods: k get pods

Describe all pods: k describe pods

Describe pod: k describe pods <pod name>

Listing all pods:

export POD_NAME=$(kubectl get pods -o go-template –template ‘{{range .items}}{{.metadata.name}}{{“\n”}}{{end}}’)

echo Name of the Pod: $POD_NAME

E.g. get output from pod using proxy

k proxy

curl http://localhost:8001/api/v1/namespaces/default/pods/<name of pod>/proxy/

Logs from Pod: k logs $POD_NAME



List services: k get services

Describe service: k describe services/kubernetes-bootcamp


See also this great tutorial: https://kubernetes.io/docs/tutorials/kubernetes-basics/explore/explore-interactive/

and Cheat Sheet: https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/


Command Summary

If you run kubectl without any arguments you’ll get some information about it. E.g. some commands:


kubectl controls the Kubernetes cluster manager.

Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

Basic Commands (Beginner):
  create Create a resource from a file or from stdin.
  expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
  run Run a particular image on the cluster
  set Set specific features on objects

Basic Commands (Intermediate):
  explain Documentation of resources
  get Display one or many resources
  edit Edit a resource on the server
  delete Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout Manage the rollout of a resource
  scale Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job
  autoscale Auto-scale a Deployment, ReplicaSet, or ReplicationController

Following examples assume we’ve got the name of the Pod in $POD_NAME, e.g. using:

export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')

(for more on Go templates see Go: templates


Commands – details


Apply additional changes.

Note: this warning can be ignore:

Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply

config view

Shows kubeconfig settings which you can query. E.g.

kubectl config view -o jsonpath='{.users[?(@.name == "minikube")].user.client-certificate}'



Create resources


Deletes resources.


Note that deleting a service does not stop an app running inside a container.


Shows details of a resource.



kubectl describe deploy <deployment name>


Run commands on the container.


E.g. to see environment variables:

kubectl exec $POD_NAME env

Or to get a terminal:

kubectl exec -it $POD_NAME bash



Creates and exposes a new service to traffic.

Note: --type can be:

  • ClusterIP
  • NodePort
  • LoadBalancer
  • ExternalName.

Default is ClusterIP.

E.g. kubectl expose deployment/kubernetes-bootcamp –type=”NodePort” –port 8080

Running kubectl get services shows the new service.

List the port with describe services/kubernetes-bootcamp.



Gets resources. E.g. for deployments:

get deployments


get services

Or pods:

get pods

Note: for a wider format use -o wide.

And for much, much more detail use: -o yaml. e.g.

kubectl get pods -o yaml.

To narrow it down you probably want to use the name of your pod as well. E.g.

kubectl get pods emoji-6fc8785888-gjd8r -o yaml


Note: you can specify the output as ‘template’ and provide a Go template as the value of the --template flag, to filter the attributes of the fetched resources. E.g.

kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'


kubectl get pods emoji-6fc8785888-gjd8r -o jsonpath --template={.status.podIP}

for the IP address of a Pod.

For more on Go Templates see: https://golang.org/pkg/text/template/



Use --no-headers for no headers. E.g. if you want to pipe into awk.


Prints logs for a container in a pod.

Anything the application sends to STDOUT becomes the logs for the container.



Creates a proxy (note: this is blocking so need to run it in a new terminal) that forwards communications into the cluster-wide private network which we can query directly using the API. E.g.

curl http://localhost:8001/version



Manage the rollout of a resource. E.g.

kubectl rollout status deployments/kubernetes-bootcamp



kubectl rollout status deployment <name of deployment>


And view the history with:

kubectl rollout history deployment <name of deployment>

Note: to make sure rollouts are recorded use --record=true. E.g.

kubectl apply --filename=deploy.yml --record=true


runNAME –image=… –port=…


  • searches for a node
  • schedules the application (i.e. the image) to run on that node
  • configures the cluster to the instance on a new node when needed
  • gives it a NAME to refer to this running image


Notes: this is the iterative method.


More on run:


kubectl run bandicoot-prod --image=gcr.io/kuar-demo/kuard-amd64:2 --replicas=2 --port=8080 --labels="ver=2,app=bandicoot,env=prod"

kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.

What’s this all about?

TLDR: it’s not kubectl run that’s being deprecated. Just the generator --generator= part. But, maintainers are trying to move people over to using kubectl create rather than kubectl run as the run code is messy and complex.




Sets a new size. E.g. number of pods in a deployment.

Trying this on kubernetes.io I scaled beyond capacity and got:

Unable to connect to the server: net/http: TLS handshake timeout



Configure application resources – e.g. to update a deployment

See also Kubernetes: update a deployed application and rollback



Errors / Problems

The connection to the server was refused – did you specify the right host or port?

Did you start your Kubernetes cluster? E.g. if you’re using minikube then:

minikube start



Leave a Reply

Your email address will not be published. Required fields are marked *