问题描述
我想按照以下步骤使用 Nginx 入口 TCP 服务公开我的 Mariadb pod https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/.Mariadb 在默认命名空间中运行,mariadb 服务类型为 ClusterIP.我在 nginx-ingress 命名空间中运行 Nginx Ingress 控制器,还为 mariadb
服务定义了 tcp-services
cofigmap.但是我无法从集群外部连接 MariaDB 数据库.
I want to expose my Mariadb pod using Nginx ingress TCP service by following this step https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/. Mariadb running in default name space, with mariadb service type as ClusterIP. I am running Nginx Ingress controller in nginx-ingress namespace, also defined tcp-services
cofigmap for mariadb
service. But I am unable to connect MariaDB database from outside of the cluster.
从 Nginx 控制器日志我可以看到它的读取 tcp-services.
From Nginx controller log I can see its reading tcp-services.
入口配置
containers:
- args:
- /nginx-ingress-controller
- --default-backend-service=nginx-ingress/nginx-ingress-default-backend
- --election-id=ingress-controller-leader
- --ingress-class=nginx
- --configmap=nginx-ingress/nginx-ingress-controller
- --default-ssl-certificate=nginx-ingress/ingress-tls
- --tcp-services-configmap=nginx-ingress/tcp-services
- --udp-services-configmap=nginx-ingress/udp-services
配置映射:
apiVersion: v1
kind: ConfigMap
metadata:
name: tcp-services
namespace: nginx-ingress
data:
3306: "default/mariadb:3306"
用于 TCP 服务的入口控制器 nginx 配置
Ingress controller nginx config for TCP Service
# TCP services
server {
preread_by_lua_block {
ngx.var.proxy_upstream_name="tcp-default-mariadb-3306";
}
listen 3306;
proxy_timeout 600s;
proxy_pass upstream_balancer;
}
当我从外部服务器连接时,收到此消息:
when I connect from external server, getting this message:
ERROR 2002 (HY000): Can't connect to MySQL server on
解决此问题的任何提示?
any tips to troubleshoot this issue?
谢谢
我缺少带有 TCP 端口信息的服务,添加它后我可以使用我的服务端口号访问 MySQL.感谢 Emanuel Bennici
指出这一点.
I was missing my service with TCP Port info, after adding it I was able to access the MySQL with my service Port Number. Thanks for Emanuel Bennici
pointing this one out.
这是我的服务:
apiVersion: v1
kind: Service
metadata:
name: nginx-ingress-controller
spec:
externalTrafficPolicy: Cluster
ports:
- name: http
port: 80
protocol: TCP
targetPort: http
- name: https
port: 443
protocol: TCP
targetPort: https
- name: 3066-tcp
port: 3066
protocol: TCP
targetPort: 3066-tcp
selector:
app: nginx-ingress
component: controller
release: nginx-ingress
sessionAffinity: None
type: NodePort
推荐答案
请检查Pod中是否打开了MySQL端口因此,要在 Kubernetes-Port 上打开端口,您必须像这样创建一个 pod:
Please check if you have opened the MySQL port in the PodSo to open the port on the Kubernetes-Port you have to create a pod like this:
apiVersion: v1
kind: Pod
metadata:
name: mysql
namespace: default
labels:
name: mysql
spec:
containers:
- name: mysql
image: docker.io/bitnami/mariadb:10.3.20-debian-9-r19
ports:
- containerPort: 3306
protocol: TCP
然后您必须创建一个服务,以便您可以通过该服务直接与 MySQL Pod 对话:
Then you have to create a service so you can talk directly to the MySQL Pod through the service:
apiVersion: v1
kind: Service
metadata:
name: svc-mysql
namespace: default
labels:
run: mysql
spec:
ports:
- port: 3306
targetPort: 3306
protocol: TCP
selector:
name: mysql
如果 Nginx 入口控制器工作正常,您现在可以将以下行添加到您的 tcp-services-configmap:
If the Nginx Ingress Controller is working correctly you can now add the following line to your tcp-services-configmap:
3306: "default/svc-mysql:3306"
请注意您必须将 MySQL 端口添加到 Nginx Ingress 服务,如下所示:
Please note that you have to add the MySQL port to the Nginx Ingress Service, like this:
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx
namespace: nginx-ingress
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: proxie-tcp-mysql
port: 3306
targetPort: 3306
protocol: TCP
selector:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
现在您可以使用 Nginx Ingress Controller 的外部 IP 连接到您的 MySQL 服务器.
Now you can use the external IP of the Nginx Ingress Controller to connect to your MySQL Server.
请在以后的问题中提供有关您的设置的更多信息:)
Please provide more information about your setup in future Questions :)
这篇关于Kubernetes 1.16 Nginx Ingress (0.26.1) TCP Mariadb/MySQL 服务不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!