All Products
Search
Document Center

Container Service for Kubernetes:Create a Service

Last Updated:Sep 04, 2023

Each pod in Kubernetes clusters has its own IP address. However, pods are frequently created and deleted. Therefore, it is not practical to directly expose pods to external access. The Service resource decouples the frontend from the backend to provide a loosely-coupled microservices architecture. This topic describes how to create Services to expose applications by using the Container Service for Kubernetes (ACK) console or kubectl.

Prerequisites

A ACK Serverless cluster is created. For more information, see ACK Serverless quick start

Background information

In Kubernetes, a Service is an abstraction that defines a logical set of pods and a policy that is used to access the pods. This pattern is known as a microservice. A label selector is used to determine which set of pods is accessed by a Service.

Each pod in Kubernetes clusters has its own IP address. However, pods are frequently created and deleted. Therefore, directly exposing pods to external access does not comply with the rules for ensuring high availability. Services decouple the frontend from the backend. The frontend clients do not need to be aware of which backend pods are used. This provides a loosely-decoupled microservices architecture.

For more information, see Kubernetes Services.

Step 1: Create a Deployment

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster that you want to manage and choose Workloads > Deployments in the left-side navigation pane.

  3. On the Deployments page, click Create from YAML in the upper-right corner.

  4. Select the sample template or enter a custom template, and then click Create.

    In this example, the Deployment template for NGINX applications is selected.

    apiVersion: apps/v1 
    kind: Deployment
    metadata:
       name: nginx-deployment-basic
       labels:
         app: nginx
    spec:
       replicas: 2
       selector:
         matchLabels:
           app: nginx
       template:
         metadata:
           labels:
             app: nginx
         spec:
           containers:
           - name: nginx
             image: nginx:1.7.9                # replace it with your exactly <image_name:tags>
             ports:
             - containerPort: 80               ## Expose this port in the Service. 
  5. On the Deployments page, find the created Deployment, and click the name of the Deployment or click Details in the Actions column. On the page that appears, you can view the status of the Deployment. View applications

Step 2: Create a Service

  1. In the left-side navigation pane of the details page, choose Network > Services

  2. On the Services page, click Create.

  3. In the Create Service dialog box, configure the parameters of the Service.

    Parameter

    Description

    Name

    Enter a name for the Service.

    Type

    The type of Service. This parameter specifies how the Service is accessed. Valid values:

    • Cluster IP: the ClusterIP type Service. This type of Service is exposed by using the internal IP address of the cluster. This is the default value. If you select this option, the Service is accessible only from within the cluster.

      Note

      The Headless Service check box is displayed only when you set Type to Cluster IP. If you select this check box, you can use a headless Service to interface with other service discovery mechanisms, without being tied to the implementation of service discovery in Kubernetes.

    • Node Port: The NodePort type Service. This type of Service is accessed by using the IP address and a static port of each node. A NodePort Service can be used to route requests to a ClusterIP Service. The ClusterIP Service is automatically created by the system. You can access a NodePort Service from outside the cluster by sending requests to <NodeIP>:<NodePort>.

      Note

      This option is supported only by ACK clusters. ACK Serverless clusters do not support this option.

    • Server Load Balancer: The LoadBalancer type Service. This type of Service uses Internet-facing Server Load Balancer (SLB) instances or internal-facing SLB instances to enable external access or internal access. You can also use a LoadBalancer Service to route traffic to NodePort Services or ClusterIP Services.

      • Create SLB Instance: You can click Modify to change the specification of the SLB instance.

      • Use Existing SLB Instance: You can select an existing SLB instance.

      Note

      You can create an SLB instance or use an existing SLB instance. You can also associate an SLB instance with more than one Service. However, you must take note of the following limits:

      • If you use an existing SLB instance, the listeners of the SLB instance overwrite the listeners of the Service.

      • The SLB instance that is created for a Service cannot be shared by other Services. If you use the SLB instance to expose other Services, the SLB instance may be deleted. Only SLB instances that are manually created in the console or by calling the API can be used to expose multiple Services.

      • Kubernetes Services that share the same SLB instance must use different frontend listening ports. Otherwise, port conflicts may occur.

      • If you use one SLB instance to expose multiple Services, Kubernetes uses listener names and vServer group names as unique identifiers. Do not modify the names of listeners or vServer groups.

      • You cannot share SLB instances across clusters.

    External Traffic Policy

    Select a policy to distribute external traffic. For more information about the external traffic policy, see Differences between external traffic policies.

    • Local: routes traffic only to pods on the node where the Service is deployed.

    • Cluster: the network traffic can be routed to pods on other nodes in the cluster.

    Note

    The External Traffic Policy parameter is available only if you set Type to Node Port or Server Load Balancer.

    Backend

    Select the backend application that you want to associate with the Service. If you do not select a backend application, no Endpoint objects are created. For more information, see Services-without-selectors.

    Port Mapping

    Specify a Service port and a container port. The Service port corresponds to the port field in the YAML file and the container port corresponds to the targetPort field in the YAML file. The container port must be the same as the one that is exposed in the backend pod.

    Annotations

    Add one or more annotations to the Service to modify the configuration of the SLB instance. You can select Custom Annotation or Alibaba Cloud Annotation from the Type drop-down list. For example, the annotation service.beta.kubernetes.io/alicloud-loadbalancer-bandwidth:2 specifies that the maximum bandwidth of the Service is 2 Mbit/s. This limits the amount of traffic that flows through the Service. For more information about Service annotations, see Add annotations to the YAML file of a Service to configure CLB instances.

    Label

    Add one or more labels to the Service. Labels are used to identify the Service.

  4. Click Create.

    On the Services page, you can view the Service that you created.

    In the Actions column, you can click Details to view the Service details, click Update to update the Service, and click Delete to delete the Service.

    Note

    On the details page of the Service, you can click the hyperlink on the right side of External Endpoint to access the backend application.