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
- Ensure you have Pulumi installed. If not, you can install it from Pulumi’s installation guide.
- Set up a new Pulumi project:
pulumi new typescript
- Install the necessary Pulumi packages:
npm install @pulumi/pulumi @pulumi/kubernetes
Step 2: Configure Kubernetes Provider
- Create a
Pulumi.yaml
file and configure the Kubernetes provider to connect to your OpenShift cluster. You can use thekubeconfig
file for authentication.
Step 3: Define the Kubernetes Deployment and Service
- Create a new TypeScript file (e.g.,
index.ts
) and define the Kubernetes Deployment and Service to deploy thegoogle/nodejs:latest
Docker image. - 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
- Run
pulumi up
to deploy the application to your OpenShift cluster. - 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 upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.