Skip to content

Kubernetes

Andres Olarte edited this page Jan 11, 2022 · 26 revisions

Contexts

kubectl config get-contexts
kubectl config current-context

Set default Namespace

kubectl config set-context my-context --namespace=demo

Remove default Namespace

kubectl config set-context my-context --namespace=

Delete all contexts

kubectl config get-contexts -o=name | xargs -n 1 kubectl config delete-context

Troubleshoot Services

Debug Services

Are there endpoints matching the service:

kubectl get endpoints hostnames

GKE

To get credentials for kubectl to work

gcloud container clusters get-credentials example-cluster

Ingress NGINX

Debug

To show the running config:

kubectl exec -it `kubectl get pods -o custom-columns=name:metadata.name  | grep nginx-ingress-controller` -- cat /etc/nginx/nginx.conf 

Label

kubectl label pods my-pod new-label=awesome

Load Balancer

Create a simple load balancer

cat > rc.yaml <<- EOM
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
EOM
kubectl create -f ./rc.yaml
kubectl expose rc nginx --port=80 --target-port=80  --name=nginx-lb --type=LoadBalancer

To delete it:

kubectl delete service nginx-lb

JSONPath

Display any HostPort for all containers in all pods.

kubectl get pods --all-namespaces \
  -o=jsonpath='{range .items[*]}Pod {.metadata.namespace}.{.metadata.name}:{range .spec.containers[*]}  Container {.name}:{range .ports[*]}    HostPort {.hostPort}:{end}:{end}:{end}:' \
  |  tr ":" "\n"

This uses tr to convert : to \n.

Port forwarding

kubectl -n NAMESPACE port-forward POD_NAME 9090:9090 &

or

kubectl -n NAMESPACE port-forward svc/SERVICE_NAME 9090:9090 &

Shell

Describe pods to get debug info

kubectl describe pods

Shell into pod

kubectl exec -it POD_NAME -- /bin/bash

Shell into pod by deployment name

kubectl exec -it deploy/DEPLOYMENT_NAME -- /bin/bash

Shell into pod w/multiple containers

kubectl get pods
kubectl exec -it POD_NAME -c CONTAINER_NAME -- /bin/bash 

Start interactive pod with bash

With busybox:

kubectl run -i --tty --rm debug --image=busybox --restart=Never -- sh

With an Ubuntu image:

kubectl run -i --tty --rm debug --image=ubuntu --restart=Never -- bash

How I make curl work directly ?

kubectl run my-shell --rm -i --tty --image byrnedo/alpine-curl -- http://web1-service.gw.svc.cluster.local/

Start interactive pod with a custom command bash

In this case we will ignore the image's entry point, and use /bin/sh as the entry point.

kubectl run my-shell --rm -i --tty --command --image byrnedo/alpine-curl -- /bin/sh 

With ssh client:

kubectl run -i --tty --rm debug --image=kroniak/ssh-client --restart=Never -- bash

Start with network utils

kubectl run --namespace ns my-shell --rm -i --tty --image amouat/network-utils -- bash

Resources

List all resources

kubectl api-resources -o wide

The output will look like:

NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND                             VERBS
bindings                                                                      true         Binding                          [create]
componentstatuses                 cs                                          false        ComponentStatus                  [get list]
serviceentries                                 networking.istio.io            true         ServiceEntry                     [delete deletecollection get list patch create update watch]
virtualservices                                networking.istio.io            true         VirtualService                   [delete deletecollection get list patch create update watch]

To use the resources:

kubectl get <NAME or SHORTNAME>.<APIGROUP>
kubectl get serviceentries.networking.istio.io

Kubectl in a Pod

kubectl create sa viewer
kubectl create clusterrolebinding viewer --clusterrole=view --serviceaccount=default:viewer
kubectl run -i --tty --rm debug --image=bitnami/kubectl --restart=Never --serviceaccount='viewer' -- get pods --all-namespaces