|
1 | 1 | .. _tutorial 11: |
2 | 2 |
|
3 | | -Using Docker to deploy Alerta |
4 | | -============================= |
| 3 | +Kubernetes Deployment |
| 4 | +===================== |
5 | 5 |
|
6 | | -In this tutorial, you will learn how to deploy Alerta using |
7 | | -Kubernetes_. |
| 6 | +In this tutorial, you will learn how to deploy Alerta to a |
| 7 | +Kubernetes_ cluster using the official Docker image. |
8 | 8 |
|
9 | | -.. _Kubernetes: |
| 9 | +.. _Kubernetes: https://kubernetes.io/ |
10 | 10 |
|
11 | 11 | **Contents** |
12 | 12 |
|
13 | 13 | * Overview_ |
14 | 14 | * Prerequisites_ |
15 | | - * `Step 1: Run the container`_ |
16 | | - * `Step 2: Customise configuration`_ |
17 | | - * `Step 3: Run using docker-compose`_ |
18 | | - * `Step 4: Install additional plugins or webhooks`_ |
19 | | - * `Step 5: Complex setups`_ |
20 | | - * `Step 6: Production deployments (Bonus)`_ |
21 | | - * `Debugging and Troubleshooting`_ |
| 15 | + * `Step 1: Create a namespace`_ |
| 16 | + * `Step 2: Deploy PostgreSQL`_ |
| 17 | + * `Step 3: Create ConfigMap and Secret`_ |
| 18 | + * `Step 4: Deploy the Alerta API`_ |
| 19 | + * `Step 5: Create an Ingress`_ |
| 20 | + * `Step 6: Verify the deployment`_ |
| 21 | + * `Next Steps`_ |
22 | 22 |
|
23 | 23 | Overview |
24 | 24 | -------- |
25 | 25 |
|
26 | | -The `"official" Docker image`_ for Alerta has been download more |
27 | | -than `2 million times`_ and contains everything needed |
28 | | -to deploy Alerta in most scenarios, such as: |
| 26 | +The `Alerta Docker image`_ contains the API server, web UI, |
| 27 | +housekeeping, plugins, and webhooks. Deploying to Kubernetes |
| 28 | +involves creating a database backend, configuring the application |
| 29 | +via ConfigMaps and Secrets, and exposing it through a Service |
| 30 | +and Ingress. |
29 | 31 |
|
30 | | - * Alerta API |
31 | | - * Alerta Web UI |
32 | | - * housekeeping |
33 | | - * built-in and contributed plugins |
34 | | - * built-in webhooks |
35 | | - * all auth providers "ready-to-go" |
| 32 | +.. _Alerta Docker image: https://hub.docker.com/r/alerta/alerta-web/ |
36 | 33 |
|
37 | | -.. _`"official" Docker image`: https://hub.docker.com/r/alerta/alerta-web/ |
38 | | -.. _`2 million times`: https://hub.docker.com/v2/repositories/alerta/alerta-web/ |
| 34 | +Prerequisites |
| 35 | +------------- |
39 | 36 |
|
40 | | -More complex deployments can either use it as a base image and |
41 | | -extend it with additional plugins, webhooks or alternatively, |
42 | | -the Dockerfile_ can be used as a starting point to build your |
43 | | -own base image. |
| 37 | +Before you begin, you should have: |
44 | 38 |
|
45 | | -.. _Dockerfile: https://github.com/alerta/docker-alerta/blob/master/Dockerfile |
| 39 | + * A Kubernetes cluster (Minikube_, kind_, or a managed cluster) |
| 40 | + * ``kubectl`` configured to access the cluster |
| 41 | + * Familiarity with the Docker deployment (see :ref:`tutorial 10`) |
46 | 42 |
|
47 | | -Prerequisites |
48 | | -------------- |
| 43 | +.. _Minikube: https://minikube.sigs.k8s.io/ |
| 44 | +.. _kind: https://kind.sigs.k8s.io/ |
| 45 | + |
| 46 | +.. _Step 1: |
| 47 | + |
| 48 | +Step 1: Create a namespace |
| 49 | +-------------------------- |
| 50 | + |
| 51 | +Create a dedicated namespace for the Alerta deployment:: |
| 52 | + |
| 53 | + $ kubectl create namespace alerta |
| 54 | + |
| 55 | +Set it as the default for subsequent commands:: |
| 56 | + |
| 57 | + $ kubectl config set-context --current --namespace=alerta |
| 58 | + |
| 59 | +.. _Step 2: |
| 60 | + |
| 61 | +Step 2: Deploy PostgreSQL |
| 62 | +------------------------- |
| 63 | + |
| 64 | +Deploy a simple PostgreSQL instance using a StatefulSet. Create |
| 65 | +a file called ``postgres.yaml``: |
| 66 | + |
| 67 | +.. code-block:: yaml |
| 68 | +
|
| 69 | + apiVersion: v1 |
| 70 | + kind: Secret |
| 71 | + metadata: |
| 72 | + name: postgres-secret |
| 73 | + namespace: alerta |
| 74 | + type: Opaque |
| 75 | + stringData: |
| 76 | + POSTGRES_USER: alerta |
| 77 | + POSTGRES_PASSWORD: ch4ng3m3 |
| 78 | + --- |
| 79 | + apiVersion: v1 |
| 80 | + kind: Service |
| 81 | + metadata: |
| 82 | + name: postgres |
| 83 | + namespace: alerta |
| 84 | + spec: |
| 85 | + selector: |
| 86 | + app: postgres |
| 87 | + ports: |
| 88 | + - port: 5432 |
| 89 | + clusterIP: None |
| 90 | + --- |
| 91 | + apiVersion: apps/v1 |
| 92 | + kind: StatefulSet |
| 93 | + metadata: |
| 94 | + name: postgres |
| 95 | + namespace: alerta |
| 96 | + spec: |
| 97 | + serviceName: postgres |
| 98 | + replicas: 1 |
| 99 | + selector: |
| 100 | + matchLabels: |
| 101 | + app: postgres |
| 102 | + template: |
| 103 | + metadata: |
| 104 | + labels: |
| 105 | + app: postgres |
| 106 | + spec: |
| 107 | + containers: |
| 108 | + - name: postgres |
| 109 | + image: postgres:16 |
| 110 | + envFrom: |
| 111 | + - secretRef: |
| 112 | + name: postgres-secret |
| 113 | + ports: |
| 114 | + - containerPort: 5432 |
| 115 | + volumeMounts: |
| 116 | + - name: pgdata |
| 117 | + mountPath: /var/lib/postgresql/data |
| 118 | + volumeClaimTemplates: |
| 119 | + - metadata: |
| 120 | + name: pgdata |
| 121 | + spec: |
| 122 | + accessModes: ["ReadWriteOnce"] |
| 123 | + resources: |
| 124 | + requests: |
| 125 | + storage: 5Gi |
| 126 | +
|
| 127 | +Apply it:: |
| 128 | + |
| 129 | + $ kubectl apply -f postgres.yaml |
| 130 | + |
| 131 | +.. note:: For production, consider using a PostgreSQL operator such |
| 132 | + as CloudNativePG_ or a managed database service. |
| 133 | + |
| 134 | +.. _CloudNativePG: https://cloudnative-pg.io/ |
| 135 | + |
| 136 | +.. _Step 3: |
| 137 | + |
| 138 | +Step 3: Create ConfigMap and Secret |
| 139 | +------------------------------------ |
| 140 | + |
| 141 | +Store the Alerta configuration in a ConfigMap and sensitive |
| 142 | +values in a Secret: |
| 143 | + |
| 144 | +.. code-block:: yaml |
| 145 | +
|
| 146 | + apiVersion: v1 |
| 147 | + kind: Secret |
| 148 | + metadata: |
| 149 | + name: alerta-secret |
| 150 | + namespace: alerta |
| 151 | + type: Opaque |
| 152 | + stringData: |
| 153 | + DATABASE_URL: "postgres://alerta:ch4ng3m3@postgres:5432/alerta" |
| 154 | + ADMIN_PASSWORD: "Pa55w0rd" |
| 155 | + ADMIN_KEY: "k8s-admin-api-key" |
| 156 | + --- |
| 157 | + apiVersion: v1 |
| 158 | + kind: ConfigMap |
| 159 | + metadata: |
| 160 | + name: alerta-config |
| 161 | + namespace: alerta |
| 162 | + data: |
| 163 | + AUTH_REQUIRED: "True" |
| 164 | + ADMIN_USERS: "admin@example.com" |
| 165 | + PLUGINS: "reject,heartbeat,blackout" |
| 166 | +
|
| 167 | +Save as ``alerta-config.yaml`` and apply:: |
| 168 | + |
| 169 | + $ kubectl apply -f alerta-config.yaml |
| 170 | + |
| 171 | +.. _Step 4: |
| 172 | + |
| 173 | +Step 4: Deploy the Alerta API |
| 174 | +------------------------------ |
| 175 | + |
| 176 | +Create a Deployment and Service for the Alerta server. Save |
| 177 | +the following as ``alerta-deployment.yaml``: |
| 178 | + |
| 179 | +.. code-block:: yaml |
| 180 | +
|
| 181 | + apiVersion: apps/v1 |
| 182 | + kind: Deployment |
| 183 | + metadata: |
| 184 | + name: alerta |
| 185 | + namespace: alerta |
| 186 | + spec: |
| 187 | + replicas: 2 |
| 188 | + selector: |
| 189 | + matchLabels: |
| 190 | + app: alerta |
| 191 | + template: |
| 192 | + metadata: |
| 193 | + labels: |
| 194 | + app: alerta |
| 195 | + spec: |
| 196 | + containers: |
| 197 | + - name: alerta |
| 198 | + image: alerta/alerta-web:latest |
| 199 | + ports: |
| 200 | + - containerPort: 8080 |
| 201 | + envFrom: |
| 202 | + - secretRef: |
| 203 | + name: alerta-secret |
| 204 | + - configMapRef: |
| 205 | + name: alerta-config |
| 206 | + readinessProbe: |
| 207 | + httpGet: |
| 208 | + path: /api/management/gtg |
| 209 | + port: 8080 |
| 210 | + initialDelaySeconds: 10 |
| 211 | + periodSeconds: 10 |
| 212 | + livenessProbe: |
| 213 | + httpGet: |
| 214 | + path: /_ |
| 215 | + port: 8080 |
| 216 | + initialDelaySeconds: 15 |
| 217 | + periodSeconds: 20 |
| 218 | + resources: |
| 219 | + requests: |
| 220 | + cpu: 100m |
| 221 | + memory: 256Mi |
| 222 | + limits: |
| 223 | + cpu: 500m |
| 224 | + memory: 512Mi |
| 225 | + --- |
| 226 | + apiVersion: v1 |
| 227 | + kind: Service |
| 228 | + metadata: |
| 229 | + name: alerta |
| 230 | + namespace: alerta |
| 231 | + spec: |
| 232 | + selector: |
| 233 | + app: alerta |
| 234 | + ports: |
| 235 | + - port: 8080 |
| 236 | + targetPort: 8080 |
| 237 | +
|
| 238 | +Apply it:: |
| 239 | + |
| 240 | + $ kubectl apply -f alerta-deployment.yaml |
| 241 | + |
| 242 | +.. _Step 5: |
| 243 | + |
| 244 | +Step 5: Create an Ingress |
| 245 | +-------------------------- |
| 246 | + |
| 247 | +Expose Alerta externally using an Ingress resource. This example |
| 248 | +assumes an nginx ingress controller is installed: |
| 249 | + |
| 250 | +.. code-block:: yaml |
| 251 | +
|
| 252 | + apiVersion: networking.k8s.io/v1 |
| 253 | + kind: Ingress |
| 254 | + metadata: |
| 255 | + name: alerta-ingress |
| 256 | + namespace: alerta |
| 257 | + annotations: |
| 258 | + nginx.ingress.kubernetes.io/rewrite-target: / |
| 259 | + spec: |
| 260 | + ingressClassName: nginx |
| 261 | + rules: |
| 262 | + - host: alerta.example.com |
| 263 | + http: |
| 264 | + paths: |
| 265 | + - path: / |
| 266 | + pathType: Prefix |
| 267 | + backend: |
| 268 | + service: |
| 269 | + name: alerta |
| 270 | + port: |
| 271 | + number: 8080 |
| 272 | +
|
| 273 | +Save as ``alerta-ingress.yaml`` and apply:: |
| 274 | + |
| 275 | + $ kubectl apply -f alerta-ingress.yaml |
| 276 | + |
| 277 | +.. _Step 6: |
| 278 | + |
| 279 | +Step 6: Verify the deployment |
| 280 | +------------------------------ |
| 281 | + |
| 282 | +Check that all pods are running:: |
| 283 | + |
| 284 | + $ kubectl get pods -n alerta |
| 285 | + NAME READY STATUS RESTARTS AGE |
| 286 | + alerta-6d8f9b7c4d-abc12 1/1 Running 0 2m |
| 287 | + alerta-6d8f9b7c4d-def34 1/1 Running 0 2m |
| 288 | + postgres-0 1/1 Running 0 5m |
| 289 | + |
| 290 | +Test the health check endpoint:: |
| 291 | + |
| 292 | + $ kubectl port-forward svc/alerta 8080:8080 -n alerta & |
| 293 | + $ curl http://localhost:8080/api/management/healthcheck |
| 294 | + {"status": "ok"} |
| 295 | + |
| 296 | +Send a test alert:: |
49 | 297 |
|
50 | | -To follow this tutorial you will need to `install Minikube`_, the "single-node Kubernetes |
51 | | -cluster in a virtual machine on your personal computer" which you |
52 | | -will need to complete `Step 6`_ where you mimic deploying to production. |
| 298 | + $ curl -X POST http://localhost:8080/api/alert \ |
| 299 | + -H 'Content-Type: application/json' \ |
| 300 | + -H 'Authorization: Key k8s-admin-api-key' \ |
| 301 | + -d '{ |
| 302 | + "resource": "k8s-test", |
| 303 | + "event": "DeploymentTest", |
| 304 | + "environment": "Development", |
| 305 | + "service": ["Kubernetes"], |
| 306 | + "severity": "informational", |
| 307 | + "text": "Alerta is running on Kubernetes" |
| 308 | + }' |
53 | 309 |
|
54 | | -.. _install Minikube: https://kubernetes.io/docs/tasks/tools/install-minikube/ |
| 310 | +Browse to the web UI at http://localhost:8080 (via port-forward) |
| 311 | +or at your Ingress hostname to confirm everything is working. |
55 | 312 |
|
| 313 | +Next Steps |
| 314 | +---------- |
56 | 315 |
|
| 316 | + * Configure TLS on the Ingress for production use |
| 317 | + * Set up Horizontal Pod Autoscaling for the Alerta Deployment |
| 318 | + * :ref:`Troubleshooting <tutorial 9>` |
0 commit comments