基于 Nginx Ingress Controller 的四层(TCP)转发配置

基于 Nginx Ingress Controller 的四层(TCP)转发配置

1. 简介

本指南将展示如何通过配置 ConfigMap 来实现 Nginx Ingress Controller 的四层转发(TCP),并通过配置测试应用程序验证配置的有效性。本文中使用的 Kubernetes 组件包括 ConfigMapServiceDeployment 以及 LoadBalancer 类型的 Service。还将演示如何热加载修改 ConfigMap,以使更改能够即时生效。 Nginx-Ingress-Controller 的安装文档可以参考此链接。

2. 配置 Ingress Controller 的 TCP 服务

2.1 创建 TCP 服务配置 ConfigMap

首先,创建一个 ConfigMap 来定义 Ingress Nginx 的 TCP 服务,将特定的端口转发到相应的服务。

apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: ingress-nginx
data:
  53: "kube-system/kube-dns:53"
  8080: "ingress-nginx/nginx-hello-world:8080"
  • 53: 用于将 DNS 请求转发到 kube-dns 服务。
  • 8080: 用于将请求转发到部署的 nginx-hello-world 服务。

2.2 创建 LoadBalancer 类型的 Ingress Nginx Service

为 Ingress Nginx 创建一个 LoadBalancer 类型的 Service,使其支持四层 TCP 负载均衡。(如果没有LoadBalancer 可以使用在启动一个有 telnet 和 curl 的容器通过 ClusterIP 访问)

apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  type: LoadBalancer
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
    - name: proxied-tcp-9000
      port: 53
      targetPort: 53
      protocol: TCP
    - name: tcp-8080
      port: 8080
      targetPort: 8080
      protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

2.3 修改 ingress-nginx-controller 的启动命令

为了使 tcp-services ConfigMap 生效,需要将其添加到 Ingress Nginx Controller 的启动参数中,执行命令 kubectl edit deploy ingress-nginx-controller -n ingress-nginx添加参数如下:

 args:
    - /nginx-ingress-controller
    - --tcp-services-configmap=ingress-nginx/tcp-services

3. 部署测试应用

接下来,我们将部署一个测试应用(基于 Nginx 的 “Hello World”),并为其创建相应的 Service

3.1 创建 Nginx Index ConfigMap

使用 ConfigMap 来定义 Nginx 的默认页面内容:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-index
data:
  index.html: |
    
    
    
        Hello World
    
    
        

Hello, World!

3.2 部署 Nginx Hello World 应用

使用以下 Deployment 来部署 Nginx 容器,容器中挂载了自定义的 index.html 页面。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-hello-world
  labels:
    app: nginx-hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-hello-world
  template:
    metadata:
      labels:
        app: nginx-hello-world
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-config
          mountPath: /usr/share/nginx/html/index.html
          subPath: index.html
      volumes:
      - name: nginx-config
        configMap:
          name: nginx-index

3.3 创建服务以暴露 Nginx 应用

使用 ClusterIP 类型的服务来暴露 Nginx 应用。

apiVersion: v1
kind: Service
metadata:
  name: nginx-hello-world
spec:
  selector:
    app: nginx-hello-world
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80
  type: ClusterIP

4. 测试四层转发配置

4.1 验证应用访问

可以通过 Kubernetes 集群的 Service IP 地址访问 Nginx 测试应用,以下是测试的示例:

[root@nettools /]# curl 10.102.195.104:8080



    Hello World


    

Hello, World!

这里,10.102.195.104ingress-nginx 服务的 Cluster IP,8080 是在配置中定义的端口。

4.2 获取服务信息

使用以下命令查看 ingress-nginx 服务的详细信息:

kubectl get svc -n ingress-nginx

输出示例:

NAME                                 TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                                                  AGE
ingress-nginx                        LoadBalancer   10.102.195.104   localhost     80:30170/TCP,443:30677/TCP,53:31825/TCP,8080:32284/TCP   23m
ingress-nginx-controller             LoadBalancer   10.100.242.98    localhost     80:30663/TCP,443:32345/TCP                               30m
ingress-nginx-controller-admission   ClusterIP      10.97.94.62              443/TCP                                                  30m
nginx-hello-world                    ClusterIP      10.97.223.28             8080/TCP                                                 113s

5. 热加载修改 ConfigMap

为了更新 TCP 服务的配置而不需要重启 Ingress 控制器,我们可以直接修改 ConfigMap,并应用变更:

5.1 更新 ConfigMap

例如,如果需要修改 TCP 服务端口配置,只需编辑 tcp-services ConfigMap:

kubectl edit configmap tcp-services -n ingress-nginx

编辑完成后保存即可,Ingress Nginx 控制器会自动加载新的配置。

5.2 验证修改是否生效

修改 ConfigMap 后,可以通过重新访问相应的服务端口来验证变更是否生效。使用 curl 或其他网络请求工具检查服务的可达性。

6. 结论

通过本文,我们成功配置了 Nginx Ingress Controller 的四层 TCP 转发,部署了一个测试应用。Ingress 可以通过 ConfigMap 的变更进行热加载。这样可以为 Kubernetes 集群中的不同服务提供灵活的四层负载均衡能力。

你可能感兴趣的:(k8s,nginx,tcp/ip,运维,kubernetes,云原生,容器)