0
votes

I have a k8s cluster like below

#kubectl get all

NAME                                                 READY   STATUS    RESTARTS   AGE
pod/nginx-ingress-controller-d78c45477-gxm59         1/1     Running   0          8d
pod/nginx-ingress-default-backend-5b967cf596-dc8ss   1/1     Running   0          8d

NAME                                    TYPE           CLUSTER-IP       EXTERNAL-IP     PORT(S)                      AGE
service/kubernetes                      ClusterIP      10.245.0.1                 443/TCP                      9d
service/nginx-ingress-controller        LoadBalancer   10.245.203.193   A.B.C.D   80:30033/TCP,443:31490/TCP   8d
service/nginx-ingress-default-backend   ClusterIP      10.245.58.229              80/TCP                       8d

NAME                                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-ingress-controller        1/1     1            1           8d
deployment.apps/nginx-ingress-default-backend   1/1     1            1           8d

NAME                                                       DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-ingress-controller-d78c45477         1         1         1       8d
replicaset.apps/nginx-ingress-default-backend-5b967cf596   1         1         1       8d

As above, I have an external ip A.B.C.D.

I also have two domains domainA.com and domainB.com.

My DNS setting is like below:

for domainA.com:

-----domain A----
A www.domainA.com A.B.C.D
-----domain B----
A www.domainB.com A.B.C.D

After I install two apps with helm

I got

# kubectl describe ingress

Name:             app1
Namespace:        default
Address:          A.B.C.D
Default backend:  default-http-backend:80 ()
Rules:
  Host            Path  Backends
  ----            ----  --------
  www.domainA.com
                     app1:80 (10.244.1.15:80,10.244.1.33:80)

Annotations:
  kubernetes.io/ingress.class:  nginx
Events:
  Type    Reason  Age    From                      Message
  ----    ------  ----   ----                      -------
  Normal  CREATE  10m    nginx-ingress-controller  Ingress default/app1
  Normal  UPDATE  9m48s  nginx-ingress-controller  Ingress default/app1


Name:             app2
Namespace:        default
Address:          A.B.C.D
Default backend:  default-http-backend:80 ()
Rules:
  Host         Path  Backends
  ----         ----  --------
  www.domainB.com
                  app2:80 (10.244.1.15:80,10.244.1.33:80)

Annotations:
  kubernetes.io/ingress.class:  nginx
Events:
  Type    Reason  Age    From                      Message
  ----    ------  ----   ----                      -------
  Normal  CREATE  8m24s  nginx-ingress-controller  Ingress default/app2
  Normal  UPDATE  7m49s  nginx-ingress-controller  Ingress default/app2

I don't know why the backends have two IPs.

www.domainA.com and www.domainB.com may route to same ip(10.244.1.15:80) which is I don't want.

I want a single external ip route to different pods by hosts like virtual server

www.domainA.com
                     app1:80 (10.244.1.15:800)

-----------------
www.domainB.com
                     app2:80 (10.244.1.33:80)

How could I fix my configuration?

Thank you

2
share ingress, service,deployment yamlArghya Sadhu

2 Answers

2
votes

If I understood the situation correctly you would like to have single external IP domains A and B resolved to, single Ingress and two different apps (one per domain). And at the moment the issue is that traffic is delivered to Both Apps instated of proper routing.

Unfortunately, you haven't provided your Ingress and Services configs. That is why I'll have to explain all needed setup :)

What is needed here is to have 2 distinct apps (with different labels), 2 distinct services that route to endpoints for each service and single Ingress that lists both domains.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: name-virtual-host-ingress
spec:
  rules:
  - host: domain-A
    http:
      paths:
      - backend:
          serviceName: service-A
          servicePort: 8080
  - host: domain-B
    http:
      paths:
      - backend:
          serviceName: Service-B
          servicePort: 8080

here we've created Ingress that routes traffic for 2 different services

apiVersion: v1
kind: Service
metadata: 
  name: Service-A 
spec: 
  selector: 
    app: nginx
  ...

I've omitted half of the services specs for clarity. The most important point is "selector"

It is needed to create 2 services with 2 different selectors. You can check services and corresponding EndPoints with the kubectl get svc -o wide and kubectl get ep

Needless to say, that both apps shall be deployed separately to have different labels.

Please check your config and compare with the above.

Hope that helps. Will be happy to elaborate further if needed.

0
votes

My working settings:

deployment_app1.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app1
spec:
  replicas: 1
  selector:
    matchLabels: 
      app: app1
  template:
    metadata:
      labels:
        app: app1
    spec:
      containers:
      - name: name
        image: image
        ports:
        - containerPort: 80                 

deployment_app2.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app2
spec:
  replicas: 1
  selector:
    matchLabels: 
      app: app2
  template:
    metadata:
      labels:
        app: app2
    spec:
      containers:
      - name: name
        image: image
        ports:
        - containerPort: 80                 

ingress_app1.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app1
spec:
  rules:
    - host: www.domainA.com
      http:
        paths:
          - backend:
              serviceName: app1
              servicePort: 80

ingress_app2.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app2
spec:
  rules:
    - host: www.domainB.com
      http:
        paths:
          - backend:
              serviceName: app2
              servicePort: 80

service_app1.yaml:

apiVersion: v1
kind: Service
metadata:
  name: app1
spec:
  type: ClusterIP
  selector:
    app: app1
  ports:
    - name: port1
      protocol: TCP
      port: 80
      targetPort: 80

service_app2.yaml:

apiVersion: v1
kind: Service
metadata:
  name: app2
spec:
  type: ClusterIP
  selector:
    app: app2
  ports:
    - name: port1
      protocol: TCP
      port: 80
      targetPort: 80