Each pod has its own IP address. Pods are automatically created and deleted. If you use pods to provide services, high availability may not be ensured. The service abstraction allows you to decouple the frontend from the backend by using services. The frontend does not depend on the specific implementation of the backend. This ensures the design of loosely-coupled microservices. You can create a service in Container Service for Kubernetes (ACK). This topic describes how to create a service in the ACK console.
For more information, see Service.
Step 1: Create a deployment
- Log on to the ACK console.
- In the left-side navigation pane, click Clusters.
- On the Clusters page, click the name of a cluster or click Details in the Actions column. The details page of the cluster appears.
- On the Deployments tab, click Create from Template.
- Set Sample Template to Custom or select a sample template from the Sample Template
drop-down list, and click Create.
In this example, the template of an NGINX deployment is used.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment-basic labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 # replace it with your exactly <image_name:tags> ports. - containerPort: 80 ##Open this port in the service
- On the Deployments tab, find the specified deployment, and click the name or click
Details in the Actions column of the deployment. On the Basic Information page, you can check
the status of the deployment.
Step 1: Create a custom service
- In the left-side navigation pane, click Services.
- Go to the Cluster Information page. In the left-side navigation pane, click Services. On the Services page, click Create.
- In the Create Service dialog box, set the following parameters.
Parameter Description Name The name of the service. In this example, nginx-svc is used. Type The type of service. This parameter determines the method that is used to access the service. Valid values:
- Cluster IP: provides the service by using the internal IP address of the cluster.
If you select this option, the service is accessible only within the cluster.
Note The Headless Service checkbox is available only when you set the service type to Cluster IP. If you select this check box, the service can be discovered and accessed by other types of services, instead of only by Kubernetes services.
- Node Port: enables access to the service by using the IP address and static port on
each node. The NodePort field specifies the static port. A NodePort service can be
used to route requests to a Cluster IP service. The system automatically creates the
Cluster IP service. You can access a Node Port service from outside the cluster by
- Server Load Balancer: provides the service by using an SLB instance This supports
Internet access or internal access. An SLB service can route requests to Node Port
and Cluster IP services.
Note You can create an SLB instance or use an existing SLB instance. Multiple services can reuse the same SLB instance. However, the following limits apply:
- Create SLB Instance: You can click Modify to change the SLB instance type.
- Use Existing SLB Instance: You can select an existing SLB instance.
- If you use an existing SLB instance, the existing listeners of the SLB instance are overwritten.
- If an SLB instance is created when you create a service, you cannot reuse this SLB instance when you create other services. Otherwise, the SLB instance may be deleted. You can reuse only SLB instances that are manually created in the console or by calling API operations.
- Services that reuse the same SLB instance cannot have the same frontend listening port. Otherwise, port conflicts may occur.
- When you reuse an SLB instance, you must use the listener name and the VServer group name as unique identifiers in Kubernetes. Do not modify the listener name or VServer group name.
- You cannot reuse SLB instances across clusters.
Backend The backend object that you want to associate with the service. In this example, nginx-deployment-basic that is created in the previous step is selected. If you do not specify a deployment, no Endpoint object will be created. You can manually associate the service with an Endpoint object. For more information, see services-without-selectors. External Traffic Policy Valid values: Local and Cluster.Note The External Traffic Policy parameter is available only when you set the service type to Node Port or Server Load Balancer. Port Mapping Set the service port and container port. The container port must be the same as the one opened by the backend pod. Annotations Add annotations to the service and configure SLB parameters. For example,
service.beta.kubernetes.io/alicloud-loadbalancer-bandwidth:20specifies that the service bandwidth is set to 20 Mbit/s. For more information, see Use annotations to configure SLB instances.
Label Add labels to the service.
- Cluster IP: provides the service by using the internal IP address of the cluster. If you select this option, the service is accessible only within the cluster.
- Click Create. The nginx-svc service appears on the Services page.
- Enter the external endpoint of the nginx-svc service in your browser to access the
You have created the service and associated the service with a backend deployment. You can visit the NGINX welcome page.