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 podslists all pods in the namespace, andkubectl get serviceslists 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.yamlcreates the resources defined in themy-resource.yamlfile. -
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.yamlupdates 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.jsondeletes 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/bashopens 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=nginxruns an nginx pod. -
kubectl config: Used to modify kubeconfig files. Commands likekubectl config vieworkubectl config use-context my-cluster-nameare frequently used. -
kubectl rollout: Manages the rollout of a resource (e.g., deployments). You can usekubectl rollout status deployment/my-deploymentto check the status of a deployment. -
kubectl scale: Scales a resource like a Deployment or ReplicaSet. For instance,kubectl scale deployment my-deployment --replicas=3changes the number of replicas to 3. -
kubectl top: Displays resource usage statistics. For example,kubectl top podshows 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.