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 golang kubernetes exposed deployment example
Signed-off-by: Matt Stratton <[email protected]>
- Loading branch information
1 parent
30f4fa3
commit 6d17cda
Showing
9 changed files
with
640 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
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,3 @@ | ||
name: kubernetes-go-exposed-deployment | ||
runtime: go | ||
description: A minimal Go 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 | ||
go get ./... | ||
``` | ||
|
||
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 kubernetes-go-exposed-deployment: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") |
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,8 @@ | ||
module kubernetes-go-exposed-deployment | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/pulumi/pulumi/sdk/v3 v3.5.1 | ||
github.com/pulumi/pulumi-kubernetes/sdk/v3 v3.0.0 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,95 @@ | ||
package main | ||
|
||
import ( | ||
appsv1 "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/apps/v1" | ||
corev1 "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/core/v1" | ||
metav1 "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/meta/v1" | ||
"github.com/pulumi/pulumi/sdk/v3/go/pulumi" | ||
"github.com/pulumi/pulumi/sdk/v3/go/pulumi/config" | ||
) | ||
|
||
func main() { | ||
pulumi.Run(func(ctx *pulumi.Context) error { | ||
// 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 := config.New(ctx, "") | ||
isMinikube := config.Require("isMinikube") | ||
|
||
// nginx container, replicated 1 time | ||
appName := "nginx" | ||
appLabels := pulumi.StringMap{ | ||
"app": pulumi.String(appName), | ||
} | ||
_, err := appsv1.NewDeployment(ctx, "nginx", &appsv1.DeploymentArgs{ | ||
// _, err := appsv1.NewDeployment(ctx, "nginx", &appsv1.DeploymentArgs{ | ||
Spec: appsv1.DeploymentSpecArgs{ | ||
Selector: &metav1.LabelSelectorArgs{ | ||
MatchLabels: appLabels, | ||
}, | ||
Replicas: pulumi.Int(1), | ||
Template: &corev1.PodTemplateSpecArgs{ | ||
Metadata: &metav1.ObjectMetaArgs{ | ||
Labels: appLabels, | ||
}, | ||
Spec: &corev1.PodSpecArgs{ | ||
Containers: corev1.ContainerArray{ | ||
corev1.ContainerArgs{ | ||
Name: pulumi.String(appName), | ||
Image: pulumi.String("nginx:1.15-alpine"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
// Allocate an IP to the nginx deployment. | ||
var frontendServiceType string | ||
if isMinikube == "true" { | ||
frontendServiceType = "ClusterIP" | ||
} else { | ||
frontendServiceType = "LoadBalancer" | ||
} | ||
|
||
frontend, err := corev1.NewService(ctx, appName, &corev1.ServiceArgs{ | ||
Metadata: &metav1.ObjectMetaArgs{ | ||
Labels: appLabels, | ||
}, | ||
Spec: &corev1.ServiceSpecArgs{ | ||
Type: pulumi.String(frontendServiceType), | ||
Ports: &corev1.ServicePortArray{ | ||
corev1.ServicePortArgs{ | ||
Port: pulumi.Int(80), | ||
TargetPort: pulumi.Int(80), | ||
Protocol: pulumi.String("TCP"), | ||
}, | ||
}, | ||
Selector: appLabels, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
// Export the public IP | ||
|
||
if isMinikube == "true" { | ||
ctx.Export("frontendIp", frontend.Spec.ApplyT(func(spec *corev1.ServiceSpec) *string { | ||
return spec.ClusterIP | ||
})) | ||
} else { | ||
ctx.Export("frontendIp", frontend.Status.ApplyT(func(status *corev1.ServiceStatus) *string { | ||
ingress := status.LoadBalancer.Ingress[0] | ||
if ingress.Hostname != nil { | ||
return ingress.Hostname | ||
} | ||
return ingress.Ip | ||
})) | ||
} | ||
|
||
return nil | ||
}) | ||
} |