Alibaba Cloud Serverless Kubernetes supports service discovery for intranet services, Headless services, and ClusterIP services.

Prerequisites

Background information

PrivateZone is an Alibaba Cloud private domain name resolution and management service based on VPC. You can map a private domain name to an IP address in one or more custom VPCs. Your private domain names are not accessible in other network environments.

Procedure

  1. Deploy a Deployment and create a Service.

    The sample template is as follows. Copy the following yaml code to the yaml file. Then run the kubectl create -f nginx-service.yaml command to create a service.

    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-headless-service
    spec:
      ports:
      - port: 80
        protocol: TCP
      selector:
        app: nginx
      clusterIP: None
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-clusterip-service
    spec:
      ports:
      - port: 80
        protocol: TCP
      selector:
        app: nginx
      type: ClusterIP
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-intranet-service
      annotations:
        service.beta.kubernetes.io/alicloud-loadbalancer-address-type: intranet
    spec:
      ports:
      - port: 80
        protocol: TCP
      selector:
        app: nginx
      type: LoadBalancer
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      labels:
        app: nginx
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image:  nginx:alpine
            ports:
            - containerPort: 80
  2. Run the following command to view the operating status of the application:
    kubectl get svc,pod,deployment
  3. Log on to the Alibaba Cloud DNS console. The Manage DNS page appears.
  4. In the left-side navigation pane, click PrivateZone > All Zones. A record is automatically generated in the zone list.
    All Zones
  5. Click Configure. The Resolution Settings page appears.
    Note The format of the host machine record is $svc.$ns, and the record corresponds to an IP resolution. Resolution rules are as follows:
    • LoadBalancer service: corresponds to only one resolution record in PrivateZone, which is the SLB IP.
    • ClusterIP service: corresponds to multiple resolution records in PrivateZone, which are the IPs of the backend pods.
    • Headless service: corresponds to multiple resolution records in PrivateZone, which are the IPs of the backend pods.
    You can access the service by using the private domain name in the VPC. The private domain name can be a long domain name or a short domain name.
    • A long domain name: You can use $svc.$ns.svc.cluster.local.$clusterId to connect to services in other clusters that are synchronized to PrivateZone.
    • A short domain name: You can use $svc to connect to the service under this namespace and use $svc.$ns to connect to services in other namespaces.

    For more information, see serverless-k8s-examples.