VOOZH about

URL: https://dzone.com/articles/a-step-by-step-guide-to-deploying-containers-in-the-cloud

⇱ A Step-by-Step Guide to Deploying Containers in the Cloud


Related

  1. DZone
  2. Software Design and Architecture
  3. Containers
  4. Mastering Cloud Containerization: A Step-by-Step Guide to Deploying Containers in the Cloud

Mastering Cloud Containerization: A Step-by-Step Guide to Deploying Containers in the Cloud

Learn to containerize a Node.js app, deploy it on Google Kubernetes Engine, and manage scaling for efficient cloud deployment.

Likes
Comment
Save
10.2K Views

Join the DZone community and get the full member experience.

Join For Free

Containers have transformed how we deploy, scale, and manage applications by packaging code and dependencies in a standardized unit that can run consistently across any environment. When used in cloud environments, containers offer:

  • Portability across development, testing, and production.
  • Scalability to quickly adapt to traffic and demand.
  • Efficiency with reduced overhead compared to traditional virtual machines.

In this tutorial, we’ll walk through a full setup of a cloud-hosted containerized application, covering:

  1. Basics of containers and why they’re ideal for the cloud.
  2. Setting up a Dockerized application.
  3. Deploying the container to a cloud provider (using Google Cloud Platform as an example).
  4. Scaling and managing your container in the cloud.

Container Basics: How Containers Fit Into Cloud Workflows

Containers encapsulate all the libraries and dependencies needed to run an application. Unlike traditional virtual machines, in which each has an OS, containers share the host OS, making them lightweight and efficient.

Why Containers for Cloud?

  • Fast startup times mean quicker scaling to handle variable traffic.
  • Consistency across environments ensures that code behaves the same from developer laptops to production.
  • Resource efficiency enables high-density deployment on the same infrastructure.

Core Components of Cloud Containerization

  • Container Engine: Manages and runs containers (e.g., Docker, containerd).
  • Orchestration: Ensures app reliability, scaling, and load balancing (e.g., Kubernetes, ECS).
  • Registry: Stores container images for access across environments (e.g., Docker Hub, GCR).

Setting Up a Dockerized Application

We’ll start by containerizing a simple Node.js application.

Step 1: Create the Application

1. In a project folder, initialize a Node.js project:

Shell
mkdir cloud-container-app && cd cloud-container-app

npm init -y


2. Create a basic server file, app.js:

JavaScript
const express = require('express');

const app = express();

app.get('/', (req, res) => {

   res.send('Hello, Cloud Container!');

});

  app.listen(3000, () => {

      console.log('App running on port 3000');

    });

3. Add Express to the project:

Shell
npm install express


Step 2: Create a Dockerfile

This Dockerfile specifies how to package the app in a Docker container.

Dockerfile
# Use the Node.js image as a base

FROM node:14

# Set working directory

WORKDIR /app

# Copy files and install dependencies

COPY . .

RUN npm install

# Expose the app’s port

EXPOSE 3000

# Start the application

CMD ["node", "app.js"]


Step 3: Build and Test the Docker Image Locally

1. Build the Docker image: 

Shell
docker build -t cloud-container-app .


2. Run the container locally:

Shell
 docker run -p 3000:3000 cloud-container-app

3. Visit `http://localhost:3000` in your browser. You should see "Hello, Cloud Container!" displayed.

Deploying the Container to Google Cloud Platform (GCP)

In this section, we’ll push the container image to Google Container Registry (GCR) and deploy it to Google Kubernetes Engine (GKE).

Step 1: Set Up a GCP Project

1. Create a GCP Project. Go to the [Google Cloud Console] (https://console.cloud.google.com) and create a new project.

2. Enable the Kubernetes Engine and Container Registry APIs for your project.

Step 2: Push the Image to Google Container Registry

1. Tag the Docker image for Google Cloud:

Shell
docker tag cloud-container-app gcr.io/<YOUR_PROJECT_ID>/cloud-container-app

2. Push the image to GCR:

Shell
docker push gcr.io/<YOUR_PROJECT_ID>/cloud-container-app


Step 3: Create a Kubernetes Cluster

1. Initialize a GKE Cluster:

Shell
gcloud container clusters create cloud-container-cluster --num-nodes=2

2. Configure kubectl to connect to your new cluster:

Shell
gcloud container clusters get-credentials cloud-container-cluster


Step 4: Deploy the Containerized App to GKE

1. Create a k8s-deployment.yaml file to define the deployment and service:

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
 name: cloud-container-app
spec:
 replicas: 2
 selector:
 matchLabels:
 app: cloud-container-app
 template:
 metadata:
 labels:
 app: cloud-container-app
 spec:
 containers:
 - name: cloud-container-app
 image: gcr.io/<YOUR_PROJECT_ID>/cloud-container-app
 ports:
 - containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
 name: cloud-container-service
spec:
 type: LoadBalancer
 selector:
 app: cloud-container-app
 ports:
 - protocol: TCP
 port: 80
 targetPort: 3000


2. Deploy the application to GKE:

Shell
kubectl apply -f k8s-deployment.yaml


3. Get the external IP to access the app:

Shell
kubectl get services


Scaling and Managing Containers in GKE

Step 1: Scale the Deployment

To adjust the number of replicas (e.g., to handle higher traffic), use the following command:

Shell
kubectl scale deployment cloud-container-app --replicas=5


GKE will automatically scale the app by adding replicas and distributing the load.

Step 2: Monitor and Manage Logs

1. Use the command below to view logs for a specific pod in Kubernetes:

Shell
kubectl logs <POD_NAME>


2. Enable the GKE dashboard to monitor pod status and resource usage and manage deployments visually.

Conclusion: Leveraging Containers for Scalable Cloud Deployments

This tutorial covered:

  1. Containerizing a Node.js app using Docker.
  2. Deploying to Google Kubernetes Engine (GKE) for scalable, managed hosting.
  3. Managing and scaling containers effectively in the cloud.

With containers in the cloud, your applications gain scalability, portability, and efficiency — ensuring they’re ready to handle demand, adapt quickly, and remain consistent across environments.

Kubernetes Cloud Docker (software) Container

Opinions expressed by DZone contributors are their own.

Related

  • A Guide to Container Runtimes
  • Docker vs Kubernetes: Which to Use and When?
  • Containerization of a Node.js Service
  • Building Secure Containers: Reducing Vulnerabilities With Clean Base Images

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: