ZhangZhihui's Blog  

If we only have one service, we can easily add an A record in the domain in AWS Route 53. 

But it will be a pain when we have a lot more of them, because we would have to create a lot of A records to route traffix to each of the services. 

A better solution in this case will be to use Ingress, which will allow us setup A record only once but can define multiple rules in the configuration file to route traffic to different services.

 

 

 

 

 

To use Ingress, first change the service type from LoadBalancer to ClusterIP, or delete the type:

apiVersion: v1
kind: Service
metadata:
  name: zimple-bank-api-service
spec:
  selector:
    app: zimple-bank-api
  ports:
    - protocol: TCP
      port: 80 # on which the server will listen to incoming request
      targetPort: 8080 # port of the container where the request will be sent to
  type: ClusterIP

Next, install the Ingress controller: https://kubernetes.github.io/ingress-nginx/deploy/#aws

Next, create ingress.yaml and apply it:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: zimple-bank-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: "api.zimple-bank.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: zimple-bank-api-service
            port:
              number: 80

Next, copy the address and paste it in the A record for the host domain:

Finally, add IngressClass in the ingress.yaml:

apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
  name: nginx
spec:
  controller: k8s.io/ingress-nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: zimple-bank-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: "api.zimple-bank.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: zimple-bank-api-service
            port:
              number: 80

 

posted on 2023-12-20 17:05  ZhangZhihuiAAA  阅读(16)  评论(0编辑  收藏  举报