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