CodeBlanks Logo Icon CodeBlanks

What is kubectl?

Learn that kubectl is the Kubernetes CLI.

What is kubectl?

kubectl is the command-line tool that sends requests to the Kubernetes API. Commands usually have the form kubectl VERB RESOURCE [NAME] [flags]:

kubectl get pods
kubectl describe deployment web
kubectl delete service web
kubectl logs pod/web-7c5b9f6d8-abcde

Resource types have abbreviations: po (pods), deploy (deployments), svc (services), ns (namespaces), cm (configmaps).

kubeconfig and context

kubectl reads which cluster to connect to and with which user from the ~/.kube/config file (kubeconfig). Several contexts (cluster + user + namespace) can be kept in the file:

$ kubectl config get-contexts
CURRENT   NAME       CLUSTER    AUTHINFO   NAMESPACE
*         minikube   minikube   minikube   default
          uretim     uretim     ali
$ kubectl config use-context uretim
$ kubectl config current-context
uretim

Practice Which cluster are you on?

Write three commands: list the contexts; switch to the minikube context; print the active context.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl config get-contexts
kubectl config use-context minikube
kubectl config current-context

Expected output

CURRENT   NAME       CLUSTER    AUTHINFO   NAMESPACE
          minikube   minikube   minikube   default
*         uretim     uretim     ali
Switched to context "minikube".
minikube

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does kubectl get svc list?

  • ServiceAccounts
  • Servers
  • Services Correct answer
  • Secrets

Explanation: kubectl api-resources lists all types and their abbreviations.

2 Change the active context.

Question
kubectl config ____ minikube
Answer
kubectl config use-context minikube

Explanation: use-context changes kubectl's target cluster.

3 Match the abbreviations.

Term Meaning
po pods
deploy deployments
svc services
cm configmaps

All lessons in Intermediate