• 16 September 2024

This article will discuss the concept and the method of using the service in the ArvanCloud Cloud Container, along with the service’s components and their definition. The concept of service in our Cloud Container is completely compatible with the concept of service in Kubernetes.

The Prerequisites of Using Service in ArvanCloud Cloud Service

Having an ArvanCloud account and access to ArvanCloud Container Service is the only requirement for using this system. For this purpose, first, log into your account. Next, go to the profile section, generate a new API KEY in the API KEYS section, and then save it somewhere.

You will need to use the ArvanCloud command line to follow the steps in this article. Once you have downloaded it (add it to your PATH if necessary), use the following command to log in.

After that, copy the API KEY you have obtained from the website here.

What Is Service?

Service is a major and widely used component in the ArvanCloud Container Service. The Service, aka svc, is an entity that performs tasks such as load balancing across similar pods, assigning a domain and identifying active pods, as well as exposing pod ports.

When using Deployment on the ArvanCloud Container Service, pods within Deployment are terminated and recreated from time to time, with a new pod being created under a new IP. When a service is assigned to Deployment, this service takes over the task of finding running pods, routing traffic to those pods, and automatically balancing that traffic. Conversely, when you build a service, you can indicate the pod’s ports that must be seen by other applications in the project, as well as sending requests to them to allow those ports to be exposed.

Creating a Service

When creating a service, you must enter the required information in a YAML file and submit it to the ArvanCloud Container Service using the command line. In the following, a basic example of a service for an Nginx service (for which a deployment was created earlier in the article on using deployment in ArvanCloud Container Service) is included, and each of its sections has been explained.

Note: Remember that indentation in YAML files is important, and the smallest shifts can lead to an error or return of unwanted settings.

In the following, each of the relevant fields is explained.

  • Kind: Indicates the type of entity. Note that this field could have values like Pod, Service, StatefulSet, etc. This set of properties is similar to those in Kubernetes. The target in this example is to define the service, so this value is given.
  • Name: Specifies the name of the service along with its domain name.
  • Selector: The service determines which pods to direct traffic to, using the value specified in this field. The value set for this field has to be in the form key:value, corresponding to the spec.template.metadata.labels field in the context of deployment (this value is, in fact, the value that indicates the label of the pod and with which the service can slowly find the corresponding pods). For this example, this value is set to app: nginx.
  • Ports: Use this section to indicate ports that other services must see in the project. These ports are indicated in the deployment in the spec.template.spec.containers.ports section.
  • ports.name: Indicates the name assigned to the port.
  • protocol: Indicates the protocol that is accepted by the corresponding port.
  • port: This field indicates the port to which other services will send requests and which is exposed by the defined service.
  • targetPort: It is the port that the container in the pod will listen to, and the service will forward incoming requests to spec.ports.port to that port.

It is important to note that if requests are required to be sent to more than one pod, then the service will send requests to the pods with the round-robin algorithm, thereby applying load balancing on the pods.

Type the above lines in a file named nginx-service.yaml and save it. Next, in the command line, provide your service to the ArvanCloud Container Service using the following command.

You can use the following command to find out the status of your service and how it is running on the ArvanCloud Container Service.

The output will be something similar to the following.

In the above output, NAME represents the service name, TYPE is the service type, and by default, the value is ClusterIP. CLUSTER-IP is the IP address of the service itself. PORT indicates the ports exposed by the service. In addition, AGE indicates the execution time of the service.

Domain Name of the Service

Among the most important features of the service is the creation of a domain name for itself. In other words, a request to this service inside the project does not need to use its IP (CLUSTER IP value). The domain name that the service takes is the same as the metadata.name value specified in the service settings.

For example, if another service would like to request this service and its port 80, all that is required is to send its request to the address nginx-service:80. Having this domain name within your project can be detected by other services.