$ curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/$(uname | awk '{print tolower($0)}')/amd64/kubectl
$ chmod +x ./kubectl
$ sudo mv ./kubectl /usr/local/bin/kubectl
# verify
$ kubectl version --client --short
Flag --short has been deprecated, and will be removed in the future. The --short output will become the default.
Client Version: v1.25.3
Kustomize Version: v4.5.
kubectl-convert
[!NOTE] A plugin for Kubernetes command-line tool kubectl, which allows you to convert manifests between different API versions. This can be particularly helpful to migrate manifests to a non-deprecated api version with newer Kubernetes release.
# intel
$ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl"
# apple silicon
$ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/arm64/kubectl"
# or via brew
$ brew install kubectl
completion
[!NOTE] The Homebrew installation of bash-completion v2 sources all the files in the BASH_COMPLETION_COMPAT_DIR directory, that's why the latter two methods work
$ kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-1 Healthy {"health":"true"}
etcd-2 Healthy {"health":"true"}
etcd-0 Healthy {"health":"true"}
get po
name
$ kubectl -n devops get po -o custom-columns='NAME:metadata.name'
or
$ kubectl -n devops get deploy jenkins -o custom-columns="NAME:metadata.name, IMAGES:..image"
NAME IMAGES
jenkins jenkins/jenkins:2.187
$ kubectl -n <namespace> get po <pod_name> -o jsonpath="{..containerID}"
# or
$ kubectl -n <namespace> get po <pod_name> \
-o go-template \
--template="{{ range .status.containerStatuses }}{{ .containerID }}{{end}}"
$ kubectl get apiservices.apiregistration.k8s.io
$ kubectl get apiservices.apiregistration.k8s.io v1beta1.metrics.k8s.io -o yaml
$ kubectl get apiservices.apiregistration.k8s.io
NAME SERVICE AVAILABLE AGE
v1. Local True 4y
v1.apps Local True 4y
v1.authentication.k8s.io Local True 4y
v1.authorization.k8s.io Local True 4y
v1.autoscaling Local True 4y
v1.batch Local True 4y
v1.monitoring.coreos.com Local True 168d
v1.networking.k8s.io Local True 4y
v1.rbac.authorization.k8s.io Local True 4y
v1.storage.k8s.io Local True 4y
v1beta1.admissionregistration.k8s.io Local True 4y
v1beta1.apiextensions.k8s.io Local True 4y
v1beta1.apps Local True 4y
v1beta1.authentication.k8s.io Local True 4y
v1beta1.authorization.k8s.io Local True 4y
v1beta1.batch Local True 4y
v1beta1.certificates.k8s.io Local True 4y
v1beta1.coordination.k8s.io Local True 4y
v1beta1.events.k8s.io Local True 4y
v1beta1.extensions Local True 4y
v1beta1.metrics.k8s.io kube-system/metrics-server False (ServiceNotFound) 188d
v1beta1.policy Local True 4y
v1beta1.rbac.authorization.k8s.io Local True 4y
v1beta1.scheduling.k8s.io Local True 4y
v1beta1.storage.k8s.io Local True 4y
v1beta2.apps Local True 4y
v2beta1.autoscaling Local True 4y
v2beta2.autoscaling Local True 4y0
$ kubectl get apiservices.apiregistration.k8s.io v1beta1.metrics.k8s.io -o yaml --export
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
name: v1beta1.metrics.k8s.io
spec:
group: metrics.k8s.io
groupPriorityMinimum: 100
insecureSkipTLSVerify: true
service:
name: prometheus-adapter
namespace: monitoring
version: v1beta1
versionPriority: 100
status:
conditions:
- lastTransitionTime: 2022-08-15T14:10:39Z
message: all checks passed
reason: Passed
status: "True"
type: Available
troubleshooting
[!NOTE|label:references:]
$ kubectl api-resources
error: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the server is currently unable to handle the request
$ kubectl get apiservices.apiregistration.k8s.io
NAME SERVICE AVAILABLE AGE
v1beta1.metrics.k8s.io kube-system/metrics-server False (ServiceNotFound) 188d
# remove metrics.k8s.io
$ kubectl delete apiservices.apiregistration.k8s.io v1beta1.metrics.k8s.io
# debug
$ kubectl get secrets $(kubectl -n kube-system get sa metrics-server -o 'jsonpath={.secrets[].name}')
Error from server (NotFound): secrets "metrics-server-token-mr49q" not found
$ kubectl get secrets $(kubectl -n kube-system get sa metrics-server -o 'jsonpath={.secrets[].name}') \
-o "jsonpath={.data.ca\.crt}" |
base64 -d |
openssl x509 -text -noout |
grep Not
# get token
$ kubectl get secrets $(kubectl -n kube-system get sa metrics-server -o 'jsonpath={.secrets[].name}') \
-o "jsonpath={.data.token}" |
base64 -d -w0
# get namespace
$ kubectl get secrets $(kubectl -n kube-system get sa metrics-server -o 'jsonpath={.secrets[].name}') \
-o "jsonpath={.data.namespace}" |
base64 -d -w0
# Edit the last-applied-configuration annotations by type/name in YAML
kubectl apply edit-last-applied deployment/nginx
# Edit the last-applied-configuration annotations by file in JSON
kubectl apply edit-last-applied -f deploy.yaml -o json