Introduction
Cloud Run is a fully managed compute platform that automatically scales your stateless containers. It abstracts away all infrastructure management, allowing you to focus on writing code. This module will cover the basics of Cloud Run, how to deploy applications, and best practices for using this service.
Key Concepts
- Serverless Computing: No need to manage servers; you only pay for the resources you use.
- Containers: Cloud Run runs your application in a container, which can be any language or library.
- Automatic Scaling: Cloud Run scales your application up or down based on traffic.
- Stateless Applications: Cloud Run is designed for stateless applications, meaning each request is independent.
Setting Up Cloud Run
Prerequisites
- A Google Cloud Platform account.
- Basic knowledge of Docker and containerization.
Steps to Set Up Cloud Run
-
Enable Cloud Run API:
gcloud services enable run.googleapis.com
-
Install Cloud SDK: Follow the instructions here to install the Google Cloud SDK.
-
Authenticate with GCP:
gcloud auth login
-
Set Your Project:
gcloud config set project [PROJECT_ID]
Deploying a Container to Cloud Run
Step-by-Step Guide
-
Create a Simple Application: Create a simple Python application and save it as
app.py
:from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Hello, World!" if __name__ == '__main__': app.run(host='0.0.0.0', port=8080)
-
Create a Dockerfile: Create a
Dockerfile
to containerize the application:# Use the official Python image from the Docker Hub FROM python:3.8-slim # Set the working directory WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install flask # Make port 8080 available to the world outside this container EXPOSE 8080 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"]
-
Build the Docker Image:
docker build -t gcr.io/[PROJECT_ID]/hello-world .
-
Push the Docker Image to Google Container Registry:
docker push gcr.io/[PROJECT_ID]/hello-world
-
Deploy to Cloud Run:
gcloud run deploy hello-world --image gcr.io/[PROJECT_ID]/hello-world --platform managed --region us-central1 --allow-unauthenticated
Best Practices
- Use Environment Variables: Store configuration settings in environment variables.
- Optimize Docker Images: Use slim or alpine versions of base images to reduce the size.
- Monitor and Log: Use Stackdriver for monitoring and logging.
- Security: Ensure your container images are secure and free from vulnerabilities.
Practical Exercise
Exercise: Deploy a Node.js Application to Cloud Run
-
Create a Node.js Application:
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello, Node.js!'); }); const PORT = process.env.PORT || 8080; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });
-
Create a Dockerfile:
FROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 8080 CMD ["node", "index.js"]
-
Build and Push the Docker Image:
docker build -t gcr.io/[PROJECT_ID]/node-app . docker push gcr.io/[PROJECT_ID]/node-app
-
Deploy to Cloud Run:
gcloud run deploy node-app --image gcr.io/[PROJECT_ID]/node-app --platform managed --region us-central1 --allow-unauthenticated
Solution
Follow the steps provided in the exercise to deploy the Node.js application. Ensure you replace [PROJECT_ID]
with your actual GCP project ID.
Common Mistakes and Tips
- Incorrect Dockerfile: Ensure your Dockerfile is correctly set up to expose the right port and run the correct command.
- Authentication Issues: Make sure you are authenticated with GCP and have the necessary permissions.
- Region Selection: Choose the appropriate region for your deployment to minimize latency.
Conclusion
In this module, you learned about Cloud Run, a serverless compute platform on GCP. You now know how to set up Cloud Run, deploy a containerized application, and follow best practices. This knowledge will help you build scalable and efficient applications without worrying about infrastructure management.
Google Cloud Platform (GCP) Course
Module 1: Introduction to Google Cloud Platform
- What is Google Cloud Platform?
- Setting Up Your GCP Account
- GCP Console Overview
- Understanding Projects and Billing
Module 2: Core GCP Services
Module 3: Networking and Security
Module 4: Data and Analytics
Module 5: Machine Learning and AI
Module 6: DevOps and Monitoring
- Cloud Build
- Cloud Source Repositories
- Cloud Functions
- Stackdriver Monitoring
- Cloud Deployment Manager
Module 7: Advanced GCP Topics
- Hybrid and Multi-Cloud with Anthos
- Serverless Computing with Cloud Run
- Advanced Networking
- Security Best Practices
- Cost Management and Optimization