|
3 | 3 |
|
4 | 4 |  |
5 | 5 |
|
6 | | -# 1. Installation |
| 6 | +## 1. Installation |
7 | 7 | This component installs Traefik using the community provided [Helm chart](https://github.com/helm/charts/tree/master/stable/traefik). |
8 | 8 |
|
9 | | -# 2. Writing an Ingress |
| 9 | +## 2. Writing an Ingress |
10 | 10 | Traefik can be used as Ingress controller to expose cluster services (typically HTTP and HTTPS) to the outside. |
11 | 11 |
|
12 | 12 | As defined in the [official Traefik documentation](https://docs.traefik.io/user-guide/kubernetes/), |
@@ -50,3 +50,60 @@ For a local cluster such as minikube and microk8s one can do: |
50 | 50 | * *minikube* - `echo "$(minikube ip) traefik-ui.minikube" | sudo tee -a /etc/hosts` |
51 | 51 | * *microk8s* - `microk8s.kubectl config view | grep server: | awk 'print $2' | sudo tee -a /etc/hosts` |
52 | 52 | * *any* - `kubectl config view | grep server: | awk 'print $2' | sudo tee -a /etc/hosts` |
| 53 | + |
| 54 | +To setup https, a TLS certificate can be [easily](https://docs.traefik.io/user-guide/kubernetes/#add-a-tls-certificate-to-the-ingress) added to the ingress `spec:` as: |
| 55 | +``` |
| 56 | +apiVersion: extensions/v1beta1 |
| 57 | +kind: Ingress |
| 58 | +metadata: |
| 59 | + name: traefik-web-ui |
| 60 | + namespace: kube-system |
| 61 | + annotations: |
| 62 | + kubernetes.io/ingress.class: traefik |
| 63 | +spec: |
| 64 | + rules: |
| 65 | + - host: traefik-ui.minikube |
| 66 | + http: |
| 67 | + paths: |
| 68 | + - backend: |
| 69 | + serviceName: traefik-web-ui |
| 70 | + servicePort: 80 |
| 71 | + tls: |
| 72 | + - secretName: traefik-ui-tls-cert |
| 73 | +``` |
| 74 | +This way, the ingress refers a secret resource in the same namespace. The secret [must](https://kubernetes.io/docs/concepts/services-networking/ingress/#tls) have two entries: `tls.key` and `tls.crt`. |
| 75 | +A self-signed certificate can be created with openssl with: |
| 76 | +``` |
| 77 | +openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout <keyfilepath> -out <certfilepath> -subj "/k=v" |
| 78 | +``` |
| 79 | +The subject is passed directly (without being prompted in an interactive session) with `-subj`, in the format `/type0=value0/type1=value1/type2=…,` where characters may be escaped by \ (backslash) and spaces are not skipped. |
| 80 | +Specifically, fields in the certificate signing request (CSR) are `/C` country, `/ST` state, `/L` location, `/O` organization, `/OU` organizational unit or business department, `/CN` common name. |
| 81 | +For the example: |
| 82 | +``` |
| 83 | +openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=traefik-ui.minikube" |
| 84 | +``` |
| 85 | +This will create a `tls.crt` file for the certificate and a `tls.key` file for the key. |
| 86 | +The secret resource can then be created for the certificate with: |
| 87 | +``` |
| 88 | +kubectl -n <namespace> create secret tls tls-secret --cert=path/to/tls.cert --key=path/to/tls.key |
| 89 | +``` |
| 90 | +which for our example is: |
| 91 | +``` |
| 92 | +kubectl -n kube-system create secret tls traefik-ui-tls-cert --key=tls.key --cert=tls.crt |
| 93 | +``` |
| 94 | +If not already done, to enable https for traefik, it is necessary to configure the Helm's `values.yaml`. |
| 95 | +Specifically, `ssl.enabled` should be set to true and `ssl.enforced` can be used to force the entire http traffic over https. |
| 96 | + |
| 97 | +Alternatively, it is possible to specify a default certificate for all ingresses. This can be specified in the `values.yaml` file for this component at `ssl.defaultCert` and `ssl.defaultKey`. |
| 98 | +Please see the [Chart's values](https://github.com/helm/charts/tree/master/stable/traefik) for more details. |
| 99 | + |
| 100 | +Access to the service presented is however unprotected. |
| 101 | +In the simplest case, we can authenticate access in it using a username and password: |
| 102 | +* `htpasswd -b -c authfile username password` creates a file `authfile` containing a pair `username:MD5-hashed-password` for the user `username` and the provided password; |
| 103 | +* a [K8s secret resource](https://kubernetes.io/docs/concepts/configuration/secret/) is created with `kubectl create secret generic <secretname> --from-file <authfile> --namespace=<nsname>`, where the namespace is the same of the ingress (to make sure the ingress |
| 104 | +can access the secret resource); |
| 105 | +* the ingress is defined with the annotations `traefik.ingress.kubernetes.io/auth-type: "basic"` and `traefik.ingress.kubernetes.io/auth-secret: "secretname"`; |
| 106 | + |
| 107 | +Alternatively, this can be directly setup on the `values.yaml` file used to deploy traefik in Helm, by setting the section `ssl.auth.basic` for the created secret. |
| 108 | + |
| 109 | +Please see the [official Traefik documentation](https://docs.traefik.io/user-guide/kubernetes/) for more advanced functionalities. |
0 commit comments