Ingress Mariadb总是返回该网站可以到达该网站

发布于 2025-01-28 12:36:36 字数 2220 浏览 2 评论 0原文

我只是使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2025-02-04 12:36:36

Mariadb 不使用 HTTP协议,这是Postman浏览器使用。入口仅与HTTP/HTTP有关。因此,对于您的用例,您很可能不需要入口

有几种简单的方法来处理此问题。

kubectl port-forward ...


如果您只是将其用于娱乐/学习快速的方法是使用kubectl例如:

kubectl port-forward service/toto-mariadb-service 3306:3306

然后,您将使用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

apiVersion: v1
kind: Service
metadata:
  name: toto-mariadb-service
spec:
  selector:
    name: toto-mariadb
  ports:
    - port: 3306
      targetPort: 3306
      protocol: TCP
  type: LoadBalancer

然后,您将使用应用程序/客户端与外部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 a browser 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:

kubectl port-forward service/toto-mariadb-service 3306:3306

Then you would use an app/client like MariaDB Client to access the db using localhost at port 3306. You could also make a /etc/hosts entry with 127.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 to type: ClusterIP which will only assign an ip from the serviceCidr 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 you kubectl 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 metallb

apiVersion: v1
kind: Service
metadata:
  name: toto-mariadb-service
spec:
  selector:
    name: toto-mariadb
  ports:
    - port: 3306
      targetPort: 3306
      protocol: TCP
  type: LoadBalancer

Then you would use your app/client to talk to the external ip the service has. Get this ip from your service with kubectl get svc ...

read more on this: https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer

无声静候 2025-02-04 12:36:36

入口仅为HTTP。大多数(包括MariaDB)(包括Mariadb)的大多数数据库都不使用HTTP作为协议。

来自 docs> docs

入口不会暴露任意端口或协议。将http和https以外的其他服务曝光到Internet通常使用类型服务的服务。Type= nodePort或service.type = loadBalancer。

如果您的入口控制器支持它,则可以将其视为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:

An Ingress does not expose arbitrary ports or protocols. Exposing services other than HTTP and HTTPS to the internet typically uses a service of type Service.Type=NodePort or Service.Type=LoadBalancer.

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文