Kubernetes, spesso abbreviato come K8s (dove l'8 rappresenta gli otto caratteri tra la 'K' e la 's' in '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 delle applicazioni containerizzate semplice ed efficiente (potente). Kubernetes fornisce un meccanismo per la distribuzione, la pianificazione, l'aggiornamento e la manutenzione delle applicazioni.
Visualizzare le informazioni sulle risorse
Nodi (Nodes)
Nome risorsa: nodes, Abbreviazione: no
$ kubectl get no # Mostra informazioni su tutti i nodi
$ kubectl get no -o wide # Mostra più informazioni su tutti i nodi
$ kubectl describe no # Mostra i dettagli del nodo
$ kubectl get no -o yaml # Mostra i dettagli del nodo in formato yaml
$ kubectl get node --selector=[label_name] # Filtra i nodi per etichetta
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# Restituisce i campi definiti dall'espressione jsonpath
$ kubectl top node [node_name] # Mostra l'utilizzo del nodo (CPU/Memoria/Archiviazione)
Pod
Nome risorsa: pods, Abbreviazione: po
$ kubectl get po # Mostra informazioni su 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 i pod utilizzando un selettore di campi
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
Account di servizio (Service Accounts)
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 (Persistent Volumes)
Nome risorsa: persistentvolumes, Abbreviazione: pv
$ kubectl get pv
$ kubectl describe pv
Richieste di volumi persistenti (Persistent Volume Claims)
Nome risorsa: persistentvolumeclaims, Abbreviazione: pvc
$ kubectl get pvc
$ kubectl describe pvc
Classi di archiviazione (Storage Classes)
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
Modificare gli 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] # Nodo in manutenzione
$ kubectl uncordon [node_name] # Nodo programmabile
Svuota nodo (Drain)
$ 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 [ns_name]
$ 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]
Account di servizio
$ kubectl edit sa [sa_name]
$ kubectl delete sa [sa_name]
Annotazioni (Annotations)
$ kubectl annotate po [pod_name] [annotation]
$ kubectl annotate no [node_name] [annotation]
Aggiungere risorse
Creazione di Pod
$ kubectl create -f [name_of_file]
$ kubectl apply -f [name_of_file]
$ 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 un 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
Ottenere aiuto
$ kubectl -h
$ kubectl create -h
$ kubectl run -h
$ kubectl explain deploy.spec
Richieste (Requests)
Chiamate API
$ kubectl get --raw /apis/metrics.k8s.io/
Informazioni sul cluster
$ kubectl config
$ kubectl cluster-info
$ kubectl get componentstatus