Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 307 additions & 0 deletions Google Cloud project
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.