k8s使用traefik暴露http服务和tcp服务

traefik官网:Traefik Proxy Documentation - Traefik

1、下载chart包

helm repo add traefik https://helm.traefik.io/traefik
helm repo update
helm pull traefik/traefik

2、下载镜像

docker pull docker.io/library/traefik:2.8

3、修改values.yaml

3.1、几个重要的端口配置

将traefik、web、websecure、metrics下的expose都改为true。

ports:
  traefik:
    port: 9000
    expose: false
    # The exposed port for this service
    exposedPort: 9000
    # The port protocol (TCP/UDP)
    protocol: TCP
  web:
    port: 8000
    # hostPort: 8000
    expose: true
    exposedPort: 80
    # The port protocol (TCP/UDP)
    protocol: TCP
  websecure:
    port: 8443
    # hostPort: 8443
    expose: true
    exposedPort: 443
    # The port protocol (TCP/UDP)
    protocol: TCP
    tls:
      enabled: false
      # this is the name of a TLSOption definition
      options: ""
      certResolver: ""
      domains: []
      # - main: example.com
      #   sans:
      #     - foo.example.com
      #     - bar.example.com
  metrics:
    port: 9100
    # hostPort: 9100
    expose: false
    # The exposed port for this service
    exposedPort: 9100
    # The port protocol (TCP/UDP)
    protocol: TCP

tlsOptions: {}

3.2、service配置:

配置使用何种方式将traefik的相关服务暴露出去,使得在集群外可以访问,我这里使用NodePort暴露。

# Options for the main traefik service, where the entrypoints traffic comes
# from.
service:
  enabled: true
  type: NodePort
  # Additional annotations applied to both TCP and UDP services (e.g. for cloud provider specific config)
  annotations: {}
  # Additional annotations for TCP service only
  annotationsTCP: {}
  # Additional annotations for UDP service only
  annotationsUDP: {}
  # Additional service labels (e.g. for filtering Service by custom labels)
  labels: {}
  # Additional entries here will be added to the service spec.
  # Cannot contain type, selector or ports entries.
  spec: {}
    # externalTrafficPolicy: Cluster
    # loadBalancerIP: "1.2.3.4"
    # clusterIP: "2.3.4.5"
  loadBalancerSourceRanges: []
    # - 192.168.0.1/32
    # - 172.16.0.0/16
  externalIPs: []

3.3、使用hostNetwork

必须将hostNetwork的值设为true。

hostNetwork: true

3.4、配置traefik ingressClass

我这里将ingressClass设为mytraefik(建议将ingressClass的值设为和部署实例名称一样)。

providers:
  kubernetesIngress:
    enabled: true
    allowExternalNameServices: false
    allowEmptyServices: false
    ingressClass: mytraefik

3.5、 添加自定义端口

在traefik中,暴露TCP服务需要在部署traefik时定义好需要使用的端口,不同于nginx-ingress可以动态修改TCP端口,traefik不支持动态增加TCP端口。

这里,我配置了两个端口:32000和32001,这两个端口的别名分别为myport32000和myport32001如果需要使用traefik暴露TCP服务,我就可以使用这两个端口。

additionalArguments:
  - --entrypoints.myport32000.Address=:32000
  - --entrypoints.myport32001.Address=:32001

3.6、允许使用80端口

ecurityContext:
  capabilities:
    drop: [ALL]
    add: [NET_BIND_SERVICE]		# 开放绑定端口
  readOnlyRootFilesystem: true
  runAsGroup: 0
  runAsNonRoot: false
  runAsUser: 

4、部署traefik

改好参数后,就可以直接部署traefik了。

helm install mytraefik .

5、访问traefik

部署完成后,查看创建的service

使用浏览器访问traefik的dashboard(9000端口对应的服务就是dashboard)

# 千万注意,这个地址不能错。必须是 服务器ip:NodePort端口/dashboard/#/
http://10.10.101.140:30332/dashboard/#/

6、使用traefik暴露http服务

6.1:创建ingress

traefik暴露HTTP服务和nginx ingress的方式是一样的,就是创建一个Ingress资源,在annotations中指定tkubernetes.io/ingress.class为mytraefik(在步骤3.4中配置的)。

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: mytraefik
  name: es-log-elasticsearch-http-ehcth3
  namespace: zeus-test
spec:
  rules:
  - host: hces.hclyl.com
    http:
      paths:
      - backend:
          serviceName: es-log-kibana
          servicePort: 5200
        path: /

6、使用traefik暴露tcp服务

6.1 创建ingressroutetcp

match的值都默认为: HostSNI('*')

entryPoints: entryPoints的值即为步骤3.5中配置的端口别名,我这里使用myport32000。

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRouteTCP
metadata:
  name: mysql
  namespace: zeus-test
spec:
  entryPoints:
    - myport32000
  routes:
  - match: HostSNI(`*`)
    services:
    - name: test-mysql
      port: 3306

创建完ingressroutetcp cr后,就可以使用32000端口访问mysql服务了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Traefik是一个基于Kubernetes的Ingress控制器,用于在Kubernetes集群中处理入站网络流量。您可以通过在Kubernetes集群中使用Traefik来实现负载均衡、路由和HTTPS等功能。 要在Kubernetes使用Traefik,您可以按照以下步骤进行操作: 1. 首先,您需要添加Traefik的Helm chart仓库到您的Kubernetes集群中。您可以使用以下命令将Traefik的Helm chart仓库添加到Helm中: ``` helm repo add traefik https://helm.traefik.io/traefik ``` 2. 然后,您可以使用Helm来安装Traefik。您可以使用以下命令来安装Traefik: ``` helm install traefik traefik/traefik ``` 3. 安装完成后,Traefik将会被部署到您的Kubernetes集群中。您可以使用以下命令来验证Traefik的部署状态: ``` kubectl get pods -n <namespace> ``` 4. 一旦Traefik部署成功,您可以通过创建Ingress资源来配置Traefik的行为。您可以使用以下命令创建一个简单的Ingress资源: ``` apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: example-ingress spec: rules: - host: example.com http: paths: - path: / pathType: Prefix backend: service: name: example-service port: number: 80 ``` 在这个示例中,Traefik将会将来自example.com的流量路由到名为example-service的后端服务。 5. 您还可以使用Traefik提供的其他功能,如TLS证书管理和动态配置等。您可以参考Traefik官方文档以了解更多详细的配置选项和用法。 总结起来,要在Kubernetes使用Traefik,您需要添加Traefik的Helm chart仓库,并使用Helm来安装Traefik。然后,您可以通过创建Ingress资源来配置Traefik的行为,实现负载均衡和路由等功能。记得查看官方文档以获取更多使用Traefik的详细信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值