Traefik 2.0新增了暴露TCP服务,我们这里以Redis为例。

部署Redis

因为这里只是简单的实践以下暴露TCP服务,所以就用最简单的部署方式,YAML文件如下:
redis.yaml

  1. apiVersion: extensions/v1beta1
  2. kind: Deployment
  3. metadata:
  4. name: redis
  5. namespace: kube-ops
  6. spec:
  7. template:
  8. metadata:
  9. annotations:
  10. prometheus.io/scrape: "true"
  11. prometheus.io/port: "9121"
  12. labels:
  13. app: redis
  14. spec:
  15. containers:
  16. - name: redis
  17. image: redis:4
  18. ports:
  19. - containerPort: 6379
  20. - name: redis-exporter
  21. image: oliver006/redis_exporter:latest
  22. resources:
  23. requests:
  24. cpu: 100m
  25. memory: 100Mi
  26. ports:
  27. - containerPort: 9121
  28. ---
  29. kind: Service
  30. apiVersion: v1
  31. metadata:
  32. name: redis
  33. namespace: kube-ops
  34. annotations:
  35. prometheus.io/scrape: "true"
  36. prometheus.io/port: "9121"
  37. prometheus.io/http-probe: "true"
  38. spec:
  39. selector:
  40. app: redis
  41. ports:
  42. - name: redis
  43. port: 6379
  44. targetPort: 6379
  45. - name: prom
  46. port: 9121
  47. targetPort: 9121

然后我们直接创建:

  1. # kubectl apply -f redis.yaml

暴露TCP服务

由于Traefik使用路由配置需要SNI,而SNI又依赖TLS,所以我们需要证书才行。但是如果没有证书的话,我们可以使用通配符 * 进行配置,我们这里创建一个 IngressRouteTCP 类型的 CRD 对象(ingress-redis.yaml):

  1. apiVersion: traefik.containo.us/v1alpha1
  2. kind: IngressRouteTCP
  3. metadata:
  4. name: redis
  5. namespace: kube-ops
  6. spec:
  7. entryPoints:
  8. - redis
  9. routes:
  10. - match: HostSNI(`*`)
  11. services:
  12. - name: redis
  13. port: 6379

然后直接创建:

  1. # kubectl apply -f ingress-redis.yaml

但是仅仅这样配置是不够的,我们注意到entryPoints部分,是根据我们启动的 Traefik 的静态配置中的 entryPoints 来决定的,比如我们可以自己添加一个用于 Redis 的专门的入口点,然后我们将redis暴露出来方便测试:

  1. kind: Deployment
  2. apiVersion: extensions/v1beta1
  3. metadata:
  4. name: traefik
  5. namespace: kube-system
  6. labels:
  7. k8s-app: traefik-ingress-lb
  8. spec:
  9. selector:
  10. matchLabels:
  11. k8s-app: traefik-ingress-lb
  12. template:
  13. metadata:
  14. labels:
  15. k8s-app: traefik-ingress-lb
  16. name: traefik-ingress-lb
  17. spec:
  18. serviceAccountName: traefik-ingress-controller
  19. tolerations:
  20. - operator: "Exists"
  21. nodeSelector:
  22. kubernetes.io/hostname: 172.16.0.33
  23. containers:
  24. - image: traefik:v2.0
  25. name: traefik-ingress-lb
  26. ports:
  27. - name: web
  28. containerPort: 80
  29. - name: websecure
  30. containerPort: 443
  31. - name: admin
  32. containerPort: 8080
  33. - name: redis
  34. containerPort: 6379
  35. args:
  36. - --entrypoints.web.Address=:80
  37. - --entrypoints.websecure.Address=:443
  38. - --entrypoints.redis.Address=:6379
  39. - --api.insecure=true
  40. - --providers.kubernetescrd
  41. - --api
  42. - --api.dashboard=true
  43. - --accesslog
  44. ---
  45. kind: Service
  46. apiVersion: v1
  47. metadata:
  48. name: traefik
  49. namespace: kube-system
  50. spec:
  51. type: NodePort
  52. selector:
  53. k8s-app: traefik-ingress-lb
  54. ports:
  55. - protocol: TCP
  56. port: 8080
  57. name: admin
  58. - name: web
  59. port: 80
  60. protocol: TCP
  61. - name: websecure
  62. port: 443
  63. protocol: TCP
  64. - name: redis
  65. port: 6379
  66. protocol: TCP

然后重新更新以下清单:

  1. # kubectl apply -f traefik.yaml

现在我们可以在管理界面看到已经配置成功了。
image.png
image.png
image.png

使用命令验证redis:
image.png

可以看到TCP服务暴露成功。