How do I deploy the nginx:latest Docker image on Kubernetes with TypeScript?
In this guide, we will deploy the nginx:latest
Docker image on a Kubernetes cluster using Pulumi and TypeScript. We will define a Kubernetes Deployment for the NGINX container and expose it using a Kubernetes Service. This setup will allow us to run an NGINX web server on our Kubernetes cluster.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Define a Kubernetes namespace
const namespace = new k8s.core.v1.Namespace("nginx-namespace", {
metadata: { name: "nginx-namespace" },
});
// Define a Kubernetes deployment for the NGINX container
const appLabels = { app: "nginx" };
const deployment = new k8s.apps.v1.Deployment("nginx-deployment", {
metadata: {
namespace: namespace.metadata.name,
},
spec: {
selector: { matchLabels: appLabels },
replicas: 1,
template: {
metadata: { labels: appLabels },
spec: {
containers: [{
name: "nginx",
image: "nginx:latest",
ports: [{ containerPort: 80 }],
}],
},
},
},
});
// Define a Kubernetes service to expose the NGINX deployment
const service = new k8s.core.v1.Service("nginx-service", {
metadata: {
namespace: namespace.metadata.name,
},
spec: {
selector: appLabels,
ports: [{ port: 80, targetPort: 80 }],
type: "LoadBalancer",
},
});
// Export the service URL
export const serviceUrl = service.status.loadBalancer.ingress[0].hostname;
Key Points
- We created a Kubernetes namespace to isolate our resources.
- We defined a Kubernetes Deployment to manage the NGINX container, specifying the container image and port.
- We exposed the Deployment using a Kubernetes Service of type
LoadBalancer
, which will create an external load balancer to access the NGINX web server. - We exported the service URL to access the NGINX server.
Conclusion
In this guide, we deployed the nginx:latest
Docker image on a Kubernetes cluster using Pulumi and TypeScript. We created a Deployment to manage the NGINX container and a Service to expose it to the internet. This setup allows us to run and access an NGINX web server on our Kubernetes cluster.
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.