| 146 | |
| 147 | This service will expose your webapp on a NodePort. |
| 148 | |
| 149 | • Apply the Service: |
| 150 | |
| 151 | `kubectl apply -f [your-service-file].yaml` |
| 152 | |
| 153 | '''Step 2: Set Up Ingress to Route to the NodePort Service''' |
| 154 | |
| 155 | 1. Define an Ingress Resource: Create an Ingress resource that routes traffic to your NodePort service. Here's an example YAML for the Ingress: |
| 156 | |
| 157 | {{{ |
| 158 | |
| 159 | apiVersion: networking.k8s.io/v1 |
| 160 | kind: Ingress |
| 161 | metadata: |
| 162 | name: webapp-ingress |
| 163 | spec: |
| 164 | ingressClassName: nginx |
| 165 | rules: |
| 166 | - host: |
| 167 | http: |
| 168 | paths: |
| 169 | - path: / |
| 170 | pathType: Prefix |
| 171 | backend: |
| 172 | service: |
| 173 | name: webapp-nodeport-service |
| 174 | port: |
| 175 | number: 80 |
| 176 | }}} |
| 177 | |
| 178 | This Ingress resource routes traffic from nginx-ingress controller external IP to the webapp-nodeport-service on port 80. |
| 179 | |
| 180 | • Apply the Ingress Resource: |
| 181 | |
| 182 | `kubectl apply -f webapp-ingress.yaml` |
| 183 | |
| 184 | |
| 185 | '''Step 3: Ensure Ingress Controller is Set Up Correctly''' |
| 186 | |
| 187 | Ensuring that your Ingress Controller is properly set up and accessible from outside the Kubernetes cluster involves several key steps. This setup is crucial for allowing external traffic to reach your services through the Ingress rules you've defined. Here's a breakdown of what this entails: |
| 188 | |
| 189 | 1. Deploying the Ingress Controller |
| 190 | |
| 191 | • Choose an Ingress Controller: There are several Ingress Controllers available, such as NGINX, Traefik, HAProxy, etc. NGINIX is a popular choice due to its stability and feature set. |
| 192 | |
| 193 | • Install the Ingress Controller: You need to deploy the Ingress Controller in your Kubernetes cluster. For NGINX, you might use a command like: |
| 194 | |
| 195 | {{{ |
| 196 | kubectl apply -f |
| 197 | https://raw.githubusercontent.com/kubernetes/ingress- |
| 198 | nginx/controller- |
| 199 | v1.8.2/deploy/static/provider/cloud/deploy.yaml |
| 200 | }}} |
| 201 | |
| 202 | 2. Exposing the Ingress Controller |
| 203 | |
| 204 | |
| 205 | • Service Type: The Ingress Controller itself is exposed via a Kubernetes Service. This Service needs to be accessible from outside the cluster. There are two common ways to do this: |
| 206 | - NodePort: The Service is exposed on a high port (e.g., 30000-32767) on each node's IP address. External traffic can reach the Ingress Controller by hitting any node's IP at this port. |
| 207 | - LoadBalancer: If your cluster is running in a cloud environment that supports LoadBalancer Services, this is a more straightforward way to expose your Ingress Controller. For our setup lets allow the metalLB setup to lease an external IP address that routes traffic to the Ingress Controller. |
| 208 | |
| 209 | '''Step 4: DNS Configuration''' |
| 210 | |
| 211 | 1. Configure DNS: Map the DNS record to the external IP address of one of your cluster nodes (if using NodePort for the Ingress Controller) or to the external IP provided by the LoadBalancer (if using LoadBalancer for the Ingress Controller). |
| 212 | |
| 213 | Read More: |
| 214 | [https://spacelift.io/blog/kubernetes-ingress] |
| 215 | [https://matthewpalmer.net/kubernetes-app-developer/articles/kubernetes-ingress-guide-nginx- example.html] |
| 216 | |