skip to Main Content

im a beginer on Kube and im trying to connect 2 different frontend (with different DNS name)
and a postgres database on my Kube cluster.

Exemple :
Front 1 = test1.mysite.com
Front 2 = test2.mysite.com

Both dns -> 162.145.23.123

I want my postgres Service to be on the same IP

Front 1

kind: Deployment
apiVersion: apps/v1
metadata:
  name: front-app
  labels:
    app: front-app
spec:
  selector:
    matchLabels:
      app: front-app
  replicas: 1
  template:
    metadata:
      labels:
        app: front-app
    spec:
      containers:
      - name: front-app
        image: nginx
        ports:
          - containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
  name: front-service
spec:
  selector:
    app: front-app
  ports:
  # Default port used by the image
  - port: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: demo-localhost
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - host: test1.mysite.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: front-service
                port:
                  number: 80
status:
  loadBalancer:
    ingress:
      - ip: 162.145.23.123

Front 2

kind: Deployment
apiVersion: apps/v1
metadata:
  name: front-app2
  labels:
    app: front-app2
spec:
  selector:
    matchLabels:
      app: front-app2
  replicas: 1
  template:
    metadata:
      labels:
        app: front-app2
    spec:
      containers:
      - name: front-app2
        image: nginx
        ports:
          - containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
  name: front-service2
spec:
  selector:
    app: front-app2
  ports:
  # Default port used by the image
  - port: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: demo-localhost2
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - host: test2.mysite.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: front-service2
                port:
                  number: 80
status:
  loadBalancer:
    ingress:
      - ip: 162.145.23.123

Postgres

apiVersion: v1
kind: Service
metadata:
  name: hello-pg
  labels:
    app: hello-pg
spec:
  type: LoadBalancer
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP
  selector:
    app: hello-pg
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: db-secret-credentials
  labels:
    app: postgresdb
data:
  POSTGRES_DB: testDB
  POSTGRES_USER: testUser
  POSTGRES_PASSWORD: testPassword
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-pg-deployment
  labels:
    app: hello-pg
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-pg
  template:
    metadata:
      labels:
        app: hello-pg
    spec:
      containers:
      - name: hello-pg
        image: postgres
        ports:
          - containerPort: 5432
        envFrom:
          - configMapRef:
              name: db-secret-credentials

Thanks for your help

2

Answers


  1. Chosen as BEST ANSWER

    So i tried to do something like this :

        ---
         apiVersion: v1
         kind: Service
         metadata:
           name: hello-pg
           labels:
             app: hello-pg
         spec:
           type: LoadBalancer
           ports:
           - port: 5432
             targetPort: 5432
             protocol: TCP
           selector:
             app: hello-pg
        ---
    
    apiVersion: v1
    kind: Service
    metadata:
      name: hello-pg
      labels:
        app: hello-pg
    spec:
      type: NodePort
      ports:
      - port: 5432
        targetPort: 5432
        protocol: TCP
      selector:
        app: hello-pg
    
    apiVersion: v1
    kind: Service
    metadata:
      name: 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-5432
          port: 9000
          targetPort: 5432
          protocol: TCP
      selector:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
    

    And the result is still the same


  2. Base on your ingress annotations I can see that you are using ingress nginx controller. So if you would like to expose your database via the same load balancer as your applications you need to add additional ConfigMap object with TCP traffic routing configuration and modify your existing Service that provision LB.

    You can find more information about such configuration in ingress nginx documentation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search