Skip to content

Instantly share code, notes, and snippets.

@mjpitz
Last active November 11, 2020 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjpitz/decebc0506e2f09445c0e52bb2b3c76d to your computer and use it in GitHub Desktop.
Save mjpitz/decebc0506e2f09445c0e52bb2b3c76d to your computer and use it in GitHub Desktop.

This gist contains snippets for my "Adventures in Path Based Routing" blog post. In the post, I use kubernetes to demonstrate several useful path based routing techniques. This is mostly the skeleton for setting something up. Nothing end to end.

Blog Post

---
# Deploys a service pointing to that pod
apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
selector:
name: api-service
ports:
- protocol: TCP
port: 8080
targetPort: 8080
name: http
---
# Setup path based routing so /api/ requests are sent to the api-service
# and all other requests are sent to gh-pages.
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: gh-pages
spec:
rules:
- host: ${DOMAIN}
http:
paths:
- path: /api/
backend:
serviceName: api-service
servicePort: http
- path: /
backend:
serviceName: gh-pages
servicePort: http
---
# Creates a reference to gh-pages within the cluster.
# This allows the ingress controllers to point paths at gh-pages.
apiVersion: v1
kind: Service
metadata:
name: gh-pages
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
name: http
- protocol: TCP
port: 443
targetPort: 443
name: https
---
# Defines a set of IP addresses to handle requests to gh-pages.
# This list can be resolved by performing an `nslookup github.io`.
apiVersion: v1
kind: Endpoints
metadata:
name: gh-pages
subsets:
- addresses:
- ip: 185.199.111.153
- ip: 185.199.110.153
- ip: 185.199.109.153
- ip: 185.199.108.153
ports:
- port: 80
name: http
- port: 443
name: https
---
# Setup path based routing so /api/ requests are sent to the api-service
# and all other requests are sent to s3-bucket.
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: s3-bucket
spec:
rules:
- host: ${DOMAIN}
http:
paths:
- path: /api/
backend:
serviceName: api-service
servicePort: http
- path: /
backend:
serviceName: s3-bucket
servicePort: '80'
---
# Creates a reference to your s3 bucket within the cluster.
# This allows the ingress controllers to point paths at your s3 bucket.
apiVersion: v1
kind: Service
metadata:
name: s3-bucket
spec:
type: ExternalName
externalName: ${BUCKET_NAME}.s3-website-${REGION}.amazonaws.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment