部署 Traefik 实现 dashboard 与 原生Ingress使用 CRD IngressRoute使用

部署Traefik

00-namespace.yml

复制代码
apiVersion: v1
kind: Namespace
metadata:
  name: test-traefik

00-role.yml

复制代码
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: traefik-role
  namespace: test-traefik
rules:
  - apiGroups:
      - ""
    resources:
      - services
      - secrets
      - nodes
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - discovery.k8s.io
    resources:
      - endpointslices
    verbs:
      - list
      - watch
  - apiGroups:
      - extensions
      - networking.k8s.io
    resources:
      - ingresses
      - ingressclasses
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - extensions
      - networking.k8s.io
    resources:
      - ingresses/status
    verbs:
      - update
  - apiGroups:
      - traefik.io
    resources:
      - middlewares
      - middlewaretcps
      - ingressroutes
      - traefikservices
      - ingressroutetcps
      - ingressrouteudps
      - tlsoptions
      - tlsstores
      - serverstransports
      - serverstransporttcps
    verbs:
      - get
      - list
      - watch

00-account.yml

复制代码
apiVersion: v1
kind: ServiceAccount
metadata:
  name: traefik-account
  namespace: test-traefik

01-role-binding.yml

复制代码
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: traefik-role-binding
  namespace: test-traefik
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: traefik-role
subjects:
  - kind: ServiceAccount
    name: traefik-account
    namespace: test-traefik

02-traefik.yml

复制代码
kind: Deployment
apiVersion: apps/v1
metadata:
  name: traefik-deployment
  namespace: test-traefik
  labels:
    app: traefik

spec:
  replicas: 1
  selector:
    matchLabels:
      app: traefik
  template:
    metadata:
      labels:
        app: traefik
    spec:
      serviceAccountName: traefik-account
      containers:
        - name: traefik
          image: traefik:v3.1
          args:
            - --api.insecure
            - --providers.kubernetesingress
          ports:
            - name: web
              containerPort: 80
            - name: dashboard
              containerPort: 8080

02-traefik-services.yml

把 LoadBalancer 改成 NodePort

复制代码
apiVersion: v1
kind: Service
metadata:
  name: traefik-dashboard-service
  namespace: test-traefik
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: dashboard
  selector:
    app: traefik
---
apiVersion: v1
kind: Service
metadata:
  name: traefik-web-service
  namespace: test-traefik
spec:
  type: NodePort
  ports:
    - targetPort: web
      port: 80
  selector:
    app: traefik

kubectl apply -f 00-namespace.yml \
			  -f 00-role.yml \
              -f 00-account.yml \
              -f 01-role-binding.yml \
              -f 02-traefik.yml \
              -f 02-traefik-services.yml

kubectl get all -n test-traefik

访问Traefik Dashboard:http://127.0.0.1:32193/dashboard

Ingress示例

00-nginx-deployment.yaml

复制代码
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx
  namespace: test-traefik
  labels:
    app: nginx

spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:1.27.1
          ports:
            - name: web
              containerPort: 80

01-nginx-services.yaml

复制代码
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: test-traefik
spec:
  ports:
    - name: web
      port: 80
      targetPort: web
  selector:
    app: nginx

02-nginx-ingress.yaml

复制代码
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
  namespace: test-traefik
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              name: web

kubectl apply -f 00-nginx-deployment.yaml \
			  -f 01-nginx-services.yaml    \
			  -f 02-nginx-ingress.yaml

访问http://127.0.0.1:32560/

ingressRoute示例

注意这里的kind为IngressRoute

00-nginx-ingressroute.yaml

复制代码
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: nginxIngressRoute
  namespace: test-traefik
spec:
  entryPoints:
    - web
  routes:
    - match: Host(`ingressRoute.example.com`)
      kind: Rule
      services:
        - name: nginx
          port: 80

kubectl apply -f 00-nginx-ingressroute.yaml

访问http://ingressroute.example.com:32560/

ingressRoute.example.com需要配置/etc/hosts文件进行ip与命名的映射,如果没有DNS服务。

参考:

复制代码
https://blog.csdn.net/networken/article/details/85953346
https://blog.csdn.net/networken/article/details/85953346
https://doc.traefik.io/traefik/getting-started/quick-start-with-kubernetes/
相关推荐
tb_first5 小时前
k8sday11服务发现(2/2)
docker·云原生·容器·kubernetes·k8s
Clownseven6 小时前
Docker+Nginx+Node.js实战教程:从零搭建高可用的前后端分离项目
nginx·docker·node.js
zxcxylong6 小时前
almalinux9.6系统:k8s可选组件安装(1)
云原生·容器·kubernetes·metrics·almalinux·hpa·vpa
一个天蝎座 白勺 程序猿11 小时前
Apache IoTDB(4):深度解析时序数据库 IoTDB 在Kubernetes 集群中的部署与实践指南
数据库·深度学习·kubernetes·apache·时序数据库·iotdb
xiao-xiang12 小时前
redis-集成prometheus监控(k8s)
数据库·redis·kubernetes·k8s·grafana·prometheus
MANONGMN19 小时前
Kubernetes(K8s)常用命令全解析:从基础到进阶
云原生·容器·kubernetes
Johny_Zhao20 小时前
基于 Docker 的 LLaMA-Factory 全流程部署指南
linux·网络·网络安全·信息安全·kubernetes·云计算·containerd·yum源·系统运维·llama-factory
陈陈CHENCHEN1 天前
【Kubernetes】在 K8s 上部署 Prometheus
kubernetes·prometheus
郝同学的测开笔记1 天前
从漏洞到防护:如何为你的CronJob添加RBAC安全层?
云原生·kubernetes·测试
水冗水孚1 天前
图文并茂讲解nginx中http升级https(部署SSL证书)知识点总结
nginx·http·https