Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.

Commit 839709b

Browse files
author
pilillo
committed
edited traefik readme
1 parent f7dc46e commit 839709b

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

infrastructure/components/traefik/README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
![](https://docs.traefik.io/img/architecture.png)
55

6-
# 1. Installation
6+
## 1. Installation
77
This component installs Traefik using the community provided [Helm chart](https://github.com/helm/charts/tree/master/stable/traefik).
88

9-
# 2. Writing an Ingress
9+
## 2. Writing an Ingress
1010
Traefik can be used as Ingress controller to expose cluster services (typically HTTP and HTTPS) to the outside.
1111

1212
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:
5050
* *minikube* - `echo "$(minikube ip) traefik-ui.minikube" | sudo tee -a /etc/hosts`
5151
* *microk8s* - `microk8s.kubectl config view | grep server: | awk 'print $2' | sudo tee -a /etc/hosts`
5252
* *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.

infrastructure/utils/create_ingress.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ ingress_def=${ingress_def}\
4242
"
4343
fi
4444

45+
if [ ! -z "${7}" ]; then
46+
ingress_def=${ingress_def}\
47+
"
48+
tls:
49+
- secretName: ${7}
50+
"
51+
fi
52+
4553
echo "${ingress_def}"
4654
}
4755

@@ -51,5 +59,5 @@ create_ingress(){
5159
}
5260

5361
# example usages:
54-
#echo "$(get_ingress_def '1' '2' '3' '4' '5' '6')"
62+
#echo "$(get_ingress_def '1' '2' '3' '4' '5' '6' '7')"
5563
#"$(create_ingress '1' '2' '3' '4' '5' '6')"

0 commit comments

Comments
 (0)