Kubernetes

Changelog
https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG.md

Dump Kubernetes Objects

kubectl get componentstatuses
kubectl get configmaps
kubectl get daemonsets
kubectl get deployments
kubectl get events
kubectl get endpoints
kubectl get horizontalpodautoscalers
kubectl get ingress
kubectl get jobs
kubectl get limitranges
kubectl get nodes
kubectl get namespaces
kubectl get pods
kubectl get pods --all-namespaces -o wide
kubectl get persistentvolumes
kubectl get persistentvolumeclaims
kubectl get quota
kubectl get resourcequotas
kubectl get replicasets
kubectl get replicationcontrollers
kubectl get secrets
kubectl get serviceaccounts
kubectl get services
kubectl get ep nginx
kubectl -n ingress-nginx get pods
 
kubectl run redis --image=redis -dry-run=client -o yaml > pod.yaml
kubectl edit pod redis
 
kubectl delete service my-service
 
kubectl run nginx --image=nginx --dry-run=client -o yaml
kubectl create deployment --image=nginx nginx
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml
kubectl create -f nginx-deployment.yaml
kubectl create deployment --image=nginx nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yaml
 
kubectl run nginx --image=nginx
kubectl run nginx --image=nginx --dry-run=client -o yaml
kubectl create deployment --image=nginx nginx --dy-run=client -o yaml
kubectl scale deployment nginx --replicas=4
kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml
kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml
 
kubectl run custom-nginx --image=nginx --port=8080
kubectl create namespace dev-ns
kubectl get all --all-namespaces
kubectl run httpd --image=httpd:alpine --port=80 --expose
 
kubectl run nginx --image=nginx
kubectl run nginx --image=nginx --dry-run=client -o yaml
kubectl create deployment --image=nginx nginxkubectl create deployment --image=nginx nginx --dry-run=client -o yaml
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml
kubectl create -f nginx-deployment.yaml
kubectl create deployment --image=nginx nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yaml
 
kubectl create deployment --image=nginx nginx --dry-run=client
 
kubectl top pod --containers=true
 
kubectl -n project-snake get pod -L app
 
kubectl get events -A --sort-by=.metadata.creationTimestamp
 
kubectl -n secret create secret generic secret5 --from-literal=user=foo --from-literal=pass=bar
 
kubeadm certs check-expiration
kubeadm certs renew all
kubeadm token list
 
# run temporary container
kubectl run tmp-shell --restart=Never --rm -i --tty --image centos -- /bin/bash

Access Kubernetes cluster without floating IP

CLUSTER_JUMPHOST=1.2.3.4
CLUSTER_MASTER=10.0.0.5
sshuttle --python /usr/bin/python3 --remote ubuntu@${CLUSTER_JUMPHOST} ${CLUSTER_MASTER}/32
export no_proxy=$no_proxy,10.0.0.5

Deploy nginx

kubectl create deployment nginx --image=nginx
kubectl scale deployment --replicas 2 nginx
kubectl get pods -o wide
# kubectl create service nodeport nginx --tcp=80:80
kubectl expose deployment nginx --type=LoadBalancer --name=nginx --port=80
kubectl get deployment nginx -o wide
kubectl get service nginx -o wide
kubectl get pods -o wide -l app=nginx
kubectl exec -it ${POD_NAME} -- /bin/shkubectl exec -it ${POD_NAME} -- bash

Deploy apache

kubectl create deployment apache --image=httpd
kubectl scale deployment --replicas 3 apache
kubectl expose deployment apache --type=LoadBalancer --name=apache --port=80

Debug

systemctl list-units --failed
systemctl status kubelet.service
systemctl status kube-apiserver.service
kubectl get pods --all-namespaces -o wide
kubectl -v=7 get nodes
kubectl logs -f -n default nginx-5c7588df-8k5lh

Documentation
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ - kubectl Cheat Sheet
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

Configuration

# ~/.kube/config 
apiVersion: v1
clusters:
- cluster:
    server: https://10.11.22.33:6443
  name: api-server
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []
foo.bar

Limits
https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource
https://kubernetes.io/docs/tasks/administer-cluster/manage-resources/memory-default-namespace/
https://kubernetes.io/docs/tasks/administer-cluster/manage-resources/cpu-default-namespace/

apiVersion: v1
kind: LimitRange
metadata:
  name: mem-limit-range
spec:
  limits:
  - default:
      memory: 512Mi
    defaultRequest:
      memory: 256Mi
    type: Container
 
apiVersion: v1
kind: LimitRange
metadata:
  name: cpu-limit-range
spec:
  limits:
  - default:
      cpu: 1
    defaultRequest:
      cpu: 0.5
    type: Container

Metrics

git clone https://github.com/kodekloudhub/kubernetes-metrics-server.git
kubectl create -f .

Edit / patch configuration of the fly

kubectl -n kube-system get deployment coredns -o yaml | \
  sed 's/allowPrivilegeEscalation: false/allowPrivilegeEscalation: true/g' | \
  kubectl apply -f -

Output / sort data with jsonpath

kubectl config view --kubeconfig=my-kube-config -o jsonpath='{.users[*].name }'
kubectl get pv -o custom-columns='NAME:.metadata..name,CAPACITY:.spec.capacity.storage' --sort-by='{.spec.capacity.storage}' > /opt/outputs/pv-and-capacity-sorted.txt
kubectl config view --kubeconfig my-kube-config -o jsonpath='{.contexts[?(@.context.user=="aws-user")].name}'

curl Kubernetes API

curl -k https://kubernetes.default/api/v1/secrets
 
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
curl -k https://kubernetes.default/api/v1/secrets -H "Authorization: Bearer ${TOKEN}"

Install k9s
https://github.com/derailed/k9s/releases

RELEASE=0.32.4
wget -q https://github.com/derailed/k9s/releases/download/v${RELEASE}/k9s_linux_amd64.deb -P /tmp/
sudo dpkg -i /tmp/k9s_linux_amd64.deb

Install helm
https://helm.sh/docs/intro/install/

curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
sudo apt-get install apt-transport-https --yes
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
sudo apt-get update
sudo apt-get install helm

Kubeconfig
https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
https://able8.medium.com/how-to-merge-multiple-kubeconfig-files-into-one-36fc987c2e2f
https://nikgrozev.com/2019/10/03/switch-between-multiple-kubernetes-clusters-with-ease/

export KUBECONFIG="$(find ~/.kube/ -type f | tr '\n' ':')"
kubectl config get-clusters
kubectl config current-context
kubectl config use-context my-cluster-01

Links
https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG.md
https://github.com/coreos/matchbox/tree/master/examples/terraform/bootkube-install
https://kubernetes.io/docs/reference/kubectl/cheatsheet/