Skip to content

Kubernetes Cheat Sheet (Prontuario)

Kubernetes, spesso abbreviato in K8s (dove l’“8” sostituisce le otto lettere della parola “ubernete”), è un sistema open-source per la gestione di applicazioni containerizzate su più host in una piattaforma cloud. L’obiettivo di Kubernetes è rendere la distribuzione di applicazioni containerizzate semplice ed efficiente (potente). Kubernetes fornisce meccanismi per la distribuzione, la pianificazione, l’aggiornamento e la manutenzione delle applicazioni.

Visualizzazione delle informazioni sulle risorse

Nodi (Nodes)

Nome risorsa: nodes, Abbreviazione: no

$ kubectl get no          # Mostra tutti i nodi
$ kubectl get no -o wide  # Mostra più informazioni su tutti i nodi
$ kubectl describe no     # Mostra i dettagli dei nodi
$ kubectl get no -o yaml  # Mostra i dettagli dei nodi in formato YAML
$ kubectl get node --selector=[label_name] # Filtra i nodi per etichetta specifica
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# Mostra le informazioni dei campi definite dall'espressione jsonpath
$ kubectl top node [node_name] # Mostra l'utilizzo del nodo (CPU/Memoria/Storage)

Pod

Nome risorsa: pods, Abbreviazione: po

$ kubectl get po 				# Mostra tutti i pod
$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po --show-labels 	# Visualizza le etichette dei pod
$ kubectl get po -l app=nginx
$ kubectl get po -o yaml
$ kubectl get pod [pod_name] -o yaml --export
$ kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml 
# Esporta le informazioni del pod in un file YAML
$ kubectl get pods --field-selector status.phase=Running 		
# Filtra le informazioni sui pod utilizzando i selettori di campo

Namespace

Nome risorsa: namespaces, Abbreviazione: ns

$ kubectl get ns
$ kubectl get ns -o yaml
$ kubectl describe ns

Deployment

Nome risorsa: deployments, Abbreviazione: deploy

$ kubectl get deploy
$ kubectl describe deploy
$ kubectl get deploy -o wide 
$ kubectl get deploy -o yaml

Servizi (Services)

Nome risorsa: services, Abbreviazione: svc

$ kubectl get svc
$ kubectl describe svc
$ kubectl get svc -o wide 
$ kubectl get svc -o yaml
$ kubectl get svc --show-labels

DaemonSet

Nome risorsa: daemonsets, Abbreviazione: ds

$ kubectl get ds
$ kubectl describe ds --all-namespaces
$ kubectl describe ds [daemonset_name] -n [namespace_name]
$ kubectl get ds [ds_name] -n [ns_name] -o yaml

Eventi (Events)

Nome risorsa: events, Abbreviazione: ev

$ kubectl get events 
$ kubectl get events -n kube-system
$ kubectl get events -w

Log

$ kubectl logs [pod_name]
$ kubectl logs --since=1h [pod_name]
$ kubectl logs --tail=20 [pod_name]
$ kubectl logs -f -c [container_name] [pod_name]
$ kubectl logs [pod_name] > pod.log

Service Account

Nome risorsa: serviceaccounts, Abbreviazione: sa

$ kubectl get sa
$ kubectl get sa -o yaml
$ kubectl get serviceaccounts default -o yaml >./sa.yaml
$ kubectl replace serviceaccount default -f ./sa.yaml

ReplicaSet

Nome risorsa: replicasets, Abbreviazione: rs

$ kubectl get rs
$ kubectl describe rs
$ kubectl get rs -o wide 
$ kubectl get rs -o yaml

Ruoli (Roles)

$ kubectl get roles --all-namespaces
$ kubectl get roles --all-namespaces -o yaml

Secret

$ kubectl get secrets
$ kubectl get secrets --all-namespaces 
$ kubectl get secrets -o yaml

ConfigMap

Nome risorsa: configmaps, Abbreviazione: cm

$ kubectl get cm
$ kubectl get cm --all-namespaces
$ kubectl get cm --all-namespaces -o yaml

Ingress

Nome risorsa: ingresses, Abbreviazione: ing

$ kubectl get ing
$ kubectl get ing --all-namespaces

Volumi Persistenti (PersistentVolumes)

Nome risorsa: persistentvolumes, Abbreviazione: pv

$ kubectl get pv 
$ kubectl describe pv

Richieste di Volumi Persistenti (PersistentVolumeClaims)

Nome risorsa: persistentvolumeclaims, Abbreviazione: pvc

$ kubectl get pvc
$ kubectl describe pvc

StorageClass

Nome risorsa: storageclasses, Abbreviazione: sc

$ kubectl get sc
$ kubectl get sc -o yaml

Risorse Multiple

$ kubectl get svc, po
$ kubectl get deploy, no
$ kubectl get all
$ kubectl get all --all-namespaces

Modifica degli attributi delle risorse

Taint

$ kubectl taint [node_name] [taint_name]

Etichette (Labels)

$ kubectl label [node_name] disktype=ssd 
$ kubectl label [pod_name] env=prod

Cordon / Uncordon

$ kubectl cordon [node_name]   # Segna il nodo come non pianificabile
$ kubectl uncordon [node_name] # Segna il nodo come pianificabile

Svuotamento dei nodi (Draining Nodes)

$ kubectl drain [node_name]    # Svuota il nodo

Nodi / Pod

$ kubectl delete node [node_name] 
$ kubectl delete pod [pod_name]
$ kubectl edit node [node_name]
$ kubectl edit pod [pod_name]

Deployment / Namespace

$ kubectl edit deploy [deploy_name]
$ kubectl delete deploy [deploy_name]
$ kubectl expose deploy [deploy_name] --port=80 --type=NodePort
$ kubectl scale deploy [deploy_name] --replicas=5
$ kubectl delete ns
$ kubectl edit ns [ns_name]

Servizi

$ kubectl edit svc [svc_name]
$ kubectl delete svc [svc_name]

DaemonSet

$ kubectl edit ds [ds_name] -n kube-system 
$ kubectl delete ds [ds_name]

Service Account

$ kubectl edit sa [sa_name]
$ kubectl delete sa [sa_name]

Annotazioni

$ kubectl annotate po [pod_name] [annotation]
$ kubectl annotate no [node_name]

Aggiunta di risorse

Creazione di Pod

$ kubectl create -f [name_of_file] 
$ kubectl apply -f [name_of_file]
$ kubectl run [pod_name] --image=nginx --restart=Never
$ kubectl run [pod_name] --generator=run-pod/v1 --image=nginx
$ kubectl run [pod_name] --image=nginx --restart=Never

Creazione di Servizi

$ kubectl create svc nodeport [svc_name] --tcp=8080:80

Creazione di Deployment

$ kubectl create -f [name_of_file] 
$ kubectl apply -f [name_of_file]
$ kubectl create deploy [deploy_name] --image=nginx

Interazione con i container

$ kubectl run [pod_name] --image=busybox --rm -it --restart=Never -- sh

Output di file YAML

$ kubectl create deploy [deploy_name] --image=nginx --dry-run -o yaml > deploy.yaml
$ kubectl get po [pod_name] -o yaml --export > pod.yaml

Aiuto

$ kubectl -h
$ kubectl create -h
$ kubectl run -h
$ kubectl explain deploy.spec

Richieste

Chiamate API

$ kubectl get --raw /apis/metrics.k8s.io/

Informazioni sul cluster

$ kubectl config
$ kubectl cluster-info
$ kubectl get componentstatus