配置 K3S 和 Traefik Ingress 以公开 SSH 的 TCP 端口

问题描述 投票:0回答:1

我部署了一个 GitLab 服务,其中的 pod 侦听 TCP 端口 22。此外,我还设置了一项服务将端口 2222 链接到端口 22,该服务运行正常。

现在,我的目标是利用 Traefik Ingress 将 TCP 流量从端口 2222 路由到服务的端口 22。

这是我当前的设置:

GitLab 部署:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: gitlab
      namespace: gitlab
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: gitlab
      template:
        metadata:
          labels:
            app: gitlab
        spec:
          containers:
            - name: gitlab
              image: gitlab/gitlab-ce:16.7.7-ce.0
              ports:
                - containerPort: 80
                - containerPort: 443
                - containerPort: 22

服务:

    apiVersion: v1
    kind: Service
    metadata:
      name: gitlab-service
      namespace: gitlab
    spec:
      selector:
        app: gitlab-shell
      ports:
        - protocol: TCP
          port: 2222
          targetPort: 22

Traefik 的入口:

    apiVersion: traefik.containo.us/v1alpha1
    kind: IngressRouteTCP
    metadata:
      name: gitlab-shell
      namespace: gitlab
    spec:
      entryPoints:
        - gitlab-shell
      routes:
      - match: HostSNI(`*`)
        services:
        - name: gitlab-gitlab-shell
          namespace: gitlab
          port: 2222

我想需要更新 Ingress 控制器本身才能侦听端口 2222,但找不到如何编辑它。

我已经尝试过:

    kubectl edit deployment traefik -n kube-system
        - args:                                          
          - --entrypoints.gitlab-shell.address=:2222/tcp 
        ports:                                                                                                                                              
           - containerPort: 2222                                                               
             name: gitlab-shell                                                                
             protocol: TCP

还是没有结果

接下来我检查了 Traefik 服务:

    kubectl get service traefik -n kube-system
    spec:                                              
      allocateLoadBalancerNodePorts: true              
      clusterIP: 10.43.208.160                         
      clusterIPs:                              
      - 10.43.208.160                          
      externalTrafficPolicy: Cluster           
      internalTrafficPolicy: Cluster           
      ipFamilies:                              
      - IPv4                                   
      ipFamilyPolicy: PreferDualStack          
      ports:                             
      - name: web                        
        nodePort: 30552              
        port: 80                     
        protocol: TCP                
        targetPort: web              
      - name: websecure              
        nodePort: 30113              
        port: 443                    
        protocol: TCP                
        targetPort: websecure

并且没有 TCP/2222 端口。

如果我手动添加TCP/2222,仍然没有成功

kubectl edit service traefik -n kube-system
ports:                                         
  - name: gitlab-shell           
    nodePort: 31250              
    port: 2222                   
    protocol: TCP                
    targetPort: 2222 

现在我找不到公开 TCP 端口的正确方法。

所需图表:

IngressController (Traefic) <--?--> Ingress <--?--> Service:2222 <--OK--> Pod:22

有人可以帮我解决这个问题吗? 我会对任何配置示例感到满意。

kubernetes-ingress traefik k3s traefik-ingress ingress-controller
1个回答
0
投票

我想需要更新 Ingress 控制器本身才能侦听端口 2222,但找不到如何编辑它。

这正是问题所在:当您使用入口时,实际绑定到端口的是入口控制器。使用 k3s 时,使用 HelmChart 资源安装

traefik

$ kubectl -n kube-system get helmchart
NAME          JOB                        CHART                                                                      TARGETNAMESPACE   VERSION   REPO   HELMVERSION   BOOTSTRAP
traefik       helm-install-traefik       https://%{KUBERNETES_API}%/static/charts/traefik-25.0.2+up25.0.0.tgz                                                        
traefik-crd   helm-install-traefik-crd   https://%{KUBERNETES_API}%/static/charts/traefik-crd-25.0.2+up25.0.0.tgz                                                    

我们可以使用 HelmChartConfig 清单配置该图表,如下所示:

apiVersion: helm.cattle.io/v1
kind: HelmChartConfig
metadata:
  name: traefik
  namespace: kube-system
spec:
  valuesContent: |-
    ports:
      ssh: 
        port: 2200
        expose: true
        exposedPort: 2200

当您将 HelmChartConfig 资源部署到

kube-system
命名空间中时,它将触发 Traefik 的重新安装,并进行以下更改:

  • 它将把

    traefik
    部署更新为:

    • --entrypoints.ssh.address=:2200/tcp
      添加到 traefik 服务器命令行。

    • ports
      部分添加条目:

      - containerPort: 9100
        name: metrics
        protocol: TCP
      
  • 它将向

    traefik
    服务添加一个新端口:

    - name: ssh
      nodePort: 32594
      port: 2200
      protocol: TCP
      targetPort: ssh
    

负载均衡器(默认为ServiceLB)将公开主机上的适当端口。

© www.soinside.com 2019 - 2024. All rights reserved.