Updated on 2023-07-07 GMT+08:00

NodePort

A workload can be accessed by other workloads in the same VPC using the IP address of a cluster node. The NodePort access type is applicable to the scenario in which other workloads in the same VPC in the cloud need to access the workload in the Kubernetes cluster.

Methods of Setting the Access Type

You can set the access type by using either of the following methods:

Setting the Access Type When Creating a Deployment

The following procedure uses an Nginx workload as an example.

  1. In the Configure Access Settings step, click Create Service. For details, see Deployments.

    • Service Name: Name of the Service to be created, which can be self-defined.
    • Access Mode: Select NodePort.
    • Protocol: Protocol used by the Service.
    • Container Port: Port on which the workload in the container image listens. The Nginx application listens on port 80.
    • Access Port: Specify a port to which the container port will be mapped when the node private IP address is used for accessing the application. The port number range is 30000–32767. You are advised to select Automatically generated.
      • Automatically generated: The system automatically assigns a port number.
      • Manually specified: Specify a fixed node port. The port number range is 30000–32767. Ensure that the port is unique in a cluster.

  2. Click OK.
  3. Click Next and configure advanced settings.
  4. Click Next and customize cluster configurations. Click OK.
  5. Obtain the access address.

    1. Click Cluster Console in the upper part of the page to access the cluster console.
    2. On the cluster console, choose Workloads > Deployments, click the name of the added workload to access the details page, and click View Access Mode to obtain the access address.
      Figure 1 Obtaining the node IP address

  6. On the homepage of the Huawei Cloud management console, choose Elastic Cloud Server under Compute.
  7. Select any ECS in the same VPC as the workload that will be accessed, and confirm that the security group is open to the IP address and port to be connected.

    Figure 2 Confirming that the security group is open

  8. Click Remote Login. On the login page, enter the username and password.
  9. Run the curl command to check whether the application can be accessed normally.

    NodePort services will also be assigned a cluster-internal IP address. You can use <service's cluster-internal IP address>:<access port> to verify whether the workload is reachable from inside the cluster. By default, <access port> in <service's cluster-internal IP address>:<access port> is the same as the container port (for example, 80).

    curl 192.168.0.160:80

    192.168.0.160:80 is the access address obtained in 5.

    If information similar to the following is displayed, the workload is accessible:

    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>

Setting the Access Type After Creating a Deployment

  1. Log in to the MCP console. In the navigation pane, choose Resource Management > Network Management. On the page displayed, click Create Service under the Services tab.
  2. Set access parameters.

    • Service Name: Name of a Service to be created. You can use the workload name as the Service name.
    • Namespace: Namespace to which the service belongs.
    • Access Type: Select NodePort.
    • Workload: Select an associated workload.
    • Port Configuration
      • Protocol: Select the protocol used by the Service.
      • Container Port: Port on which the workload listens. For example, the Nginx application listens on port 80 (container port).
      • Access Port: Specify a port to which the container port will be mapped when the node private IP address is used for accessing the workload. The port number range is 30000–32767. You are advised to select Automatically generated.
        • Automatically generated: The system automatically assigns a port number.
        • Manually specified: Specify a fixed node port. The port number range is 30000–32767. Ensure that the port is unique in a cluster.
    • Cluster: The cluster is the same as that on which the associated workload is deployed. It cannot be manually changed.

  3. Click Create. After the Service is created, you can view it in the list on the Services tab page.