Skip to content

Kubernetes Ściąga

Kubernetes, często skracany jako K8s (gdzie „8” zastępuje osiem liter w słowie „ubernete”), to system open-source do zarządzania skonteneryzowanymi aplikacjami na wielu hostach w platformie chmurowej. Celem Kubernetes jest uczynienie wdrażania skonteneryzowanych aplikacji prostym i wydajnym (potężnym). Kubernetes zapewnia mechanizmy wdrażania aplikacji, planowania, aktualizacji i konserwacji.

Przeglądanie informacji o zasobach

Węzły (Nodes)

Nazwa zasobu: nodes, Skrót: no

$ kubectl get no          # Wyświetl wszystkie węzły
$ kubectl get no -o wide  # Wyświetl więcej informacji o wszystkich węzłach
$ kubectl describe no     # Wyświetl szczegóły węzła
$ kubectl get no -o yaml  # Wyświetl szczegóły węzła w formacie YAML
$ kubectl get node --selector=[label_name] # Filtruj węzły według określonej etykiety
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# Wyświetl informacje o polach zdefiniowane przez wyrażenie jsonpath
$ kubectl top node [node_name] # Wyświetl użycie węzła (CPU/Pamięć/Storage)

Pody (Pods)

Nazwa zasobu: pods, Skrót: po

$ kubectl get po 				# Wyświetl wszystkie pody
$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po --show-labels 	# Wyświetl etykiety podów
$ 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 
# Eksportuj informacje o podzie do pliku YAML
$ kubectl get pods --field-selector status.phase=Running 		
# Filtruj informacje o podach za pomocą selektorów pól

Przestrzenie nazw (Namespaces)

Nazwa zasobu: namespaces, Skrót: ns

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

Deploymenty (Deployments)

Nazwa zasobu: deployments, Skrót: deploy

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

Usługi (Services)

Nazwa zasobu: services, Skrót: svc

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

DaemonSety

Nazwa zasobu: daemonsets, Skrót: 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

Zdarzenia (Events)

Nazwa zasobu: events, Skrót: ev

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

Logi (Logs)

$ 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

Konta serwisowe (Service Accounts)

Nazwa zasobu: serviceaccounts, Skrót: sa

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

ReplicaSety

Nazwa zasobu: replicasets, Skrót: rs

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

Role (Roles)

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

Sekrety (Secrets)

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

ConfigMapy

Nazwa zasobu: configmaps, Skrót: cm

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

Ingressy

Nazwa zasobu: ingresses, Skrót: ing

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

Wolumeny trwałe (PersistentVolumes)

Nazwa zasobu: persistentvolumes, Skrót: pv

$ kubectl get pv 
$ kubectl describe pv

Żądania wolumenów trwałych (PersistentVolumeClaims)

Nazwa zasobu: persistentvolumeclaims, Skrót: pvc

$ kubectl get pvc
$ kubectl describe pvc

Klasy pamięci (StorageClasses)

Nazwa zasobu: storageclasses, Skrót: sc

$ kubectl get sc
$ kubectl get sc -o yaml

Wiele zasobów

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

Modyfikowanie atrybutów zasobów

Tainty

$ kubectl taint [node_name] [taint_name]

Etykiety (Labels)

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

Cordon / Uncordon

$ kubectl cordon [node_name]   # Wyłącz węzeł z planowania
$ kubectl uncordon [node_name] # Włącz węzeł do planowania

Opróżnianie węzłów (Draining Nodes)

$ kubectl drain [node_name]    # Opróżnij węzeł

Węzły / Pody

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

Deploymenty / Przestrzenie nazw

$ 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]

Usługi

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

DaemonSety

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

Konta serwisowe

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

Adnotacje (Annotations)

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

Dodawanie zasobów

Tworzenie Podów

$ 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

Tworzenie usług

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

Tworzenie deploymentów

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

Interakcja z kontenerem

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

Generowanie plików YAML

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

Uzyskiwanie pomocy

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

Żądania

Wywołania API

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

Informacje o klastrze

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