-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathGoogle Cloud project
More file actions
307 lines (244 loc) · 6.06 KB
/
Google Cloud project
File metadata and controls
307 lines (244 loc) · 6.06 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
I'll help you build and deploy an application to Google Kubernetes Engine (GKE). Here's a complete step-by-step guide:
## Prerequisites
1. **Google Cloud Account** with billing enabled
2. **Google Cloud SDK** installed
3. **Docker** installed locally
4. **kubectl** installed
## Step 1: Set Up Your Project
```bash
# Set your Google Cloud project
gcloud config set project YOUR_PROJECT_ID
# Enable required APIs
gcloud services enable container.googleapis.com
gcloud services enable containerregistry.googleapis.com
```
## Step 2: Create a Simple Application
Create a basic Node.js application:
**package.json:**
```json
{
"name": "gke-app",
"version": "1.0.0",
"description": "Sample app for GKE deployment",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.18.2"
}
}
```
**server.js:**
```javascript
const express = require('express');
const app = express();
const PORT = process.env.PORT || 8080;
app.get('/', (req, res) => {
res.send(`
<html>
<body>
<h1>Hello from GKE!</h1>
<p>Server time: ${new Date().toISOString()}</p>
<p>Host: ${process.env.HOSTNAME || 'unknown'}</p>
</body>
</html>
`);
});
app.get('/health', (req, res) => {
res.json({ status: 'healthy', timestamp: new Date().toISOString() });
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
```
## Step 3: Create Dockerfile
**Dockerfile:**
```dockerfile
# Use the official Node.js runtime as base image
FROM node:18-alpine
# Set working directory in container
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm install --production
# Copy application code
COPY . .
# Expose port
EXPOSE 8080
# Start the application
CMD ["npm", "start"]
```
**.dockerignore:**
```
node_modules
npm-debug.log
.git
.gitignore
README.md
Dockerfile
.dockerignore
```
## Step 4: Build and Test Docker Image Locally
```bash
# Build the Docker image
docker build -t my-node-app .
# Test locally
docker run -p 8080:8080 my-node-app
# Visit http://localhost:8080 to verify it works
```
## Step 5: Push to Google Container Registry (GCR)
```bash
# Configure Docker to use gcloud as credential helper
gcloud auth configure-docker
# Tag the image for GCR
docker tag my-node-app gcr.io/YOUR_PROJECT_ID/my-node-app:v1.0
# Push to GCR
docker push gcr.io/YOUR_PROJECT_ID/my-node-app:v1.0
```
## Step 6: Create a GKE Cluster
```bash
# Create a cluster
gcloud container clusters create my-gke-cluster \
--num-nodes=2 \
--zone=us-central1-a \
--machine-type=e2-small
# Get cluster credentials
gcloud container clusters get-credentials my-gke-cluster \
--zone=us-central1-a
```
## Step 7: Create Kubernetes Deployment Files
**deployment.yaml:**
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-node-app
labels:
app: my-node-app
spec:
replicas: 3
selector:
matchLabels:
app: my-node-app
template:
metadata:
labels:
app: my-node-app
spec:
containers:
- name: my-node-app
image: gcr.io/YOUR_PROJECT_ID/my-node-app:v1.0
ports:
- containerPort: 8080
env:
- name: NODE_ENV
value: "production"
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
```
**service.yaml:**
```yaml
apiVersion: v1
kind: Service
metadata:
name: my-node-app-service
labels:
app: my-node-app
spec:
type: LoadBalancer
selector:
app: my-node-app
ports:
- port: 80
targetPort: 8080
protocol: TCP
```
## Step 8: Deploy to GKE
```bash
# Apply the deployment
kubectl apply -f deployment.yaml
# Apply the service
kubectl apply -f service.yaml
# Check deployment status
kubectl get deployments
# Check pods
kubectl get pods
# Check service (get external IP)
kubectl get services
```
## Step 9: Verify Deployment
```bash
# Get the external IP
kubectl get service my-node-app-service
# Test the application (replace EXTERNAL_IP with actual IP)
curl http://EXTERNAL_IP
# View pod logs
kubectl logs -l app=my-node-app --tail=50
```
## Step 10: Scaling and Updates
```bash
# Scale the deployment
kubectl scale deployment my-node-app --replicas=5
# Update the application (after building new image)
docker build -t gcr.io/YOUR_PROJECT_ID/my-node-app:v2.0 .
docker push gcr.io/YOUR_PROJECT_ID/my-node-app:v2.0
kubectl set image deployment/my-node-app my-node-app=gcr.io/YOUR_PROJECT_ID/my-node-app:v2.0
# Rollback if needed
kubectl rollout undo deployment/my-node-app
```
## Step 11: Clean Up
```bash
# Delete the deployment and service
kubectl delete -f deployment.yaml
kubectl delete -f service.yaml
# Delete the cluster
gcloud container clusters delete my-gke-cluster --zone=us-central1-a
# Delete the container image
gcloud container images delete gcr.io/YOUR_PROJECT_ID/my-node-app:v1.0 --force-delete-tags
```
## Automation Script
Create a **deploy.sh** script for automation:
```bash
#!/bin/bash
PROJECT_ID="YOUR_PROJECT_ID"
IMAGE_NAME="my-node-app"
VERSION="v1.0"
CLUSTER_NAME="my-gke-cluster"
ZONE="us-central1-a"
echo "Building Docker image..."
docker build -t $IMAGE_NAME .
echo "Tagging image for GCR..."
docker tag $IMAGE_NAME gcr.io/$PROJECT_ID/$IMAGE_NAME:$VERSION
echo "Pushing to GCR..."
docker push gcr.io/$PROJECT_ID/$IMAGE_NAME:$VERSION
echo "Deploying to GKE..."
gcloud container clusters get-credentials $CLUSTER_NAME --zone $ZONE
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
echo "Deployment complete!"
kubectl get services
```
Make it executable:
```bash
chmod +x deploy.sh
```
This complete workflow shows you how to containerize an application, push it to Google Container Registry, and deploy it to a GKE cluster with proper configuration for production use.