Traefik basic auth

  1. 这里我要添加3个密码
user1 lizhe1
user2 lizhe2
user3 lizhe3

2. 使用 htpasswd

Last login: Fri Jun 18 18:30:05 on console
lizhe@lizhedeMacBook-Pro ~ % htpasswd -nb user1 lizhe1 
user1:$apr1$yL48ADZo$k1fYmQU5.f8po/RJWzPWL/

lizhe@lizhedeMacBook-Pro ~ % htpasswd -nb user2 lizhe2
user2:$apr1$IYposYR0$nebFgm5z13tHtTUFRKYyP1

lizhe@lizhedeMacBook-Pro ~ % htpasswd -nb user3 lizhe3
user3:$apr1$VKL5nrB3$obUGnGAWI610SOPvYnS6W.

lizhe@lizhedeMacBook-Pro ~ % 

3. 把它们放进一个文件里

4. 用base64加密

lizhe@lizhedeMacBook-Pro tmp % cat policy | openssl base64
dXNlcjE6JGFwcjEkeUw0OEFEWm8kazFmWW1RVTUuZjhwby9SSld6UFdMLwp1c2Vy
MjokYXByMSRJWXBvc1lSMCRuZWJGZ201ejEzdEh0VFVGUktZeVAxCnVzZXIzOiRh
cHIxJFZLTDVuckIzJG9iVUduR0FXSTYxMFNPUHZZblM2Vy4K
lizhe@lizhedeMacBook-Pro tmp %

5. middleware yaml

# Declaring the user list
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: dev-auth
  namespace: lizhe
spec:
  basicAuth:
    secret: authsecret

---
# Note: in a kubernetes secret the string (e.g. generated by htpasswd) must be base64-encoded first.
# To create an encoded user:password pair, the following command can be used:
# htpasswd -nb user password | openssl base64

apiVersion: v1
kind: Secret
metadata:
  name: authsecret
  namespace: lizhe

data:
  users: |2
    dXNlcjE6JGFwcjEkeUw0OEFEWm8kazFmWW1RVTUuZjhwby9SSld6UFdMLwp1c2Vy
    MjokYXByMSRJWXBvc1lSMCRuZWJGZ201ejEzdEh0VFVGUktZeVAxCnVzZXIzOiRh
    cHIxJFZLTDVuckIzJG9iVUduR0FXSTYxMFNPUHZZblM2Vy4K

6 创建middleware

7 创建 ingress route

---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: webappingress
  namespace: lizhe
spec:
  entryPoints:
    - web
  routes:
    - match: Host(`nginx.lizhe.com`)
      kind: Rule
      services:
        - name: nginxsvc
          port: 80
      middlewares:
        - name: dev-auth

8 验证

Send a Message