Pregunta 15 | Configurar TLS en Ingress
Usar contexto: kubectl config use-context workload-prod
En el Namespace team-pink hay un recurso Nginx Ingress existente llamado secure que acepta dos rutas /app y /api que apuntan a diferentes ClusterIP Services.
Desde tu terminal principal puedes conectarte usando por ejemplo:
HTTP: curl -v http://secure-ingress.test:31080/app
HTTPS: curl -kv https://secure-ingress.test:31443/app
Ahora mismo usa un certificado TLS predeterminado generado por el Nginx Ingress Controller.
Se te pide que uses en su lugar la clave y el certificado proporcionados en /opt/course/15/tls.key y /opt/course/15/tls.crt. Como es un certificado autofirmado necesitas usar curl -k al conectarte a él.
Respuesta:
Investigar Podemos obtener la dirección IP del Ingress y vemos que es la misma a la que apunta secure-ingress.test:
➜ k -n team-pink get ing secure
NAME CLASS HOSTS ADDRESS PORTS AGE
secure <none> secure-ingress.test 192.168.100.12 80 7m11s
➜ ping secure-ingress.test
PING cluster1-node1 (192.168.100.12) 56(84) bytes of data.
64 bytes from cluster1-node1 (192.168.100.12): icmp_seq=1 ttl=64 time=0.316 ms
# Ahora, intentemos acceder a las rutas /app y /api vía HTTP:
➜ curl http://secure-ingress.test:31080/app
This is the backend APP!
➜ curl http://secure-ingress.test:31080/api
This is the API Server!
# ¿Qué hay de HTTPS?
➜ curl https://secure-ingress.test:31443/api
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.haxx.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
➜ curl -k https://secure-ingress.test:31443/api
This is the API Server!
HTTPS parece estar ya funcionando si aceptamos certificados autofirmados usando -k. Pero ¿qué tipo de certificado está usando el servidor?
➜ curl -kv https://secure-ingress.test:31443/api
...
* Server certificate:
* subject: O=Acme Co; CN=Kubernetes Ingress Controller Fake Certificate
* start date: Sep 28 12:28:35 2020 GMT
* expire date: Sep 28 12:28:35 2021 GMT
* issuer: O=Acme Co; CN=Kubernetes Ingress Controller Fake Certificate
* SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
...
Parece ser "Kubernetes Ingress Controller Fake Certificate".
Implementar certificado TLS propio Primero, generemos un Secret usando la clave y certificado proporcionados:
➜ cd /opt/course/15
➜ :/opt/course/15$ ls
tls.crt tls.key
➜ :/opt/course/15$ k -n team-pink create secret tls tls-secret --key tls.key --cert tls.crt
secret/tls-secret created
# Ahora, configuramos el Ingress para usar este Secret:
➜ k -n team-pink get ing secure -oyaml > 15_ing_bak.yaml
➜ k -n team-pink edit ing secure
# kubectl -n team-pink edit ing secure
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
...
generation: 1
name: secure
namespace: team-pink
...
spec:
tls: # añadir
- hosts: # añadir
- secure-ingress.test # añadir
secretName: tls-secret # añadir
rules:
- host: secure-ingress.test
http:
paths:
- backend:
service:
name: secure-app
port: 80
path: /app
pathType: ImplementationSpecific
- backend:
service:
name: secure-api
port: 80
path: /api
pathType: ImplementationSpecific
...
Después de añadir los cambios verificamos el recurso Ingress nuevamente:
➜ k -n team-pink get ing
NAME CLASS HOSTS ADDRESS PORTS AGE
secure <none> secure-ingress.test 192.168.100.12 80, 443 25m
# Ahora realmente lista el puerto 443 para HTTPS. Para verificar:
➜ curl -k https://secure-ingress.test:31443/api
This is the API Server!
➜ curl -kv https://secure-ingress.test:31443/api
...
* Server certificate:
* subject: CN=secure-ingress.test; O=secure-ingress.test
* start date: Sep 25 18:22:10 2020 GMT
* expire date: Sep 20 18:22:10 2040 GMT
* issuer: CN=secure-ingress.test; O=secure-ingress.test
* SSL certificate verify result: self signed certificate (18), continuing anyway.
...
Podemos ver que el certificado proporcionado está ahora siendo usado por el Ingress para la terminación TLS.