This topic describes how to access services by using Alibaba Cloud Server Load Balancer (SLB).

Use the command line

  1. Create an NGINX application on the command line.
    root@master # kubectl run nginx --image=registry.aliyuncs.com/acs/netdia:latest
    root@master # kubectl get po 
    NAME                                   READY     STATUS    RESTARTS   AGE
    nginx-2721357637-d****                 1/1       Running   1          6s
  2. Create a service for the NGINX application and specify type=LoadBalancer to expose the NGINX service to the public network through an SLB instance.
    root@master # kubectl expose deployment nginx --port=80 --target-port=80 --type=LoadBalancer
    root@master # kubectl get svc
    NAME                  CLUSTER-IP      EXTERNAL-IP      PORT(S)                        AGE
    nginx                 172. **. **. ***   101. **. ***. **   80:3****/TCP                   4s
  3. Visit http://101. **. ***. ** in a browser to access your NGINX service.

More information

Alibaba Cloud SLB supports a variety of parameters for you to configure, such as the health check, billing method, and SLB instance type.

Annotations

Alibaba Cloud allows you to use SLB features by specifying annotations.

  • Use an existing internal SLB instance
    1. Connect to the Kubernetes cluster in CloudShell.
    2. Create the slb.svc file, copy the following code to the file, and run the kubectl apply -f slb.svc command:
      apiVersion: v1
      kind: Service
      metadata:
        annotations:
          service.beta.kubernetes.io/alicloud-loadbalancer-address-type: intranet
          service.beta.kubernetes.io/alicloud-loadbalancer-id: your-loadbalancer-id
          service.beta.kubernetes.io/alicloud-loadbalancer-force-override-listeners: "true"
        labels:
          run: nginx
        name: nginx
        namespace: default
      spec:
        ports:
        - name: web
          port: 80
          protocol: TCP
          targetPort: 80
        selector:
          run: nginx
        sessionAffinity: None
        type: LoadBalancer
      Note You must specify three annotations. Replace your-loadbalancer-id with the ID of your SLB instance.
  • Create an HTTPS-type SLB instance
    Create a certificate in the Alibaba Cloud console and record the certificate ID. Then create an HTTPS-type SLB instance by specifying the required annotations.
    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        service.beta.kubernetes.io/alicloud-loadbalancer-cert-id: your-cert-id
        service.beta.kubernetes.io/alicloud-loadbalancer-protocol-port: "https:443"
      labels:
        run: nginx
      name: nginx
      namespace: default
    spec:
      ports:
      - name: web
        port: 443
        protocol: TCP
        targetPort: 443
      selector:
        run: nginx
      sessionAffinity: None
      type: LoadBalancer
    Note The values of the annotations are case-sensitive.
    Annotation Description Default value
    service.beta.kubernetes.io/alicloud-loadbalancer-protocol-port The listening port. Separate multiple ports with commas (,), for example, https:443,http:80. None
    service.beta.kubernetes.io/alicloud-loadbalancer-address-type The type of the SLB instance. Valid values: internet and intranet. internet
    service.beta.kubernetes.io/alicloud-loadbalancer-slb-network-type The network type of the SLB instance. Valid values: classic and vpc. classic
    service.beta.kubernetes.io/alicloud-loadbalancer-charge-type The billing method of the SLB instance. Valid values: paybytraffic and paybybandwidth. paybybandwidth
    service.beta.kubernetes.io/alicloud-loadbalancer-id The ID of the SLB instance. You can specify an existing SLB instance by setting the loadbalancer-id annotation. If you specify an existing SLB instance, its listeners are overwritten. If you delete the service, the SLB instance is not deleted. None
    service.beta.kubernetes.io/alicloud-loadbalancer-backend-label The labels for specifying the nodes to be added as back-end servers of the SLB instance. None
    service.beta.kubernetes.io/alicloud-loadbalancer-region The region where the SLB instance is located. None
    service.beta.kubernetes.io/alicloud-loadbalancer-bandwidth The bandwidth of the SLB instance. 50
    service.beta.kubernetes.io/alicloud-loadbalancer-cert-id The ID of the certificate in Alibaba Cloud. You must upload a certificate first. ""
    service.beta.kubernetes.io/alicloud-loadbalancer-health-check-flag Specifies whether to enable the health check feature. Valid values: on and off. off. This annotation is not required for TCP listeners. The health check feature is enabled for TCP listeners and cannot be disabled by default.
    service.beta.kubernetes.io/alicloud-loadbalancer-health-check-type For more information, see HealthCheck. None
    service.beta.kubernetes.io/alicloud-loadbalancer-health-check-uri For more information, see HealthCheck. None
    service.beta.kubernetes.io/alicloud-loadbalancer-health-check-connect-port For more information, see HealthCheck. None
    service.beta.kubernetes.io/alicloud-loadbalancer-healthy-threshold For more information, see HealthCheck. None
    service.beta.kubernetes.io/alicloud-loadbalancer-unhealthy-threshold For more information, see HealthCheck. None
    service.beta.kubernetes.io/alicloud-loadbalancer-health-check-interval For more information, see HealthCheck. None
    service.beta.kubernetes.io/alicloud-loadbalancer-health-check-connect-timeout For more information, see HealthCheck. None
    service.beta.kubernetes.io/alicloud-loadbalancer-health-check-timeout For more information, see HealthCheck. None