1. Answers
  2. Deploy The Google/nodejs:latest Docker Image On Opensshift With TypeScript.

Deploy the Google/Nodejs:latest Docker Image on Opensshift With TypeScript.

Introduction

In this guide, we will deploy the google/nodejs:latest Docker image on OpenShift using Pulumi with TypeScript. We will use Pulumi’s Kubernetes provider to interact with the OpenShift cluster and deploy the Docker image as a Kubernetes Deployment and Service.

Step-by-Step Explanation

Step 1: Set Up Pulumi and Install Dependencies

  1. Ensure you have Pulumi installed. If not, you can install it from Pulumi’s installation guide.
  2. Set up a new Pulumi project:
    pulumi new typescript
    
  3. Install the necessary Pulumi packages:
    npm install @pulumi/pulumi @pulumi/kubernetes
    

Step 2: Configure Kubernetes Provider

  1. Create a Pulumi.yaml file and configure the Kubernetes provider to connect to your OpenShift cluster. You can use the kubeconfig file for authentication.

Step 3: Define the Kubernetes Deployment and Service

  1. Create a new TypeScript file (e.g., index.ts) and define the Kubernetes Deployment and Service to deploy the google/nodejs:latest Docker image.
  2. Use the following code as a reference:
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Create a Kubernetes namespace
const namespace = new k8s.core.v1.Namespace("app-ns", {
    metadata: { name: "app-ns" },
});

// Create a Kubernetes Deployment
const appLabels = { app: "nodejs-app" };
const deployment = new k8s.apps.v1.Deployment("nodejs-deployment", {
    metadata: {
        namespace: namespace.metadata.name,
        name: "nodejs-deployment",
    },
    spec: {
        selector: { matchLabels: appLabels },
        replicas: 1,
        template: {
            metadata: { labels: appLabels },
            spec: {
                containers: [{
                    name: "nodejs",
                    image: "google/nodejs:latest",
                    ports: [{ containerPort: 8080 }],
                }],
            },
        },
    },
});

// Create a Kubernetes Service
const service = new k8s.core.v1.Service("nodejs-service", {
    metadata: {
        namespace: namespace.metadata.name,
        name: "nodejs-service",
    },
    spec: {
        selector: appLabels,
        ports: [{ port: 8080, targetPort: 8080 }],
        type: "LoadBalancer",
    },
});

Step 4: Deploy the Application

  1. Run pulumi up to deploy the application to your OpenShift cluster.
  2. Verify that the Deployment and Service are created successfully and that the google/nodejs:latest Docker image is running.

Summary

In this guide, we deployed the google/nodejs:latest Docker image on OpenShift using Pulumi with TypeScript. We created a Kubernetes Deployment and Service to manage the application. By following these steps, you can easily deploy Docker images on OpenShift using Pulumi.

Full Code Example

import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Create a Kubernetes namespace
const namespace = new k8s.core.v1.Namespace("app-ns", {
    metadata: { name: "app-ns" },
});

// Create a Kubernetes Deployment
const appLabels = { app: "nodejs-app" };
const deployment = new k8s.apps.v1.Deployment("nodejs-deployment", {
    metadata: {
        namespace: namespace.metadata.name,
        name: "nodejs-deployment",
    },
    spec: {
        selector: { matchLabels: appLabels },
        replicas: 1,
        template: {
            metadata: { labels: appLabels },
            spec: {
                containers: [{
                    name: "nodejs",
                    image: "google/nodejs:latest",
                    ports: [{ containerPort: 8080 }],
                }],
            },
        },
    },
});

// Create a Kubernetes Service
const service = new k8s.core.v1.Service("nodejs-service", {
    metadata: {
        namespace: namespace.metadata.name,
        name: "nodejs-service",
    },
    spec: {
        selector: appLabels,
        ports: [{ port: 8080, targetPort: 8080 }],
        type: "LoadBalancer",
    },
});

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up