Kubectl

Kubectl is the preferred k8s client used to interact with k8s objects. By default the kubectl client interacts with the cluster kubeconfig specified in the KUBECONFIG i.e export KUBECONFIG=/PATH/TO/KUBECONFIG.yaml environment variable file location or explicitly specify kubeconfig using the --kubeconfig <KUBE_CONFIG_FILE_LOCATION>

ℹ️

For easier and faster working with kubectl, create an alias, so that we don’t have to write the whole thing again

alias k='kubectl' # add to .bashrc or .zshrc

# Usage:
k get pods # instead of `kubectl get pods`

Structure of Command

kubectl [command] [TYPE] [NAME] [flags]

kubectl logs nginx-nxs1a -n test

# Command => create, get, describe, delete, run etc
# Type => configmaps, events, namespaces, nodes etc
# name => name of the k8s resource
# flags => additional flags for the kubectl i.e 

Detailed kubectl docs can be found in the link

k8s Object types/resources

available k8s objects/resource can be fetched using the command kubectl api-resources

Most commonly used ones are:

  1. configmaps / cm
  2. endpoints / ep
  3. events / ev
  4. namespaces / ns
  5. nodes / no
  6. persistentvolumeclaims / pvc
  7. persistentvolumes / pv
  8. pods / po
  9. secrets
  10. serviceaccounts
  11. service / svc
  12. customresourcedefinitions / crds
  13. deployments / deploy
  14. statefulsets / sts
  15. horizontalpodautoscalers / hpa
  16. cronjobs / cj
  17. jobs
  18. ingress / ing
  19. storageclass / sc

kubectl flags

  • For exhaustive flags list refer docs
OptionsDescription
--kubeconfig='<CONFIG_PATH>'Explicit specify the kubeconfig file otherwise will use the default kubeconfig specified in KUEBCONFIG env variable
--context='<CONTEXT_NAME>'specify the context to use
--cluster='<CLUSTER_NAME>'specify cluster name
--insecure-skip-tls-verify=falseIf true, the server’s certificate will not be checked for validity. This will make your HTTPS connections insecure
-v
or
--v=0
log level verbose
-n <NAME_SPACE>execute the command for the specified NAME_SPACE

Kubectl Examples