-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclusterManager_setup-local.sh
More file actions
1437 lines (1279 loc) · 43.3 KB
/
clusterManager_setup-local.sh
File metadata and controls
1437 lines (1279 loc) · 43.3 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Exit on error
set -e
# Define cluster names
SERVICE_CLUSTER="api-cluster"
HOST_CLUSTER="host-cluster"
# Define credentials
MONGODB_USERNAME="berrybytes"
MONGODB_PASSWORD="password"
MONGODB_DATABASE="cloud"
RABBITMQ_USERNAME="berrybytes"
RABBITMQ_PASSWORD="password"
# Get the host IP from the internal network
HOST_IP=$(hostname -I | awk '{print $1}')
# Create clusters using Kind
function create_clusters() {
echo "Creating $HOST_CLUSTER and $SERVICE_CLUSTER clusters..."
# Add host entry to /etc/hosts
echo "$HOST_IP hostcluster.example.com" | sudo tee -a /etc/hosts > /dev/null
echo "Added '$HOST_IP hostcluster.example.com' to /etc/hosts"
# Define Kind configuration for host cluster (with certSANs and public endpoint)
cat <<EOF | kind create cluster --name $HOST_CLUSTER --config -
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: host-cluster
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: ClusterConfiguration
apiServer:
certSANs:
- "hostcluster.example.com"
- "127.0.0.1"
- "$HOST_IP"
controlPlaneEndpoint: "hostcluster.example.com:7443"
extraPortMappings:
- containerPort: 6443
hostPort: 7443
protocol: TCP
EOF
# Create service cluster
kind create cluster --name $SERVICE_CLUSTER
echo "$HOST_CLUSTER and $SERVICE_CLUSTER created successfully."
# Generate separate kubeconfigs
kind get kubeconfig --name $HOST_CLUSTER > $HOST_CLUSTER-kubeconfig
kind get kubeconfig --name $SERVICE_CLUSTER > $SERVICE_CLUSTER-kubeconfig
# Update host cluster kubeconfig to include additional SANs
sed -i "s/https:\/\/0.0.0.0:7443/https:\/\/hostcluster.example.com:7443/g" $HOST_CLUSTER-kubeconfig
}
# Set up Dapr in service cluster
function setup_dapr() {
echo "Setting up Dapr in $SERVICE_CLUSTER..."
KUBECONFIG=$SERVICE_CLUSTER-kubeconfig kubectl config use-context kind-$SERVICE_CLUSTER --kubeconfig=$SERVICE_CLUSTER-kubeconfig
#kubectl config use-context kind-$SERVICE_CLUSTER --kubeconfig=$SERVICE_CLUSTER-kubeconfig
helm repo add dapr https://dapr.github.io/helm-charts
helm install dapr dapr/dapr -n dapr-system --create-namespace
}
# Set up Keycloak in service cluster
function setup_keycloak() {
echo "Setting up Dapr in $SERVICE_CLUSTER..."
KUBECONFIG=$SERVICE_CLUSTER-kubeconfig kubectl config use-context kind-$SERVICE_CLUSTER
# Define the Google Drive File ID and destination
GOOGLE_DRIVE_FILE_ID="1McJObDuBtqyHniT4pRZVoqZX-AzIxqx3"
DOWNLOAD_FILE="realm.json"
# Download the file from Google Drive
echo "Downloading realm configuration file from Google Drive..."
# Step 1: Get the confirmation token (if required)
confirm_token=$(curl -s -L "https://drive.google.com/uc?export=download&id=${GOOGLE_DRIVE_FILE_ID}" | grep -o 'confirm=[^&]*' | sed 's/confirm=//')
# Step 2: Download the file with or without confirmation token
if [ -z "$confirm_token" ]; then
wget --no-check-certificate "https://drive.google.com/uc?export=download&id=${GOOGLE_DRIVE_FILE_ID}" -O "${DOWNLOAD_FILE}"
else
wget --no-check-certificate "https://drive.google.com/uc?export=download&confirm=${confirm_token}&id=${GOOGLE_DRIVE_FILE_ID}" -O "${DOWNLOAD_FILE}"
fi
# Create the values.yaml file
cat > keycloak-values.yml << 'EOF'
extraEnvVars:
- name: KEYCLOAK_EXTRA_ARGS
value: --import-realm
extraVolumes:
- name: realm-secret
secret:
secretName: realm-secret
extraVolumeMounts:
- name: realm-secret
mountPath: "/opt/bitnami/keycloak/data/import"
readOnly: true
EOF
# Create the secret directly from the file
echo "Creating Kubernetes Secret..."
kubectl create secret generic realm-secret --from-file=realm.json
# Install Keycloak using the values file
echo "Installing Keycloak with Helm chart..."
helm install keycloak bitnami/keycloak \
--create-namespace \
-f keycloak-values.yml \
--version 15.1.8
echo "Keycloak setup completed successfully."
}
# Set up RabbitMQ in service cluster
function setup_rabbitmq() {
echo "Setting up RabbitMQ in $SERVICE_CLUSTER..."
KUBECONFIG=$SERVICE_CLUSTER-kubeconfig kubectl config use-context kind-$SERVICE_CLUSTER
helm install rabbitmq bitnami/rabbitmq \
--namespace rabbitmq \
--create-namespace \
--set auth.username=$RABBITMQ_USERNAME \
--set auth.password=$RABBITMQ_PASSWORD \
--set auth.vhost=$RABBITMQ_VHOST \
--set resources.requests.cpu=100m \
--set resources.requests.memory=256Mi \
--set resources.limits.cpu=500m \
--set resources.limits.memory=512Mi \
--set service.ports.management=15672
}
# Set up MongoDB in service cluster
function setup_mongodb() {
echo "Setting up MongoDB in $SERVICE_CLUSTER..."
KUBECONFIG=$SERVICE_CLUSTER-kubeconfig kubectl config use-context kind-$SERVICE_CLUSTER
helm install mongodb bitnami/mongodb \
--namespace mongodb \
--create-namespace \
--set auth.rootUser=$MONGODB_USERNAME \
--set auth.rootPassword=$MONGODB_PASSWORD \
--set auth.usernames[0]=$MONGODB_USERNAME \
--set auth.passwords[0]=$MONGODB_PASSWORD \
--set auth.databases[0]=$MONGODB_DATABASE \
--set resources.requests.cpu=100m \
--set resources.requests.memory=256Mi \
--set resources.limits.cpu=500m \
--set resources.limits.memory=512Mi
}
# Set up Vault in service cluster
function setup_vault() {
echo "Setting up Vault in $SERVICE_CLUSTER..."
KUBECONFIG=$SERVICE_CLUSTER-kubeconfig kubectl config use-context kind-$SERVICE_CLUSTER
helm repo add hashicorp https://helm.releases.hashicorp.com
helm install vault hashicorp/vault \
--namespace vault --create-namespace \
--set "server.resources.requests.memory=256Mi" \
--set "server.resources.requests.cpu=100m" \
--set "server.resources.limits.memory=512Mi" \
--set "server.resources.limits.cpu=500m"
echo "Waiting for Vault pod to reach Running state..."
until kubectl get pod -n vault -l "app.kubernetes.io/name=vault" -o jsonpath='{.items[0].status.phase}' | grep -q "Running"; do
echo "Waiting for Vault pod to be in Running state..."
sleep 5
done
echo "Initializing Vault..."
kubectl exec -it vault-0 -n vault -- /bin/sh -c '
vault operator init -key-shares=5 -key-threshold=3 | tee /tmp/vault-init.txt
grep "Initial Root Token" /tmp/vault-init.txt | cut -d":" -f2 | tr -d " " > /tmp/vault-token.txt
for i in $(seq 1 3); do
UNSEAL_KEY=$(grep "Unseal Key $i" /tmp/vault-init.txt | cut -d":" -f2 | tr -d " ")
vault operator unseal $UNSEAL_KEY
done
'
# Get the token and keys
kubectl cp vault-0:/tmp/vault-init.txt vault-keys.txt -n vault
kubectl cp vault-0:/tmp/vault-token.txt vault-token.txt -n vault
VAULT_TOKEN=$(cat vault-token.txt)
echo "Vault setup completed successfully!"
}
# Set up Cert-Manager in a cluster
function setup_cert_manager() {
local cluster=$1
echo "Setting up Cert-Manager in $cluster..."
KUBECONFIG=$cluster-kubeconfig kubectl config use-context kind-$cluster
# Check if cert-manager is already installed
if ! helm list -n cert-manager --kubeconfig=$cluster-kubeconfig | grep -q cert-manager; then
helm repo add jetstack https://charts.jetstack.io
helm install cert-manager jetstack/cert-manager -n cert-manager --create-namespace \
--set crds.enabled=true --kubeconfig=$cluster-kubeconfig
else
echo "Cert-Manager is already installed in $cluster, skipping installation."
fi
}
# Set up Ingress-Nginx in a cluster
function setup_ingress_nginx() {
local cluster=$1
echo "Setting up Ingress-Nginx in $cluster..."
KUBECONFIG=$cluster-kubeconfig kubectl config use-context kind-$cluster
# Check if ingress-nginx is already installed
if ! helm list -n ingress-nginx --kubeconfig=$cluster-kubeconfig | grep -q ingress-nginx; then
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm install ingress-nginx ingress-nginx/ingress-nginx -n ingress-nginx --create-namespace \
--set controller.extraArgs.enable-ssl-passthrough="" \
--set controller.extraArgs."default-ssl-certificate=default/tls-secret" \
--kubeconfig=$cluster-kubeconfig
else
echo "Ingress-Nginx is already installed in $cluster, skipping installation."
fi
}
# Set up MetalLB in a cluster
function setup_metallb_host() {
local max_retries=5
local retry_count=0
local wait_time=60
echo "Setting up MetalLB in $HOST_CLUSTER..."
# Validate KUBECONFIG and context
if [ ! -f "$HOST_CLUSTER-kubeconfig" ]; then
echo "Error: Kubeconfig file $HOST_CLUSTER-kubeconfig not found"
return 1
fi
export KUBECONFIG="$HOST_CLUSTER-kubeconfig"
# Switch context and verify
if ! kubectl config use-context "kind-$HOST_CLUSTER" --kubeconfig="$KUBECONFIG"; then
echo "Error: Failed to switch kubectl context"
return 1
fi
# Check if MetalLB is already installed
if kubectl get namespace metallb-system --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; then
echo "MetalLB namespace already exists. Checking deployment status..."
if kubectl get deployment -n metallb-system controller --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; then
echo "MetalLB controller deployment already exists."
echo "Would you like to (r)einstall MetalLB, (c)ontinue with existing installation, or (a)bort? [r/c/a]: "
read -r choice
case "$choice" in
r|R)
echo "Removing existing MetalLB installation..."
kubectl delete namespace metallb-system --kubeconfig="$KUBECONFIG" --grace-period=0 --force
echo "Waiting for namespace deletion..."
while kubectl get namespace metallb-system --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; do
echo "Waiting for metallb-system namespace deletion..."
sleep 5
done
;;
c|C)
echo "Continuing with existing installation..."
;;
*)
echo "Aborting setup..."
return 1
;;
esac
fi
fi
# Apply MetalLB manifests if not already installed
if ! kubectl get namespace metallb-system --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; then
echo "Installing MetalLB..."
if ! kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.5/config/manifests/metallb-native.yaml --kubeconfig="$KUBECONFIG"; then
echo "Error: Failed to apply MetalLB manifests"
return 1
fi
fi
# Wait for namespace with timeout
echo "Waiting for MetalLB namespace to be ready..."
for i in {1..12}; do # 2 minute timeout (10s * 12)
if kubectl get namespace metallb-system --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; then
break
fi
if [ $i -eq 12 ]; then
echo "Error: Timeout waiting for MetalLB namespace"
return 1
fi
echo "Waiting for namespace... ($(( 12 - i )) attempts remaining)"
sleep 10
done
# Wait for controller deployment
echo "Waiting for MetalLB controller deployment to be ready..."
while [ $retry_count -lt $max_retries ]; do
if kubectl wait --for=condition=available --timeout="${wait_time}s" deployment/controller -n metallb-system --kubeconfig="$KUBECONFIG"; then
break
fi
retry_count=$((retry_count + 1))
if [ $retry_count -lt $max_retries ]; then
echo "Attempt $retry_count of $max_retries failed. Collecting debug information..."
echo "--- MetalLB Pod Status ---"
kubectl get pods -n metallb-system --kubeconfig="$KUBECONFIG"
echo "--- MetalLB Pod Logs ---"
kubectl logs -n metallb-system -l component=controller --tail=50 --kubeconfig="$KUBECONFIG" || true
echo "--- MetalLB Pod Events ---"
kubectl get events -n metallb-system --sort-by=.metadata.creationTimestamp --kubeconfig="$KUBECONFIG"
echo "Retrying in 30 seconds..."
sleep 30
else
echo "Error: Maximum retry attempts reached. MetalLB controller failed to become ready."
return 1
fi
done
# Detect network configuration
echo "Detecting network configuration..."
local network=""
local control_plane_id
# First try: Get network from control plane container
control_plane_id=$(docker ps -a | grep "kind-control-plane" | grep "$HOST_CLUSTER" | awk '{print $1}')
if [ -n "$control_plane_id" ]; then
network=$(docker inspect "$control_plane_id" | grep -oP '"Gateway": "\K[0-9]+\.[0-9]+' | head -1)
fi
# Second try: Get network from kind network directly
if [ -z "$network" ]; then
network=$(docker network inspect kind | grep -oP '"Gateway": "\K[0-9]+\.[0-9]+' | head -1)
fi
# Fallback to default if both methods fail
if [ -z "$network" ]; then
network="172.18"
echo "Warning: Could not detect network. Using default: $network"
fi
echo "Using network: $network"
local IP_START="$network.254.202"
local IP_END="$network.254.250"
# Configure IPAddressPool if not already configured
if ! kubectl get ipaddresspool -n metallb-system example --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; then
echo "Creating IPAddressPool with range $IP_START-$IP_END..."
# Apply IPAddressPool
cat <<EOF | kubectl apply -f - --kubeconfig="$KUBECONFIG"
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: example
namespace: metallb-system
spec:
addresses:
- $IP_START-$IP_END
EOF
# Apply L2Advertisement separately
cat <<EOF | kubectl apply -f - --kubeconfig="$KUBECONFIG"
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: default
namespace: metallb-system
spec:
ipAddressPools:
- example
EOF
else
echo "IPAddressPool 'example' already exists"
fi
# Verify configuration with timeout
echo "Verifying MetalLB configuration..."
local verify_timeout=30
local verify_start=$SECONDS
while true; do
if kubectl get ipaddresspool -n metallb-system example --kubeconfig="$KUBECONFIG" >/dev/null 2>&1 && \
kubectl get l2advertisement -n metallb-system default --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; then
break
fi
if (( SECONDS - verify_start >= verify_timeout )); then
echo "Error: Timeout waiting for MetalLB configuration verification"
return 1
fi
echo "Waiting for MetalLB configuration to be ready..."
sleep 5
done
echo "MetalLB setup completed successfully."
return 0
}
function setup_metallb_service() {
local max_retries=5
local retry_count=0
local wait_time=60
echo "Setting up MetalLB in $SERVICE_CLUSTER..."
# Validate KUBECONFIG and context
if [ ! -f "$SERVICE_CLUSTER-kubeconfig" ]; then
echo "Error: Kubeconfig file $SERVICE_CLUSTER-kubeconfig not found."
return 1
fi
export KUBECONFIG="$SERVICE_CLUSTER-kubeconfig"
# Switch context and verify
if ! kubectl config use-context "kind-$SERVICE_CLUSTER" --kubeconfig="$KUBECONFIG"; then
echo "Error: Failed to switch kubectl context."
return 1
fi
# Check if MetalLB namespace already exists
if kubectl get namespace metallb-system --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; then
echo "MetalLB namespace already exists. Checking deployment status..."
if kubectl get deployment -n metallb-system controller --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; then
echo "MetalLB controller deployment already exists."
echo "Would you like to (r)einstall MetalLB, (c)ontinue with existing installation, or (a)bort? [r/c/a]: "
read -r choice
case "$choice" in
r|R)
echo "Removing existing MetalLB installation..."
kubectl delete namespace metallb-system --kubeconfig="$KUBECONFIG" --grace-period=0 --force
echo "Waiting for namespace deletion..."
while kubectl get namespace metallb-system --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; do
sleep 5
done
;;
c|C)
echo "Continuing with existing installation..."
;;
*)
echo "Aborting setup..."
return 1
;;
esac
fi
fi
# Apply MetalLB manifests if not already installed
if ! kubectl get namespace metallb-system --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; then
echo "Installing MetalLB..."
if ! kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.5/config/manifests/metallb-native.yaml --kubeconfig="$KUBECONFIG"; then
echo "Error: Failed to apply MetalLB manifests."
return 1
fi
fi
echo "Waiting for MetalLB namespace and controller to be ready..."
kubectl wait --for=condition=available --timeout=180s deployment/controller -n metallb-system --kubeconfig="$KUBECONFIG" || {
echo "Error: MetalLB controller failed to become ready."
return 1
}
# Detect network configuration
echo "Detecting network configuration..."
local network=""
local control_plane_id
# First try: Get network from control plane container
control_plane_id=$(docker ps -a | grep "kind-control-plane" | grep "$SERVICE_CLUSTER" | awk '{print $1}')
if [ -n "$control_plane_id" ]; then
network=$(docker inspect "$control_plane_id" | grep -oP '"Gateway": "\K[0-9]+\.[0-9]+' | head -1)
fi
# Second try: Get network from kind network directly
if [ -z "$network" ]; then
network=$(docker network inspect kind | grep -oP '"Gateway": "\K[0-9]+\.[0-9]+' | head -1)
fi
# Fallback to default if both methods fail
if [ -z "$network" ]; then
network="172.18"
echo "Warning: Could not detect network. Using default: $network"
fi
echo "Using network: $network"
local IP_START="$network.254.200"
local IP_END="$network.254.250"
# Configure IPAddressPool if not already configured
if ! kubectl get ipaddresspool -n metallb-system example --kubeconfig="$KUBECONFIG" >/dev/null 2>&1; then
echo "Creating IPAddressPool with range $IP_START-$IP_END..."
cat <<EOF | kubectl apply -f - --kubeconfig="$KUBECONFIG"
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: example
namespace: metallb-system
spec:
addresses:
- $IP_START-$IP_END
EOF
# Create L2Advertisement in a separate apply
cat <<EOF | kubectl apply -f - --kubeconfig="$KUBECONFIG"
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: default
namespace: metallb-system
spec:
ipAddressPools:
- example
EOF
else
echo "IPAddressPool 'example' already exists."
fi
# Verify MetalLB configuration
echo "Verifying MetalLB configuration..."
kubectl get ipaddresspool -n metallb-system example --kubeconfig="$KUBECONFIG" || {
echo "Error: Failed to configure IPAddressPool."
return 1
}
kubectl get l2advertisement -n metallb-system default --kubeconfig="$KUBECONFIG" || {
echo "Error: Failed to configure L2Advertisement."
return 1
}
echo "MetalLB setup completed successfully."
return 0
}
# Set up clusters
function setup_clusters() {
echo "Setting up namespaces for $SERVICE_CLUSTER and $HOST_CLUSTER..."
KUBECONFIG=$SERVICE_CLUSTER-kubeconfig kubectl config use-context kind-$SERVICE_CLUSTER
kubectl create namespace $SERVICE_CLUSTER || echo "Namespace $SERVICE_CLUSTER already exists"
KUBECONFIG=$HOST_CLUSTER-kubeconfig kubectl config use-context kind-$HOST_CLUSTER
kubectl create namespace $HOST_CLUSTER || echo "Namespace $HOST_CLUSTER already exists"
echo "Linking Service Cluster components to Host Cluster..."
KUBECONFIG=$SERVICE_CLUSTER-kubeconfig kubectl config use-context kind-$SERVICE_CLUSTER
kubectl label namespace $SERVICE_CLUSTER cluster-role=service
KUBECONFIG=$HOST_CLUSTER-kubeconfig kubectl config use-context kind-$HOST_CLUSTER
kubectl label namespace $HOST_CLUSTER cluster-role=host
}
# Deploy the service manifest in the service cluster
function deploy_service_manifest() {
echo "Deploying service manifest to $SERVICE_CLUSTER..."
KUBECONFIG=$SERVICE_CLUSTER-kubeconfig kubectl config use-context kind-$SERVICE_CLUSTER
cat <<EOF | kubectl apply -f - --kubeconfig=$SERVICE_CLUSTER-kubeconfig
apiVersion: apps/v1
kind: Deployment
metadata:
name: cluster-service-python
labels:
app.kubernetes.io/instance: cluster-service
app.kubernetes.io/version: 1.16.0
helm.sh/chart: python-0.0.1
dapr.io/app-id: subscriber-dapr
dapr.io/metrics-enabled: "true"
dapr.io/sidecar-injected: "true"
annotations:
dapr.io/app-id: subscriber-dapr
dapr.io/app-port: "8082"
dapr.io/disable-builtin-k8s-secret-store: "true"
dapr.io/enabled: "true"
dapr.io/log-level: debug
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: python
template:
metadata:
labels:
app: subscriber-dapr
app.kubernetes.io/name: python
app.kubernetes.io/instance: cluster-service
annotations:
dapr.io/app-id: subscriber-dapr
dapr.io/app-port: "8082"
dapr.io/disable-builtin-k8s-secret-store: "true"
dapr.io/enabled: "true"
dapr.io/log-level: debug
spec:
containers:
- name: python
image: rajivgs/cm-service:v18
imagePullPolicy: Always
env:
- name: HOST_NAME
value: clustermanager.local
- name: DAPR_HTTP_PORT
value: "3500"
- name: DAPR_GRPC_PORT
value: "50001"
ports:
- containerPort: 8082
name: http
protocol: TCP
livenessProbe:
initialDelaySeconds: 30
periodSeconds: 30
tcpSocket:
port: http
readinessProbe:
initialDelaySeconds: 30
periodSeconds: 30
tcpSocket:
port: http
resources:
limits:
cpu: "512m"
memory: 512Mi
requests:
cpu: 20m
memory: 20Mi
dnsPolicy: ClusterFirst
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: cluster-service-python
namespace: default
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8082
selector:
app.kubernetes.io/instance: cluster-service
app.kubernetes.io/name: python
type: ClusterIP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: cluster-service-python
namespace: default
spec:
ingressClassName: nginx
rules:
- host: cluster-service-python.clustermanager.local
http:
paths:
- backend:
service:
name: cluster-service-python
port:
number: 80
path: /
pathType: ImplementationSpecific
tls:
- hosts:
- cluster-service-python.clustermanager.local
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app.kubernetes.io/instance: cluster-api
app.kubernetes.io/name: python
name: cluster-api-python
namespace: default
spec:
selector:
matchLabels:
app.kubernetes.io/instance: cluster-api
app.kubernetes.io/name: python
template:
metadata:
annotations:
dapr.io/app-id: publisher-dapr
dapr.io/app-port: "8081"
dapr.io/disable-builtin-k8s-secret-store: "true"
dapr.io/enabled: "true"
creationTimestamp: null
labels:
app: publisher-dapr
app.kubernetes.io/instance: cluster-api
app.kubernetes.io/name: python
dapr.io/app-id: publisher-dapr
dapr.io/disable-builtin-k8s-secret-store: "true"
dapr.io/metrics-enabled: "true"
dapr.io/sidecar-injected: "true"
spec:
containers:
- env:
- name: ENV
value: local
- name: ADMIN_CLIENT_ID
value: admin-cli
- name: ADMIN_CLIENT_SECRET
value: nZAEUFy0he6sZPI58KqG9JvK5qOtHJtY
- name: ATLAS_URI
value: mongodb://berrybytes:password@mongodb.mongodb.svc.cluster.local:27017
- name: DB_NAME
value: cloud
- name: PYTHONDEVMODE
value: "True"
- name: DEBUG
value: "True"
- name: KEYCLOAK_URL
value: https://keycloak.clustermanager.local
- name: REALM_NAME
value: clusterManager
- name: REQUEST_GROUP_NAME
value: request-user
- name: REQUESTS_CA_BUNDLE
value: /etc/tls/ca-certificates/ca.crt
- name: SERVICE_URL
value: https://cluster-service-python.clustermanager.local
- name: CLIENT_ID
value: clustermanagerclient
image: umesh1212/cluster-api:tag16
imagePullPolicy: Always
livenessProbe:
failureThreshold: 3
initialDelaySeconds: 30
periodSeconds: 30
successThreshold: 1
tcpSocket:
port: http
timeoutSeconds: 1
name: python
ports:
- containerPort: 8081
name: http
protocol: TCP
readinessProbe:
failureThreshold: 3
initialDelaySeconds: 30
periodSeconds: 30
successThreshold: 1
tcpSocket:
port: http
timeoutSeconds: 1
resources:
limits:
cpu: "1"
memory: 1Gi
requests:
cpu: 20m
memory: 20Mi
volumeMounts:
- mountPath: /etc/tls/ca-certificates/
name: mkcert-ca
volumes:
- configMap:
defaultMode: 420
name: mkcert-ca
name: mkcert-ca
---
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/instance: cluster-api
app.kubernetes.io/name: python
name: cluster-api-python
namespace: default
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8081
selector:
app.kubernetes.io/name: python
sessionAffinity: None
type: ClusterIP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: cluster-api-python
namespace: default
spec:
ingressClassName: nginx
rules:
- host: cluster-api.clustermanager.local
http:
paths:
- backend:
service:
name: cluster-api-python
port:
number: 80
path: /
pathType: ImplementationSpecific
tls:
- hosts:
- cluster-api.clustermanager.local
---
apiVersion: v1
data:
config.js: |
window.config = {
VITE_APP_RESTAPI_ENDPOINT: "https://cluster-api.clustermanager.local/v1",
VITE_APP_KEYCLOAK_URL: "https://keycloak.clustermanager.local",
VITE_APP_REALM: "clusterManager",
VITE_APP_CLIENT_ID: "clustermanagerclient",
}
kind: ConfigMap
metadata:
name: config
namespace: default
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: cluster-ui-node
labels:
app: node
spec:
replicas: 1
selector:
matchLabels:
app: node
template:
metadata:
labels:
app: node
app.kubernetes.io/provider: zerone
spec:
containers:
- image: rajivgs/cluster-ui:v20
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 60
initialDelaySeconds: 30
periodSeconds: 30
successThreshold: 1
tcpSocket:
port: http
timeoutSeconds: 5
name: node
ports:
- containerPort: 4173
name: http
protocol: TCP
readinessProbe:
failureThreshold: 60
initialDelaySeconds: 10
periodSeconds: 30
successThreshold: 1
tcpSocket:
port: http
timeoutSeconds: 5
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 20m
memory: 20Mi
volumeMounts:
- mountPath: /app/dist/config.js
name: config
subPath: config.js
volumes:
- configMap:
defaultMode: 420
name: config
name: config
---
apiVersion: v1
kind: Service
metadata:
name: cluster-ui-node-service
namespace: default
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 4173
selector:
app: node
app.kubernetes.io/provider: zerone
type: ClusterIP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ui-service
namespace: default
spec:
ingressClassName: nginx
rules:
- host: clusterui.clustermanager.local
http:
paths:
- backend:
service:
name: cluster-ui-node-service
port:
number: 80
path: /
pathType: ImplementationSpecific
tls:
- hosts:
- clusterui.clustermanager.local
---
EOF
echo "Service manifest deployed successfully to $SERVICE_CLUSTER"
}
function setup_dapr_components() {
echo "Deploying dapr components manifest to $SERVICE_CLUSTER..."
KUBECONFIG=$SERVICE_CLUSTER-kubeconfig kubectl config use-context kind-$SERVICE_CLUSTER
echo "Setting up Dapr components..."
VAULT_TOKEN=$(cat vault-token.txt)
cat <<EOF | kubectl apply -f -
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: messagebus
namespace: default
spec:
type: pubsub.rabbitmq
version: v1
metadata:
- name: host
value: amqp://$RABBITMQ_USERNAME:$RABBITMQ_PASSWORD@rabbitmq.rabbitmq.svc.cluster.local/
- name: port
value: 5672
- name: username
value: $RABBITMQ_USERNAME
- name: password
value: $RABBITMQ_PASSWORD
- name: durable
value: "true"
- name: autoDelete
value: "true"
---
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: vault
namespace: default
spec:
type: secretstores.hashicorp.vault
version: v1
metadata:
- name: vaultAddr
value: http://vault.vault.svc.cluster.local:8200
- name: skipVerify
value: true
- name: vaultToken
value: $VAULT_TOKEN
- name: vaultKVUsePrefix
value: "false"
---
EOF
}
function setup_ingress() {
echo "Setting up Ingress "
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/proxy-buffer-size: 128k
nginx.ingress.kubernetes.io/proxy-buffers: 4 256k