Skip to content

Commit 9ab0898

Browse files
committed
Add a Python example of the NGINX Ingress Controller
1 parent bf6b301 commit 9ab0898

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.pyc
2+
venv/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: simple-nginx-py
2+
runtime:
3+
name: python
4+
options:
5+
virtualenv: venv
6+
description: A simple NGINX ingress controller example
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Example inspired by:
2+
# https://www.digitalocean.com/community/tutorials/how-to-set-up-an-nginx-ingress-on-digitalocean-kubernetes-using-helm
3+
# Note that, as described in that article, you will need to configure DNS for hw1/hw2.your_domain_name.
4+
5+
import pulumi
6+
from pulumi_kubernetes.apps.v1 import Deployment
7+
from pulumi_kubernetes.core.v1 import Service
8+
from pulumi_kubernetes.networking.v1 import Ingress
9+
from pulumi_kubernetes_ingress_nginx import IngressController, ControllerArgs, ControllerPublishServiceArgs
10+
11+
# Install the NGINX ingress controller to our cluster. The controller
12+
# consists of a Pod and a Service. Install it and configure the controller
13+
# to publish the load balancer IP address on each Ingress so that
14+
# applications can depend on the IP address of the load balancer if needed.
15+
ctrl = IngressController('myctrl',
16+
controller=ControllerArgs(
17+
publish_service=ControllerPublishServiceArgs(
18+
enabled=True,
19+
),
20+
),
21+
)
22+
23+
# Now let's deploy two applications which are identical except for the
24+
# names. We will later configure the ingress to direct traffic to them,
25+
# one domain name per application instance.
26+
apps = []
27+
app_base = 'hello-k8s'
28+
app_names = [ f'{app_base}-first', f'{app_base}-second' ]
29+
for app_name in app_names:
30+
app_svc = Service(f'{app_name}-svc',
31+
metadata={ 'name': app_name },
32+
spec={
33+
'type': 'ClusterIP',
34+
'ports': [{ 'port': 80, 'targetPort': 8080 }],
35+
'selector': { 'app': app_name },
36+
},
37+
)
38+
app_dep = Deployment(f'{app_name}-dep',
39+
metadata={ 'name': app_name },
40+
spec={
41+
'replicas': 3,
42+
'selector': {
43+
'matchLabels': { 'app': app_name },
44+
},
45+
'template': {
46+
'metadata': {
47+
'labels': { 'app': app_name },
48+
},
49+
'spec': {
50+
'containers': [{
51+
'name': app_name,
52+
'image': 'paulbouwer/hello-kubernetes:1.8',
53+
'ports': [{ 'containerPort': 8080 }],
54+
'env': [{ 'name': 'MESSAGE', 'value': 'Hello K8s!' }],
55+
}],
56+
},
57+
},
58+
},
59+
)
60+
apps.append(app_svc.status)
61+
62+
# Next, expose the app using an Ingress.
63+
app_ingress = Ingress(f'{app_base}-ingress',
64+
metadata={
65+
'name': 'hello-k8s-ingress',
66+
'annotations': {
67+
'kubernetes.io/ingress.class': 'nginx',
68+
},
69+
},
70+
spec={
71+
'rules': [
72+
{
73+
# Replace this with your own domain!
74+
'host': 'myservicea.foo.org',
75+
'http': {
76+
'paths': [{
77+
'pathType': 'Prefix',
78+
'path': '/',
79+
'backend': {
80+
'service': {
81+
'name': app_names[0],
82+
'port': { 'number': 80 },
83+
},
84+
},
85+
}],
86+
},
87+
},
88+
{
89+
# Replace this with your own domain!
90+
'host': 'myserviceb.foo.org',
91+
'http': {
92+
'paths': [{
93+
'pathType': 'Prefix',
94+
'path': '/',
95+
'backend': {
96+
'service': {
97+
'name': app_names[1],
98+
'port': { 'number': 80 },
99+
},
100+
},
101+
}],
102+
},
103+
},
104+
],
105+
},
106+
)
107+
108+
pulumi.export('app_statuses', apps)
109+
pulumi.export('controller_status', ctrl.status)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pulumi>=3.0.0,<4.0.0
2+
pulumi-kubernetes>=3.0.0,<4.0.0

0 commit comments

Comments
 (0)