forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Python kubernetes exposed deployment example (pulumi#1067)
- Loading branch information
1 parent
311403a
commit 14884ef
Showing
11 changed files
with
468 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.pyc | ||
venv/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name: kubernetes-py-exposed-deployment | ||
runtime: | ||
name: python | ||
options: | ||
virtualenv: venv | ||
description: A minimal Python Pulumi program |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new) | ||
|
||
# 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://www.pulumi.com/docs/get-started/install/) and [Configuring Pulumi | ||
Kubernetes](https://www.pulumi.com/docs/intro/cloud-providers/kubernetes/setup/) to get setup with | ||
Pulumi and Kubernetes. | ||
|
||
Now, install dependencies: | ||
|
||
```sh | ||
pip3 install -r requirements.txt | ||
``` | ||
|
||
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 is_minikube "true" | ||
``` | ||
|
||
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 the cluster was allocated a public IP, in this | ||
case `35.226.79.225`. It is exported with a stack output variable, `frontend_ip`. 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 service/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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Copyright 2016-2019, Pulumi Corporation. All rights reserved.""" | ||
|
||
import pulumi | ||
import pulumi_kubernetes as k8s | ||
|
||
"""Minikube does not implement services of type `LoadBalancer` require the user to specify if we're running on minikube, and if so, create only services of type ClusterIP.""" | ||
config = pulumi.Config() | ||
is_minikube = config.require("is_minikube") | ||
|
||
"""nginx container, replicated 1 time.""" | ||
app_name = "nginx" | ||
app_labels = { "app": app_name } | ||
|
||
nginx = k8s.apps.v1.Deployment( | ||
app_name, | ||
spec=k8s.apps.v1.DeploymentSpecArgs( | ||
replicas=1, | ||
selector=k8s.meta.v1.LabelSelectorArgs(match_labels=app_labels), | ||
template=k8s.core.v1.PodTemplateSpecArgs( | ||
metadata=k8s.meta.v1.ObjectMetaArgs(labels=app_labels), | ||
spec=k8s.core.v1.PodSpecArgs( | ||
containers=[ | ||
k8s.core.v1.ContainerArgs( | ||
name=app_name, | ||
image="nginx:1.15-alpine", | ||
) | ||
] | ||
), | ||
), | ||
) | ||
) | ||
|
||
|
||
"""Allocate an IP to the nginx Deployment.""" | ||
frontend = k8s.core.v1.Service( | ||
app_name, | ||
metadata=k8s.meta.v1.ObjectMetaArgs( | ||
labels=app_labels), | ||
spec=k8s.core.v1.ServiceSpecArgs( | ||
selector=app_labels, | ||
ports=[ | ||
k8s.core.v1.ServicePortArgs( | ||
port=80, | ||
target_port=80, | ||
protocol="TCP" | ||
) | ||
], | ||
type="ClusterIP" if is_minikube == "true" else "LoadBalancer", | ||
), | ||
) | ||
|
||
# """When "done", this will print the public IP.""" | ||
if is_minikube == "true": | ||
pulumi.export("frontend_IP", frontend.spec.apply(lambda s: s.cluster_ip)) | ||
else: | ||
pulumi.export("frontend_IP", frontend.status.apply(lambda s: s.load_balancer.ingress[0].ip)) |
Oops, something went wrong.