Created
December 11, 2020 01:11
-
-
Save mikesparr/48a2186faea72bb2e17ba3a6618feaab to your computer and use it in GitHub Desktop.
Kubernetes Ingress with multiple backends and wildcard defaults
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
#!/usr/bin/env bash | |
#!/usr/bin/env bash | |
# ref: https://cloud.google.com/config-connector/docs/how-to/getting-started | |
export PROJECT_ID=$(gcloud config get-value project) | |
export PROJECT_USER=$(gcloud config get-value core/account) # set current user | |
export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)") | |
export IDNS=${PROJECT_ID}.svc.id.goog # workload identity domain | |
export GCP_REGION="us-central1" | |
export GCP_ZONE="us-central1-a" | |
export CLUSTER_NAME="central" | |
# enable apis | |
gcloud services enable compute.googleapis.com \ | |
container.googleapis.com | |
# create cluster | |
gcloud beta container --project $PROJECT_ID clusters create $CLUSTER_NAME \ | |
--region $GCP_REGION \ | |
--no-enable-basic-auth \ | |
--cluster-version "1.17.13-gke.2001" \ | |
--release-channel "regular" \ | |
--machine-type "e2-small" \ | |
--image-type "COS" \ | |
--disk-type "pd-standard" \ | |
--disk-size "100" \ | |
--metadata disable-legacy-endpoints=true \ | |
--scopes "https://www.googleapis.com/auth/cloud-platform" \ | |
--preemptible \ | |
--num-nodes "1" \ | |
--enable-stackdriver-kubernetes \ | |
--enable-ip-alias \ | |
--network "projects/mike-test-gke-tickets/global/networks/default" \ | |
--subnetwork "projects/mike-test-gke-tickets/regions/us-central1/subnetworks/default" \ | |
--default-max-pods-per-node "110" \ | |
--enable-autoscaling --min-nodes "0" --max-nodes "3" \ | |
--enable-master-authorized-networks --master-authorized-networks 174.45.73.139/32 \ | |
--addons HorizontalPodAutoscaling,HttpLoadBalancing,NodeLocalDNS \ | |
--enable-autoupgrade --enable-autorepair \ | |
--max-surge-upgrade 2 --max-unavailable-upgrade 1 \ | |
--workload-pool $IDNS \ | |
--enable-shielded-nodes \ | |
--shielded-secure-boot | |
# create demo namespace | |
kubectl create ns demo | |
# deploy and expose hello | |
kubectl run --restart=Never --image=gcr.io/google-samples/hello-app:1.0 hello -n demo | |
kubectl expose pod hello --name hello-svc --type NodePort --port 8080 -n demo | |
# deploy and expose hello k8s | |
kubectl run --restart=Never --image=gcr.io/google-samples/node-hello:1.0 hello-k8s -n demo | |
kubectl expose pod hello-k8s --name hello-k8s-svc --type NodePort --port 8080 -n demo | |
# deploy and expose echoserver | |
kubectl run --restart=Never --image=gcr.io/google-containers/echoserver:1.8 echo -n demo | |
kubectl expose pod echo --name echo-svc --type NodePort --port 8080 -n demo | |
# create ingress | |
cat > ingress.yaml << EOF | |
apiVersion: networking.k8s.io/v1beta1 | |
kind: Ingress | |
metadata: | |
name: demo-ingress | |
namespace: demo | |
annotations: | |
# If the class annotation is not specified it defaults to "gce". | |
kubernetes.io/ingress.class: "gce" | |
spec: | |
rules: | |
- http: | |
paths: | |
- path: /* | |
backend: | |
serviceName: echo-svc | |
servicePort: 8080 | |
- path: /api/* | |
backend: | |
serviceName: hello-k8s-svc | |
servicePort: 8080 | |
- path: /hello/* | |
backend: | |
serviceName: hello-svc | |
servicePort: 8080 | |
EOF | |
# apply manifest | |
kubectl apply -f ingress.yaml | |
################################################################# | |
# | |
# WAIT FOR 10-20 MINUTES FIRST DEPLOY OF INGRESS FOR NETWORKING | |
# health checks may fail for a while so don't panic ;-) | |
# "Some backend services are in UNKNOWN" is normal for a while | |
# | |
################################################################# | |
export EXT_IP=$(kubectl get ingress demo-ingress -n demo -o jsonpath='{.status.loadBalancer.ingress[0].ip}') | |
echo "External IP: ${EXT_IP}" | |
# fetch the public (external) IP address and test in browser | |
curl $EXT_IP # echo | |
curl $EXT_IP/api/1 # hello-k8s | |
curl $EXT_IP/api/2 # hello-k8s | |
curl $EXT_IP/hello/888 # hello | |
curl $EXT_IP/free-willy # echo | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment