Initialer Commit – Prototyp mit Istio und Zero Trust

main
David Miller 2025-06-10 15:08:35 +02:00
commit 91469b00e9
11 changed files with 356 additions and 0 deletions

BIN
.DS_Store vendored 100644

Binary file not shown.

216
README.md 100644
View File

@ -0,0 +1,216 @@
# Prototyp-Dokumentation: Sichere DevOps Microservices mit Istio (Mac + Docker Desktop)
## Voraussetzungen
✅ **Betriebssystem:**
- macOS mit Adminrechten
✅ **Tools, die du installiert hast:**
1. **Homebrew (wenn nicht vorhanden):**
```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
2. **Mit Homebrew installiert:**
```bash
brew install kubectl
brew install minikube
brew install istioctl
brew install helm
```
3. **Docker Desktop für Mac** installieren und starten
---
## Cluster & Istio aufsetzen
```bash
minikube start --driver=docker
```
Istio installieren:
```bash
curl -L https://istio.io/downloadIstio | sh -
cd istio-1.22.0
export PATH=$PWD/bin:$PATH
istioctl install --set profile=demo -y
```
---
## Namespace & Sidecar-Injection vorbereiten
```bash
kubectl create namespace demo
kubectl label namespace demo istio-injection=enabled
```
---
## Docker-Images erstellen & hochladen
**DockerHub:**
- Accountname: `deinName!`
In den Verzeichnissen `metrics-api` und `dashboard`:
```bash
docker build -t deinName!/metrics-api:latest .
docker push deinName!/metrics-api:latest
docker build -t deinName!/dashboard:latest .
docker push deinName!/dashboard:latest
```
**Hinweis:** Dockerfile wurde angepasst, damit `curl` installiert ist.
---
## Kubernetes YAMLs anpassen
In `k8s/metrics-api.yaml` und `k8s/dashboard.yaml`:
```yaml
image: deinName!/metrics-api:latest
image: deinName!/dashboard:latest
```
---
## Deployments ausrollen
```bash
kubectl apply -n demo -f k8s/metrics-api.yaml
kubectl apply -n demo -f k8s/dashboard.yaml
```
Optional vorher löschen:
```bash
kubectl delete -n demo -f k8s/*.yaml
```
---
## mTLS und Zero Trust aktivieren
```bash
kubectl apply -n demo -f k8s/peerauth-strict.yaml
kubectl apply -n demo -f k8s/authorization-policy.yaml
```
---
## Funktion testen
### ✅ Zugriff aus dem autorisierten `dashboard`-Pod
```bash
kubectl exec -n demo $(kubectl get pod -n demo -l app=dashboard -o jsonpath='{.items[0].metadata.name}') -c dashboard -- curl http://metrics-api:8000/metrics
```
Erwartete Ausgabe:
```json
{
"uptime": "72h",
"build_status": "success",
"deployment_frequency": "5/week"
}
```
---
### 🔒 Zugriff aus dem nicht autorisierten `intruder`-Pod
```bash
kubectl apply -n demo -f k8s/intruder.yaml
kubectl exec -n demo -it intruder -- sh
apk add curl
curl http://metrics-api:8000/metrics
```
Erwartete Ausgabe:
```
RBAC: access denied
```
---
## Optional: Kiali starten und Visualisierung öffnen
```bash
kubectl apply -f samples/addons
istioctl dashboard kiali
```
Danach öffnet sich ein Browserfenster mit Kiali. Dort kannst du unter "Graph" die Kommunikation zwischen den Services (z.B. `dashboard → metrics-api`) sowie Policy-Durchsetzungen visualisieren.
---
## 🔄 Prototyp erneut starten (nach Rechner-Neustart oder späterem Zeitpunkt)
### ✅ 1. Docker Desktop öffnen
Stelle sicher, dass **Docker Desktop läuft**, bevor du mit Minikube arbeitest.
### ✅ 2. Minikube starten
```bash
minikube start --driver=docker
```
> Falls du den Cluster vorher gelöscht hast, kannst du ihn mit `minikube delete` zurücksetzen und dann wieder starten.
### ✅ 3. (Falls nötig) Istio wieder installieren
Nur wenn du Minikube komplett gelöscht hast:
```bash
cd istio-1.22.0
export PATH=$PWD/bin:$PATH
istioctl install --set profile=demo -y
kubectl create namespace demo
kubectl label namespace demo istio-injection=enabled
```
### ✅ 4. Deployments und Sicherheitsrichtlinien anwenden
Navigiere in deinen YAML-Ordner:
```bash
cd ~/devops-prototype/k8s
```
Dann:
```bash
kubectl apply -n demo -f metrics-api.yaml
kubectl apply -n demo -f dashboard.yaml
kubectl apply -n demo -f peerauth-strict.yaml
kubectl apply -n demo -f authorization-policy.yaml
kubectl apply -n demo -f intruder.yaml
```
### ✅ 5. Warten bis die Pods laufen
```bash
kubectl get pods -n demo
```
Die Pods sollten den Status `2/2 Running` haben.
### ✅ 6. Testen, ob alles läuft
```bash
kubectl exec -n demo $(kubectl get pod -n demo -l app=dashboard -o jsonpath='{.items[0].metadata.name}') -c dashboard -- curl http://metrics-api:8000/metrics
```
Wenn du die JSON-Daten erhältst, funktioniert alles korrekt.
---
## Zusammenfassung Was jetzt läuft
| Komponente | Status |
|---------------------------------------------|--------|
| Minikube mit Docker Driver | ✅ |
| Istio mit Sidecars | ✅ |
| Zwei FastAPI-Services (metrics-api, dashboard) | ✅ |
| Kommunikation abgesichert mit mTLS (STRICT) | ✅ |
| Zugriff beschränkt mit Zero Trust Policy | ✅ |
| Test mit autorisiertem Zugriff (`dashboard`) | ✅ |
| Test mit blockiertem Zugriff (`intruder`) | ✅ |
| Visualisierung mit Kiali | ✅ |

View File

@ -0,0 +1,5 @@
FROM python:3.10-slim
WORKDIR /app
COPY main.py .
RUN pip install fastapi uvicorn requests
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

View File

@ -0,0 +1,9 @@
from fastapi import FastAPI
import requests
app = FastAPI()
@app.get("/")
def read_metrics():
res = requests.get("http://metrics-api:8000/metrics")
return res.json()

View File

@ -0,0 +1,14 @@
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: allow-dashboard-to-metrics
namespace: demo
spec:
selector:
matchLabels:
app: metrics-api
rules:
- from:
- source:
principals:
- cluster.local/ns/demo/sa/dashboard-sa

41
k8s/dashboard.yaml 100644
View File

@ -0,0 +1,41 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: dashboard-sa
namespace: demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dashboard
namespace: demo
labels:
app: dashboard
spec:
replicas: 1
selector:
matchLabels:
app: dashboard
template:
metadata:
labels:
app: dashboard
spec:
serviceAccountName: dashboard-sa
containers:
- name: dashboard
image: brick99/dashboard:latest
ports:
- containerPort: 8000
---
apiVersion: v1
kind: Service
metadata:
name: dashboard
namespace: demo
spec:
selector:
app: dashboard
ports:
- port: 8000
targetPort: 8000

13
k8s/intruder.yaml 100644
View File

@ -0,0 +1,13 @@
apiVersion: v1
kind: Pod
metadata:
name: intruder
namespace: demo
labels:
app: intruder
spec:
containers:
- name: intruder
image: alpine
command: ["/bin/sh"]
args: ["-c", "sleep 3600"]

View File

@ -0,0 +1,34 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: metrics-api
namespace: demo
labels:
app: metrics-api
spec:
replicas: 1
selector:
matchLabels:
app: metrics-api
template:
metadata:
labels:
app: metrics-api
spec:
containers:
- name: metrics-api
image: brick99/metrics-api:latest
ports:
- containerPort: 8000
---
apiVersion: v1
kind: Service
metadata:
name: metrics-api
namespace: demo
spec:
selector:
app: metrics-api
ports:
- port: 8000
targetPort: 8000

View File

@ -0,0 +1,8 @@
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: demo
spec:
mtls:
mode: STRICT

View File

@ -0,0 +1,5 @@
FROM python:3.10-slim
WORKDIR /app
COPY main.py .
RUN pip install fastapi uvicorn requests
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

View File

@ -0,0 +1,11 @@
from fastapi import FastAPI
app = FastAPI()
@app.get("/metrics")
def get_metrics():
return {
"uptime": "72h",
"build_status": "success",
"deployment_frequency": "5/week"
}