Skip to main content

Kubectl Explain - Another Tool to add to your belt

·256 words·2 mins
Table of Contents

Kubectl Explain
#

As great as the kubernetes documentation site is. It is nice to able to query info while you are locked inside a terminal. I’ve always been making use of “kubectl describe” but haven’t started really using “kubectl explain” until now.

It makes querying acceptable fields for a resource very easy (kubectl explain deployment.spec), but the really shines when you pipe it’s outputs into CLI tools. Take yq for example, a non standard Linux tool used to process YAML content in the command line or bash script.

Instead of manually opening or using kubectl describe to view a resources specific values, you can use yq to only output the content you need i.e “spec.strategy”. kubectl explain also works with custom resource definitions!

Output of kubectl -n karakeep get deployments.apps web -o yaml | yq .spec.strategy

{
  "rollingUpdate": {
    "maxSurge": "25%",
    "maxUnavailable": "25%"
  },
  "type": "RollingUpdate"
}

Output of kubectl explain deployment.spec.strategy

GROUP:      apps
KIND:       Deployment
VERSION:    v1

FIELD: strategy <DeploymentStrategy>

DESCRIPTION:
    The deployment strategy to use to replace existing pods with new ones.
    DeploymentStrategy describes how to replace existing pods with new ones.

FIELDS:
  rollingUpdate	<RollingUpdateDeployment>
    Rolling update config params. Present only if DeploymentStrategyType =
    RollingUpdate.

  type	<string>
  enum: Recreate, RollingUpdate
    Type of deployment. Can be "Recreate" or "RollingUpdate". Default is
    RollingUpdate.

    Possible enum values:
     - `"Recreate"` Kill all existing pods before creating new ones.
     - `"RollingUpdate"` Replace the old ReplicaSets by new one using rolling
    update i.e gradually scale down the old ReplicaSets and scale up the new
    one.

Sources
#

https://www.baeldung.com/linux/yq-utility-processing-yaml

https://kubernetes.io/docs/reference/kubectl/generated/kubectl_explain/

Related