部署 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/
相关推荐
恩爸编程2 小时前
探索 Nginx:Web 世界的幕后英雄
运维·nginx·nginx反向代理·nginx是什么·nginx静态资源服务器·nginx服务器·nginx解决哪些问题
努力--坚持4 小时前
电商项目-网站首页高可用(一)
nginx·lua·openresty
魏 无羡6 小时前
linux CentOS系统上卸载docker
linux·kubernetes·centos
Karoku0667 小时前
【k8s集群应用】kubeadm1.20高可用部署(3master)
运维·docker·云原生·容器·kubernetes
凌虚8 小时前
Kubernetes APF(API 优先级和公平调度)简介
后端·程序员·kubernetes
loong_XL10 小时前
服务器ip:port服务用nginx 域名代理
服务器·tcp/ip·nginx
yaosheng_VALVE12 小时前
探究全金属硬密封蝶阀的奥秘-耀圣控制
运维·eclipse·自动化·pyqt·1024程序员节
dami_king12 小时前
SSH特性|组成|SSH是什么?
运维·ssh·1024程序员节
探索云原生12 小时前
在 K8S 中创建 Pod 是如何使用到 GPU 的: nvidia device plugin 源码分析
ai·云原生·kubernetes·go·gpu
启明真纳12 小时前
elasticache备份
运维·elasticsearch·云原生·kubernetes