-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunner_k8s.sh
More file actions
executable file
Β·160 lines (133 loc) Β· 4.82 KB
/
runner_k8s.sh
File metadata and controls
executable file
Β·160 lines (133 loc) Β· 4.82 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
set -e # Exit script on errors
#set -x # Enable debugging
# Source common Docker utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/runner_utils.sh"
UPLOADS_PV_PATH_ON_HOST="/tmp/k8-uploads"
function start_cluster() {
echo "π Applying Kubernetes configurations..."
echo "π Creating/verifying directory location for Uploads persistent volume on host server: $UPLOADS_PV_PATH_ON_HOST"
mkdir -p $UPLOADS_PV_PATH_ON_HOST
kubectl apply -f "k8s/restaurants-service/uploads-pv.yaml"
kubectl apply -f "k8s/restaurants-service/uploads-pvc.yaml"
# All services
jq -c '.services[]' $CONFIG_FILE | while read -r svc; do
NAME=$(echo $svc | jq -r '.name')
PREFIX=$(echo $svc | jq -r '.prefix')
K8S_PATH=$(echo $svc | jq -r '.k8sPath')
echo "β Applying $NAME resources from $K8S_PATH"
kubectl apply -f "$K8S_PATH/${PREFIX}-deployment.yaml"
kubectl apply -f "$K8S_PATH/${PREFIX}-service.yaml"
done
# NGINX
NGINX_PATH=$(jq -r '.nginx.k8sPath' $CONFIG_FILE)
echo "β Applying NGINX resources from $NGINX_PATH"
kubectl apply -f "$NGINX_PATH/nginx-config.yaml"
kubectl apply -f "$NGINX_PATH/nginx-deployment.yaml"
kubectl apply -f "$NGINX_PATH/nginx-service.yaml"
echo "β
Kubernetes resources applied."
list_resources
}
function stop_cluster() {
echo "π Deleting Kubernetes resources..."
# All services
jq -c '.services[]' $CONFIG_FILE | while read -r svc; do
NAME=$(echo $svc | jq -r '.name')
PREFIX=$(echo $svc | jq -r '.prefix')
K8S_PATH=$(echo $svc | jq -r '.k8sPath')
echo "β Deleting $NAME resources from $K8S_PATH"
kubectl delete -f "$K8S_PATH/${PREFIX}-deployment.yaml" || echo "β οΈ Warning: Failed to delete from $K8S_PATH, continuing..."
kubectl delete -f "$K8S_PATH/${PREFIX}-service.yaml" || echo "β οΈ Warning: Failed to delete from $K8S_PATH, continuing..."
done
# NGINX
NGINX_PATH=$(jq -r '.nginx.k8sPath' $CONFIG_FILE)
echo "β Deleting NGINX resources from $NGINX_PATH"
kubectl delete -f "$NGINX_PATH/nginx-config.yaml" || echo "β οΈ Warning: Failed to delete from $NGINX_PATH, continuing..."
kubectl delete -f "$NGINX_PATH/nginx-deployment.yaml" || echo "β οΈ Warning: Failed to delete from $NGINX_PATH, continuing..."
kubectl delete -f "$NGINX_PATH/nginx-service.yaml" || echo "β οΈ Warning: Failed to delete from $NGINX_PATH, continuing..."
kubectl delete -f "k8s/restaurants-service/uploads-pvc.yaml" || echo "β οΈ Warning: Failed to delete from uploads-pvc.yaml, continuing..."
kubectl delete -f "k8s/restaurants-service/uploads-pv.yaml" || echo "β οΈ Warning: Failed to delete uploads-pv.yaml, continuing..."
echo "β
Kubernetes resources deleted."
echo "ποΈ Clean MongoDB persistent volume directory location on host server manually -> rm -rf $UPLOADS_PV_PATH_ON_HOST"
list_resources
}
function print_logs() {
if [ -z "$2" ]; then
echo "β Please provide a pod name or part of it. Example: ./runner_k8s.sh logs order"
return
fi
POD_NAME=$(kubectl get pods --no-headers -o custom-columns=":metadata.name" | grep "$2" | head -n 1)
if [ -z "$POD_NAME" ]; then
echo "β No pod found matching '$2'"
else
echo "π Showing logs for pod: $POD_NAME"
kubectl logs "$POD_NAME"
fi
}
function exec_into_pod() {
if [ -z "$2" ]; then
echo "β Please provide a pod name or part of it. Example: ./runner_k8s.sh exec order"
return
fi
POD_NAME=$(kubectl get pods --no-headers -o custom-columns=":metadata.name" | grep "$2" | head -n 1)
if [ -z "$POD_NAME" ]; then
echo "β No pod found matching '$2'"
else
echo "πͺ Executing shell into pod: $POD_NAME"
# Just always exec with /bin/sh (Mongo pod also needs this)
kubectl exec -it "$POD_NAME" -- sh
fi
}
function list_resources() {
echo "π Current Kubernetes Resources:"
kubectl get all,cm,pv,pvc -o wide
}
function help_menu() {
echo "Usage: ./runner_k8s.sh [command] [command] ..."
echo
echo "Commands:"
echo " rebuild Rebuild all docker images"
echo " start Create k8s resources"
echo " stop Delete k8s resources"
echo " up Rebuild docker + create k8s"
echo " logs [pod] Print logs from a selected pod"
echo " exec [pod] Execute shell into a selected pod"
echo " help Show this help menu"
}
# Run all commands passed as arguments
while [[ $# -gt 0 ]]; do
cmd="$1"
shift
case "$cmd" in
rebuild)
rebuild_images
;;
start)
start_cluster
;;
up)
rebuild_images
start_cluster
;;
stop)
stop_cluster
;;
exec)
exec_into_pod "$cmd" "$1"
shift
;;
logs)
print_logs "$cmd" "$1"
shift
;;
help)
help_menu
;;
*)
echo "β Unknown command: $cmd"
help_menu
exit 1
;;
esac
done