Skip to main content

Exposing a deployment with a public IP address

A simple Deployment running nginx, exposed to the Internet with a Service

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set kubernetes-ts-exposed-deployment
cd pulumi-examples/kubernetes-ts-exposed-deployment

Deploys nginx to a Kubernetes cluster, and publicly exposes it to the Internet with an IP address, using a Kubernetes Service.

In the gif below we see the experience of deploying this example with pulumi up. Notice that Pulumi has an inherent notion of “done-ness” — Pulumi waits for the IP address to be allocated to the Service. Because this example uses the Pulumi concept of stack exports to report this IP address, in this example we are also able to use curl to reach the nginx server.

Allocating a public IP to a Deployment

Prerequisites#

  1. Install Pulumi
  2. Configure Kubernetes
  3. Install Node.js

Deploying the example#

  1. Create a new stack:

    Terminal window
    pulumi stack init dev
  2. This example will attempt to expose the nginx deployment to the Internet with a Service of type LoadBalancer. Since minikube does not support LoadBalancer, the application already knows to use type ClusterIP instead; all you need to do is tell it whether you’re deploying to minikube:

    Terminal window
    pulumi config set isMinikube <value>
  3. Install dependencies:

    Terminal window
    npm install
  4. Deploy the stack:

    Terminal window
    pulumi up
    Updating stack 'exposed-deployment-dev'
    Performing changes:
    Type Name Status Info
    + pulumi:pulumi:Stack exposed-deployment-exposed-deployment-dev created 1 warning
    + ├─ kubernetes:apps:Deployment nginx created
    + └─ kubernetes:core:Service nginx created 2 info messages
    ---outputs:---
    frontendIp: "35.226.79.225"
    info: 3 changes performed:
    + 3 resources created
    Update duration: 46.555593397s
  5. We can see here in the ---outputs:--- section that nginx was allocated a public IP, in this case 35.226.79.225. It is exported with a stack output variable, frontendIp. Use curl and grep to retrieve the <title> of the site the proxy points at:

    Note: minikube does not support type LoadBalancer; if you are deploying to minikube, make sure to run kubectl port-forward svc/frontend 8080:80 to forward the cluster port to the local machine and access the service via localhost:8080.

    Terminal window
    curl -sL $(pulumi stack output frontendIp) | grep "<title>"
    <title>Welcome to nginx!</title>

Next steps#

Now that nginx is deployed and exposed to the Internet with an IP, try playing around with the example!

If we change the nginx image to nginx:1.16-alpine, we can run pulumi preview --diff and see this change reported to us:

Diff

Notice also that if you provide an image that does not exist, Pulumi will report errors as it sees them. You should see something similar in principle to this:

Diff

Cleaning up#

Once you’re finished experimenting, you can destroy your stack and remove it to avoid incurring any additional cost:

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure as code platform for any cloud.