-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-k8s.sh
More file actions
212 lines (173 loc) · 5.49 KB
/
Copy pathdeploy-k8s.sh
File metadata and controls
212 lines (173 loc) · 5.49 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
#!/bin/bash
# Deployment script for manual deployment or debugging
# Usage: ./deploy.sh [environment]
set -e
ENVIRONMENT=${1:-production}
IMAGE_TAG=$(git rev-parse --short HEAD)
REGISTRY="ghcr.io"
IMAGE_PREFIX="yuvraj-rathod-1202/gms"
echo "=========================================="
echo "Deploying GMS to Kubernetes"
echo "Environment: $ENVIRONMENT"
echo "Image Tag: $IMAGE_TAG"
echo "=========================================="
# Function to check if kubectl is available
check_kubectl() {
if ! command -v kubectl &> /dev/null; then
echo "Error: kubectl not found. Please install kubectl."
exit 1
fi
}
# Function to check cluster connectivity
check_cluster() {
echo "Checking cluster connectivity..."
if ! kubectl cluster-info &> /dev/null; then
echo "Error: Cannot connect to Kubernetes cluster."
exit 1
fi
echo "✓ Connected to cluster"
}
# Function to update image tags in manifests
update_manifests() {
echo "Updating image tags in manifests..."
for service in analytics auth_user courses frontend gateway marks policy; do
manifest_dir="${service}/manifests"
if [ -d "$manifest_dir" ]; then
find "$manifest_dir" -name "*-deploy.yaml" -o -name "*deploy.yaml" | while read file; do
sed -i "s|image: .*/${service}:.*|image: ${REGISTRY}/${IMAGE_PREFIX}-${service}:${IMAGE_TAG}|g" "$file"
done
fi
done
echo "✓ Manifests updated"
}
# Function to build backup container image
build_backup_image() {
echo ""
echo "Building backup container image..."
if [ -f "mysql/backup.Dockerfile" ]; then
docker build -t ${REGISTRY}/${IMAGE_PREFIX}-mysql-backup:${IMAGE_TAG} \
-f mysql/backup.Dockerfile \
mysql/ 2>&1 | tail -20
if [ $? -eq 0 ]; then
echo "- Tagging as latest..."
docker tag ${REGISTRY}/${IMAGE_PREFIX}-mysql-backup:${IMAGE_TAG} \
${REGISTRY}/${IMAGE_PREFIX}-mysql-backup:latest
echo "- Pushing to registry..."
docker push ${REGISTRY}/${IMAGE_PREFIX}-mysql-backup:${IMAGE_TAG}
docker push ${REGISTRY}/${IMAGE_PREFIX}-mysql-backup:latest
echo "✓ Backup image built and pushed"
else
echo "Warning: Failed to build backup image"
fi
fi
}
# Function to deploy infrastructure
deploy_infrastructure() {
echo ""
echo "Deploying infrastructure..."
# Deploy MySQL (with backup PVC)
if [ -d "mysql/manifests" ]; then
echo "- Deploying MySQL and backup storage..."
kubectl apply -f mysql/manifests/backup-pvc.yaml
sleep 2
kubectl apply -f mysql/manifests/
sleep 5
fi
# Deploy RabbitMQ
if [ -d "rabbitmq/manifests" ]; then
echo "- Deploying RabbitMQ..."
kubectl apply -f rabbitmq/manifests/
sleep 5
fi
echo "✓ Infrastructure deployed"
}
# Function to deploy backup automation
deploy_backup_automation() {
echo ""
echo "Deploying backup automation..."
if [ -d "mysql/manifests" ]; then
echo "- Deploying backup RBAC..."
kubectl apply -f mysql/manifests/backup-rbac.yaml
sleep 2
echo "- Deploying backup CronJob..."
kubectl apply -f mysql/manifests/backup-cronjob.yaml
sleep 2
echo "✓ Backup automation deployed"
fi
}
# Function to deploy backend services
deploy_backend() {
echo ""
echo "Deploying backend services..."
for service in auth_user courses gateway marks analytics policy; do
if [ -d "${service}/manifests" ]; then
echo "- Deploying ${service}..."
kubectl apply -f ${service}/manifests/
fi
done
sleep 10
echo "✓ Backend services deployed"
}
# Function to deploy frontend
deploy_frontend() {
echo ""
echo "Deploying frontend..."
if [ -d "frontend/manifests" ]; then
kubectl apply -f frontend/manifests/
fi
echo "✓ Frontend deployed"
}
# Function to wait for deployments
wait_for_deployments() {
echo ""
echo "Waiting for deployments to be ready..."
deployments=(
"auth-user"
"courses"
"gateway"
"marks"
"analytics"
"policy"
"frontend"
)
for deployment in "${deployments[@]}"; do
echo "- Waiting for ${deployment}..."
kubectl rollout status deployment/${deployment} -n default --timeout=300s || echo "Warning: ${deployment} deployment timeout"
done
echo "✓ All deployments processed"
}
# Function to show deployment status
show_status() {
echo ""
echo "=========================================="
echo "Deployment Status"
echo "=========================================="
echo ""
echo "Pods:"
kubectl get pods -n default
echo ""
echo "Services:"
kubectl get services -n default
echo ""
echo "Ingress:"
kubectl get ingress -n default 2>/dev/null || echo "No ingress resources found"
echo ""
echo "=========================================="
echo "Deployment Complete!"
echo "=========================================="
}
# Main execution
main() {
check_kubectl
check_cluster
update_manifests
build_backup_image
deploy_infrastructure
deploy_backup_automation
deploy_backend
deploy_frontend
wait_for_deployments
show_status
}
# Run main function
main