helm 安装 ingress-nginx

helm upgrade --install ingress-nginx ingress-nginx \
  --repo https://kubernetes.github.io/ingress-nginx \
  --namespace ingress-nginx --create-namespace

执行命令

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.2/deploy/static/provider/cloud/deploy.yaml

创建 configMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: ingress-nginx
data:
  3306: "default/mysql:3306" # port:namespace/service:port

编辑 depolyments ingress-nginx-controller

kubectl edit deployment ingress-nginx-controller -n ingress-nginx

# 在 args 下添加参数
- --tcp-services-configmap=ingress-nginx/tcp-services

编辑service ingress-nginx-controller

kubectl edit service ingress-nginx-controller -n ingress-nginx

# 增加端口
- name: tcp-service-3306
  port: 3306
  protocol: TCP
  targetPort: 3306

测试

添加测试 mysql ingress yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: mysql-ingress
spec:
  ingressClassName: nginx
  rules:
    - host: foo.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: mysql
                port:
                  number: 3306

打开电脑hosts文件, 增加测试域名
127.0.0.1 foo.com

使用 Navicat 或者其它 mysql 管理工具测试连接
主机: foo.com
端口: 3306


参考:
ingress 官方介绍 https://kubernetes.io/zh-cn/docs/concepts/services-networking/ingress/
ingress-nginx 公开TCP和UDP服务 https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/





Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐