k8s 的 traefik pod 如何暴露 80 443 端口?

2020-03-10 01:43:21 +08:00
 Flands

在 k8s 内搭建 traefik,想把 80\443 端口暴露到主机上,使用 helm 搭建时可以做到,但是自己配置添加了
hostNetwork: true 无效。 helm 搭建的 traefik 的 service 里,在 k8s dashboard 里可以看到,外部端点是有显示

localhost:80
localhost:443

并且在主机上可以直接访问

traefik-deploy.yaml

apiVersion: v1
kind: Service
metadata:
  name: traefik
  namespace: kube-system
spec:
  ports:
    - name: web
      protocol: TCP
      nodePort: 30080
      port: 80
      targetPort: http
    - name: websecure
      protocol: TCP
      nodePort: 30443
      port: 443
    - name: admin
      port: 8080
      nodePort: 30880
  selector:
    app: traefik
  type: NodePort
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: traefik-ingress-controller
  namespace: kube-system
  labels:
    app: traefik
spec:
  selector:
    matchLabels:
      app: traefik
  template:
    metadata:
      name: traefik
      labels:
        app: traefik
    spec:
      serviceAccountName: traefik-ingress-controller
      terminationGracePeriodSeconds: 1
      hostNetwork: true
      containers:
        - image: traefik:v2.1.6
          name: traefik-ingress-lb
          ports:
            - name: http
              containerPort: 80
              hostPort: 80           #hostPort 方式,将端口暴露到集群节点
            - name: https
              containerPort: 443
              hostPort: 443          #hostPort 方式,将端口暴露到集群节点
            - name: admin
              containerPort: 8080
          resources:
            limits:
              cpu: 2000m
              memory: 1024Mi
            requests:
              cpu: 1000m
              memory: 1024Mi
          securityContext:
            capabilities:
              drop:
                - ALL
              add:
                - NET_BIND_SERVICE
          args:
            - --configfile=/config/traefik.yaml
          volumeMounts:
            - mountPath: "/config"
              name: "config"
      volumes:
        - name: config
          configMap:
            name: traefik-config 
      tolerations:              #设置容忍所有污点,防止节点被设置污点
        - operator: "Exists"
      nodeSelector:             #设置 node 筛选器,在特定 label 的节点上启动
        IngressProxy: "true"

搜了一圈文档都没发现怎么做到的,求助一下各位大佬。谢谢

5263 次点击
所在节点    Kubernetes
4 条回复
binux
2020-03-10 02:05:14 +08:00
bind IP address
hzfyjgw
2020-03-10 09:16:39 +08:00
可以考虑用 nodeselector 的有特定标签的 daemonset 的形式部署 traefik
Flands
2020-03-10 13:26:06 +08:00
@hzfyjgw 看 yaml 是 DaemonSet 方式部署的
Flands
2020-03-10 15:10:57 +08:00
service 里 type: LoadBalancer 就好了。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/651379

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX