|
| 1 | +# Kubernetes SSL Ingress and Service Deployment Setup on GCP |
| 2 | + |
| 3 | +A simple k8s project that demonstrates how to set up a web service, route traffic to it via a load balancer and configure the load balancer to use SSL certificates. |
| 4 | + |
| 5 | +1 - create container cluster. can also be done via gcp console. |
| 6 | + |
| 7 | +``` |
| 8 | +gcloud container clusters create <cluster-name> --zone <zone-name> --machine-type g1-small --num-nodes 1 |
| 9 | +``` |
| 10 | + |
| 11 | +2 - retrieve cluster credentials if needed. |
| 12 | + |
| 13 | +``` |
| 14 | +gcloud container clusters get-credentials <cluster-name> |
| 15 | +``` |
| 16 | + |
| 17 | +3 - deploy the application. |
| 18 | + |
| 19 | +``` |
| 20 | +kubectl run <deployment-name> --image=gcr.io/google-samples/hello-app:1.0 --port=8080 |
| 21 | +``` |
| 22 | + |
| 23 | +4 - expose deployment as internal service. This will not make the application public yet. |
| 24 | + |
| 25 | +``` |
| 26 | +kubectl expose deployment <deployment-name> --target-port=8080 --type=NodePort |
| 27 | +
|
| 28 | +or |
| 29 | +
|
| 30 | +kubectl apply -f deployment.yaml |
| 31 | +``` |
| 32 | + |
| 33 | +5 - create a service for the pods created by the deployment during step 4. |
| 34 | + |
| 35 | +``` |
| 36 | +kubectl apply -f service.yaml |
| 37 | +``` |
| 38 | + |
| 39 | +6 - create an SSL certificate and a set of keys. |
| 40 | + |
| 41 | +``` |
| 42 | +// key |
| 43 | +openssl genrsa -out test-ingress.key 2048 |
| 44 | +
|
| 45 | +// signing request |
| 46 | +openssl req -new -key test-ingress.key -out test-ingress.csr \ |
| 47 | + -subj "/CN=k8s.shapes.ai" |
| 48 | +
|
| 49 | +// certificate |
| 50 | +openssl x509 -req -days 365 -in test-ingress.csr -signkey test-ingress.key \ |
| 51 | + -out test-ingress.crt |
| 52 | +``` |
| 53 | + |
| 54 | +7a - Create a secret for holding certificate and key. |
| 55 | + |
| 56 | +``` |
| 57 | +kubectl create secret tls my-secret \ |
| 58 | + --cert test-ingress.crt --key test-ingress.key |
| 59 | +``` |
| 60 | + |
| 61 | +7b - create ingress resource to generate a load balancer that will route traffic to the application and deploy it. In the ingress manifest specify to use the certificate generated in the previous step. |
| 62 | + |
| 63 | +``` |
| 64 | +kubectl apply -f ingress.yaml |
| 65 | +``` |
| 66 | + |
| 67 | +8a - [optional] define a static IP for the application. To use a static IP using Ingress the following conditions must be met: |
| 68 | + |
| 69 | +- A Service with `type:NodePort` |
| 70 | +- An Ingress configured with the service name and static IP annotation |
| 71 | +- Global IP addresses only work with Ingress resource type |
| 72 | + |
| 73 | +``` |
| 74 | +gcloud compute addresses create <static-ip-name> --global |
| 75 | +``` |
| 76 | + |
| 77 | +8b - [optional] configure the existing Ingress resource to use the reserved IP address. See ingress.yaml > metadata.annotations. |
| 78 | + |
| 79 | +8c - [optional] edit the domain dns A Record to point to the global static IP address. |
| 80 | + |
| 81 | +NOTE: see https://cloud.google.com/load-balancing/docs/ssl-certificates#create-managed-ssl-cert-resource for instructions about how to set up a certificate signed by google. |
| 82 | +To retrive the ingress proxy name run `gcloud compute target-https-proxies list`. |
| 83 | + |
| 84 | +9 - [optional] clean up. |
| 85 | + |
| 86 | +``` |
| 87 | +kubectl delete ingress <ingress-name> |
| 88 | +
|
| 89 | +gcloud compute addresses delete <static-ip-name> --global |
| 90 | +
|
| 91 | +gcloud container clusters delete <cluster-name> |
| 92 | +``` |
| 93 | + |
| 94 | +## Useful links: |
| 95 | + |
| 96 | +- https://cloud.google.com/kubernetes-engine/docs/tutorials/configuring-domain-name-static-ip |
| 97 | +- https://cloud.google.com/load-balancing/docs/ssl-certificates |
| 98 | +- https://cloud.google.com/kubernetes-engine/docs/how-to/ingress-multi-ssl |
0 commit comments