This section uses an Nginx workload as an example to describe how to create an ELB ingress using kubectl.
The following describes how to run the kubectl command to automatically create a load balancer when creating an ingress.
vi ingress-test.yaml
You can create a load balancer as required. The YAML files are as follows:
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: ingress-test annotations: kubernetes.io/elb.class: union kubernetes.io/ingress.class: cce kubernetes.io/elb.port: '80' kubernetes.io/elb.autocreate: '{ "type":"public", "bandwidth_name":"cce-bandwidth-******", "bandwidth_chargemode":"traffic", "bandwidth_size":5, "bandwidth_sharetype":"PER", "eip_type":"5_bgp" }' spec: rules: - host: '' http: paths: - path: '/' backend: serviceName: <your_service_name> # Replace it with the name of your target Service. servicePort: 80 property: ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
Example of using a dedicated public network load balancer:
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: ingress-test namespace: default annotations: kubernetes.io/elb.class: performance kubernetes.io/ingress.class: cce kubernetes.io/elb.port: '80' kubernetes.io/elb.autocreate: '{ "type": "public", "bandwidth_name": "cce-bandwidth-******", "bandwidth_chargemode": "traffic", "bandwidth_size": 5, "bandwidth_sharetype": "PER", "eip_type": "5_bgp", "available_zone": [ "eu-de-01" ], "l7_flavor_name": "L7_flavor.elb.s1.small" }' spec: rules: - host: '' http: paths: - path: '/' backend: serviceName: <your_service_name> # Replace it with the name of your target Service. servicePort: 80 property: ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
kubernetes.io/elb.class |
No |
String |
Select a proper load balancer type. The value can be:
The default value is union. |
kubernetes.io/ingress.class |
Yes |
String |
cce: The self-developed ELBIngress is used. This parameter is mandatory when an ingress is created by calling the API. |
kubernetes.io/elb.port |
Yes |
Integer |
This parameter indicates the external port registered with the address of the LoadBalancer Service. Supported range: 1 to 65535 |
kubernetes.io/elb.subnet-id |
- |
String |
ID of the subnet where the cluster is located. The value can contain 1 to 100 characters.
|
kubernetes.io/elb.enterpriseID |
No |
String |
Kubernetes clusters of v1.15 and later versions support this field. In Kubernetes clusters earlier than v1.15, load balancers are created in the default project by default. ID of the enterprise project in which the load balancer will be created. The value contains 1 to 100 characters. How to obtain: Log in to the management console and choose Enterprise > Project Management on the top menu bar. In the list displayed, click the name of the target enterprise project, and copy the ID on the enterprise project details page. |
kubernetes.io/elb.autocreate |
Yes |
elb.autocreate object |
Whether to automatically create a load balancer associated with an ingress. For details about the field description, see Table 2. Example
|
host |
No |
String |
Domain name for accessing the Service. By default, this parameter is left blank, and the domain name needs to be fully matched. |
path |
Yes |
String |
User-defined route path. All external access requests must match host and path. |
serviceName |
Yes |
String |
Name of the target Service bound to the ingress. |
servicePort |
Yes |
Integer |
Access port of the target Service. |
ingress.beta.kubernetes.io/url-match-mode |
No |
String |
Route matching policy. Default: STARTS_WITH (prefix match) Options:
|
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
type |
No |
String |
Network type of the load balancer.
The default value is inner. |
bandwidth_name |
Yes for public network load balancers |
String |
Bandwidth name. The default value is cce-bandwidth-******. Value range: a string of 1 to 64 characters, including lowercase letters, digits, and underscores (_). The value must start with a lowercase letter and end with a lowercase letter or digit. |
bandwidth_chargemode |
Yes |
String |
Bandwidth billing mode.
|
bandwidth_size |
Yes for public network load balancers |
Integer |
Bandwidth size. The value ranges from 1 Mbit/s to 2000 Mbit/s by default. The actual range varies depending on the configuration in each region.
|
bandwidth_sharetype |
Yes for public network load balancers |
String |
Bandwidth type. PER: dedicated bandwidth |
eip_type |
Yes for public network load balancers |
String |
EIP type, which may vary depending on sites. For details, see the type parameter specified when creating an EIP.
|
name |
No |
String |
Name of the automatically created load balancer. Value range: a string of 1 to 64 characters, including lowercase letters, digits, and underscores (_). The value must start with a lowercase letter and end with a lowercase letter or digit. Default value: cce-lb+ingress.UID |
kubectl create -f ingress-test.yaml
If information similar to the following is displayed, the ingress has been created.
ingress/ingress-test created
kubectl get ingress
If information similar to the following is displayed, the ingress has been created successfully and the workload is accessible.
NAME HOSTS ADDRESS PORTS AGE ingress-test * 121.**.**.** 80 10s
121.**.**.** indicates the IP address of the unified load balancer.
If the cluster version is 1.15 or later, the YAML file configuration is as follows:
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: ingress-test annotations: kubernetes.io/elb.class: performance # Load balancer type kubernetes.io/elb.id: <your_elb_id> # Replace it with the ID of your existing load balancer. kubernetes.io/elb.ip: <your_elb_ip> # Replace it with your existing load balancer IP. kubernetes.io/elb.port: '80' kubernetes.io/ingress.class: cce spec: rules: - host: '' http: paths: - path: '/' backend: serviceName: <your_service_name> # Replace it with the name of your target Service. servicePort: 80 property: ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
kubernetes.io/elb.class |
No |
String |
Select a proper load balancer type. The value can be:
Defaults to union. |
kubernetes.io/elb.id |
Yes |
String |
This parameter indicates the ID of a load balancer. The value can contain 1 to 100 characters. How to obtain: On the management console, click Service List, and choose Networking > Elastic Load Balance. Click the name of the target load balancer. On the Summary tab page, find and copy the ID. |
kubernetes.io/elb.ip |
Yes |
String |
This parameter indicates the service address of a load balancer. The value can be the public IP address of a public network load balancer or the private IP address of a private network load balancer. |
Ingress supports TLS certificate configuration and provides security services in HTTPS mode.
vi ingress-test-secret.yaml
apiVersion: v1 data: tls.crt: LS0******tLS0tCg== tls.key: LS0tL******0tLS0K kind: Secret metadata: annotations: description: test for ingressTLS secrets name: ingress-test-secret namespace: default type: IngressTLS
In the preceding information, tls.crt and tls.key are only examples. Replace them with the actual files. The values of tls.crt and tls.key are the content encrypted using Base64.
kubectl create -f ingress-test-secret.yaml
If information similar to the following is displayed, the secret is being created:
secret/ingress-test-secret created
View the created secrets.
kubectl get secrets
If information similar to the following is displayed, the secret has been created successfully:
NAME TYPE DATA AGE ingress-test-secret IngressTLS 2 13s
vi ingress-test.yaml
Security policy (kubernetes.io/elb.tls-ciphers-policy) is supported only in clusters of v1.17.11 or later.
Example YAML file to associate an existing load balancer:
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: ingress-test annotations: kubernetes.io/elb.class: performance # Load balancer type kubernetes.io/elb.id: <your_elb_id> # Replace it with the ID of your existing load balancer. kubernetes.io/elb.ip: <your_elb_ip> # Replace it with the IP of your existing load balancer. kubernetes.io/ingress.class: cce kubernetes.io/elb.port: '443' kubernetes.io/elb.tls-ciphers-policy: tls-1-2 spec: tls: - secretName: ingress-test-secret rules: - host: '' http: paths: - path: '/' backend: serviceName: <your_service_name> # Replace it with the name of your target Service. servicePort: 80 property: ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
kubernetes.io/elb.tls-ciphers-policy |
No |
String |
The default value is tls-1-2, which is the security policy used by the listener and takes effect only when the HTTPS protocol is used. Options:
For details of cipher suites for each security policy, see Table 5. |
tls |
No |
Array of strings |
This parameter is mandatory if HTTPS is used. Multiple independent domain names and certificates can be added to this parameter. For details, see Configuring the Server Name Indication (SNI). |
secretName |
No |
String |
This parameter is mandatory if HTTPS is used. Set this parameter to the name of the created secret. |
Security Policy |
TLS Version |
Cipher Suite |
---|---|---|
tls-1-0 |
TLS 1.2 TLS 1.1 TLS 1.0 |
ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:AES128-SHA256:AES256-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-SHA:AES256-SHA |
tls-1-1 |
TLS 1.2 TLS 1.1 |
|
tls-1-2 |
TLS 1.2 |
|
tls-1-2-strict |
TLS 1.2 |
ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:AES128-SHA256:AES256-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384 |
kubectl create -f ingress-test.yaml
If information similar to the following is displayed, the ingress has been created.
ingress/ingress-test created
View the created ingress.
kubectl get ingress
If information similar to the following is displayed, the ingress has been created successfully and the workload is accessible.
NAME HOSTS ADDRESS PORTS AGE ingress-test * 121.**.**.** 80 10s
121.**.**.** indicates the IP address of the unified load balancer.
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: ingress-test annotations: kubernetes.io/elb.class: performance # Load balancer type kubernetes.io/elb.id: <your_elb_id> # Replace it with the ID of your existing load balancer. kubernetes.io/elb.ip: <your_elb_ip> # Replace it with the IP of your existing load balancer. kubernetes.io/ingress.class: cce kubernetes.io/elb.port: '443' kubernetes.io/elb.tls-ciphers-policy: tls-1-2 spec: tls: - secretName: ingress-test-secret - hosts: - example.top # Domain name specified a certificate is issued secretName: sni-test-secret-1 - hosts: - example.com # Domain name specified a certificate is issued secretName: sni-test-secret-2 rules: - host: '' http: paths: - path: '/' backend: serviceName: <your_service_name> # Replace it with the name of your target Service. servicePort: 80 property: ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
Ingresses can route requests to multiple backend Services based on different matching policies. The spec field in the YAML file is set as below. You can access www.example.com/foo, www.example.com/bar, and foo.example.com/ to route to three different backend Services.
The URL registered in an ingress forwarding policy must be the same as the URL exposed by the backend Service. Otherwise, a 404 error will be returned.
spec: rules: - host: 'www.example.com' http: paths: - path: '/foo' backend: serviceName: <your_service_name> # Replace it with the name of your target Service. servicePort: 80 property: ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH - path: '/bar' backend: serviceName: <your_service_name> # Replace it with the name of your target Service. servicePort: 80 property: ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH - host: 'foo.example.com' http: paths: - path: '/' backend: serviceName: <your_service_name> # Replace it with the name of your target Service. servicePort: 80 property: ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH