This chapter gathers recipes around the basic usage of the Kubernetes command-line interface (CLI), kubectl
. See Chapter 1 for how to install the CLI tool; for advanced use cases, see Chapter 6, where we show how to use the Kubernetes API.
Use the get
verb of kubectl
along with the resource type. To list all pods:
$ kubectl get pods
To list all services and deployments:
$ kubectl get services,deployments
To list a specific deployment:
$ kubectl get deployment myfirstk8sapp
To list all resources:
$ kubectl get all
Note that kubectl get
is a very basic but extremely useful command to get a quick overview what is going on in the cluster—it’s essentially the equivalent to ps
on Unix.
Many resources have short names you can use with kubectl
, sparing your time and sanity. Here are some examples:
configmaps
(aka cm
)
daemonsets
(aka ds
)
deployments
(aka deploy
)
endpoints
(aka ep
)
events
(aka ev
)
horizontalpodautoscalers
(aka hpa
)
ingresses
(aka ing
)
namespaces
(aka ns
)
nodes
(aka no
)
persistentvolumeclaims
(aka pvc
)
persistentvolumes
(aka pv
)
pods
(aka po
)
replicasets
(aka rs
)
replicationcontrollers
(aka rc
)
resourcequotas
(aka quota
)
serviceaccounts
(aka sa
)
services
(aka svc
)
Use the delete
verb of kubectl
along with the type and name of the resource you wish to delete.
To delete all resources in the namespace my-app
, do:
$ kubectl get ns NAME STATUS AGE default Active 2d kube-public Active 2d kube-system Active 2d my-app Active 20m $ kubectl delete ns my-app namespace "my-app" deleted
If you’re wondering how to create a namespace, see Recipe 6.3.
You can also delete specific resources and/or influence the process by which they are destroyed. To delete services and deployments labeled with app=niceone
, do:
$ kubectl delete svc,deploy -l app=niceone
To force deletion of a pod, do:
$ kubectl delete pod hangingpod --grace-period=0 --force
To delete all pods in the namespace test
, do:
$ kubectl delete pods --all --namespace test
Do not delete supervised objects such as pods controlled by a deployment directly. Rather, kill their supervisors or use dedicated operations to get rid of the managed resources. For example, if you scale a deployment to zero replicas (see Recipe 9.1), then you effectively delete all the pods it looks after.
Another aspect to take into account is cascading versus direct deletion—for example, when you delete a custom resource definition (CRD) as shown in Recipe 13.4, all its dependent objects are deleted too. To learn more about how to influence the cascading deletion policy, read Garbage Collection in the Kubernetes docs.
The --watch
option is useful, but sometimes not very reliable, in terms of refreshing the screen correctly. Alternatively, you can use the watch
command, as in:
$ watch kubectl get pods
If you have editor issues, use EDITOR=vi
. Also be aware that not all changes trigger a deployment.
Some triggers have shortcuts, for example, if you want to change the image version a deployment uses, simply use kubectl set image
, which updates the existing container images of resources (valid for deployments, replica sets/replication controllers, daemon sets, jobs, and simple pods).
Use the explain
verb of kubectl
:
$ kubectl explain svc DESCRIPTION: Service is a named abstraction of software service (for example, mysql) consisting of local port (for example 3306) that the proxy listens on, and the selector that determines which pods will answer requests sent through the proxy. FIELDS: status <Object> Most recently observed status of the service. Populated by the system. Read-only. More info: https://git.k8s.io/community/contributors/devel/ api-conventions.md#spec-and-status/ apiVersion <string> APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources kind <string> Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions .md#types-kinds metadata <Object> Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata spec <Object> Spec defines the behavior of a service. https://git.k8s.io/community/ contributors/devel/api-conventions.md#spec-and-status/ $ kubectl explain svc.spec.externalIPs FIELD: externalIPs <[]string> DESCRIPTION: externalIPs is a list of IP addresses for which nodes in the cluster will also accept traffic for this service. These IPs are not managed by Kubernetes. The user is responsible for ensuring that traffic arrives at a node with this IP. A common example is external load-balancers that are not part of the Kubernetes system.
Ross Kukulinski’s blog post, “kubectl explain — #HeptioProTip”
1 Kubernetes, “Kubectl Reference Docs: Explain”.
2 Kubernetes, “The Kubernetes API”.
3.144.104.29