In this post, we will explore (briefly) the Ingress in Kubernetes and see how to traffic multiple host names at the same IP address (localhost).
What is Ingress?
Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource.
To use Ingress, you will need to install an Ingress Controller. There are many available, all of which have different features:
Today, we will use the NGINX Ingress Controller.
helm repo add nginx-stable https://helm.nginx.com/stable helm repo update helm install my-nginx-release nginx-stable/nginx-ingress
kubectl get ingressclass and
kubectl get services to check the installation:
NAME CONTROLLER PARAMETERS AGE nginx nginx.org/ingress-controller <none> 39h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-release-nginx-ingress LoadBalancer 10.99.133.25 localhost 80:31918/TCP,443:32278/TCP 39h
http://localhost/ in your browser:
Using Ingress to expose Services
Let's create a
deployment.yml file with the following content:
apiVersion: apps/v1 kind: Deployment metadata: name: api-deployment labels: app: api spec: replicas: 2 selector: matchLabels: app: api template: metadata: labels: app: api spec: containers: - name: api-container image: nginx ports: - name: http containerPort: 80 protocol: TCP
service.yml file as follows:
apiVersion: v1 kind: Service metadata: name: api-service labels: app: api spec: type: ClusterIP ports: - port: 80 targetPort: http protocol: TCP name: http selector: app: api
ingress.yml file, to route every request to
www.api.com to the
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: api-ingress spec: ingressClassName: nginx rules: - host: www.api.com http: paths: - path: / pathType: Prefix backend: service: name: api-service port: number: 80
kubectl get ingress:
NAME CLASS HOSTS ADDRESS PORTS AGE api-ingress nginx www.api.com localhost 80 8h
Everything looks fine, but if we type
http://www.api.com/ in a browser, we will not be able to reach our service. That happens because nothing tells the browser to resolve that URL to our
localhost. Let's change that, open the
C:\Windows\System32\drivers\etc\hosts file and add
127.0.0.1 www.api.com at the end:
192.168.1.105 host.docker.internal 192.168.1.105 gateway.docker.internal 127.0.0.1 kubernetes.docker.internal 127.0.0.1 www.api.com
http://www.api.com/ in your browser:
And that's all. Thanks, and happy coding.