Kubectl notes
kubectl
is a command-line tool for Kubernetes, a powerful system for managing containerized applications.
Here are some of the most useful kubectl
commands that are essential for interacting with a Kubernetes cluster
-
kubectl get
: Fetches a list of resources. For example,kubectl get pods
lists all pods in the namespace, andkubectl get services
lists all services. -
kubectl describe
: Shows detailed information about a specific resource, such askubectl describe nodes my-node
. -
kubectl create
: Used to create a resource from a file or stdin. For example,kubectl create -f my-resource.yaml
creates the resources defined in themy-resource.yaml
file. -
kubectl apply
: Applies a configuration to a resource from a file or stdin. It’s used for applying changes to your resources. For instance,kubectl apply -f deployment.yaml
updates the deployment defined indeployment.yaml
. -
kubectl delete
: Deletes resources either by filenames, stdin, resources and names, or by resources and label selector. For example,kubectl delete -f ./pod.json
deletes a pod based on the type and name specified inpod.json
. -
kubectl exec
: Executes a command in a container. For example,kubectl exec -it my-pod -- /bin/bash
opens a bash shell inmy-pod
. -
kubectl logs
: Retrieves logs from a container. Useful for troubleshooting, e.g.,kubectl logs my-pod
. -
kubectl port-forward
: Forwards one or more local ports to a pod. This is useful for accessing a service locally, e.g.,kubectl port-forward my-pod 5000:6000
. -
kubectl run
: Runs a particular image on the cluster. For example,kubectl run my-nginx --image=nginx
runs an nginx pod. -
kubectl config
: Used to modify kubeconfig files. Commands likekubectl config view
orkubectl config use-context my-cluster-name
are frequently used. -
kubectl rollout
: Manages the rollout of a resource (e.g., deployments). You can usekubectl rollout status deployment/my-deployment
to check the status of a deployment. -
kubectl scale
: Scales a resource like a Deployment or ReplicaSet. For instance,kubectl scale deployment my-deployment --replicas=3
changes the number of replicas to 3. -
kubectl top
: Displays resource usage statistics. For example,kubectl top pod
shows the usage of resources by pods. -
kubectl label
: Updates the labels on a resource, which can be used for organizing and selecting subsets of objects.