Kubernetes Ingress Subdomain - Cloudfare Setup

12/24/2020

So I have my own website that I am running and I want to migrate one of my services to my current cluster under a subdomain of my actual website and I'm having some trouble.

I have a website that I purchased off of NameCheap and I'm using Cloudfare for all the DNS stuff. So everything is setup correctly. What I can't seem to figure out what to do is getting my subdomain website to actually work.

I have tried to add a "A" and "CNAME" record and still can't get it to work.

I also tried to follow this site and got no luck. I have tried other stackoverflow links and links posted by cloudfare. But I couldn't get anything to work still: https://www.digitalocean.com/community/tutorials/how-to-set-up-an-nginx-ingress-with-cert-manager-on-digitalocean-kubernetes

My services are also running with no issues. My pods and deployments are also fine showing no errors and my website is already running on another link which I'm removing to save money. www.ecoders.ca. All I did to migrate over my service was add stuff to my ingress and re-deploy everything to my current cluster. On my current cluster I'm using NGINX.

LMK if more information is required.

Ingress.yaml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
#    nginx.ingress.kubernetes.io/rewrite-target: /
  name: ingress
spec:
  rules:
   - host: www.foo.com
     http:
       paths:
         - backend:
             serviceName: nk-webapp-service
             servicePort: 80
           path: /
         - backend:
             serviceName: stockapp-service
             servicePort: 80
           path: /stock
   - host: www.bar.foo.com <----------- this does not work
     http:
       paths:
         - backend:
             serviceName: ecoders-webapi-service
             servicePort: 80
           path: /

Cloudfare Setup enter image description here

CNAME -> www -> foo.com  
CNAME -> bar -> foo.com  
A -> foo.com -> IP ADDRESS  

Service Setup

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ecoders-webapi
spec:
  replicas: 1
  selector:
    matchLabels:
      name: ecoders-webapi
  template:
    metadata:
      labels:
        name: ecoders-webapi
    spec:
      containers:
        - name: webapi
          image: astronik/webservice:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
apiVersion: v1
kind: Service
metadata:
  name: ecoders-webapi-service
spec:
  type: ClusterIP
  ports:
    - name: http
      port: 80
      targetPort: 8080
  selector:
    name: ecoders-webapi

UPDATED

Ingress.yaml

   - host: www.bar.foo.com
     http:
       paths:
         - backend:
             serviceName: ecoders-webapi-service
             servicePort: 80
           path: /
   - host: bar.foo.com
     http:
       paths:
         - backend:
             serviceName: ecoders-webapi-service
             servicePort: 80
           path: /

I added in a "www" link version as well, and now I'm getting this (before I was getting noting):

enter image description here

Does this have something to do with TLS/SSL? Meaning my subdomain doesn't have a certificate?

NEW UPDATE

So under "SSL/TLS" on the Cloudfare dashboard. Once I turned it off I was able to access my subdomain with no problem. But now how do I get it to run on full? Does my kubernetes cluster require a certificate?

enter image description here

SOLVED

So it's all fixed up and it had to do with 2 little problems.

Problem 1:

enter image description here

Essentially I need to change my DNS settings adding www. was adding another subdomain. I removed the 2 CNAMEs I created before and did this.

A -> bar -> 10.0.0.0 
A -> foo.com -> 10.0.0.0 

Problem 2:

Needed to update my ingress to remove this line

nginx.ingress.kubernetes.io/from-to-www-redirect: "true"

Updated Ingress

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  name: ingress
spec:
  rules:
   - host: foo.com
     http:
       paths:
         - backend:
             serviceName: nk-webapp-service
             servicePort: 80
           path: /
         - backend:
             serviceName: stockapp-service
             servicePort: 80
           path: /stock
   - host: bar.foo.com
     http:
       paths:
         - backend:
             serviceName: ecoders-web-service
             servicePort: 80
           path: /
-- Nikster
kubernetes
kubernetes-ingress
nginx

1 Answer

12/24/2020

SOLVED

So it's all fixed up and it had to do with 2 little problems. Link Cloudfare Page

Problem 1:

enter image description here

Essentially I need to change my DNS settings adding www. was adding another subdomain. I removed the 2 CNAMEs I created before and did this.

A -> bar -> 10.0.0.0 
A -> foo.com -> 10.0.0.0 

Problem 2:

Needed to update my ingress to remove this line

nginx.ingress.kubernetes.io/from-to-www-redirect: "true"

Updated Ingress

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  name: ingress
spec:
  rules:
   - host: foo.com
     http:
       paths:
         - backend:
             serviceName: nk-webapp-service
             servicePort: 80
           path: /
         - backend:
             serviceName: stockapp-service
             servicePort: 80
           path: /stock
   - host: bar.foo.com
     http:
       paths:
         - backend:
             serviceName: ecoders-web-service
             servicePort: 80
           path: /
-- Nikster
Source: StackOverflow