Ingress Mariadb总是返回该网站可以到达该网站
我只是使用Ingress和Mariadb了解了Kubernetes。我不知道哪一个犯了我的代码错误,即返回“无法到达此站点”。包括服务,配置和秘密在内的要求。确保它通过入口暴露(在 /etc /hosts中制作简单的本地DNS),并且可以通过Postman /Web浏览器访问(请勿端口转发)。视频演示中必须包含所有CRUD功能(文章)。
apiVersion: v1
kind: PersistentVolume
metadata:
name: toto-mariadb-volume
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
hostPath:
path: /data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: toto-mariadb-volume-claim
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Secret
metadata:
name: toto-mariadb-secret
stringData:
MYSQL_DATABASE: toto
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: P@ssw0rd
MYSQL_USER: toto_user
data:
toto_VERY_SECRET: "a3ViZXJuZXRlcwo="
---
apiVersion: v1
kind: ConfigMap
metadata:
name: toto-mariadb-config
data:
APP_NAME: "toto-mariadb"
APP_PORT: "3306"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: toto-mariadb
labels:
app: toto-mariadb
spec:
replicas: 1
selector:
matchLabels:
app: toto-mariadb
template:
metadata:
name: toto-mariadb
labels:
app: toto-mariadb
spec:
containers:
- name: toto-mariadb
image: mariadb
resources:
limits:
memory: "1Gi"
cpu: "500m"
ports:
- containerPort: 3306
protocol: TCP
envFrom:
- secretRef:
name: toto-mariadb-secret
- configMapRef:
name: toto-mariadb-config
---
apiVersion: v1
kind: Service
metadata:
name: toto-mariadb-service
spec:
selector:
name: toto-mariadb
ports:
- port: 3306
targetPort: 3306
protocol: TCP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: toto-mariadb-ingress
labels:
app: toto-mariadb-ingress
spec:
rules:
- host: toto-mariadb.lovanto.local
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: toto-mariadb-service
port:
number: 3306
使用Port-Forward时,代码正常工作,但是使用主机时仍无法正常工作
I'm just learning about Kubernetes using ingress and MariaDB. I don't know which one make my code error that return "This site can’t be reached". The requirement including Services, ConfigMap, and Secret. Make sure it is exposed by Ingress (make a simple local DNS in /etc/hosts) and can be accessed by Postman / Web Browser (do not port forwarding). All the CRUD features (Article) must be included in the video demo.
apiVersion: v1
kind: PersistentVolume
metadata:
name: toto-mariadb-volume
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
hostPath:
path: /data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: toto-mariadb-volume-claim
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Secret
metadata:
name: toto-mariadb-secret
stringData:
MYSQL_DATABASE: toto
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: P@ssw0rd
MYSQL_USER: toto_user
data:
toto_VERY_SECRET: "a3ViZXJuZXRlcwo="
---
apiVersion: v1
kind: ConfigMap
metadata:
name: toto-mariadb-config
data:
APP_NAME: "toto-mariadb"
APP_PORT: "3306"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: toto-mariadb
labels:
app: toto-mariadb
spec:
replicas: 1
selector:
matchLabels:
app: toto-mariadb
template:
metadata:
name: toto-mariadb
labels:
app: toto-mariadb
spec:
containers:
- name: toto-mariadb
image: mariadb
resources:
limits:
memory: "1Gi"
cpu: "500m"
ports:
- containerPort: 3306
protocol: TCP
envFrom:
- secretRef:
name: toto-mariadb-secret
- configMapRef:
name: toto-mariadb-config
---
apiVersion: v1
kind: Service
metadata:
name: toto-mariadb-service
spec:
selector:
name: toto-mariadb
ports:
- port: 3306
targetPort: 3306
protocol: TCP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: toto-mariadb-ingress
labels:
app: toto-mariadb-ingress
spec:
rules:
- host: toto-mariadb.lovanto.local
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: toto-mariadb-service
port:
number: 3306
The code work properly when using port-forward, but when using the host still not working
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Mariadb 不使用 HTTP协议,这是
Postman
和浏览器
使用。入口仅与HTTP/HTTP有关。因此,对于您的用例,您很可能不需要入口。有几种简单的方法来处理此问题。
kubectl port-forward ...
如果您只是将其用于娱乐/学习快速的方法是使用
kubectl
例如:然后,您将使用app/client,例如mariadb client 使用
local-host 在端口
3306
上。您也可以使用127.0.0.1<您的desired-hostName>
在此上进行
/ett/hosts
条目进行更多信息: https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-access-application-cluster/#forward-forward-forward-a-local-port-port-to-a-port-to-a-port-on-port-on-the-pod类型的服务:LoadBalancer,
如果您想长期可以连接到端口,这是一种更好的方法。您需要将
类型:loadBalancer
添加到服务的规格(例如,请参见下文)。默认情况下,如果
类型:
是从服务的规格字段省略的,它将默认为type:clusterip
,它将仅从servicecidr
ranga从集群外部无法访问。这种方法的潜在问题是,如果您未配置群集以向您提供
外部IP
当您kubectl get svc ...
外部IP列将留在待处理上时。 。如果这种情况发生在您身上,您将需要查看哪种方法对您的K8风味有用。一种常见的方法是使用 metallb然后,您将使用应用程序/客户端与
外部IP 服务具有。使用
kubectl获取SVC ...
从您的服务中获取此IP: https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer
MariaDB does not use the HTTP protocol which is what
postman
and abrowser
use. Ingress are only concerned with http/https. So for your use case you most likely do not need an ingress.There are a couple of easy ways to handle this.
kubectl port-forward ...
If you are just using this for fun/learning a quick way is to use port-forward using
kubectl
for example:Then you would use an app/client like MariaDB Client to access the db using
localhost
at port3306
. You could also make a/etc/hosts
entry with127.0.0.1 <your-desired-hostname>
read more on this: https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/#forward-a-local-port-to-a-port-on-the-pod
Service of type: LoadBalancer
This is a better way if you want to have a port easily available long term to connect to. You will need to add
type: LoadBalancer
to the spec of your service (see below for example).By default if
type:
is omitted from the service's spec field it will default totype: ClusterIP
which will only assign an ip from theserviceCidr
range which is not accessible from outside the cluster.The potential problem with this approach is if you cluster is not configured to provide you an
external ip
when youkubectl get svc ...
the external ip column will stay stuck at pending. If this happens to you you will need to see what methods will work for your flavor of K8s. A common approach is to use metallbThen you would use your app/client to talk to the
external ip
the service has. Get this ip from your service withkubectl get svc ...
read more on this: https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer
入口仅为HTTP。大多数(包括MariaDB)(包括Mariadb)的大多数数据库都不使用HTTP作为协议。
来自 docs> docs :
如果您的入口控制器支持它,则可以将其视为TCP服务。例如,请参见以下内容: httpps:// kubernetes .github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/
Ingress is HTTP only. Most if not all databases, including MariaDB, don't use http as protocol.
From the docs:
You could expose this as a TCP service, if your ingress controller supports it. For example, see this: https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/