Skip to content

Instantly share code, notes, and snippets.

@mtdefelice
Last active November 20, 2019 20:01
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 mtdefelice/1776d1b2ad2aeceff81cc173b814e14d to your computer and use it in GitHub Desktop.
Save mtdefelice/1776d1b2ad2aeceff81cc173b814e14d to your computer and use it in GitHub Desktop.
#! /bin/bash
# Get credentials for cluster <cluster> (which must exist in your working project)
# gcloud container clusters list
# gcloud container clusters get-credentials <cluster>
# Poke around ...
# kubectl get deployments
# kubectl get services
# kubectl get ingress
# Create the first deployment "web1" and service "web1-service". To keep this documentation simple I'm using heredocs. Note that the container exposes port 8080 and the service maps port 80 to the container's 8080.
cat << EOF | kubectl apply -f -
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: web1
namespace: default
spec:
selector:
matchLabels:
app: web1
template:
metadata:
labels:
app: web1
spec:
containers:
- image: gcr.io/google-samples/hello-app:1.0
imagePullPolicy: IfNotPresent
name: web1
ports:
- containerPort: 8080
protocol: TCP
EOF
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: web1-service
spec:
selector:
app: web1
type: NodePort
ports:
- protocol: TCP
port: 80
targetPort: 8080
EOF
# Create the second deployment "web2" and service "web2-service"
cat << EOF | kubectl apply -f -
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: web2
namespace: default
spec:
selector:
matchLabels:
app: web2
template:
metadata:
labels:
app: web2
spec:
containers:
- image: gcr.io/google-samples/hello-app:1.0
imagePullPolicy: IfNotPresent
name: web2
ports:
- containerPort: 8080
protocol: TCP
EOF
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: web2-service
spec:
selector:
app: web2
type: NodePort
ports:
- protocol: TCP
port: 80
targetPort: 8080
EOF
# Name of the GCP reserved IP address. Note that it must be "global". I created this from the GCP web console: https://console.cloud.google.com/networking/addresses/list
SIP=reserved-global-static-address-001
# Create an ingress resource
cat << EOF | kubectl apply -f -
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: web-fanout-ingress
annotations:
kubernetes.io/ingress.global-static-ip-name: "$SIP"
spec:
rules:
- http:
paths:
- path: /*
backend:
serviceName: web1-service
servicePort: 80
- path: /v2/*
backend:
serviceName: web2-service
servicePort: 80
EOF
# Wait a few minutes. Note the difference between routes / and /v2/ :-)
# What if I only wanted a basic ingress to a single exposed service? In this case "web1-service"
# SIP=reserved-global-static-address-002
# cat << EOF | kubectl apply -f -
# apiVersion: extensions/v1beta1
# kind: Ingress
# metadata:
# name: web1-ingress
# annotations:
# kubernetes.io/ingress.global-static-ip-name: "$SIP"
# spec:
# backend:
# serviceName: web1-service
# servicePort: 80
#
# EOF
# What if I wanted an ingress to a service prepended by a specific url pattern with a failback? In this case "web1-service" is the failback and "web2-service" uses a prepended path
# SIP=reserved-global-static-address-003
# apiVersion: extensions/v1beta1
# kind: Ingress
# metadata:
# name: web-fanout-ingress-with-default-backend
# annotations:
# kubernetes.io/ingress.global-static-ip-name: "$SIP"
# spec:
# backend:
# serviceName: web1-service
# servicePort: 80
# rules:
# - http:
# paths:
# - path: /v2/*
# backend:
# serviceName: web2-service
# servicePort: 80
#
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment