百度智能云

All Product Document

          Cloud Container Engine

          Create LoadBalancer Service

          This document details how to create a service of type LoadBalancer under CCE.

          Note: The following annotation may not work for versions below 1.16.3. Please contact the administrator by submitting a Ticket.

          Official tutorial of Kubernetes: Services

          Getting Started

          When the user creates a service of LoadBalancer type, by default, CCE will create BLB and bind EIP for BLB.

          Take creating a simple Nginx as an example:

          ---
          kind: Service 
          apiVersion: v1 
          metadata: 
            name: nginx-service 
          spec: 
            selector: 
              app: nginx 
            type: LoadBalancer 
            ports: 
            - name: nginx-port 
              port: 80 
              targetPort: 80 
              protocol: TCP 
          ---
          apiVersion: apps/v1beta1 
          kind: Deployment 
          metadata: 
            name: nginx-deployment 
          spec: 
            replicas: 1 
            template: 
              metadata: 
                labels: 
                  app: nginx 
              spec: 
                containers: 
                - name: nginx 
                  image: nginx 
                  ports: 
                  - containerPort: 80 

          (1) Create

          $ kubectl create -f nginx.yaml

          (2) Query EIP

          IP 8.8.8.8 is the EIP of Nginx.

          $ kubectl get svc 
          NAME            CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE 
          nginx-service   1.1.1.1        8.8.8.8          80:30274/TCP   5m 

          (3) Query BLB

          $ kubectl get svc nginx-service -o jsonpath={.metadata.annotations} 
          map[service.beta.kubernetes.io/cce-load-balancer-id:lb-xxxxxx] 

          lb xxxxx is the id of the BLB for this service.

          (4) Access test

          $ curl -i http://8.8.8.8

          Advanced Configuration

          Fixed Elastic IP

          When Service is deleted and recreated by a user, the EIP will change, so all other services that depend on the IP need to change, so CCE provides a way to fix the EIP.

          Scheme of fixing EIP:

          (1) Users purchase an EIP instance on cloud platform in advance.
          (2) When creating a service, set loadBalancerIP as this EIP.
          (3) Create Service, and EXTERNAL-IP is the EIP.
          (4) Delete Service, CCE will only unbind the EIP without releasing it. The user can continue to use it next time.

          The following is an example:

          ---
          kind: Service 
          apiVersion: v1 
          metadata: 
            name: nginx-service-eip-with-load-balancer-ip 
          spec: 
            selector: 
              app: nginx-eip-with-load-balancer-ip 
            type: LoadBalancer 
            loadBalancerIP: 8.8.8.8 
            ports: 
            - name: nginx-port 
              port: 80 
              targetPort: 80 
              protocol: TCP 
          ---
          apiVersion: apps/v1beta1 
          kind: Deployment 
          metadata: 
            name: nginx-deployment-eip-with-load-balancer-ip 
          spec: 
            replicas: 1 
            template: 
              metadata: 
                labels: 
                  app: nginx-eip-with-load-balancer-ip 
              spec: 
                containers: 
                - name: nginx 
                  image: nginx 
                  ports: 
                  - containerPort: 80 

          The EXTERNAL-IP thus qurried is EIP:

          kubectl get svc nginx-service 
          NAME                                    TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)        AGE 
          nginx-service-eip-with-loadBalancerIP   LoadBalancer   1.1.1.1          8.8.8.8          80:30601/TCP   1m 

          Do not assign EIP (i.e. BLB in VPC)

          When used by users:
          (1) Set Service. Spec.Type=LoadBalancer.
          (2) Add annotations for service, that is, service.beta.Kubernetes.io/cce-load-balancer-internal-vpc: "true".

          The following is an example:

          ---
          kind: Service 
          apiVersion: v1 
          metadata: 
            name: nginx-service-blb-internal-vpc 
            annotations: 
              service.beta.kubernetes.io/cce-load-balancer-internal-vpc: "true" 
          spec: 
            selector: 
              app: nginx-blb-internal-vpc 
            type: LoadBalancer 
            ports: 
            - name: nginx-port 
              port: 80 
              targetPort: 80 
              protocol: TCP 
          ---
          apiVersion: apps/v1beta1 
          kind: Deployment 
          metadata: 
            name: nginx-deployment-blb-internal-vpc 
          spec: 
            replicas: 1 
            template: 
              metadata: 
                labels: 
                  app: nginx-blb-internal-vpc 
              spec: 
                containers: 
                - name: nginx 
                  image: nginx 
                  ports: 
                  - containerPort: 80 

          The EXTERNAL-IP queried in this way can only be accessed in VPC:

          kubectl get svc nginx-service 
          NAME                             TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)        AGE 
          nginx-service-blb-internal-vpc   LoadBalancer   1.1.1.1          2.2.2.2          80:30601/TCP   1m 

          Note: This intranet BLB can only be used normally between clusters in one VPC. When using the intranet BLB in the same cluster, the problems will arise. It is recommended to use Service's ClusterIP directly in the same cluster.

          Custom EIP Configuration

          EIP support configuration type

          Prepaid

          Item Limit
          Public network bandwidth 1-200Mbps: Int
          Length of purchase [1,2,3,4,5,6,7,8,9,12,24,36], time unit: month

          Postpaid Postpaid)

          Charging mode Public network bandwidth Example of cost
          Bytraffic 1~200Mbps, Int Configuration fee: ¥ 0.00032/min; flow charge: ¥0.76/GB
          ByBandwidth 1-200Mbps, Int Configuration cost (1Mbps for example): ¥ 0.00094/min

          Usage
          When creating a service, set the corresponding Annotation as follows:

          // Payment method, default: Postpaid; optional: Postpaid: Prepaid 
          service.beta.kubernetes.io/cce-elastic-ip-payment-timing:"Postpaid" 
          // Billing method, default: ByTraffic; optional: ByTraffic: ByBandwidth 
          service.beta.kubernetes.io/cce-elastic-ip-billing-method:"ByTraffic" 
          // Public network bandwidth, Mbps, default: 100; for the prepay and bandwidth types of EIP, it is limited to an integer between 1 and 200; for the EIP of type traffic, it is limited to an integer between 1 and 200. 
          service.beta.kubernetes.io/cce-elastic-ip-bandwidth-in-mbps:"1000"
          // For prepayment, time length must be set, [1,2,3,4,5,6,7,8,9,12,24,36], unit month; for post payment, this setting is invalid 
          service.beta.kubernetes.io/cce-elastic-ip-reservation-length:"36"

          Example of postpaid:

          kind: Service 
          apiVersion: v1 
          metadata: 
            name: nginx-service 
            annotations: 
              service.beta.kubernetes.io/cce-elastic-ip-payment-timing: "Postpaid" 
              service.beta.kubernetes.io/cce-elastic-ip-billing-method: "ByTraffic" 
              service.beta.kubernetes.io/cce-elastic-ip-bandwidth-in-mbps: "200"
          spec: 
            selector: 
              app: nginx 
            type: LoadBalancer 
            ports: 
            - name: http 
              port: 80 
              targetPort: 80 

          Example of prepaid (please make sure the balance is sufficient, otherwise it will fail):

          kind: Service 
          apiVersion: v1 
          metadata: 
            name: nginx-service 
            annotations: 
              service.beta.kubernetes.io/cce-elastic-ip-payment-timing: "Prepaid" 
              service.beta.kubernetes.io/cce-elastic-ip-bandwidth-in-mbps: "10"
              service.beta.kubernetes.io/cce-elastic-ip-reservation-length:"1"
          spec: 
            selector: 
              app: nginx 
            type: LoadBalancer 
            ports: 
            - name: http 
              port: 80 
              targetPort: 80 

          Explanation matters

          Default configuration: The default is: Post payment+by traffic+100M bandwidth.

          Fixed EIP: It is not supported to update the configuration of the fixed EIP. Please go to the console to modify by yourself.

          The user updates the configuration of the service EIP (that is, manually edit the annotation): The configurations that support updates are: Public network bandwidth.

          Prepaid:

          (1) For prepayment, due to the limitation of EIP API, automatic renewal is not supported at present. Users need to renew on console by themselves.

          (2) For prepaid, it is not needed to set the charging method.

          (3) When deleting Service, the prepaid EIP will not be released until it expires.

          UDP-Service

          Modify spec.ports.protocol to UDP to use the feature of UDP service, for example:

          ---
          apiVersion: v1 
          kind: Service 
          metadata: 
            name: udp-server-demo-svc 
            labels: 
              app: udp-server-demo 
          spec: 
            type: LoadBalancer 
            ports: 
            - name: udp-server-demo-port 
              port: 3005 
              targetPort: 3005 
              protocol: UDP 
            selector: 
              app: udp-server-demo 
          ---
          apiVersion: extensions/v1beta1 
          kind: Deployment 
          metadata: 
            name: udp-server-demo 
            labels: 
              app: udp-server-demo 
          spec: 
            replicas: 1 
            selector: 
              matchLabels: 
                app: udp-server-demo 
            template: 
              metadata: 
                labels: 
                  app: udp-server-demo 
              spec: 
                containers: 
                - name: udp-server-demo 
                  image: hub.agilecloud.com/jpaas-public/udp-server-demo:latest 
                  ports: 
                  - containerPort: 3005 
                    protocol: UDP 

          (1) Deploy UDP test service

          $ kubectl apply -f udp.yaml 

          (2) UDP service created successfully

          $ kubectl get svc 
          NAME                  TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)          AGE 
          kubernetes            ClusterIP      172.16.0.1      <none>           443/TCP          6h 
          udp-server-demo-svc   LoadBalancer   172.16.122.139   10.10.10.10      3005:31441/UDP   1m 

          (3) View service log

          $ kubectl logs -f udp-server-demo-6fdf5d796f-h6595 
          Received: HealthCheck 
          Get Health Check, response OK 
          Received: HealthCheck 
          Get Health Check, response OK 
          Received: HealthCheck 
          Get Health Check, response OK 

          Note: According to the requirements of BLB, for the service monitoring UDP, it is necessary to pass UDP health check before BLB will forward the traffic to the back end, so the user's back-end UDP service needs to respond to the health check string, see: Introduction to UDP Health Check

          Specify Subnet for BLB of Service

          To facilitate users to manage network related resources, it supports specifying subnets for BLB when creating services. When used by users:

          (1) Set Service. Spec.Type=LoadBalancer.

          (2) Add annotations for Service, and specify the subnet ID, that is, service.beta.Kubernetes.io/cce-load-balancer-subnet-id: "sbn-*".

          The following is an example:

          ---
          kind: Service 
          apiVersion: v1 
          metadata: 
            name: nginx-service-blb-subnet-id 
            annotations: 
              service.beta.kubernetes.io/cce-load-balancer-subnet-id: "sbn-123456"
          spec: 
            selector: 
              app: nginx 
            type: LoadBalancer 
            ports: 
            - name: nginx-port 
              port: 80 
              targetPort: 80 
              protocol: TCP 
          ---
          apiVersion: apps/v1beta1 
          kind: Deployment 
          metadata: 
            name: nginx-deployment-blb-subnet-id 
          spec: 
            replicas: 1 
            template: 
              metadata: 
                labels: 
                  app: nginx 
              spec: 
                containers: 
                - name: nginx 
                  image: nginx 
                  ports: 
                  - containerPort: 80 

          After deploying service, you can go to BLB page to query the subnet where the BLB is created for verification. Note: Only specifying subnet when creating service is supported, editing service after creating service adding or modifying the annotation is not supported.

          Specify BLB to Create LB Service

          Specify the BLB of LB by adding annotations for Service, that is, service.beta.kubernetes.io/cce-load-balancer-id: "lb-xxxxxxxx". The following is an example:

          kind: Service 
          apiVersion: v1 
          metadata: 
            name: nginx-service-blb-assigned-id 
            annotations: 
              service.beta.kubernetes.io/cce-load-balancer-id: "xxxxxx"
          spec:
            selector:
              app: nginx
            type: LoadBalancer
            ports:
            - name: nginx-port
              port: 80
              targetPort: 80
              protocol: TCP

          BLB will not be reserved when deleting. If you want to reserve it, please use the annotation "Delete Service and Reserve BLB".

          Delete Service and Reserve BLB

          Adding annotations to Service, i.e, service.beta.kubernetes.io/cce-load-balancer-reserve-lb: "true", to specify reserving BLB when deleting Service. The following is an example:

          kind: Service
          apiVersion: v1
          metadata:
            name: nginx-service-blb-reserve-lb
            annotations:
              service.beta.kubernetes.io/cce-load-balancer-reserve-lb: "true" 
          spec: 
            selector: 
              app: nginx 
            type: LoadBalancer 
            ports: 
            - name: nginx-port 
              port: 80 
              targetPort: 80 
              protocol: TCP 
          Previous
          Set Ingress Traffic Forwarding
          Next
          Gated Launch