Files
work/notes/nextcloud/Notes/IT-Know-How/web und cloud gedoens/kubectl.md
T
2026-03-17 19:04:55 +01:00

358 lines
6.0 KiB
Markdown
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Hier ist eine kompakte, praxisorientierte Anleitung zu `kubectl` mit den wichtigsten Funktionen und Use Cases.
---
## 1. Was ist `kubectl`?
`kubectl` ist das Kommandozeilen-Tool, um mit einem Kubernetes-Cluster zu kommunizieren.
Damit kannst du:
- Ressourcen erstellen, ändern und löschen (Deployments, Pods, Services, …)
- Logs einsehen und Probleme debuggen
- Rollouts steuern (Deployments updaten/rollback)
- Konfigurationen verwalten (Kontexte, Namespaces, Kubeconfig)
---
## 2. Grundlagen: Aufbau von `kubectl`-Befehlen
Allgemeines Schema:
```bash
kubectl <verb> <ressource> [name] [flags]
```
Beispiele:
```bash
kubectl get pods
kubectl describe pod mein-pod
kubectl delete service mein-service
```
Wichtige Verben:
- `get` Anzeigen
- `describe` Detailinformationen
- `create` / `apply` Ressourcen anlegen/aktualisieren
- `delete` Löschen
- `logs` Logs anzeigen
- `exec` Befehle innerhalb eines Pods ausführen
---
## 3. Konfiguration & Kontexte
### 3.1 Cluster-Zugriff prüfen
```bash
kubectl version
kubectl cluster-info
```
### 3.2 Aktuellen Kontext anzeigen und wechseln
```bash
kubectl config get-contexts
kubectl config current-context
kubectl config use-context mein-context
```
### 3.3 Namespace festlegen
Temporär per Flag:
```bash
kubectl get pods -n my-namespace
```
Standard-Namespace setzen:
```bash
kubectl config set-context --current --namespace=my-namespace
```
---
## 4. Ressourcen anzeigen und inspizieren
### 4.1 Ressourcen auflisten
```bash
kubectl get pods
kubectl get deployments
kubectl get services
kubectl get all
```
Mit mehr Details:
```bash
kubectl get pods -o wide
kubectl get pods -o yaml
```
### 4.2 Detailinformationen zu einer Ressource
```bash
kubectl describe pod mein-pod
kubectl describe deployment mein-deployment
```
Use Case: Debugging (Events, Container-Status, Restart-Gründe).
---
## 5. Deployments & Anwendungen verwalten
### 5.1 Deployment aus YAML erstellen
`deployment.yaml` Beispiel (stark vereinfacht):
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
```
Anwenden:
```bash
kubectl apply -f deployment.yaml
```
### 5.2 Deployment aktualisieren (Rolling Update)
Image ändern:
```bash
kubectl set image deployment/nginx-deployment nginx=nginx:1.26
```
Rollout-Status beobachten:
```bash
kubectl rollout status deployment/nginx-deployment
```
Rollback:
```bash
kubectl rollout undo deployment/nginx-deployment
```
### 5.3 Replica-Anzahl skalieren
```bash
kubectl scale deployment nginx-deployment --replicas=5
```
---
## 6. Services & Zugriff auf Anwendungen
### 6.1 Services anzeigen
```bash
kubectl get svc
```
Beispiel-Service:
```yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: ClusterIP
selector:
app: nginx
ports:
- port: 80
targetPort: 80
```
Anwenden:
```bash
kubectl apply -f service.yaml
```
### 6.2 Port-Forwarding (lokaler Zugriff)
```bash
kubectl port-forward svc/nginx-service 8080:80
# Aufruf im Browser: http://localhost:8080
```
---
## 7. Logs & Debugging
### 7.1 Logs eines Pods
```bash
kubectl logs mein-pod
kubectl logs mein-pod -c container-name
kubectl logs -f mein-pod # -f = "follow"
```
### 7.2 Befehle im Container ausführen
```bash
kubectl exec -it mein-pod -- /bin/sh
# oder
kubectl exec -it mein-pod -- bash
```
Typischer Use Case: Debugging von laufenden Containern, z.B. Netzwerkprobleme, Dateisystem prüfen.
### 7.3 Problem-Analyse mit `describe` & Events
```bash
kubectl describe pod mein-pod
kubectl get events --sort-by=.metadata.creationTimestamp
```
---
## 8. Ressourcen ändern & löschen
### 8.1 Änderungen anwenden
Konfiguration in einer Datei ändern und dann:
```bash
kubectl apply -f deployment.yaml
```
Um tatsächliche Unterschiede zu sehen:
```bash
kubectl diff -f deployment.yaml
```
### 8.2 Ressourcen löschen
```bash
kubectl delete pod mein-pod
kubectl delete -f deployment.yaml
kubectl delete deployment nginx-deployment
```
---
## 9. Konfigurationen & Secrets
### 9.1 ConfigMaps
Erstellen aus einer Datei:
```bash
kubectl create configmap app-config --from-file=config.properties
```
Oder aus Literalwert:
```bash
kubectl create configmap app-config --from-literal=KEY=VALUE
```
ConfigMaps anzeigen:
```bash
kubectl get configmaps
kubectl describe configmap app-config
```
### 9.2 Secrets
```bash
kubectl create secret generic db-secret \
--from-literal=username=user \
--from-literal=password=geheim
```
Achtung: Standard-`kubectl get secret -o yaml` zeigt Base64-codierte, aber nicht verschlüsselte Daten.
---
## 10. Weitere nützliche Features
### 10.1 Autocomplete
Bash-Beispiel:
```bash
source <(kubectl completion bash)
echo 'source <(kubectl completion bash)' >> ~/.bashrc
```
### 10.2 Kustomize (ohne extra Tool, ab neueren kubectl-Versionen)
```bash
kubectl apply -k ./overlays/production
```
Struktur z.B.:
```text
base/
deployment.yaml
kustomization.yaml
overlays/production/
kustomization.yaml
patches.yaml
```
---
## 11. Typische Praxis-Workflows
### 11.1 Neue Version deployen
```bash
git pull
kubectl apply -f k8s/
kubectl rollout status deployment mein-service
kubectl get pods -o wide
```
### 11.2 Fehlerhafte Anwendung debuggen
```bash
kubectl get pods
kubectl describe pod fehler-pod
kubectl logs fehler-pod
kubectl exec -it fehler-pod -- sh
kubectl get events --sort-by=.metadata.creationTimestamp
```
### 11.3 Schnell in einen anderen Namespace/Cluster wechseln
```bash
kubectl config use-context staging-cluster
kubectl config set-context --current --namespace=team-a
kubectl get pods
```
---
Wenn du möchtest, kann ich dir für deinen konkreten Use Case (z.B. “Web-App deployen”, “CI/CD-Pipeline”, “lokales Minikube-Cluster”) eine konkrete Schritt-für-Schritt-Anleitung mit fertigen `kubectl`-Befehlen erstellen.