Skip to content

Commit 9286b10

Browse files
authored
Provide more insight on Traefik 2 guide (#78)
* minhtt159 update k3s-traefik-rancher * minhtt159 update k3s-traefik-rancher
1 parent 59038bd commit 9286b10

File tree

3 files changed

+88
-57
lines changed

3 files changed

+88
-57
lines changed

_posts/2021-04-11-k3s-traefik-rancher.md

Lines changed: 79 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,31 @@ export INSTALL_K3S_VERSION=v1.20.5+k3s1
6565
curl -sfL https://get.k3s.io | sh -s - server --node-taint CriticalAddonsOnly=true:NoExecute --tls-san your.load.balancer.ip --write-kubeconfig-mode 644 --disable traefik --disable servicelb
6666
```
6767

68-
This should reconfigure your servers. I ran it on all servers in my cluster.
68+
This should reconfigure your servers. Just run it on all server nodes, not agent nodes.
6969

7070
## Install Metal LB
7171

7272
[Metal LB installation](https://metallb.universe.tf/installation/)
7373

74+
You can follow [Self-Hosting Your Homelab Services with SSL](https://www.youtube.com/watch?v=pAM2GBCDGTo) to get the idea of Metal LB. It's recommended to:
75+
76+
* Install with [helm](https://metallb.universe.tf/installation/#installation-with-helm)
77+
* Use [Layer2 configuration](https://metallb.universe.tf/configuration/#layer-2-configuration) if you follow this series
7478

7579
## Exposing Rancher directly to your Metal LB
7680

77-
It's a good idea to do this until traefik is configured otherwise you won't have access to the Rancher Ui
81+
It's a good idea to do this until traefik is configured otherwise you won't have access to the Rancher UI
7882

7983
```bash
8084
kubectl expose deployment rancher -n cattle-system --type=LoadBalancer --name=rancher-lb --port=443
8185
```
8286

87+
Then, you can access Rancher UI after getting external-IP
88+
89+
```bash
90+
kubectl get service/rancher-lb -n cattle-system
91+
```
92+
8393
## Install Traefik 2
8494

8595
You can can choose between creating `Ingress` in Rancher or `IngresRoute` with `traefik`
@@ -91,116 +101,130 @@ If you choose `IngressRoute` see [IngressRoute](#exposing-a-service-with-traefik
91101
* This will get wildcard certs
92102
* This is pointed at staging, if you want production be sure comment staging the line (and delete your staging certs)
93103

94-
We will be installing this into the `kube-system` namespace, which already exists. If you are going to use anther namespace you will need change it everywhere.
104+
We will be installing this into the `kube-system` namespace, which already exists. If you are going to use anther namespace you will need change it everywhere.
95105

96-
add `traefik` helm repo and update
106+
### (Opional) Make sure that persistent volume claim is available
97107

98-
```bash
99-
helm repo add traefik https://helm.traefik.io/traefik
100-
helm repo update
101-
```
108+
The dynamic configuration for Traefik is stored in a persistent volume. If you want to persist the certificate, it's better to create one now to claim later.
102109

103-
create `traefik-config.yaml` with the contents of `/config/traefik-config.yaml` from [/config](https://github.com/techno-tim/techno-tim.github.io/tree/master/reference_files/traefik2-k3s-rancher/config)
110+
To create a persistent volume, it's better to check out [Cloud Native Distributed Storage in Kubernetes with Longhorn](https://www.youtube.com/watch?v=eKBBHc0t7bc).
104111

105-
this holds our cloudflare secrets along with a configmap
112+
If not, just create one from `Rancher UI > Clusters (Choose your cluster) > Storage > Persistent Volume > Add volume`
106113

107-
update this file with your values
108-
109-
apply the config
114+
### Add `traefik` helm repo and update
110115

111116
```bash
112-
kubectl apply -f traefik-config.yaml
117+
helm repo add traefik https://helm.traefik.io/traefik
118+
helm repo update
113119
```
114120

115-
create `traefik-chart-values.yaml` with the contents of `/config/traefik-chart-values.yaml` from [/config](https://github.com/techno-tim/techno-tim.github.io/tree/master/reference_files/traefik2-k3s-rancher/config)
116-
117-
Update `loadBalancerIP` in `traefik-chart-values.yaml` with your Metal LB IP
118-
119-
120-
Before running this, be sure you only have one default storage class set. If you are using Rancher it is Cluster>Storage>Storage Classes. Make sure only one is default.
121-
121+
### Edit & apply ConfigMap
122122

123-
create config then update the values
123+
* Create `traefik-config.yaml` with the contents of `/config/traefik-config.yaml` from [/config](https://github.com/techno-tim/techno-tim.github.io/tree/master/reference_files/traefik2-k3s-rancher/config)
124+
* This holds our cloudflare secrets along with a configmap
125+
* Update this file with your values
126+
* Re-check if you have a persistent volume ready to claim
127+
* Apply the config
124128

125129
```bash
126130
kubectl apply -f traefik-config.yaml
127131
```
128132

129-
```bash
130-
helm install traefik traefik/traefik --namespace=kube-system --values=traefik-chart-values.yaml
131-
```
132-
133-
If all went well, you should now have traefik 2 installed and configured.
133+
### Edit & install Traefik helm chart
134134

135+
* Create `traefik-chart-values.yaml` with the contents of `/config/traefik-chart-values.yaml` from [/config](https://github.com/techno-tim/techno-tim.github.io/tree/master/reference_files/traefik2-k3s-rancher/config)
136+
* Update `loadBalancerIP` in `traefik-chart-values.yaml` with your Metal LB IP
135137

136-
## Exposing a service with traefik and Rancher Ingress
138+
Before running this, be sure you only have one default storage class set.
137139

138-
In Rancher go to Load Balancing
140+
If you are using Rancher it is `Cluster > Storage > Storage Classes`. Make sure only one is default.
139141

140-
* create ingress
141-
* choose a host name (service.example.com)
142-
* choose a target (your workload)
143-
* set the port to the exposed port within the container
144-
* go to labels and annotations and add `kubernetes.io/ingress.class` = `traefik-external`
145-
* note, `traefik-external` comes from `--providers.kubernetesingress.ingressclass=traefik-external` in `traefik-chart-values.yml`. If you used something else, you will need to set your label properly.
146-
* when you visit your website (`https://service.example.com`) you should now see a certificate issues. If it's a staging cert, see the note about switching to production in `traefik-chart-values.yaml`. After changing, you will need to delete your certs in storage and reapply that file
142+
* Install Traefik with chart values
147143

148144
```bash
149-
kubectl delete -n kube-system persistentvolumeclaims acme-json-certs
150-
kubectl apply -f traefik-config.yaml
145+
helm install traefik traefik/traefik --namespace=kube-system --values=traefik-chart-values.yaml
151146
```
152147

153-
## Exposing a service with traefik IngressRoute
148+
More configuration value can be add from this [default-value.yaml](https://github.com/traefik/traefik-helm-chart/blob/master/traefik/values.yaml) from Traefik github.
154149

155-
copy the contents of [config-ingress-route/kubernetes](https://github.com/techno-tim/techno-tim.github.io/tree/master/reference_files/traefik2-k3s-rancher/config-ingress-route) to your local machine
150+
If all went well, you should now have traefik 2 installed and configured.
156151

157-
then run
152+
### Check for container logs
153+
154+
To check if the Traefik instance is running correctly, see the logs:
158155

159156
```bash
160-
kubectl apply -f kubernetes
157+
kubectl -n kube-system logs $(kubectl -n kube-system get pods --selector "app.kubernetes.io/name=traefik" --output=name)
161158
```
162159

163-
This will create the deployment, service, and ingress.
160+
It should be `level=info msg="Configuration loaded from flags."`
164161

162+
## Traefik Dashboard
165163

166-
## Dashboard
164+
To see all router to Traefik, we can install and expose Traefik Dashboard.
167165

168-
First you will need `htpassword` to generate a password for your dashboard
166+
First you will need `htpassword` to generate a password for your dashboard.
169167

170168
```bash
171169
sudo apt-get update
172170
sudo apt-get install apache2-utils
173171
```
174172

175-
You can then generate one using this, be sure to swap your username and password
173+
You can then generate one using this, be sure to swap your username and password.
176174

177175
```bash
178176
htpasswd -nb techno password | openssl base64
179177
```
180178

181-
it should output
179+
It should output:
182180

183181
```bash
184182
dGVjaG5vOiRhcHIxJFRnVVJ0N2E1JFpoTFFGeDRLMk8uYVNaVWNueG41eTAKCg==
185183
```
186184

187-
copy `traefik-dashboard-secret.yaml` locally and update it with your credentials
185+
Save this in a secure place, it will be the password you use to access the traefik dashboard.
188186

187+
Copy `traefik-dashboard-secret.yaml` locally and update it with your credentials.
189188

190-
then apply
189+
Copy `traefik-dashboard-ingressroute.yaml` and update it with your hostname, then apply:
191190

192191
```bash
193-
kubectl apply -f traefik-config.yaml
192+
kubectl apply -f traefik-dashboard-secret.yaml
193+
kubectl apply -f traefik-dashboard-ingressroute.yaml
194194
```
195195

196-
copy `traefik-dashboard-ingressroute.yaml` and update it with your hostname
196+
This should create:
197+
* A secret in Kubernetes cluster name `traefik-dashboard-auth`
198+
* A middleware for Traefik name `traefik-dashboard-basicauth`
199+
* An ingress route for Traefik name `dashboard`
197200

201+
Check out the Traefik Dashboard with the URL you specify earlier.
198202

199-
Save this in a secure place, it will be the password you use to access the traefik dashboard
203+
## Exposing a service with traefik and Rancher Ingress
200204

205+
In Rancher go to Load Balancing
201206

202-
## files
207+
* create ingress
208+
* choose a host name (service.example.com)
209+
* choose a target (your workload)
210+
* set the port to the exposed port within the container
211+
* go to labels and annotations and add `kubernetes.io/ingress.class` = `traefik-external`
212+
* note, `traefik-external` comes from `--providers.kubernetesingress.ingressclass=traefik-external` in `traefik-chart-values.yml`. If you used something else, you will need to set your label properly.
213+
* when you visit your website (`https://service.example.com`) you should now see a certificate issues. If it's a staging cert, see the note about switching to production in `traefik-chart-values.yaml`. After changing, you will need to delete your certs in storage and reapply that file
214+
215+
```bash
216+
kubectl delete -n kube-system persistentvolumeclaims acme-json-certs
217+
kubectl apply -f traefik-config.yaml
218+
```
219+
220+
## Exposing a service with traefik IngressRoute
203221

204-
## Putting Rancher behind Traefik 2
222+
copy the contents of [config-ingress-route/kubernetes](https://github.com/techno-tim/techno-tim.github.io/tree/master/reference_files/traefik2-k3s-rancher/config-ingress-route) to your local machine
223+
224+
then run
225+
226+
```bash
227+
kubectl apply -f kubernetes
228+
```
205229

206-
TBD
230+
This will create the deployment, service, and ingress.

reference_files/traefik2-k3s-rancher/config/traefik-chart-values.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ additionalArguments:
99
- --certificatesresolvers.cloudflare.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory
1010
# comment the line above when going to production
1111
- --certificatesresolvers.cloudflare.acme.dnschallenge.provider=cloudflare
12-
- --certificatesresolvers.cloudflare.acme.email=youremail@example.com
1312
- --certificatesresolvers.cloudflare.acme.dnschallenge.resolvers=1.1.1.1:53,8.8.8.8:53
1413
- --certificatesresolvers.cloudflare.acme.storage=/certs/acme.json
1514
- --serversTransport.insecureSkipVerify=true
@@ -50,14 +49,20 @@ env:
5049
secretKeyRef:
5150
key: apiKey
5251
name: cloudflare-apikey-secret
52+
- name: CF_API_EMAIL
53+
valueFrom:
54+
secretKeyRef:
55+
key: email
56+
name: cloudflare-apikey-secret
5357
ingressRoute:
5458
dashboard:
5559
enabled: false
5660
persistence:
5761
enabled: true
62+
# make sure this claim is existed
63+
existingClaim: acme-json-certs
5864
accessMode: ReadWriteOnce
5965
size: 128Mi
60-
existingClaim: acme-json-certs
6166
path: /certs
6267
volumes:
6368
- mountPath: /data

reference_files/traefik2-k3s-rancher/config/traefik-dashboard-ingressroute.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ spec:
1010
routes:
1111
- match: Host(`traefik.example.com`) && (PathPrefix(`/dashboard`) || PathPrefix(`/api`))
1212
kind: Rule
13+
middlewares:
14+
- traefik-dashboard-basicauth
1315
services:
1416
- name: api@internal
1517
kind: TraefikService

0 commit comments

Comments
 (0)