Last active
March 26, 2025 11:17
-
-
Save IJMacD/11a75914f7c6f22d41e706545ce3fe12 to your computer and use it in GitHub Desktop.
Quick deploy a static site to a Kubernetes cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
usage () { | |
printf "Usage:\n $0 <directory> <domain.com> [<version>]\n" | |
} | |
if [ $# -lt 2 ]; then | |
usage | |
exit 1 | |
fi | |
dir=$1 | |
domain=$2 | |
version=${3:-1.0.0} | |
repo=ijmacd | |
image=$repo/$domain:$version | |
name=${domain%%.*} | |
echo "Deploying directory '$dir' as $image to https://$domain" | |
if [[ ! -d $dir ]]; then | |
echo "Cannot find directory '$dir'" | |
exit 1 | |
fi | |
docker build -t $image --push -f - . << EOF | |
FROM nginx:1.27.4 | |
COPY "$dir" /usr/share/nginx/html | |
EOF | |
kubectl apply -f - << EOF | |
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: "$name-web" | |
labels: | |
app.kubernetes.io/name: "static-to-cluster" | |
app.kubernetes.io/instance: "$domain" | |
app.kubernetes.io/version: "$version" | |
app.kubernetes.io/component: web | |
spec: | |
replicas: 1 | |
selector: | |
matchLabels: | |
app.kubernetes.io/component: web | |
app.kubernetes.io/instance: "$domain" | |
template: | |
metadata: | |
labels: | |
app.kubernetes.io/component: web | |
app.kubernetes.io/instance: "$domain" | |
spec: | |
containers: | |
- name: web | |
image: "$image" | |
ports: | |
- name: web | |
containerPort: 80 | |
--- | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: "$name-web" | |
labels: | |
app.kubernetes.io/component: web | |
app.kubernetes.io/name: "static-to-cluster" | |
app.kubernetes.io/instance: "$domain" | |
spec: | |
selector: | |
app.kubernetes.io/component: web | |
app.kubernetes.io/instance: "$domain" | |
ports: | |
- protocol: TCP | |
name: "web" | |
port: 80 | |
targetPort: web | |
--- | |
apiVersion: networking.k8s.io/v1 | |
kind: Ingress | |
metadata: | |
name: "$name-web" | |
annotations: | |
kubernetes.io/tls-acme: "true" | |
ingress.kubernetes.io/ssl-redirect: "true" | |
spec: | |
rules: | |
- host: "$domain" | |
http: | |
paths: | |
- path: / | |
pathType: Prefix | |
backend: | |
service: | |
name: "$name-web" | |
port: | |
name: "web" | |
tls: | |
- hosts: | |
- '$domain' | |
secretName: $name-cert | |
EOF | |
echo "In a few minutes the site will be available at https://$domain" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment