Updated on 2024-10-29 GMT+08:00

Public Network Access

Overview

CCI allows workloads to be accessed from public networks. To implement this access, you need to bind a shared load balancer to a workload. The load balancer must be in the same VPC as the workload. Public network access at both Layer 4 and Layer 7 is supported.

  • TCP and UDP are supported for Layer-4 public network access. After you complete the configuration, the workload can be accessed using Public network IP address of the load balancer:Load balancer port.
  • HTTP and HTTPS are supported for Layer-7 public network access. After you complete the configuration, the workload can be accessed using http://Public network domain name or public network IP address of the load balancer:Load balancer port/Mapping path.

Services forward requests using TCP and UDP protocols at Layer 4. Ingresses forward requests using HTTP and HTTPS at Layer 7. You can use domain names and paths to achieve finer granularity, as shown in the following figure.

Figure 1 Ingress-Service

The following figure shows an example of accessing a workload using the HTTP protocol.

Figure 2 Public network access

Constraints

Before using an EIP, familiarize yourself with the EIP restrictions.

Setting Public Network Access When Creating a Workload

When you create a workload, select Internet access for Access Type and configure the following parameters:

  • Service Name: Specify the Service, which will be used to manage pod access. For details, see Service.
  • CoreDNS: Enable this option if you want to use the CoreDNS add-on. The CoreDNS add-on resolves internal domain names of workloads. If you do not install this add-on, the workload cannot be accessed through Service name:Workload access port.
  • Load Balancer: Select a load balancer. If no load balancer is available, click Create Load Balancer to create one.

    The load balancer must be in the same VPC as the workload.

    CCI does not support dedicated load balancers. Create a shared load balancer.

  • ELB Protocol: Specify the communication protocol for public network access, which can be HTTP/HTTPS or TCP/UDP.
  • Ingress Name: Specify the ingress, which will be used to manage Layer-7 access. If you do not set this parameter, the workload name will be used as the ingress name by default. For details, see Ingress.
  • Public Domain Name (configurable if you have selected HTTP or HTTPS): If you want the workload to be accessed using a domain name, you need to have purchased a public domain name and ensure that the domain name points to the EIP of the load balancer.
  • Certificate (mandatory when the HTTPS protocol is selected): For details about how to import an SSL certificate, see SSL Certificates.
  • ELB Port: Select the protocol and port for accessing the workload using the load balancer.
  • Workload Port Protocol: Specify the communication protocol for accessing the workload, which can be TCP or UDP. If you have set the ELB protocol to HTTP/HTTPS, the workload port protocol will be automatically displayed as TCP.
  • Workload Port Settings:
    • Workload Access Port: Specify the port for accessing the workload.
    • Container Port: Specify the port on which the container listens. The workload access port will be mapped to the container port.
  • HTTP Route Settings:
    • Mapping Path: Specify the path to be accessed. It must start with a slash (/). For example, /api/web. It can also be the root path /.
    • Workload Access Port: Specify the previously configured workload access port.

As shown in the following figure, if the IP address of the load balancer is 10.10.10.10, you can access the workload by visiting http://10.10.10.10:6071/.

Figure 3 Configuring public network access parameters

Setting Public Network Access After a Workload Is Created

You can configure Service-based access settings after a workload is created. The settings have no impact on the workload status and take effect immediately.

  1. Log in to the CCI console. In the navigation pane on the left, choose Network Management > Services. On the page displayed, click Create Service.
  2. On the Create Service page, select LoadBalancer for Access Type.
  3. Set ELB-based access parameters.

    • Service Name: Specify the Service, which will be used to manage pod access.
    • Namespace: Select the namespace that the workload belongs to.
    • Workload: Select a workload that you want to add the Service for.
    • Load Balancer: Select a public network load balancer. If no load balancer is available, click Create Load Balancer to create one.

      The load balancer must be in the same VPC as the workload.

      CCI does not support dedicated load balancers. Create a shared load balancer.

    • Port Settings:
      • Protocol: Specify the protocol that will be used to access the workload. Select TCP or UDP.
      • Access Port: Specify the port for accessing the workload.
      • Container Port: Specify the port on which the container listens. The workload access port will be mapped to the container port.

  4. Click Submit. The LoadBalancer Service will be added for the workload.

Setting DNAT-based Workload Access

After a workload is created, you can access the workload pods through ELB or DNAT from a public network. The following describes how to set DNAT for workload access:

  1. Create a NAT gateway.
  2. Use kubectl to create a DNAT Service. For details, see Service. The following is an example of creating a DNAT Service:

    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
      namespace: default  # User namespace. The default value is default.
      annotations:
        kubernetes.io/elb.class: dnat       # The type is set to DNAT.
        kubernetes.io/natgateway.id: 4b8cda3d-3543-4ebd-a55e-ca610b3b3c43 # NAT gateway ID
    spec:
      loadBalancerIP: 100.85.218.195      # EIP used by DNAT
      selector:
        app: nginx
      ports:
      - name: service0
        targetPort: 80     # Port exposed by the pod
        port: 8080         # DNAT access port
        protocol: TCP
       type: LoadBalancer   # Service type

  1. After the Service is created, you can run the kubectl describe <service_name> -n <service_namespace> command to view the Service update status.

After the Service is created and updated, you can access the pod by using EIP and port.

Constraints:

  1. One DNAT rule can be forwarded to only one backend. Therefore, a DNAT Service can be associated with only one backend pod. If more than one backend pod is associated, the DNAT rule fails.
  2. A maximum of 200 DNAT rules can be added to a NAT gateway. For details, see the NAT Gateway documentation.
  3. You can view the information about the DNAT Service on the frontend. Do not modify the information on the frontend.
  4. If the subnet does not use the default route, add a route of the NAT gateway to the corresponding route table.
  5. If you configure the DNAT Service for the gateway used by the SNAT rule, ensure that the EIP used by the DNAT Service is different from that bound to the SNAT rule.
  6. For details about how to use the NAT gateway, see the NAT Gateway documentation.

Setting Ingress-based Workload Access

You can configure ingress-based access settings after a workload is created. The settings have no impact on the workload status and take effect immediately.

  1. Log in to the CCI console. In the navigation pane on the left, choose Network Management > Ingresses. On the page displayed, click Create Ingress.
  2. Set ingress parameters.

    • Ingress Name: Enter a custom ingress name.
    • Namespace: Select the namespace to which you want to add the ingress.
    • Load Balancer: Specify a load balancer, which will automatically distribute Internet access traffic to multiple nodes running the workload.
    • External Port: Specify a port number that is open to the ELB service address.
    • External Protocol: HTTP and HTTPS are available. If you select HTTPS, choose a key certificate. For details about the certificate format, see Certificate and Private Key Format.
      • The key certificate ingress-test-secret.yaml is required only if you have selected HTTPS. For details on how to create a key, see Secrets.
      • If there is already an HTTPS ingress for the chosen port on the load balancer, the certificate of the new HTTPS ingress must be the same as the certificate of the existing ingress. This means that a listener has only one certificate. If two certificates, each with a different ingress, are added to the same listener of the same load balancer, only the certificate that was added first will take effect for the load balancer.
    • Domain Name: (Optional) Specify the actual domain name that will be used for access. You need to have purchased the domain name and completed ICP filing. Ensure that the domain name can be resolved into the service address of the selected load balancer. If you have configured a domain name rule, the domain name must always be used for access.
    • Ingress Rule
      • Rule Matching: Currently, only Prefix match is supported.

        Prefix match: Specify the prefix to be matched. If the mapping URL is /healthz, the URL that meets the prefix can be accessed. For example, /healthz/v1 and /healthz/v2.

      • URL: Specify the access path to be registered.
      • Service Name: Select the Service whose ingress you want to add.
      • Service Port: Specify the port on which the container in the container image listens.

  3. Click Submit.

    After the ingress is created, it is displayed in the ingress list.

Troubleshooting the Failure to Access a Workload from the Public Network

  1. A workload can be accessed from the public network only when it is in the running state. If your workload is abnormal or not ready, it cannot be accessed from the public network.
  2. It may take 1 to 3 minutes from the time when the workload was created to the time for it to be ready for public network access. During this time period, the network route has not yet been configured. As a result, the workload cannot be accessed from the public network.
  3. If the workload cannot be accessed 3 minutes after being created, click the workload. On the displayed details page, click the Events tab under Access Settings to check whether any alarm events are reported. The following are two common events:
    • Listener port is repeated: This event occurs when you delete a workload for which a load balancer port is configured, and immediately after that, create a workload using the same load balancer port. It takes some time for a load balancer port to be deleted. It is recommended that you delete the workload and create it again or wait for 5–10 minutes until the Internet access is normal.
    • Create listener failed: This event typically occurs because the listener quota is exceeded. Select another load balancer with a sufficient quota.
  4. The workload is inaccessible three minutes after it is created, and there is no alarm event. The possible reason is that no corresponding process is actually listening to the user-configured container port. Currently, CCI cannot detect this type of exception. You need to check whether the image is listening to the container port. If the container port is being properly listened to, the access failure may be due to the load balancer. In this case, check the status of the load balancer.

Enabling Public Network Access Using kubectl

To enable the access to a workload from the public network, two Kubernetes objects (that is, Service and ingress) are required. For details, see Service and Ingress.

Updating a Service

After you add a Service, you can update the port configuration of the Service.

  1. Log in to the CCI console. In the navigation pane on the left, choose Network Management > Services. On the Services page, select the corresponding namespace. In the row that contains the Service, click Update.
  2. Update load balancing parameters.

    • Namespace: Specify the namespace that the workload belongs to. The value is inherited from the workload creation page and cannot be changed.
    • Workload: Specify the workload that you want to update the Service for. The value is inherited from the workload creation page and cannot be changed.
    • Service Name: Specify the Service, which will be used to manage pod access. The value is inherited from the workload creation page and cannot be changed.
    • Load Balancer: The value is inherited from the workload creation page and cannot be changed.
    • Port Settings
      • Protocol: Specify the protocol that will be used to access the workload. Select TCP or UDP.
      • Access Port: Specify the port for accessing the workload.
      • Container Port: Specify the port on which the container listens. The workload access port will be mapped to the container port.

  3. Click Submit. The Service will be updated for the workload.

Updating an Ingress

After adding an ingress, you can update its port, domain name, and route configuration.

  1. Log in to the CCI console. In the navigation pane on the left, choose Network Management > Ingresses and then select the corresponding namespace. In the row that contains the ingress to be updated, click Update.
  2. On the Update Ingress page, set the following parameters:

    • External Port: Specify a port number that is open to the ELB service address.
    • Domain Name: (Optional) Specify the domain name that will be used for access. You need to have registered a domain name. Ensure that the domain name can be mapped to the IP address of the selected load balancer. If you have configured a domain name rule, the domain name must always be used for access.
    • Ingress Rule: Click Add Ingress Rule to add a rule.
      • Rule Matching: Currently, only Prefix match is supported.

        Prefix match: Specify the prefix to be matched. If the mapping URL is /healthz, the URL that meets the prefix can be accessed. For example, /healthz/v1 and /healthz/v2.

      • URL: Specify the access path to be registered, for example, /healthz.
      • Service Name: Select the Service whose ingress you want to update.
      • Service Port: Specify the port on which the container in the container image listens.

  3. Click Submit. The ingress will be updated for the workload.