In Kubernetes, Pods are ephemeral and their IP addresses are not stable. A Service provides a stable IP address and routes traffic to a logical set of backend Pods, decoupling clients from individual Pod lifecycles. You can create and expose a Service by using the ACS console or the kubectl CLI.
Method 1: Create a service 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 access method for the service. The following types are available:
-
Cluster IP: Exposes the service on a cluster-internal IP address. The service is accessible only from within the cluster.
NoteWhen you select Cluster IP, you can configure a Headless Service. You can use a Headless Service to integrate with other service discovery mechanisms without being tied to the Kubernetes implementation.
-
Server Load Balancer: Uses an Alibaba Cloud Classic Load Balancer (CLB) instance to expose the service. You can select Public Access or Internal Access. The CLB routes traffic to the service's ClusterIP.
-
Create Resource: If you select Pay-by-specification, you can click Change Specification to modify the CLB specification.
-
Use Existing Resource: You can select an existing CLB instance from the list.
Note-
Using an existing CLB instance overwrites its existing listeners.
-
CLB instances created by a service cannot be reused and can be accidentally deleted.
-
Only CLB instances that you create manually in the console or by calling an API can be reused.
-
If multiple services reuse the same CLB instance, ensure that their frontend listener ports are different to avoid port conflicts.
-
SLB instances cannot be reused across clusters.
-
Create Service:
-
Service Type: LoadBalancer
-
SLB Type: Classic Load Balancer (CLB)
-
Select Resource: Create Resource
Create CLB Resource:
-
Access Method: internal access
-
Use the default values for other settings.
Backend
The backend application that receives traffic from the service. If you do not select a backend, Kubernetes does not create a corresponding Endpoints object. For more information, see Services without selectors.
-
Name: app
-
Value: nginx
Port Mapping
Add a service port (the
portfield in the service YAML) and a container port (thetargetPortfield in the service YAML). 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 ClusterIP or LoadBalancer resources for the service.
NoteAlibaba Cloud-specific annotations take effect only when the Service Type is set to Server Load Balancer. For example, the annotation
service.beta.kubernetes.io/alicloud-loadbalancer-bandwidth:2sets the peak bandwidth of the service to 2 Mbit/s to control traffic. For a full list of annotations, see Configure a Classic Load Balancer (CLB) by using annotations.None
Label
Add a label to identify the service.
None
After configuring the parameters, click OK.
-
-
After the service is created, you can find the new service named
my-nginx-svcon the Services page.
Method 2: Create a service using kubectl
-
Connect to your Kubernetes cluster using kubectl in CloudShell.
-
Create a file named my-nginx-svc.yaml with the following content.
Field
Description
kindDefines the resource object type as Service.
metadataDefines basic information about the service, such as its name, labels, and namespace.
metadata.annotationsACS provides a rich set of annotations for configuring LoadBalancer services. In this example, the annotation
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-typespecifiesintranet(internal access). For more annotations, see Configure a Classic Load Balancer (CLB) by using annotations.spec.selectorThe label selector for the service. The service matches Pods with these labels and routes traffic to them.
spec.ports.portThe port exposed on the service's ClusterIP. Clients inside the cluster access the service at
clusterIP:port.spec.ports.targetPortThe port on the backend Pods. Traffic arriving at the
portis forwarded by kube-proxy to thetargetPorton the backend Pod.spec.typeSpecifies how the service is exposed.
-
LoadBalancer: Exposes the service using an Alibaba Cloud CLB instance. If you do not specify an existing CLB instance for the service, a public-facing CLB instance is created by default. To create a service with internal access and a corresponding private CLB, set theservice.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-typeannotation tointranet. -
ClusterIP: Exposes the service on a cluster-internal IP address for access from within the cluster.
ImportantAlibaba Cloud Container Compute Service (ACS) does not support services of the NodePort type. Attempts to create a service of this type fail or the service does not take effect.
-
-
Run the following command to create the service:
kubectl apply -f my-nginx-svc.yaml -
Run the following command to verify that the service is 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: To edit an existing service directly, run the following command. This opens the service configuration in your default editor.
kubectl edit service my-nginx-svc -
Method 2: To update a service from a manifest file, modify the local YAML file and apply the changes.
kubectl apply -f my-nginx-svc.yaml
View a service
Run the following command to view details about the service:
kubectl get service my-nginx-svc
Expected 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
Delete a service
Run the following command to delete the service:
kubectl delete service my-nginx-svc