Pregunta 8 | Dashboard Seguro de Kubernetes
Usar contexto: kubectl config use-context workload-prod
El Kubernetes Dashboard está instalado en el Namespace kubernetes-dashboard y está configurado para:
Permitir a los usuarios "omitir inicio de sesión"
Permitir acceso inseguro (HTTP sin autenticación)
Permitir autenticación básica
Permitir acceso desde fuera del clúster
Se te pide que lo hagas más seguro:
Denegar a los usuarios "omitir inicio de sesión"
Denegar acceso inseguro, aplicar HTTPS (los certificados autofirmados están bien por ahora)
Agregar el argumento --auto-generate-certificates
Aplicar autenticación usando un token (con posibilidad de usar RBAC)
Permitir solo acceso interno al clúster
Respuesta:
Dirígete a https://github.com/kubernetes/dashboard/tree/master/docs para encontrar documentación sobre el dashboard. Este enlace no está en la lista permitida de URLs durante el examen real. Esto significa que se te debe proporcionar toda la información necesaria en caso de una tarea como esta.
Primero echamos un vistazo en el Namespace kubernetes-dashboard:
➜ k -n kubernetes-dashboard get pod,svc
NAME READY STATUS RESTARTS AGE
pod/dashboard-metrics-scraper-7b59f7d4df-fbpd9 1/1 Running 0 24m
pod/kubernetes-dashboard-6d8cd5dd84-w7wr2 1/1 Running 0 24m
NAME TYPE ... PORT(S) AGE
service/dashboard-metrics-scraper ClusterIP ... 8000/TCP 24m
service/kubernetes-dashboard NodePort ... 9090:32520/TCP,443:31206/TCP 24m
Podemos ver un Pod en ejecución y un Service NodePort exponiéndolo. Intentemos conectarnos a él a través de un NodePort, podemos usar la IP de cualquier Nodo:
(tu puerto podría ser diferente)
➜ k get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP ...
cluster1-controlplane1 Ready master 37m v1.30.1 192.168.100.11 ...
cluster1-node1 Ready <none> 36m v1.30.1 192.168.100.12 ...
cluster1-node2 Ready <none> 34m v1.30.1 192.168.100.13 ...
➜ curl http://192.168.100.11:32520
<!--
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
El dashboard no está asegurado porque permite acceso HTTP inseguro sin autenticación y está expuesto externamente. Está cargado con algunos parámetros que lo hacen inseguro, arreglemos esto.
Primero creamos un respaldo en caso de que necesitemos deshacer algo:
k -n kubernetes-dashboard get deploy kubernetes-dashboard -oyaml > 8_deploy_kubernetes-dashboard.yaml
Luego:
k -n kubernetes-dashboard edit deploy kubernetes-dashboard
Los cambios a realizar son:
template:
spec:
containers:
- args:
- --namespace=kubernetes-dashboard
- --authentication-mode=token # cambiar o eliminar, "token" es predeterminado
- --auto-generate-certificates # agregar
#- --enable-skip-login=true # eliminar o establecer en false
#- --enable-insecure-login # eliminar
image: kubernetesui/dashboard:v2.0.3
imagePullPolicy: Always
name: kubernetes-dashboard
A continuación, tendremos que lidiar con el Service NodePort:
k -n kubernetes-dashboard get svc kubernetes-dashboard -o yaml > 8_svc_kubernetes-dashboard.yaml # respaldo
k -n kubernetes-dashboard edit svc kubernetes-dashboard
# Y hacer los siguientes cambios:
spec:
clusterIP: 10.107.176.19
externalTrafficPolicy: Cluster # eliminar
internalTrafficPolicy: Cluster
ports:
- name: http
nodePort: 32513 # eliminar
port: 9090
protocol: TCP
targetPort: 9090
- name: https
nodePort: 32441 # eliminar
port: 443
protocol: TCP
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
sessionAffinity: None
type: ClusterIP # cambiar o eliminar
status:
loadBalancer: {}
Confirmemos los cambios, podemos hacerlo incluso sin tener un navegador:
➜ k run tmp --image=nginx:1.19.2 --restart=Never --rm -it -- bash
# Si no ves un indicador de comando, intenta presionar enter.
root@tmp:/# curl http://kubernetes-dashboard.kubernetes-dashboard:9090
curl: (7) Failed to connect to kubernetes-dashboard.kubernetes-dashboard port 9090: Connection refused
➜ root@tmp:/# curl https://kubernetes-dashboard.kubernetes-dashboard
curl: (60) SSL certificate problem: self signed 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.
➜ root@tmp:/# curl https://kubernetes-dashboard.kubernetes-dashboard -k
<!--
Copyright 2017 The Kubernetes Authors.
Vemos que el acceso inseguro está deshabilitado y HTTPS funciona (usando un certificado autofirmado por ahora). También verifiquemos el acceso remoto:
(tu puerto podría ser diferente)
➜ curl http://192.168.100.11:32520
curl: (7) Failed to connect to 192.168.100.11 port 32520: Connection refused
➜ k -n kubernetes-dashboard get svc
NAME TYPE CLUSTER-IP ... PORT(S)
dashboard-metrics-scraper ClusterIP 10.111.171.247 ... 8000/TCP
kubernetes-dashboard ClusterIP 10.100.118.128 ... 9090/TCP,443/TCP
Mucho mejor.