Create a ClusterIP Service

更新时间:
复制 MD 格式

In Kubernetes, pods are ephemeral and have IP addresses that can change. A Service provides a stable endpoint, such as a virtual IP address, to access a set of pods. This decouples frontend clients from backend pods, ensuring reliable communication even as pods are created or destroyed. This topic describes how to create a ClusterIP Service by using the Alibaba Cloud Container Service (ACS) console.

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 ACS console. In the left navigation pane, click Clusters.

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

  3. On the Deployments page, click Create from YAML.

  4. Select a template or enter your own YAML, then click Create.

    This example template defines a Deployment for Nginx.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-nginx-svc
      namespace: default
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: registry.cn-hangzhou.aliyuncs.com/acs-sample/nginxdemos:latest
            ports:
            - containerPort: 80
  5. On the Deployments page, click the name of the target application or Details in the Actions column. Monitor the status of the Deployment and wait for all its pod instances to become available.

Step 2: Create a Service

  1. On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Network > Services.

  2. On the Services page, click Create. In the Create Service dialog box, configure the following parameters and click OK.

    Parameter

    Description

    Example

    Name

    The name of the Service.

    my-nginx-svc

    Service Type

    Select Cluster IP. This type exposes the Service on a cluster-internal IP address, making the Service reachable only from within the cluster. This is the default Service type.

    Note

    You can optionally enable Headless Service. This allows you to interface with other service discovery systems without being tied to the Kubernetes implementation.

    ClusterIP

    Backend

    Select the backend application to which the Service routes traffic. If you do not associate a backend, the system does not create an Endpoints object. For more information, see Services without selectors.

    • Name: app

    • Value: nginx

    Port Mapping

    Add a mapping between the Service Port, which corresponds to the port field in the Service YAML, and the Container Port, which corresponds to the targetPort field. The container port must match the port exposed by the backend pods.

    • Service Port: 80

    • Container Port: 80

    • Protocol: TCP

    Annotations

    Add an annotation to the Service.

    None

    Label

    Add a label to identify the Service.

    None

    After the Service is created, it appears on the Services page. In the Actions column, click Update, Edit YAML, or Delete to manage it.