简体   繁体   中英

Reverse DNS for Kubernetes Ingress Website External IP

I need help to create a reverse zone for the external IP of Kubernetes Ingress Website or something that do the some function like a reverse zone.

Basically I need that when I enter the IP of the ingress in the browser, redirects me to the domain name.

Thanks for the help.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: prueba-web-ingress
 annotations:
  nginx.ingress.kubernetes.io/permanent-redirect: http://example.com
  networking.gke.io/managed-certificates: certificateexample
  kubernetes.io/ingress.global-static-ip-name: test
  kubernetes.io/ingress.allow-http: "false"
spec:
    backend:
           serviceName: prueba-web
           servicePort: 80

Try the following, just change the example.com to your domain:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: reverse-redirect
  annotations:
    nginx.ingress.kubernetes.io/permanent-redirect: http://example.com
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: "somerandomname" # needs some name, doesn't need to exist
            port:
              number: 80

When sending a request to nginx ingress without Host header, it will default to the ingress without specified host filed (just like the example above). Such request will receive a following response:

$ curl 123.123.123.123 -I
HTTP/1.1 301 Moved Permanently
Date: Wed, 28 Apr 2021 11:36:05 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: http://example.com

Browser receiving this redirect, will redirect to the domain specified in Location header.

Docs:


EDIT:

Since you are not using nginx ingress you can try to use a redirect app.

Here is a deployment with some random image I have found on dockerhub that responds to every request with redirect. I don't want to lecture you on security but I feel that I should at leat mention that you should never use random container images from the internet, and if you are, you are doing it on your own resposibiliy. Preferably build one from source and push to your own repo.

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: redir
  name: redir
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redir
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: redir
    spec:
      containers:
      - image: themill/docker-nginx-redirect-301
        name: docker-nginx-redirect-301
        env:
        - name: REDIRECT_CODE
          value: "302"
        - name: REDIRECT_URL
          value: https://example.com

Service for the above deployment:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: redir
  name: redir
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: redir
status:
  loadBalancer: {}

Now the ingress part:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: prueba-web-ingress
 annotations:
  networking.gke.io/managed-certificates: certificateexample
  kubernetes.io/ingress.global-static-ip-name: test
  kubernetes.io/ingress.allow-http: "false"
spec:
  rules:
  - http:
      paths:
      - path: /
          backend:
            serviceName: redir
            servicePort: 80
  - host: example.com
    http:
      paths:
      - path: /
         backend:
           serviceName: prueba-web
           servicePort: 80

Notice the same applies here: no host field set for redir service, although prueba-web service has its host filed set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM