|
| 1 | +# Upgrading Applications without Downtime |
| 2 | + |
| 3 | +This guide shows how the specific features of NGINX Kubernetes Gateway can help upgrade your applications without |
| 4 | +downtime. |
| 5 | + |
| 6 | +> We assume you are familiar with the upgrade methods mentioned in this guide. As a result, we will not cover |
| 7 | +> them in depth, but instead focus on how NGINX Kubernetes Gateway supports them. |
| 8 | +<!--- This comment silences the linter. Otherwise, it will complain about the empty line, which is intentional here--> |
| 9 | +> By application downtime we mean the clients cannot get responses from your application. Instead, |
| 10 | +> they get responses with an error status code like 502 from NGINX. |
| 11 | +
|
| 12 | +This guide covers: |
| 13 | + |
| 14 | +- Rolling Deployment upgrade |
| 15 | +- Blue-green Deployments |
| 16 | +- Canary releases |
| 17 | + |
| 18 | +## Assumptions |
| 19 | + |
| 20 | +For the upgrade methods covered in the next sections, we make the following assumptions: |
| 21 | + |
| 22 | +- You deploy your application as a [Deployment][deployment]. |
| 23 | +- The Pods of the Deployment belong to a [Service][service] so that Kubernetes creates an [Endpoint][endpoints] for |
| 24 | + each Pod. |
| 25 | +- You expose the application to the clients via an [HTTPRoute][httproute] resource that references that Service. |
| 26 | + |
| 27 | +[deployment]:https://kubernetes.io/docs/concepts/workloads/controllers/deployment/ |
| 28 | + |
| 29 | +[service]:https://kubernetes.io/docs/concepts/services-networking/service/ |
| 30 | + |
| 31 | +[httproute]:https://gateway-api.sigs.k8s.io/api-types/httproute/ |
| 32 | + |
| 33 | +[endpoints]:https://kubernetes.io/docs/reference/kubernetes-api/service-resources/endpoints-v1/ |
| 34 | + |
| 35 | +For example, an application can be exposed using a routing rule like below: |
| 36 | + |
| 37 | +```yaml |
| 38 | +- matches: |
| 39 | + - path: |
| 40 | + type: PathPrefix |
| 41 | + value: / |
| 42 | + backendRefs: |
| 43 | + - name: my-app |
| 44 | + port: 80 |
| 45 | +``` |
| 46 | +
|
| 47 | +> See the [Cafe example](/examples/cafe-example) for a basic example. |
| 48 | +
|
| 49 | +Before we cover the upgrade methods, we will explain some features of NGINX that help prevent application downtime |
| 50 | +regardless of the chosen upgrade method. |
| 51 | +
|
| 52 | +## Preventing Downtime During Upgrade |
| 53 | +
|
| 54 | +NGINX graceful configuration reloads combined with how NGINX handles changes of Endpoints help prevent |
| 55 | +application downtime. |
| 56 | +
|
| 57 | +### Graceful Configuration Reloads |
| 58 | +
|
| 59 | +If a relevant Gateway API or Kubernetes built-in resource is changed, NGINX Kubernetes Gateway will update NGINX |
| 60 | +accordingly by regenerating NGINX configuration and then sending a reload signal to the NGINX master process to apply |
| 61 | +the new configuration (this process is further explained in [NGINX documentation][reconfiguration]). We call such an |
| 62 | +operation a reload. Because client requests do not get dropped during a reload, it is considered graceful. |
| 63 | +
|
| 64 | +[reconfiguration]:https://nginx.org/en/docs/control.html?#reconfiguration |
| 65 | +
|
| 66 | +
|
| 67 | +> See also the [Architecture doc](/docs/architecture.md) to learn more about NGINX Kubernetes Gateway architecture. |
| 68 | +
|
| 69 | +A subset of all possible configuration changes is changes to Endpoints, which are the most frequent changes during |
| 70 | +an application upgrade. How NGINX handles them also prevents downtime. |
| 71 | +
|
| 72 | +### Adding or Removing Endpoints |
| 73 | +
|
| 74 | +During an upgrade of an application, Kubernetes starts the Pods of the new version and brings down the old ones. It also |
| 75 | +deletes and creates the corresponding Endpoints. NGINX Kubernetes Gateway sees the changes to the Endpoints by watching |
| 76 | +for changes to the corresponding [EndpointSlices][endpoint-slices]. |
| 77 | +
|
| 78 | +[endpoint-slices]:https://kubernetes.io/docs/concepts/services-networking/endpoint-slices/ |
| 79 | +
|
| 80 | +In NGINX configuration, a Service is represented as an [upstream][upstream], and an Endpoint as an |
| 81 | +[upstream server][upstream-server]. |
| 82 | +
|
| 83 | +[upstream]:https://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream |
| 84 | +
|
| 85 | +[upstream-server]:https://nginx.org/en/docs/http/ngx_http_upstream_module.html#server |
| 86 | +
|
| 87 | +Let's consider two cases: |
| 88 | +
|
| 89 | +- If an Endpoint is added, NGINX Kubernetes Gateway adds an upstream server to NGINX that corresponds to the Endpoint, |
| 90 | + and then reloads NGINX. After that, NGINX will start proxying traffic to that Endpoint. |
| 91 | +- If an Endpoint is removed, NGINX Kubernetes Gateway removes the corresponding upstream server from NGINX. After |
| 92 | + a reload, NGINX will stop proxying traffic to it. However, it will finish proxying any pending requests to that |
| 93 | + server, which prevents downtime. |
| 94 | +
|
| 95 | +As a result, as long you have more than one ready Endpoint, the clients should not experience any downtime during |
| 96 | +an upgrade. |
| 97 | +
|
| 98 | +> It is a good practice to configure a [Readiness probe][readiness-probe] in the Deployment so that a Pod can advertise |
| 99 | +> when it is ready to receive traffic. Note that NGINX Kubernetes Gateway will not add any Endpoint to NGINX that is not |
| 100 | +> ready. |
| 101 | +
|
| 102 | +[readiness-probe]:https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/ |
| 103 | +
|
| 104 | +The upgrade methods from the next sections will change upstream servers in NGINX. |
| 105 | +
|
| 106 | +## Rolling Deployment Upgrade |
| 107 | +
|
| 108 | +To start a [rolling Deployment upgrade][rolling-upgrade], you update the Deployment to use the new version tag of |
| 109 | +the application. As a result, Kubernetes terminates the Pods with the old version and create new ones. By default, |
| 110 | +Kubernetes also ensures that some number of Pods always stay available during the upgrade. |
| 111 | +
|
| 112 | +[rolling-upgrade]:https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#rolling-update-deployment |
| 113 | +
|
| 114 | +Such an upgrade will add new upstream servers to NGINX and remove the old ones. As long as the number |
| 115 | +of Pods (ready Endpoints) during an upgrade does not reach zero, NGINX will be able to proxy traffic, and thus prevent |
| 116 | +any downtime. |
| 117 | +
|
| 118 | +To use this method, it is not necessary to update the HTTPRoute. |
| 119 | +
|
| 120 | +## Blue-Green Deployments |
| 121 | +
|
| 122 | +With this method, you deploy a new version of the application (blue version) as a separate Deployment, |
| 123 | +while the old version (green) keeps running and handling client traffic. Next, you switch the traffic from the |
| 124 | +green version to the blue. If the blue works as expected, you terminate the green. Otherwise, you switch the traffic |
| 125 | +back to the green. |
| 126 | +
|
| 127 | +There are two ways to switch the traffic: |
| 128 | +
|
| 129 | +- Update the Service selector to select the Pods of the blue version instead of the green. As a result, NGINX Kubernetes |
| 130 | + Gateway removes the green upstream servers from NGINX and add the blue ones. With this approach, it is not |
| 131 | + necessary to update the HTTPRoute. |
| 132 | +- Create a separate Service for the blue version and update the backend reference in the HTTPRoute to reference this |
| 133 | + Service, which leads to the same result as with the previous option. |
| 134 | +
|
| 135 | +## Canary Releases |
| 136 | +
|
| 137 | +To support canary releases, you can implement an approach with two Deployments behind the same Service (see |
| 138 | +[Canary deployment][canary] in the Kubernetes documentation). However, this approach lacks precision for defining the |
| 139 | +traffic split between the old and the canary version. You can greatly influence it by controlling the number of Pods |
| 140 | +(for example, four Pods of the old version and one Pod of the canary). However, note that NGINX Kubernetes Gateway uses |
| 141 | +[`random two least_conn`][random-method] load balancing method, which doesn't guarantee an exact split based on the |
| 142 | +number of Pods (80/20 in the given example). |
| 143 | + |
| 144 | +[canary]:https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#canary-deployment |
| 145 | +[random-method]:https://nginx.org/en/docs/http/ngx_http_upstream_module.html#random |
| 146 | + |
| 147 | +A more flexible and precise way to implement canary releases is to configure a traffic split in an HTTPRoute. In this |
| 148 | +case, you create a separate Deployment for the new version with a separate Service. For example, for the rule below, |
| 149 | +NGINX will proxy 95% of the traffic to the old version Endpoints and only 5% to the new ones. |
| 150 | + |
| 151 | +```yaml |
| 152 | +- matches: |
| 153 | + - path: |
| 154 | + type: PathPrefix |
| 155 | + value: / |
| 156 | + backendRefs: |
| 157 | + - name: my-app-old |
| 158 | + port: 80 |
| 159 | + weight: 95 |
| 160 | + - name: my-app-new |
| 161 | + port: 80 |
| 162 | + weight: 5 |
| 163 | +``` |
| 164 | + |
| 165 | +> There is no stickiness for the requests from the same client. NGINX will independently split each request among |
| 166 | +> the backend references. |
| 167 | + |
| 168 | +By updating the rule you can further increase the share of traffic the new version gets and finally completely switch |
| 169 | +to the new version: |
| 170 | + |
| 171 | +```yaml |
| 172 | +- matches: |
| 173 | + - path: |
| 174 | + type: PathPrefix |
| 175 | + value: / |
| 176 | + backendRefs: |
| 177 | + - name: my-app-old |
| 178 | + port: 80 |
| 179 | + weight: 0 |
| 180 | + - name: my-app-new |
| 181 | + port: 80 |
| 182 | + weight: 1 |
| 183 | +``` |
| 184 | + |
| 185 | +See the [Traffic splitting example](/examples/traffic-splitting) from our repo. |
0 commit comments