Kubectl: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 68: Line 68:


  kubectl ... -o jsonpath="{.users[?(@.name=="blue")].user.password}"
  kubectl ... -o jsonpath="{.users[?(@.name=="blue")].user.password}"
  kubectl -n ${ns} get pod -l hostname=${hostname} -o jsonpath='{.items[0].spec.volumes[?(@.name=="vault")].hostPath.path}' 2>/dev/null
  kubectl get pod ... -o jsonpath='{.items[0].spec.volumes[?(@.name=="vault")].hostPath.path}' 2>/dev/null


==describe==
==describe==

Revision as of 23:08, 18 October 2019

Internal

Overview

kubectl is the main Kubernetes command line tool, used to send REST API requests with JSON-formatted payloads into the API server.

Configuration

.kube config

Commands

Options

Obtaining Information about API Objects

get

kubectl get

kubectl get and kubectl describe mask sensitive information such as a secret's content to protect it from being exposed accidentally to an onlooker or from being stored in a terminal log.

Output in YAML Format

The "-o yaml" option instructs get to return the full copy of the object's manifest from the cluster store. The output is divided into a .spec section, which represents the desired state and the .status section, which represents the current observed state.

kubectl get -o yaml ...

Get the Manifest for an Existing Object

The manifest can be used to recreate the object:

kubectl get pod pod-name --export -o yaml

Note that --export is deprecated and will be removed in the future so find an equivalent.

JSONPath Support

https://kubernetes.io/docs/reference/kubectl/jsonpath/

Get an Individual Attribute Only

TODO: https://gist.github.com/so0k/42313dbb3b547a0f51a547bb968696ba

kubectl ... -o jsonpath="{.status.phase}"
kubectl ... -o jsonpath="{.items[?(@.spec.unschedulable)].metadata.name}"

Alternative, to explore and document:

kubectl get pods  --no-headers -o custom-columns=\":metadata.name\" ...

Filter Elements of an Array based on a Key Value

We assume that the elements of the array are maps, which contain the specified key:

kubectl ... -o jsonpath="{.users[?(@.name=="blue")].user.password}"
kubectl get pod ... -o jsonpath='{.items[0].spec.volumes[?(@.name=="vault")].hostPath.path}' 2>/dev/null

describe

The 'describe' command provides a multi-line overview of an object. It includes important object lifecycle events.

kubectl describe

POSTing a Manifest

kubectl apply -f filename.yaml

Port Fowarding

 while ! kubectl -n my-namespace port-forward service/my-service 8787:8787; do sleep 1; done