Replacing ip address in kubernetes with custom name

Viewed 255

I have created a sample spring boot app and did the following:-

1.created a docker image

2.created an Azure container registry and did a docker push to this

3.Created a cluster in Azure Kubernetes service and deployed it successfully.I have chosen external endpoint option for this.

Kubernetes external end point

say for service to service call i dont want to use IP like http://20.37.134.68:80 but another custom name how can i do it? Also if i chose internal then is there any way to replace the name. Tried editing YAML with endpoint name property but failed.Any ideas?

1 Answers

I think you mixing some concept, so I'll try to explain and help you to reach what you want.

  1. When you deploy a container image in a Kubernetes cluster, in the most cases you will use a pod or deployment spec, that basically is a yaml file with all your deployment/pod configuration, name, image name etc. Here is an example of a simple echo-server app:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo
spec:
  selector:
    matchLabels:
      app: echo
  template:
    metadata:
      labels:
        app: echo
    spec:
      containers:
      - name: echo
        image: mendhak/http-https-echo
        ports:
        - name: http
          containerPort: 80

Observe the fields name in the file. Here you can configure the name for your deployment and for your containers.

  1. In order to expose your application, you will need to use a service. Services can be internal and external. Here you can find all service types.

For a internal service, you need to use the service type ClusterIP (default), it means only your cluster will reach the pods. To reach your service from other pods, you can use the service name composed by my-svc.my-namespace.svc.cluster-domain.example.

Here is an example of a service for the deployment above:

apiVersion: v1
kind: Service
metadata:
  name: echo-svc
spec:
  selector:
    app: echo
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  1. To expose your service externally, you have the option to use a service type NodePort, LoadBalancer or use an ingress.

You can configure your DNS name in the ingress rules and make path rules if you want, or even configure a HTTPS for your application. There are few options to ingresses in kubernetes, and one of the most popular is nginx-ingress.

Here is an example of how to configure a simple ingress for our example service:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "false"
  name: echo-ingress
spec:
  rules:
  - host: myapp.mydomain.com
    http:
      paths:
      - path: "/"
        backend:
          serviceName: echo-svc
          servicePort: 80

In the example, i'm using the dns name myapp.mydomain.com, so it means you can only will reach your application by this name.

After create the ingress, you can see the external ip with the command kubectl get ing, and you can create a A entry in your dns server.

Related