Connecting to an Application
You can choose different ways to connect to an application based on your requirements. You can connect to an application from inside the cluster with a Service, or from outside the cluster with an Ingress.
From Inside the Cluster
In Kubernetes, Services (service) provide network access to applications from inside the cluster. Like an entry point for a set of Pods, these Services distribute incoming traffic among them. There are different types of Services:
-
ClusterIPIn this mode, an internal IP is assigned to the application so other services can communicate with the application through it. The name used to call applications this way has the format
<service-name>.<namespace>.svc.cluster.local.For example, if you have a WordPress application and a MySQL application, you can use
mysql.project.svc.cluster.localto call MySQL from inside WordPress. -
NodePortWhen you open the application's ports in its private network settings, you can connect to applications inside the cluster with Kubernetes NodePort. For example, by opening port 3306 of the MySQL application, you can use
mysql:3306to communicate with it inside the cluster.
From Outside the Cluster
To access applications from outside the cluster, you can use a LoadBalancer Service or an Ingress.
-
LoadBalancer
A load balancer, which is a type of Service, makes a Pod accessible from outside the cluster with a public IP. In the Arvancloud Cloud Container, you can connect to your application from outside the cluster by setting a dedicated IP.
-
Ingress
With an Ingress, you can define routing rules for incoming HTTP/HTTPS traffic and manage access to one or more Services through URLs. When you connect a domain to an application, an Ingress is created for it behind the scenes.
Imagine we have an application named
nginxon port 80 and want to connect to it through the addresshttp://example.com. To do this, an Ingress like the following must be written and applied:
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
generation: 1
name: nginx-ingress
namespace: arvandocs
annotations:
# Example annotation for enabling SSL redirect. Actual annotations depend on your ingress controller
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: example.com
http:
paths:
- backend:
service:
name: nginx
port:
number: 80
path: /
pathType: Prefix
This YAML file is made up of different parts:
apiVersion: Specifies the Ingress API version.kind: Ingress: Specifies the Kubernetes resource type, which here is Ingress.metadata: Includes details such as the name, project, and annotations.spec: Specifies the state of the Ingress, including routing rules.rules: The HTTP traffic routing rules are specified in this section. Each rule contains ahostand a set of HTTP paths.host: The domain the traffic is sent to.http: Specifies the HTTP routing rules.paths: Specifies the paths for each node.path: The URL path that traffic should be sent to is written here.pathType: Specifies how the path is matched.backend: Specifies the Service and port the traffic should be sent to.
Ingresses can be more complex and include more settings based on your requirements. One of its widely used features is multi-tenancy. With this feature, you can use a wildcard in routing rules (for example *.example.com) to share one Ingress among several applications.
For example:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: wildcard-ingress
namespace: default
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: "*.example.com"
http:
paths:
- path: /service1(/|$)(.*)
pathType: ImplementationSpecific
backend:
service:
name: service1
port:
number: 80
- path: /service2(/|$)(.*)
pathType: ImplementationSpecific
backend:
service:
name: service2
port:
number: 80
In this example, the Ingress is configured to match any subdomain of example.com. It also sends requests to different Services based on different paths:
- Requests to
http://<any-subdomain>.example.com/service1are sent to theservice1application. - Requests to
http://<any-subdomain>.example.com/service2are sent to theservice2application.
Note that:
- Using a wildcard (*) in
hostand how it works depends on the Ingress Controller. This example assumes NGINX, which supports these settings. pathType: ImplementationSpecificlets paths be interpreted based on regular expressions or other rules.- The
nginx.ingress.kubernetes.io/rewrite-targetannotation is used to change the path sent to the application. In this example, whatever comes after /service1/ or /service2/ is sent to the application as part of the request.