Create a service

更新时间:
复制 MD 格式

In Kubernetes, a Service is an abstraction that exposes an application running on a set of pods as a network service. It provides a stable DNS name and load balancing for the pods. This topic explains how Kubernetes Services work, outlines important considerations, and provides recommendations for choosing a Service type.

Prerequisites

An ACK Serverless cluster is required. For more information, see Quick start for ACK Serverless.

Background information

In Kubernetes, a Service is an abstraction that defines a logical set of pods and a policy to access them, often referred to as a microservice. The set of pods targeted by a Service is typically determined by a label selector.

In Kubernetes, pods have their own IP addresses but are ephemeral—they can be created and destroyed dynamically. Exposing pods directly to external traffic prevents you from achieving a high availability architecture. A Service decouples the frontend from the backend, which allows the frontend to operate without knowing the specific backend pods. This enables a loosely coupled microservice design.

For more information, see Kubernetes Service.

Step 1: Create a Deployment

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

  2. On the Clusters page, click the name of your cluster. In the left navigation pane, click Workloads > Deployments.

  3. In the upper-right corner of the Deployments page, click Create from YAML.

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

    In this example, the sample template is an NGINX Deployment.

    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 with your image name and tag.
             ports:
             - containerPort: 80               # This port must be exposed in the service.
  5. In the list of deployments, click the name of the target application or Details in the Actions column to view the status of the Deployment. The details page for nginx-deployment-basic shows its basic information: the default namespace, the selector and labels are both app:nginx, the annotation is deployment.kubernetes.io/revision:1, and the update policy is RollingUpdate (25% max surge, 25% max unavailable). The status shows that 2/2 pods are ready, 2 are updated, and 2 are available. In the pod list, both pods use the nginx:1.7.9 image, are in the Running state, and have a restart count of 0.

Step 2: Create a service

  1. In the left navigation pane of the cluster management page, choose Network > Services.

  2. On the Services page, click Create.

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

    Parameter

    Description

    Name

    Enter a name for the service.

    Service type

    Select a service type, which determines how the service is accessed. Options:

    • Cluster IP: Exposes the service on an internal IP address in the cluster. This type of service is accessible only from within the cluster. This is the default service type.

      Note

      You can configure a Headless Service only when the Service Type is Cluster IP. You can use a Headless Service to interface with other service discovery mechanisms without being tied to the Kubernetes implementation.

    • Node Port: Exposes the service on a static port (the NodePort) on each node's IP address. A NodePort service routes traffic to a ClusterIP service, which is created automatically. You can access a NodePort service from outside the cluster by sending a request to <NodeIP>:<NodePort>.

      Note

      Only ACK clusters support NodePort. ACK Serverless clusters do not.

    • Server Load Balancer: Uses an Alibaba Cloud Server Load Balancer (SLB) instance to expose the service. You can choose to expose the service over the Internet or only within a private network. A Server Load Balancer can route traffic to NodePort and ClusterIP services.

      • Create SLB Instance: Click Modify to change the SLB instance specification.

      • Use Existing SLB Instance: Select an SLB instance from the list of existing instances.

      Note

      The Load balancer type supports creating a new SLB instance or using an existing one. Multiple Kubernetes services can share the same SLB instance, subject to the following limits:

      • If you use an existing SLB instance, its existing listeners are overwritten.

      • Do not reuse an SLB instance that was automatically created by another service. This can lead to accidental deletion.

      • When multiple services reuse the same SLB instance, ensure that their frontend listener ports are different to avoid port conflicts.

      • When you reuse an SLB instance, Kubernetes uses the listener names and vServer group names as unique identifiers. Do not modify these names.

      • You cannot reuse an SLB instance across different clusters.

    External Traffic Policy

    Set the external traffic policy. For a detailed comparison, see Comparison of external traffic policies.

    • Local: Traffic is routed only to pods on the same node that received the request.

    • Cluster: Traffic can be forwarded to pods on other nodes in the cluster.

    Note

    You can configure the External Traffic Policy only when the Service type is Node Port or Server Load Balancer.

    Backend

    Select the backend application to which the service sends traffic. If you do not associate the service with a backend, Kubernetes does not create a corresponding Endpoints object. For details, see Services without selectors.

    Port Mapping

    Add a service port (the port field in the service YAML file) and a container port (the targetPort field in the service YAML file). The container port must match the port exposed by the backend pods.

    Annotations

    Add an annotation to configure parameters for the load balancer. You can select Custom Annotation or Alibaba Cloud Annotation. For example, the annotation service.beta.kubernetes.io/alicloud-loadbalancer-bandwidth:2 sets the peak bandwidth of the service to 2 Mbit/s to control traffic. For more parameters, see Configure a Classic Load Balancer (CLB) by using annotations.

    Labels

    Add a label to identify the service.

  4. Click Create.

    The newly created service appears in the list on the Services page.

    On the Services page, click Update or Delete in the Actions column of the Service that you want to manage.

    Note

    On the service details page, you can click the link next to External Endpoint to access the application.