In Kubernetes, Pods are frequently created and destroyed, so their IP addresses are not stable. This prevents the direct use of Pod IPs to provide a stable Service. A Service solves this problem by providing a fixed IP address and DNS name. Clients connect to the Service without needing to know the IP addresses of the backend Pods. This decouples the frontend from the backend and ensures stable service delivery. This topic describes how to create and expose a Service by using the Alibaba Cloud Container Compute Service (ACS) console and the kubectl CLI.
Method 1: Create a Service by using the console
Log on to the ACS console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Network > Services.
On the Services page, click Create. In the Create Service dialog box, configure the parameters for the Service.
Parameter
Description
Example
Name
The name of the Service.
my-nginx-svc
Service Type
The method used to access the Service. The following types are available:
Cluster IP: Exposes the Service on a cluster-internal IP address. This type of service is reachable only from within the cluster.
NoteA Headless Service can be configured to interface with other service discovery mechanisms, independent of the Kubernetes native implementation.
SLB: Corresponds to the
LoadBalancertype. It uses an Alibaba Cloud Server Load Balancer (SLB) instance to expose the service. Select Public Access or Internal Access. It routes external traffic to the internal ClusterIP service.Create Resource: For Pay-by-specification, click Change Specification to modify the SLB specification.
Use Existing Resource: Select an existing SLB specification from the list.
NoteUsing an existing SLB instance overwrites its existing listeners.
To prevent accidental deletion, you cannot reuse SLB instances that are automatically provisioned by a service. You can only reuse SLB instances that you create manually.
If multiple Services reuse the same SLB instance, ensure their frontend listener ports are unique to avoid conflicts.
When you reuse an SLB instance, Kubernetes uses the listener name and virtual server group name as unique identifiers. Do not modify these names.
Reusing an SLB instance across different clusters is not supported.
Create Service:
Service Type: SLB
SLB Type: CLB
Select Resource: Create Resource
Create CLB Instance:
Access Method: Internal Access
Use the default values for other configurations.
Backend
Select the backend application to which the Service will be bound. If no backend is selected, Kubernetes does not create the corresponding
Endpointsobject. For more information, see Services without selectors.Name: app
Value: nginx
Port Mapping
Add a service port (the
portfield in the Service YAML file) and a container port (thetargetPortfield in the Service YAML file). 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 configure the Service.
NoteAlibaba Cloud-specific annotations take effect only when Service Type is set to SLB.
For example, setting
service.beta.kubernetes.io/alicloud-loadbalancer-bandwidth: 2sets the peak bandwidth of the service to 2 Mbit/s to control traffic.For more annotations, see Configure a Classic Load Balancer (CLB) instance using annotations.
None
Label
Add a label to identify the Service.
None
After you configure the parameters, click OK.
After the Service is created, you can find
my-nginx-svcon the Services page.
Method 2: Create a service by using kubectl
Create a file named
my-nginx-svc.yamlwith the following content.Field
Description
kindSpecifies that the resource type is
Service.metadataDefines basic information for the Service, such as its name, label, and namespace.
metadata.annotationsACK provides many annotations for configuring SLB instances. For example, in the YAML file,
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-type: "intranet"specifies that the access method of the service is internal. For more annotations, see Configure a Classic Load Balancer (CLB) instance using annotations.spec.selectorDefines the selector for the service. The Service uses the selector to find Pods with matching labels and routes traffic to them.
spec.ports.portDefines the port exposed by the service's ClusterIP. This is the entry point for clients inside the cluster to access the service (
clusterIP:port).spec.ports.targetPortSpecifies the port on the backend Pods. Traffic entering through the
portis forwarded bykube-proxyto thetargetPortof the Pod, and finally to the container.spec.typeDefines the access method of the service.
LoadBalancer: Exposes the Service using an Alibaba Cloud SLB instance.If you do not specify an existing SLB instance, a public-facing SLB instance is created by default.
To create an internal-facing service and a corresponding internal SLB instance, set the
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-typeannotation tointranet.
ClusterIP: Exposes the service on an internal IP address within the cluster. This type is used for internal access only.
ImportantAlibaba Cloud Container Compute Service (ACS) does not support the
NodePortService type. Creating a Service of this type will fail or the Service will not take effect.Run the following command to create the Service.
kubectl apply -f my-nginx-svc.yamlRun the following command to verify that the Service was created.
kubectl get svc my-nginx-svcExpected output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-nginx-svc LoadBalancer 172.16.XX.XX 192.168.XX.XX 80/TCP 13s
Related operations
Update a Service
Method 1: Edit in-place
To edit the service configuration directly on the cluster, run the following command. This opens your default text editor.
kubectl edit service my-nginx-svcMethod 2: Apply from file
To update the service from a modified YAML file, run the following command.
kubectl apply -f my-nginx-svc.yaml
View a Service
To view the details of the Service, run the following command:
kubectl get service my-nginx-svcExpected output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-nginx-svc LoadBalancer 172.16.XX.XX 192.168.XX.XX 80/TCP 13sDelete a Service
To delete the Service, run the following command:
kubectl delete service my-nginx-svc