-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathk8s-deploy.sh
More file actions
108 lines (83 loc) · 3.43 KB
/
k8s-deploy.sh
File metadata and controls
108 lines (83 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
set -e
if ! sudo systemctl is-active --quiet k3s 2>/dev/null; then
sudo systemctl start k3s
fi
while ! sudo k3s kubectl get nodes &>/dev/null || ! sudo k3s ctr version &>/dev/null; do
sleep 2
done
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
BACKEND_DIR="$SCRIPT_DIR/backend"
FRONTEND_DIR="$SCRIPT_DIR/frontend"
K8S_DIR="$SCRIPT_DIR/k8s"
NAMESPACE="cityzen"
BACKEND_SERVICES=(
"account-service"
"analytics-service"
"assignment-service"
"authentication-service"
"feedback-service"
"geolocation-service"
"map-service"
"media-service"
"notification-service"
"parameter-configuration-service"
"registration-service"
"report-service"
"ticket-service"
)
for service in "${BACKEND_SERVICES[@]}"; do
docker build -t "cityzen/$service:latest" "$BACKEND_DIR/$service"
done
docker build -t "cityzen/frontend:latest" --build-arg VITE_API_URL=http://cityzen.local "$FRONTEND_DIR"
import_image() {
local tar_file="$1"
while ! sudo k3s ctr images import "$tar_file" 2>/dev/null; do
sudo systemctl restart k3s
while ! sudo k3s ctr version &>/dev/null; do sleep 2; done
done
}
for service in "${BACKEND_SERVICES[@]}"; do
docker save "cityzen/$service:latest" -o "/tmp/$service.tar"
import_image "/tmp/$service.tar"
rm "/tmp/$service.tar"
done
docker save "cityzen/frontend:latest" -o "/tmp/frontend.tar"
import_image "/tmp/frontend.tar"
rm "/tmp/frontend.tar"
while ! kubectl get nodes &>/dev/null; do
sudo systemctl restart k3s
sleep 5
done
kubectl apply -f "$K8S_DIR/namespace.yaml"
for service in "${BACKEND_SERVICES[@]}"; do
configmap="$K8S_DIR/${service%-service}-configmap.yaml"
[ -f "$configmap" ] && kubectl apply -f "$configmap"
done
for service in "${BACKEND_SERVICES[@]}"; do
secret_name="${service%-service}-secret"
env_file="$BACKEND_DIR/$service/.env"
[ -f "$env_file" ] && kubectl create secret generic "$secret_name" --from-env-file="$env_file" -n "$NAMESPACE" --dry-run=client -o yaml | kubectl apply -f -
done
kubectl create secret generic minio-secret --from-literal=MINIO_ROOT_USER=cityzenG03 --from-literal=MINIO_ROOT_PASSWORD=cityzenG03 -n "$NAMESPACE" --dry-run=client -o yaml | kubectl apply -f -
kubectl apply -f "$K8S_DIR/mongodb-statefulset.yaml"
kubectl apply -f "$K8S_DIR/minio-deployment.yaml"
kubectl wait --for=condition=ready pod -l app=mongodb -n "$NAMESPACE" --timeout=120s || true
kubectl wait --for=condition=ready pod -l app=minio -n "$NAMESPACE" --timeout=120s || true
for service in "${BACKEND_SERVICES[@]}"; do
kubectl apply -f "$K8S_DIR/${service%-service}-deployment.yaml"
done
kubectl apply -f "$K8S_DIR/frontend-deployment.yaml"
kubectl apply -f "$K8S_DIR/ingress.yaml"
kubectl apply -f "$K8S_DIR/hpa.yaml"
kubectl scale deployment traefik -n kube-system --replicas=0 2>/dev/null || true
kubectl delete daemonset -n kube-system -l svccontroller.k3s.cattle.io/svcname=kube-system/traefik --ignore-not-found 2>/dev/null || true
kubectl patch svc ingress-nginx-controller -n ingress-nginx -p '{"spec":{"type":"LoadBalancer"}}' 2>/dev/null || true
echo "Configurazione automatica endpoint MinIO..."
HOST_IP=$(hostname -I | awk '{print $1}')
kubectl patch configmap media-config -n "$NAMESPACE" --type merge -p "{\"data\":{\"MINIO_EXTERNAL_ENDPOINT\":\"http://$HOST_IP:30090\"}}"
kubectl rollout restart deployments -n "$NAMESPACE"
kubectl get pods -n "$NAMESPACE"
APP_URL="http://$HOST_IP"
echo ""
echo "URL: $APP_URL"