Skip to content

Commit e9c6f44

Browse files
authored
Merge pull request #19 from paulbouwer/ingress-support
Ingress support
2 parents 388adc6 + 3a96058 commit e9c6f44

File tree

15 files changed

+224
-62
lines changed

15 files changed

+224
-62
lines changed

.devcontainer/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
3434
&& echo ". /etc/bash_completion" >> ~/.bashrc \
3535
&& mkdir -p ~/completions \
3636
#
37-
# Install eslint globally
38-
&& npm install -g eslint \
37+
# Install node packages globally
38+
&& npm install -g eslint pino-pretty \
3939
#
4040
# Add add sudo support for non-root user
4141
&& apt-get install -y sudo \

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This container image can be deployed on a Kubernetes cluster. It runs a web app,
1010

1111
![Hello world! from the hello-kubernetes image](hello-kubernetes.png)
1212

13-
## Deploy
13+
## Quick start
1414

1515
You can deploy `hello-kubernetes` to your Kubernetes cluster using [Helm 3](https://helm.sh/docs/intro/install/). The Helm chart installation and configuration options can be found in the [Deploy using Helm](docs/deploy-using-helm.md) guide.
1616

@@ -22,46 +22,50 @@ cd deploy/helm
2222

2323
### Example 1: Default
2424

25-
Deploy the `hello-kubernetes` app into the `hello-kubernetes-default` namespace with the default "Hello world!" message. The app is exposed via a public Load Balancer on port 80 by default - note that this only works in cloud provider based Kubernetes offerings.
25+
Deploy the `hello-kubernetes` app into the `hello-kubernetes` namespace with the default "Hello world!" message. The app is exposed via a public Load Balancer on port 80 by default - note that a LoadBalancer service typically only works in cloud provider based Kubernetes offerings.
2626

2727
```bash
28-
helm install --create-namespace --namespace hello-kubernetes-default hello-world ./hello-kubernetes
28+
helm install --create-namespace --namespace hello-kubernetes hello-world ./hello-kubernetes
2929

3030
# get the LoadBalancer ip address.
31-
kubectl get svc hello-kubernetes -n hello-kubernetes-default -o 'jsonpath={ .status.loadBalancer.ingress[0].ip }'
31+
kubectl get svc hello-kubernetes-hello-world -n hello-kubernetes -o 'jsonpath={ .status.loadBalancer.ingress[0].ip }'
3232
```
3333

3434
### Example 2: Custom message
3535

36-
Deploy the `hello-kubernetes` app into the `hello-kubernetes-custom` namespace with an "I just deployed this on Kubernetes!" message. The app is exposed via a public Load Balancer on port 80 by default - note that this only works in cloud provider based Kubernetes offerings.
36+
Deploy the `hello-kubernetes` app into the `hello-kubernetes` namespace with an "I just deployed this on Kubernetes!" message. The app is exposed via a public Load Balancer on port 80 by default - note that a LoadBalancer service typically only works in cloud provider based Kubernetes offerings.
3737

3838
```bash
39-
helm install --create-namespace --namespace hello-kubernetes-custom hello-custom ./hello-kubernetes \
39+
helm install --create-namespace --namespace hello-kubernetes custom-message ./hello-kubernetes \
4040
--set message="I just deployed this on Kubernetes!"
4141

4242
# get the LoadBalancer ip address.
43-
kubectl get svc hello-kubernetes -n hello-kubernetes-custom -o 'jsonpath={ .status.loadBalancer.ingress[0].ip }'
43+
kubectl get svc hello-kubernetes-custom-message -n hello-kubernetes -o 'jsonpath={ .status.loadBalancer.ingress[0].ip }'
4444
```
4545

4646
### Example 3: Ingress
4747

48-
Deploy the `hello-kubernetes` app into the `hello-kubernetes-ingress` namespace. This example assumes that an ingress has been deployed into the cluster and that the ingress has a path of `/app/hello-kubernetes/` mapped to the `hello-kubernetes` service.
48+
Deploy the `hello-kubernetes` app into the `hello-kubernetes` namespace. This example assumes that an ingress has been deployed and configured in the cluster, and that the ingress has a path of `/app/hello-kubernetes/` mapped to the `hello-kubernetes` service.
4949

5050
The `hello-kubernetes` app can be reached on the ip address of the ingress via the `/app/hello-kubernetes/` path.
5151

5252
```bash
53-
helm install --create-namespace --namespace hello-kubernetes-ingress hello-ingress ./hello-kubernetes \
54-
--set ingress.enabled=true \
53+
helm install --create-namespace --namespace hello-kubernetes ingress ./hello-kubernetes \
54+
--set ingress.configured=true \
5555
--set ingress.pathPrefix="/app/hello-kubernetes/" \
5656
--set service.type="ClusterIP"
5757
```
5858

59-
## Additional
59+
## Documentation
60+
61+
### Deploying
62+
63+
If you'd like to explore the various Helm chart configuration options, then read the [Deploy with Helm](docs/deploy-using-helm.md) documentation. You can also discover more about the ingress configuration options in the [Deploy with ingress](docs/deploy-with-ingress.md) documentation
6064

6165
### Building your own images
6266

6367
If you'd like to build the `hello-kubernetes` container image yourself and reference from your own registry or DockerHub repository, then you can get more details on how to do this in the [Build and push container images](docs/build-and-push-container-images.md) documentation.
6468

6569
### Development environment
6670

67-
If you have [VS Code](https://code.visualstudio.com/) and the [Visual Studio Code Remote Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension installed, the `.devcontainer` folder will be used to provide a container based development environment. You can read more about how to use this in the [Development environments](docs/development-environment.md) documentation.
71+
If you have [VS Code](https://code.visualstudio.com/) and the [VS Code Remote Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension installed, the `.devcontainer` folder will be used to provide a container based development environment. You can read more about how to use this in the [Development environments](docs/development-environment.md) documentation.
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
{{/*
2-
Expand the name of the chart.
2+
Create a unique app name
33
*/}}
44
{{- define "hello-kubernetes.name" -}}
5-
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
5+
{{- printf "%s-%s" .Chart.Name .Release.Name | trunc 63 | trimSuffix "-" }}
6+
{{- end }}
7+
8+
{{/*
9+
Common labels
10+
*/}}
11+
{{- define "hello-kubernetes.labels" -}}
12+
{{ include "hello-kubernetes.selectorLabels" . }}
13+
{{- if .Chart.AppVersion }}
14+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
15+
{{- end }}
616
{{- end }}
717

818
{{/*
919
Selector labels
1020
*/}}
1121
{{- define "hello-kubernetes.selectorLabels" -}}
12-
app.kubernetes.io/name: {{ include "hello-kubernetes.name" . }}
22+
app.kubernetes.io/name: {{ .Chart.Name }}
1323
app.kubernetes.io/instance: {{ .Release.Name }}
1424
{{- end }}

deploy/helm/hello-kubernetes/templates/deployment.yaml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
apiVersion: apps/v1
22
kind: Deployment
33
metadata:
4-
name: {{ .Values.deployment.name }}
4+
name: {{ include "hello-kubernetes.name" . }}
5+
labels:
6+
{{- include "hello-kubernetes.labels" . | nindent 4 }}
57
spec:
68
replicas: {{ .Values.deployment.replicaCount }}
79
selector:
@@ -12,7 +14,7 @@ spec:
1214
labels:
1315
{{- include "hello-kubernetes.selectorLabels" . | nindent 8 }}
1416
spec:
15-
serviceAccountName: {{ .Values.serviceAccount.name }}
17+
serviceAccountName: {{ include "hello-kubernetes.name" . }}
1618
containers:
1719
- name: {{ .Chart.Name }}
1820
image: "{{ .Values.deployment.container.image.repository }}:{{ .Values.deployment.container.image.tag | default .Chart.AppVersion }}"
@@ -38,8 +40,14 @@ spec:
3840
- name: MESSAGE
3941
value: "{{ .Values.message }}"
4042
{{- end }}
41-
{{- if .Values.ingress.enabled }}
42-
- name: PATH_PREFIX
43+
{{- if .Values.ingress.configured }}
44+
- name: HANDLER_PATH_PREFIX
45+
{{- if .Values.ingress.rewritePath }}
46+
value: ""
47+
{{- else }}
48+
value: "{{ .Values.ingress.pathPrefix }}"
49+
{{- end }}
50+
- name: RENDER_PATH_PREFIX
4351
value: "{{ .Values.ingress.pathPrefix }}"
4452
{{- end }}
4553
{{- with .Values.deployment.resources }}

deploy/helm/hello-kubernetes/templates/service.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
apiVersion: v1
22
kind: Service
33
metadata:
4-
name: {{ .Values.service.name }}
4+
name: {{ include "hello-kubernetes.name" . }}
5+
labels:
6+
{{- include "hello-kubernetes.labels" . | nindent 4 }}
57
spec:
68
type: {{ .Values.service.type }}
79
ports:
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
apiVersion: v1
22
kind: ServiceAccount
33
metadata:
4-
name: {{ .Values.serviceAccount.name }}
4+
name: {{ include "hello-kubernetes.name" . }}
5+
labels:
6+
{{- include "hello-kubernetes.labels" . | nindent 4 }}

deploy/helm/hello-kubernetes/values.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
# Provide a custom message
22
message: ""
33

4+
# Remember to set service.type=ClusterIP if you are using an ingress
45
ingress:
5-
enabled: false
6+
configured: false
7+
rewritePath: true
68
pathPrefix: ""
79

810
service:
9-
name: "hello-kubernetes"
1011
type: LoadBalancer
1112
port: 80
1213

13-
serviceAccount:
14-
name: "hello-kubernetes"
15-
1614
deployment:
1715
replicaCount: 2
18-
name: "hello-kubernetes"
1916
container:
2017
image:
2118
repository: "paulbouwer/hello-kubernetes"

docs/build-and-push-container-images.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ The `hello-kubernetes` container image can be built and pushed to your own regis
66

77
- [make](https://www.gnu.org/software/make/)
88
- [Docker cli](https://www.docker.com/)
9-
- Docker registry
9+
- Container registry
1010

11-
If you are using the [Visual Studio Code Remote Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) based development environment, all of these prerequisites will be available in the terminal.
11+
If you are using the [VS Code Remote Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) based development environment, all of the prerequisites will be available in the terminal.
1212

1313
## Makefile configuration
1414

@@ -64,7 +64,6 @@ make push-image
6464
# - paulbouwer.azurecr.io/paulbouwer/hello-kubernetes:1.10.0
6565
# - paulbouwer.azurecr.io/paulbouwer/hello-kubernetes:1.10
6666
# - paulbouwer.azurecr.io/paulbouwer/hello-kubernetes:1
67-
6867
export REGISTRY=paulbouwer.azurecr.io
6968
make push-image
7069
```

docs/deploy-using-helm.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ The `hello-kubernetes` Helm chart can be used to deploy the `hello-kubernetes` a
99
## Prerequisites
1010

1111
- [Helm 3](https://v3.helm.sh/)
12-
- [Kubernetes](https://kubernetes.io/) cluster
12+
13+
If you are using the [VS Code Remote Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) based development environment, all of the prerequisites will be available in the terminal.
1314

1415
## Configuration and installation
1516

@@ -18,14 +19,12 @@ The following table lists the configuration parameters of the hello-kubernetes c
1819
| Parameter | Type | Default | Description |
1920
| --------- | ---- | ------- | ----------- |
2021
| `message` | `string` | `""` | A custom message to display instead of the default. |
21-
| `ingress.enabled` | `bool` | `false` | Indicate whether an ingress is being used. |
22+
| `ingress.configured` | `bool` | `false` | Indicates whether an ingress has been configured in the cluster. <br/>Note: this chart will not install or configure an ingress. You will need to install an ingress controller and add ingress record to the app namespace. |
23+
| `ingress.rewritePath` | `bool` | `true` | Indicates whether pathPrefix is rewritten by the ingress. <br/> If this is set to `true` then the hello-kubernetes dynamic content and static assets will be served from `/`, otherwise, they will be served from `/$pathPrefix`. |
2224
| `ingress.pathPrefix` | `string` | `""` | The path prefix configured in the ingress for the hello-kubernetes service.<br/> Must be provided when ingress is used. |
23-
| `service.name` | `string` | `"hello-kubernetes"` | The name of the service configured for the hello-kubernetes pods. |
2425
| `service.type` | `string` | `"LoadBalancer"` | The service type. |
2526
| `service.port` | `int` | `80` | The port exposed by the service. |
26-
| `serviceAccount.name` | `string` | `"hello-kubernetes"` | The service account bound to the pods. |
2727
| `deployment.replicaCount` | `int` | `2` | The number of replicas for the hello-kubernetes deployment. |
28-
| `deployment.name` | `string` | `hello-kubernetes` | The name of the deployment containing the hello-kubernetes pods. |
2928
| `deployment.container.image.repository` | `string` | `paulbouwer/hello-kubernetes` | The container image to run in the hello-kubernetes pods. |
3029
| `deployment.container.image.tag` | `string` | `""` | The container image tag. If not specified, the chart's appVersion is used. |
3130
| `deployment.container.image.pullPolicy` | `string` | `"IfNotPresent"` | The pull policy for the container image. |
@@ -44,18 +43,18 @@ cd deploy/helm
4443
```
4544

4645
To install `hello-kubernets` via the Helm chart, use the following to:
47-
- create the hello-kubernetes-default namespace
48-
- deploy the chart located in the ./hello-kubernetes folder
46+
- create the hello-kubernetes namespace if it doesn't exist
47+
- deploy the chart located in the ./hello-kubernetes folder into the hello-kubernetes namespace
4948
- create a Helm release named hello-world
5049

5150
```bash
52-
helm install --create-namespace --namespace hello-kubernetes-default hello-world ./hello-kubernetes
51+
helm install --create-namespace --namespace hello-kubernetes hello-world ./hello-kubernetes
5352
```
5453

5554
You can override the values for the configuration parameter defined in the table above, either directly in the `hello-kubernetes/values.yaml` file, or via the `--set` switches.
5655

5756
```bash
58-
helm install --create-namespace --namespace hello-kubernetes-custom hello-custom ./hello-kubernetes \
57+
helm install --create-namespace --namespace hello-kubernetes custom-message ./hello-kubernetes \
5958
--set message="I just deployed this on Kubernetes!"
6059
```
6160

@@ -70,7 +69,7 @@ cd deploy/helm
7069
You can modify the `hello-kubernetes` app by providing new values for the configuration parameter defined in the table above, either directly in the `hello-kubernetes/values.yaml` file, or via the `--set` switches.
7170

7271
```bash
73-
helm upgrade --namespace hello-kubernetes-custom hello-custom ./hello-kubernetes \
72+
helm upgrade --namespace hello-kubernetes custom-message ./hello-kubernetes \
7473
--set message="This is a different message"
7574
```
7675

@@ -79,5 +78,5 @@ helm upgrade --namespace hello-kubernetes-custom hello-custom ./hello-kubernetes
7978
You can uninstall the `hello-kubernetes` app as follows:
8079

8180
```bash
82-
helm uninstall --namespace hello-kubernetes-custom hello-custom
81+
helm uninstall --namespace hello-kubernetes custom-message
8382
```

docs/deploy-with-ingress.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Deploy with ingress
2+
3+
The `hello-kubernetes` Helm chart can be used to deploy and configure the `hello-kubernetes` application for use with an ingress controller.
4+
5+
> **Note:**
6+
>
7+
> The `hello-kubernetes` Helm chart does **not** deploy an [Ingress Controller](https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/) and does **not** deploy the [Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/) definition.
8+
>
9+
> The chart aims to support deployment to as many platforms and providers as possible, so the choice of Ingress Controller and configuration of Ingress resource is left to the person deploying.
10+
11+
## Prerequisites
12+
13+
- [Helm 3](https://v3.helm.sh/)
14+
15+
If you are using the [VS Code Remote Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) based development environment, all of the prerequisites will be available in the terminal.
16+
17+
## Install ingress controller
18+
19+
Install an [Ingress Controller](https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/) that is available for your platform or provider. Here is an example that uses the [Nginx Ingress Controller](https://kubernetes.github.io/ingress-nginx/deploy/) on a cloud provider:
20+
21+
```bash
22+
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
23+
24+
helm install nginx-ingress ingress-nginx/ingress-nginx \
25+
--create-namespace --namespace ingress \
26+
--set controller.replicaCount=2
27+
```
28+
29+
## Use hello-kubernetes with ingress
30+
31+
### Deploy hello-kubernetes instances
32+
33+
Install two `hello-kubernetes` instances that will be available via 2 different paths on the ingress.
34+
35+
The `hello-world` instance will display the default "Hello world!" message, and the `custom-message` instance will display a "This is my custom message!" message.
36+
37+
```bash
38+
helm install --create-namespace --namespace hello-kubernetes hello-world ./hello-kubernetes \
39+
--set ingress.configured=true --set ingress.pathPrefix=hello-world \
40+
--set service.type=ClusterIP
41+
42+
helm install --create-namespace --namespace hello-kubernetes custom-message ./hello-kubernetes \
43+
--set ingress.configured=true --set ingress.pathPrefix=custom-message \
44+
--set service.type=ClusterIP \
45+
--set message="This is my custom message!"
46+
```
47+
48+
### Deploy ingress definition
49+
50+
The `hello-kubernetes` Helm chart has a `ingress.rewritePath` configuration parameter that is `true` by default. When used together with the `ingress.configured=true` configuration parameter, there is an assumption that the ingress being used supports path rewrites. See the [Deploy using Helm](deploy-using-helm.md) guidance for more details.
51+
52+
So from our example, a request to `/hello-world` should be rewritten to `/` before being passed to the `hello-world` app instance.
53+
54+
Create a file named `hello-kubernetes-ingress.yaml` with the content below. This ingress definition will be serviced by the nginx ingress controller due to the `kubernetes.io/ingress.class: nginx` annotation. It will also leverage the path rewrite capabilities of nginx via the `nginx.ingress.kubernetes.io/rewrite-target: /$2` annotation.
55+
56+
```yaml
57+
# hello-kubernetes-ingress.yaml
58+
apiVersion: networking.k8s.io/v1beta1
59+
kind: Ingress
60+
metadata:
61+
name: hello-kubernetes-ingress
62+
annotations:
63+
kubernetes.io/ingress.class: nginx
64+
nginx.ingress.kubernetes.io/rewrite-target: /$2
65+
spec:
66+
rules:
67+
- http:
68+
paths:
69+
- backend:
70+
serviceName: hello-kubernetes-hello-world
71+
servicePort: 80
72+
path: /hello-world(/|$)(.*)
73+
- backend:
74+
serviceName: hello-kubernetes-custom-message
75+
servicePort: 80
76+
path: /custom-message(/|$)(.*)
77+
```
78+
79+
Deploy the contents of the `hello-kubernetes-ingress.yaml` into the same namespace as the two `hello-kubernetes` apps.
80+
81+
```bash
82+
kubectl apply -n hello-kubernetes -f hello-kubernetes-ingress.yaml
83+
```
84+
85+
### Browse
86+
87+
You can browse to each of the `hello-kubernetes` apps via the $INGRESS_CONTROLLER_IPADDRESS and each of the configured paths. So for our example at:
88+
89+
- `$INGRESS_CONTROLLER_IPADDRESS/hello-world` - the `hello-world` instance with the default "Hello world!" message
90+
- `$INGRESS_CONTROLLER_IPADDRESS/custom-message` - the `custom-message` instance with the "This is my custom message!" message
91+
92+
## Alternatives
93+
94+
You can deploy the `hello-kubernetes` app via the Helm chart with the `ingress.rewritePath=false` configuration parameter if you are deploying with an ingress controller that does not support path rewrites.
95+
96+
In this case, the `hello-kubernetes` apps will serve dynamic content and static assets from the path defined by the `ingress.pathPrefix` configuration parameter.

0 commit comments

Comments
 (0)