Kubernetes is hard

Dashboards

:::danger Ask someone for the dashboard urls :::

kubectx & kubens

% brew install kubectx

Switch Context and NameSpace

% kubectx # list contexts
docker-desktop
% kubectx --current # list current context
docker-desktop
% kubectx docker-desktop # set context
% kubens # list namespaces
default
kube-node-lease
kube-public
kube-system
postgres
% kubens --current # list current namespace
postgres
% kubens default # set namespace
% kubens - # set namespace to the prior one

Connect to a pod

% kubectl exec --stdin --tty <pod-name> -- /bin/bash

Other Commands

% kubectl get pods --all-namespaces
% kubectl config get-contexts

Logs with kubetail

Install

% asdf plugin-add kubetail https://github.com/janpieper/asdf-kubetail.git
% asdf install kubetail latest
% asdf global kubetail <version>

Use

% kubetail <service name>

Or with a specific namespace

KUBETAIL_NAMESPACE=my-namespace kubetail <pod name>

Kubectl useful commands

List all objects

% k u b e c t l g e t a l l - A

Get object ID

% k u b e c t l g e t n a m e s p a c e < n a m e s p a c e _ n a m e > o u t p u t j s o n p a t h = { . m e t a d a t a . u i d }

List all services

% kubectl -n kube-system get services

or

% kubectl get services --all-namespaces

View Config

% kubectl config view

List and use contexts

% kubectl config get-contexts
% kubectl use-context <name>

List and use namespaces

% kubectl get namespaces
% kubectl config set-context --current --namespace=<name>

Pods

Get all pods

% kubectl get pods --namespace <namespace>

Describe pod

% kubectl describe pod <pod_name> --namespace <namespace>

Count pods

% k g e t p o d s | g r e p ' d a e m o n ' | w c - l

Restart a Pod

You can use the following methods to restart a pod with kubectl. Once new pods are re-created they will have a different name than the old ones. A list of pods can be obtained using the kubectl get pods command.

% kubectl rollout restart

This method is the recommended first port of call as it will not introduce downtime as pods will be functioning. A rollout restart will kill one pod at a time, then new pods will be scaled up. This method can be used as of K8S v1.15.

% kubectl rollout restart deployment <deployment_name> -n <namespace>

Services

Get services

% kubectl get svc

Describe service

% kubectl describe svc <service_name>

Get a token

% kubectl create token default

List Resources

% kubectl api-resources --namespaced=true

Specific resource get

% kubectl get secrets

Delete Resources

Delete all resources.

% kubectl delete all --all -n {namespace}

The previous command doesn’t delete admin level resource, using the following script will get rid of things such as limits, quotas, secrets, etc.

% kubectl delete "$(kubectl api-resources --namespaced=true --verbs=delete -o name | tr "\n" "," | sed -e 's/,$//')" --all

Create Secret

% kubectl -n auth0-inter create secret generic auth0-inter-management-client-secret --from-literal=auth0__management_client_secret='fuck-you-ezekiel' --from-file secret/

Get deployed image

% k get deployment -n <namespace> api -oyaml | grep -e "image:"

Port Forwarding

% kubectl port-forward -n default service/halleck-kube-prometheus-stack-grafana <from_port>:<to_port

Kubenetes with Docker Image

Build the image

% docker build -t jaiken/fast-api .

Docker login and push

% docker login && docker push jaiken/fast-api

Example deploy document, deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: fast-api-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: fast-api
  template:
    metadata:
      labels:
        app: fast-api
    spec:
      containers:
        - name: fast-api
          image: bravinwasike/fast-api
          resources:
            limits:
              memory: "128Mi"
              cpu: "500m"
          ports:
            - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: fast-api-service
spec:
  selector:
    app: fast-api
  ports:
    - port: 80
      targetPort: 8080
  type: LoadBalancer

Apply deploy document

kubectl apply -f deploy.yaml

Create a Workstation Pod

ubuntu client

m a k m s E i p i e p O c i n t n n e c F r V d a a a c o o e : d m m : n - k r a e e t 8 s P t : s a n i c s i o a p i a m o o d : u a n m a m - - k n b c e e g m u : u e r : e a s i b n : s : n l n e v t : u d e f c 1 u d b u : e i t e u b p n l f n u i a t n t a u u t y p l u p t l y - f - < E O F

Connect

% k u b e c t l e x e c - n d e f a u l t - i t u b u n t u / b i n / b a s h

How to delete Persistent Volume stuck in terminating state?

Change state of the PV

% k u b e c t l p a t c h p v < p v - n a m e > - p ' { " m e t a d a t a " : { " f i n a l i z e r s " : n u l l } } '

Delete PV

% k u b e c t l d e l e t e p v < p v - n a m e >