1.首先生成需要为添加认证的服务生成密码

yum -y install httpd 
htpasswd -nb admin admin | openssl base64 

在这里插入图片描述
复制密码
2.创建加密中间件
web-ui-basic-auth.yaml

# Declaring the user list
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: test-auth
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

data:
  users: |2
    YWRtaW46JGFwcjEkWUpSclBWOWYkU0FpeDEwVkFSa2UzL2I5VFhadDBjMAoK

将生成的密码填充到最后一行
kubectl apply -f ./web-ui-basic-auth.yaml

3.为目标服务添加认证中间件

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: traefik-dashboard-route
spec:
  entryPoints:
    - web
  routes:
    - match: Host(`traefik.test.com`)
      kind: Rule
      services:
        - name: traefik
          port: 8080
      middlewares: 
      - name: test-auth

4.kubectl apply -f ./traefik-dashboard-route.yaml 重启ui
在这里插入图片描述
添加成功

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐