CNCF CKAD Useful kubectl commands – Fundamentals

kubectl api-resources

Very useful command to check the correct resource name (& case) for all available Kubernetes API types, for both IMPERATIVE (e.g. kubectl command-line) and DECLARATIVE (e.g. K8s Manifest, usually yaml file) usage.

For Imperative usage, command lists K8s API resource NAME (plurals) & SHORTNAME values in lowercase names.

Example

=> kubectl api-resources
NAME          SHORTNAMES
configmaps    cm

 so a command line usage could be…

=> kubectl create cm example-create-config-map

For Declarative usage, command lists K8s API resource KIND values as a PascalCase name.

Example

=> kubectl api-resources
APIVERSION      KIND
v1             ConfigMap

so a MANIFEST FILE would look like…               

apiVersion: v1                  # NOTE  apiVersion: <api-group-name>/<version> where empty api-group-name means core (representation “”).
kind: ConfigMap
metadata:
spec:

USEFUL LINKS:  kubernetes.io – kubectl api-resources

 

kubectl api-versions

Lists the available K8s API versions, equivalent to the distinct set of APIVersion column values returned from the kubectl api-resources command.

Output form <api-group-name> / <version>

where empty api-group-name means core (representation “”).

 

Example

=> kubectl api-versions
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1
...

 

USEFUL LINKS:  kubernetes.io – kubectl api-versions

kubectl config view

=> kubectl config view
apiVersion: v1
clusters:
– cluster:
certificate-authority-data: DATA+OMITTED
server: https://127.0.0.1:6443
name: jess
contexts:
– context:
cluster: jess
user: jess
name: jess
current-context: jess
kind: Config
preferences: {}
users:
– name: jess
user:
client-certificate-data: DATA+OMITTED
client-key-data: DATA+OMITTED

Working with a  Kubernetes Cluster requires access to a K8s Context which associates a particular Cluster with a K8s User (from the current ‘kubeconfig’ file). The config view command lists the current kubeconfig file contents.

Which kube config file is in use when there are multiple config files? Order of precedence is…

1. Explicitly set the kube config file;

 use of --kubeconfig option
=> kubectl config view --kubeconfig=my-config-file

2. Use $KUBECONFIG if defined (export env var)

3. ~/.kube/config (user home directory).

USEFUL LINKS: kubernetes.io – kubectl config current-context

kubectl config current-context

=> kubectl config current-context
jess

Returns the name of the current K8s Context (from current kubeconfig file).

USEFUL LINKS: kubernetes.io – kubectl config current-context

kubectl config use-context <target-context>
=> kubectl config use-context exam-Q-context-ksdksn
Switched to context "exam-Q-context-ksdksn".

Sets the current-context in current kubeconfig file. You will use this command as the first step in answering most CKAD exam questions. Not to be confused with config set-context which adds or updates a context entry in a kubeconfig file.

USEFUL LINKS: kubernetes.io – kubectl config use-context

kubectl config set-context --current --namespace=<exam-Q-namespace>
=> kubectl config set-context --current --namespace exam-Q-ns
Context "jess" modified.

Then using config view shows the working namespace for the current context is now exam-Q-ns.

=> kubectl config view
... 
- context:
    cluster: jess
    namespace: exam-Q-ns
    user: jess
  name: jess 
...

The use of

--current

with set-context is a nice shorthand way of setting the working namespace in the current context, so you don’t have to keep explicitly typing -n exam-Q-ns at the end of each and every kubectl command you have to type in the exam.

USEFUL LINKS: kubernetes.io – kubectl config set-context

kubectl version --short
=> kubectl version --short
Client Version: v1.27.1+k3s1
Kustomize Version: v5.0.1
Server Version: v1.27.1+k3s1
kubectl version   # deprecated to --short output
kubectl version --short # deprecated to be default 
kubectl version --output=yaml   # -o yaml or json 
kubectl version --client   # get client-only version

USEFUL LINKS:  kubernetes.io – kubectl version

kubectl run / exec / logs (implicit pod commands)

sGenerally kubectl commands require input of the explicit K8s resource type the command is acting on. For example to create say a namespace, we explicitly type the resource type names (namespace or ns) in the command;

e.g. kubectl create namespace my-ns

However, the kubectl run, exec and logs commands implicitly act on the Pod resource type (so these command do not need to explicitly include the pod resource type).

Use single-use 'Emphemeral' Pod say for test purposes
=> kubectl run --rm -it temp-pod --image nginx --restart=Never -- /bin/bash
(container shell)
root@temp-pod:/# date
root@temp-pod:/# Sat Jul 22 12:51:41 UTC 2023

When u exit the container this one-off pod & container are automagically removed.

OR we can Interact with a target container to test connectivity or some aspect of a solution for an exam Q;

=> kubectl run my-pod --image nginx
=> kubectl exec my-pod -- date
Sat Jul 22 13:05:43 UTC 2023

 AND View output from the logs for a pod.

=> kubectl logs my-pod

…2023/07/22 13:05:29 [notice] 1#1: start worker processes
2023/07/22 13:05:29 [notice] 1#1: start worker process 79
2023/07/22 13:05:29 [notice] 1#1: start worker process 80
2023/07/22 13:05:29 [notice] 1#1: start worker process 81
2023/07/22 13:05:29 [notice] 1#1: start worker process 82…

USEFUL LINKS:
kubernetes.io – kubectl run
kubernetes.io – kubectl exec
kubernetes.io – kubectl logs

kubectl cluster-info dump

Use the base command to dump the K8s KAS (K8s API Server), DNS & Metrics server endpoints.

=> kubectl cluster-info 
Kubernetes control plane is running at https://127.0.0.1:6443
CoreDNS is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/https:metrics-server:https/proxy

The dump option lists extended cluster and control plane details in JSON format (verbose).

=> kubectl cluster-info dump
{ "kind": "NodeList", "apiVersion": "v1", "metadata": { "resourceVersion": "817" }, "items": [ { "metadata": { "name": "controlplane", "uid": "4039a0d7-ab9a-4b90-a464-09639efb6652", "resourceVersion": "785", "creationTimestamp": "2023-07-22T13:14:37Z", "labels": { "beta.kubernetes.io/arch": "amd64", "beta.kubernetes.io/instance-type": "k3s", "beta.kubernetes.io/os": "linux", "kubernetes.io/arch": "amd64", "kubernetes.io/hostname": "controlplane", "kubernetes.io/os": "linux", "node-role.kubernetes.io/control-plane": "true", "node-role.kubernetes.io/master": "true", "node.kubernetes.io/instance-type": "k3s" }, "annotations": {... 
USEFUL LINKS: kubernetes.io - kubectl cluster-info
kubectl top {node / pod}

Conceptually similar to unix top, the kubectl top commands display point-in-time cpu & memory usage values for node or pod resources.

For current node;

=> kubectl top node
NAME    CPU(cores) CPU%  MEMORY(bytes) MEMORY% 
cp      72m        0%    1182Mi        0%

For all pods on current node (-A for all namespaces);

=> kubectl top pod -A

NAMESPACE   NAME              CPU(cores) MEMORY(bytes)
kube-system coredns-...  2m        19Mi
kube-system local-path-pro... 1m  10Mi
kube-system metrics-server... 5m 21Mi
...

USEFUL LINKS: kubernetes.io – kubectl top