Skip to content

Instantly share code, notes, and snippets.

@sidpalas
Last active August 30, 2024 14:08
Show Gist options
  • Save sidpalas/e388f1a63bacc4c365d6cebf366f492d to your computer and use it in GitHub Desktop.
Save sidpalas/e388f1a63bacc4c365d6cebf366f492d to your computer and use it in GitHub Desktop.
Kubernetes temporary maintenance page
apiVersion: v1
kind: ConfigMap
metadata:
name: maintenance-page
data:
maintenance.html: |-
<!--HTML GOES HERE-->
<!doctype html>
<title>Site Maintenance</title>
<link rel="stylesheet" href="maintenance.css">
<article>
<h1>We&rsquo;ll be back soon!</h1>
<div>
<p>Sorry for the inconvenience but we&rsquo;re performing some maintenance at the moment. If you need to you can always <a href="mailto:#">contact us</a>, otherwise we&rsquo;ll be back online shortly!</p>
<p>&mdash; The Team</p>
</div>
<div><img src="https://pbs.twimg.com/profile_images/1326958623587700736/_sXRf1ch_400x400.jpg"></div>
</article>
maintenance.css: |-
/* CSS GOES HERE */
body { text-align: center; padding: 150px; background-color: #D3D3D3;}
h1 { font-size: 50px; }
body { font: 20px Helvetica, sans-serif; color: #333; }
article { display: block; text-align: left; width: 650px; margin: 0 auto; }
a { color: #dc8100; text-decoration: none; }
a:hover { color: #333; text-decoration: none; }
img { border-radius: 50%; }
default.conf: |-
# NGINX CONFIGURATION GOES HERE
server {
listen 80 default_server;
server_name _ ;
location / {
if (-f /usr/share/nginx/html/maintenance/maintenance.html) {
return 503;
}
}
# for all routes, return maintenance page
error_page 503 @maintenance;
location @maintenance {
root /usr/share/nginx/html/maintenance/;
rewrite ^(.*)$ /maintenance.html break;
}
# allow images and css to be retrieved
location ~* \.(png|jpg|jpeg|css) {
root /usr/share/nginx/html/maintenance/;
}
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: maintenance-page
labels:
app: maintenance-page
spec:
replicas: 1
selector:
matchLabels:
app: maintenance-page
template:
metadata:
labels:
app: maintenance-page
spec:
containers:
- name: nginx
image: nginx:1.23
ports:
- containerPort: 80
volumeMounts:
# Because no subPath is specified, all keys in configmap willb
# be mounted as files at the specified mountPath
- name: config-volume
mountPath: /usr/share/nginx/html/maintenance/
- name: config-volume
mountPath: /etc/nginx/conf.d/default.conf
subPath: default.conf
volumes:
- name: config-volume
configMap:
name: maintenance-page
---
apiVersion: v1
kind: Service
metadata:
name: maintenance-page
spec:
selector:
app: maintenance-page
ports:
- protocol: TCP
port: 80
targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.org/rewrites: serviceName=maintenance-page rewrite=/;
name: maintenance-page
spec:
rules:
- host: maintenance.devopsdirective.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: maintenance-page
port:
number: 80
# Can still have other paths defined
@nfabredev
Copy link

Hey @sidpalas thanks for sharing this, it's working great - and I love the use of configMaps :) - however line 117 the port number to the maintenance-page backend service should be 80 instead of 8080!

@sidpalas
Copy link
Author

sidpalas commented Mar 1, 2023

Hey @sidpalas thanks for sharing this, it's working great - and I love the use of configMaps :) - however line 117 the port number to the maintenance-page backend service should be 80 instead of 8080!

Fixed! Thanks for pointing this out! 🙏

@Fedcomp
Copy link

Fedcomp commented Mar 11, 2023

Wow. This is awesome, thank you! saves me so much time.

@EvgeniGordeev
Copy link

NB: nginx.org/rewrites is available only in paid Nginx Plus version of ingress controller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment