Skip to content

Commit

Permalink
Add "hello world" Kubernetes nginx example
Browse files Browse the repository at this point in the history
  • Loading branch information
hausdorff committed Aug 17, 2018
1 parent 6387a25 commit 48cfcff
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 0 deletions.
3 changes: 3 additions & 0 deletions kubernetes-ts-exposed-deployment/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: exposed-deployment
description: A simple Deployment running nginx, exposed to the Internet with a Service
runtime: nodejs
95 changes: 95 additions & 0 deletions kubernetes-ts-exposed-deployment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Kubernetes: Exposing a Deployment with a public IP address

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](images/deploy.gif "Allocating a public IP to a Deployment")

## Running the App

If you haven't already, follow the steps in [Pulumi Installation and
Setup](https://docs.pulumi.com/install/) and [Configuring Pulumi
Kubernetes](https://docs.pulumi.com/reference/kubernetes.html#configuration) to get setup with
Pulumi and Kubernetes.

Now, install dependencies:

```sh
npm install
```

Create a new stack:

```sh
$ pulumi stack init
Enter a stack name: exposed-deployment-dev
```

This example will attempt to expose the `nginx` deployment 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 to tell it whether you're deploying to minikube:

```sh
pulumi config set nginx:isMinikube <value>
```

Perform the deployment:

```sh
$ 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

Diagnostics:
kubernetes:core:Service: nginx
info: ✅ Service 'nginx-rn6uipeg' successfully created endpoint objects

info: ✅ Service has been allocated an IP

---outputs:---
frontendIp: "35.226.79.225"

info: 3 changes performed:
+ 3 resources created
Update duration: 46.555593397s

Permalink: https://app.pulumi.com/hausdorff/exposed-deployment-dev/updates/1
```

We can see here in the `---outputs:---` section that Wordpress was allocated a public IP, in this
case `35.226.79.225`. It is exported with a stack output variable, `frontendIp`. We can 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`.
```sh
$ 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](images/diff.gif "Reporting a diff after we change the app")

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](images/error.gif "Error reporting")
100 changes: 100 additions & 0 deletions kubernetes-ts-exposed-deployment/images/deploy.cast

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions kubernetes-ts-exposed-deployment/images/diff.cast
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{"version": 2, "width": 150, "height": 30, "timestamp": 1534316531, "env": {"SHELL": "/bin/zsh", "TERM": "xterm-256color"}}
[1.690079, "o", "\u001b[1m\u001b[7m%\u001b[27m\u001b[1m\u001b[0m \r \r"]
[1.6918, "o", "\u001b]2;alex@fabienne: ~/src/pulumi/test-app\u0007\u001b]1;..lumi/test-app\u0007"]
[1.788054, "o", "\r\u001b[0m\u001b[27m\u001b[24m\u001b[J\u001b[38;5;15m\u001b[48;5;31m ~ \u001b[48;5;237m\u001b[38;5;31m\u001b[38;5;250m\u001b[48;5;237m src \u001b[48;5;237m\u001b[38;5;244m\u001b[38;5;250m\u001b[48;5;237m pulumi \u001b[48;5;237m\u001b[38;5;244m\u001b[38;5;254m\u001b[48;5;237m test-app \u001b[48;5;161m\u001b[38;5;237m\u001b[38;5;15m\u001b[48;5;161m (Detached) \u001b[48;5;238m\u001b[38;5;161m\u001b[38;5;39m\u001b[48;5;238m 1 \u001b[48;5;236m\u001b[38;5;238m\u001b[38;5;15m\u001b[48;5;236m $ \u001b[0m\u001b[38;5;236m\u001b[0m \u001b[K"]
[1.788277, "o", "\u001b[?1h\u001b=\u001b[?2004h"]
[2.960446, "o", "p"]
[3.063257, "o", "\bpu"]
[3.21505, "o", "l"]
[3.27021, "o", "u"]
[3.495035, "o", "m"]
[3.591179, "o", "i"]
[3.679165, "o", " "]
[4.159147, "o", "p"]
[4.262894, "o", "r"]
[4.367115, "o", "e"]
[4.550027, "o", "v"]
[4.743231, "o", "i"]
[4.855098, "o", "e"]
[4.943161, "o", "w"]
[5.054996, "o", " "]
[5.191109, "o", "-"]
[5.327083, "o", "-"]
[5.542865, "o", "d"]
[5.655161, "o", "i"]
[5.743131, "o", "f"]
[5.894366, "o", "f"]
[6.766993, "o", "\u001b[?1l\u001b>"]
[6.767403, "o", "\u001b[?2004l\r\r\n"]
[6.76868, "o", "\u001b]2;pulumi preview --diff\u0007\u001b]1;pulumi\u0007"]
[7.559699, "o", "\u001b[38;5;13mPreviewing update of stack 'test-app'\u001b[0m\r\n"]
[7.741244, "o", "✨ Previewing update of...⠋"]
[7.862861, "o", "\b \b⠙"]
[7.93333, "o", "\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b"]
[7.933571, "o", "\u001b[38;5;8mPreviewing changes:\u001b[0m\r\n"]
[7.990793, "o", "✨ Previewing update of...⠋"]
[8.11185, "o", "\b \b⠙"]
[8.240535, "o", "\b \b⠚"]
[8.364302, "o", "\b \b⠒"]
[8.490512, "o", "\b \b⠂"]
[8.61175, "o", "\b \b⠂"]
[8.720421, "o", "\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b"]
[8.720641, "o", "\b \b\b \b\b \b\b \b\b \b\b \b\u001b[38;5;8m* pulumi:pulumi:Stack: (same)\r\n\u001b[38;5;8m [urn=urn:pulumi:test-app::test-app::pulumi:pulumi:Stack::test-app-test-app]\r\n\u001b[0m\u001b[0m"]
[8.740518, "o", "✨ Previewing update of...⠋"]
[8.763852, "o", "\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b"]
[8.764054, "o", "\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b"]
[8.864884, "o", "✨ Previewing update of...⠋"]
[8.990653, "o", "\b \b⠙"]
[9.11241, "o", "\b \b⠚"]
[9.240599, "o", "\b \b⠒"]
[9.295248, "o", "\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b \b"]
[9.295481, "o", "\b \b\b \b\b \b\b \b\b \b\b \b\b \b"]
[9.296282, "o", "\u001b[38;5;11m~ kubernetes:apps/v1beta1:Deployment: (update)\r\n\u001b[38;5;8m [id=default.nginx-tsz35hqn]\r\n\u001b[0m\u001b[38;5;8m [urn=urn:pulumi:test-app::test-app::kubernetes:apps/v1beta1:Deployment::nginx]\r\n\u001b[0m\u001b[38;5;8m apiVersion: \u001b[0m\u001b[38;5;8m\"apps/v1beta1\"\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;8m kind : \u001b[0m\u001b[38;5;8m\"Deployment\"\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;8m metadata : \u001b[0m\u001b[38;5;8m{\r\n\u001b[0m\u001b[38;5;8m annotations: \u001b[0m\u001b[38;5;8m{\r\n\u001b[0m\u001b[38;5;8m pulumi.com/autonamed: \u001b[0m\u001b[38;5;8m\"true\"\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;8m }\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;8m name : \u001b[0m\u001b[38;5;8m\"nginx-tsz35hqn\"\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;8m }\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;11m ~ spec : \u001b[0m\u001b[38;5;11m{\r\n\u001b[0m\u001b[38;5;8m replicas: \u001b[0m\u001b[38;5;8m3\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;8m selector: \u001b[0m\u001b[38;5;8m{\r\n\u001b[0m\u001b[38;5;8m matchLabels: \u001b[0m\u001b[38;5;8m{\r\n\u001b[0m\u001b[38;5;8m app: \u001b[0m\u001b[38;5;8m\"nginx\"\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;8m }\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;8m }\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38"]
[9.296394, "o", ";5;11m ~ template: \u001b[0m\u001b[38;5;11m{\r\n\u001b[0m\u001b[38;5;8m metadata: \u001b[0m\u001b[38;5;8m{\r\n\u001b[0m\u001b[38;5;8m labels: \u001b[0m\u001b[38;5;8m{\r\n\u001b[0m\u001b[38;5;8m app: \u001b[0m\u001b[38;5;8m\"nginx\"\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;8m }\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;8m }\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;11m ~ spec : \u001b[0m\u001b[38;5;11m{\r\n\u001b[0m\u001b[38;5;11m ~ containers: \u001b[0m\u001b[38;5;11m[\r\n\u001b[0m\u001b[38;5;11m ~ [0]: \u001b[0m\u001b[38;5;11m{\r\n\u001b[0m\u001b[38;5;11m ~ image: \u001b[0m\u001b[38;5;1m\"nginx:1.15-alpine\"\u001b[0m\u001b[38;5;11m => \u001b[0m\u001b[38;5;2m\"nginx:1.16-alpine\"\u001b[0m\u001b[38;5;11m\r\n\u001b[0m\u001b[38;5;8m name : \u001b[0m\u001b[38;5;8m\"nginx\"\u001b[0m\u001b[38;5;8m\r\n\u001b[0m\u001b[38;5;11m }\r\n\u001b[0m\u001b[38;5;11m ]\r\n\u001b[0m\u001b[38;5;11m }\r\n\u001b[0m\u001b[38;5;11m }\r\n\u001b[0m\u001b[38;5;11m }\r\n\u001b[0m\u001b[0m"]
[9.313711, "o", "\u001b[38;5;5minfo\u001b[0m: 1 change previewed:\r\n \u001b[38;5;11m~ 1 resource to update\u001b[0m\r\n 1 resource unchanged\r\n"]
[9.324145, "o", "\u001b[1m\u001b[7m%\u001b[27m\u001b[1m\u001b[0m \r \r"]
[9.324361, "o", "\u001b]2;alex@fabienne: ~/src/pulumi/test-app\u0007"]
[9.324502, "o", "\u001b]1;..lumi/test-app\u0007"]
[9.424517, "o", "\r\u001b[0m\u001b[27m\u001b[24m\u001b[J\u001b[38;5;15m\u001b[48;5;31m ~ \u001b[48;5;237m\u001b[38;5;31m\u001b[38;5;250m\u001b[48;5;237m src \u001b[48;5;237m\u001b[38;5;244m\u001b[38;5;250m\u001b[48;5;237m pulumi \u001b[48;5;237m\u001b[38;5;244m\u001b[38;5;254m\u001b[48;5;237m test-app \u001b[48;5;161m\u001b[38;5;237m\u001b[38;5;15m\u001b[48;5;161m (Detached) \u001b[48;5;238m\u001b[38;5;161m\u001b[38;5;39m\u001b[48;5;238m 1 \u001b[48;5;236m\u001b[38;5;238m\u001b[38;5;15m\u001b[48;5;236m $ \u001b[0m\u001b[38;5;236m\u001b[0m \u001b[K"]
[9.424595, "o", "\u001b[?1h\u001b=\u001b[?2004h"]
[14.838559, "o", "\u001b[?2004l\r\r\n"]
Binary file added kubernetes-ts-exposed-deployment/images/diff.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 48cfcff

Please sign in to comment.