Alibaba Cloud Serverless Kubernetes supports service discovery for intranet services, headless services, and clusterIP services.
- Alibaba Cloud Domain Name System (DNS) PrivateZone is activated in the Alibaba Cloud DNS console.
- A Serverless Kubernetes cluster is created. For more information, see Create a serverless Kubernetes cluster.
- The Kubernetes cluster is connected. For more information, see Connect to a Kubernetes cluster by using kubectl.
Alibaba Cloud DNS PrivateZone is a private domain name resolution and management service based on Alibaba Cloud Virtual Private Cloud (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.
- Create a deployment and services.
Copy the following code into a YAML file and run the
kubectl create -f nginx-service.yamlcommand 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
- Run the following command to view the operating status of the created resources:
kubectl get svc,pod,deployment
- Log on to the Alibaba Cloud DNS console.
- In the left-side navigation pane, click PrivateZone. The All Zones tab of the PrivateZone page appears.
- In the zone list, find the target zone and click Configure in the Actions column. The Resolution Settings tab of the Resolution Settings page
Note Each host record is in the
$svc.$nsformat and corresponds to a resolved IP address. The following resolution rules apply:
You can access a 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.
- LoadBalancer service: corresponds to only one resolution record in PrivateZone, which is the IP address of Server Load Balancer (SLB).
- ClusterIP service: corresponds to multiple resolution records in PrivateZone, which are the IP addresses of backend pods.
- Headless service: corresponds to multiple resolution records in PrivateZone, which are the IP addresses of backend pods.
- Long domain name: You can use
$svc.$ns.svc.cluster.local.$clusterIdto connect to services in other clusters that are synchronized to PrivateZone.
- Short domain name: You can use
$svcto connect to services under the same namespace and use
$svc.$nsto connect to services in other namespaces.
For more information, see serverless-k8s-examples.