In a CCE Turbo cluster, you can configure subnets and security groups for containers by namespace or workload using NetworkAttachmentDefinition CRDs. If you want to configure a specified container subnet and security group for a specified namespace or workload, create a container network configuration and associate it with the target namespace or workload. In this way, service subnets can be planned or services can be securely isolated.
Category |
Resources a Container Network Configuration Can Associate with |
|
---|---|---|
Namespace |
Workload |
|
Subnet and security group configurations |
All workloads created in the namespace associated with a container network configuration use the same subnet and security group configurations. |
The workloads associated with the same container network configuration use the same subnet and security group configurations. |
Supported cluster versions |
Available only in CCE Turbo clusters of 1.23.8-r0, 1.25.3-r0, or later. |
Available only in CCE Turbo clusters of 1.23.11-r0, 1.25.6-r0, 1.27.3-r0, 1.28.1-r0, or later. |
Constraints |
The namespaces associated with different container network configurations must be unique. |
Only the custom container network configurations that are not associated with any namespace can be specified. |
If default-network is available in the cluster, it takes effect on all pods where no custom container network configuration has been configured. The default container subnet in the network settings on the Overview page is the container subnet in default-network.
If default-network is available in the cluster, it takes effect on all pods where no custom container network configuration has been configured. The default container subnet in the network settings on the Overview page is the container subnet in default-network.
Return to the Settings page. In the container network configuration list, the name of the resource associated with the created container network configuration is displayed.
This section describes how to use kubectl to create a container network configuration of the namespace type.
vi networkattachment-test.yaml
apiVersion: k8s.cni.cncf.io/v1 kind: NetworkAttachmentDefinition metadata: annotations: yangtse.io/project-id: 05e38** name: example namespace: kube-system spec: config: '{ "type":"eni-neutron", "args":{ "securityGroups":"41891**", "subnets":[ { "subnetID":"27d95**" } ] }, "selector":{ "namespaceSelector":{ "matchLabels":{ "kubernetes.io/metadata.name":"default" } } } }'
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
apiVersion |
Yes |
String |
API version. The value is fixed at k8s.cni.cncf.io/v1. |
kind |
Yes |
String |
Type of the object to be created. The value is fixed at NetworkAttachmentDefinition. |
yangtse.io/project-id |
Yes |
String |
Project ID. |
name |
Yes |
String |
Configuration item name. |
namespace |
Yes |
String |
Namespace of the configuration resource. The value is fixed to kube-system. |
config |
Yes |
Table 3 object |
Configuration content, which is a string in JSON format. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
type |
Yes |
String |
The value is fixed at eni-neutron. |
args |
No |
object |
Configuration parameters. |
selector |
No |
Table 5 object |
Namespace on which the configuration takes effect. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
securityGroups |
No |
String |
Security group ID. If no security group is planned, ensure that the security group is the same as that in default-network. How to obtain: Log in to the VPC console. In the navigation pane, choose Access Control > Security Groups. Click the target security group name and copy the ID on the Summary tab page. |
subnets |
Yes |
Array of subnetID Objects |
List of container subnet IDs. At least one subnet ID must be entered. The format is as follows: [{"subnetID":"27d95**"},{"subnetID":"827bb**"},{"subnetID":"bdd6b**"}] Subnet ID not used by the cluster in the same VPC. How to obtain: Log in to the VPC console. In the navigation pane, choose Virtual Private Cloud > Subnets. Click the target subnet name and copy the Subnet ID on the Summary tab page. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
namespaceSelector |
No |
matchLabels Object |
A Kubernetes standard selector. Enter the namespace label in the following format: "matchLabels":{ "kubernetes.io/metadata.name":"default" } The namespaces of different configurations cannot overlap. |
kubectl create -f networkattachment-test.yaml
If information similar to the following is displayed, the NetworkAttachmentDefinition has been created.
networkattachmentdefinition.k8s.cni.cncf.io/example created
This section describes how to use kubectl to create a container network configuration of the workload type.
vi networkattachment-test.yaml
apiVersion: k8s.cni.cncf.io/v1 kind: NetworkAttachmentDefinition metadata: annotations: yangtse.io/project-id: 05e38** name: example namespace: kube-system spec: config: '{ "type":"eni-neutron", "args":{ "securityGroups":"41891**", "subnets":[ { "subnetID":"27d95**" } ] }'
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
apiVersion |
Yes |
String |
API version. The value is fixed at k8s.cni.cncf.io/v1. |
kind |
Yes |
String |
Type of the object to be created. The value is fixed at NetworkAttachmentDefinition. |
yangtse.io/project-id |
Yes |
String |
Project ID. |
name |
Yes |
String |
Configuration item name. |
namespace |
Yes |
String |
Namespace of the configuration resource. The value is fixed to kube-system. |
config |
Yes |
Table 3 object |
Configuration content, which is a string in JSON format. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
type |
Yes |
String |
The value is fixed at eni-neutron. |
args |
No |
object |
Configuration parameters. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
securityGroups |
No |
String |
Security group ID. If no security group is planned, select the same security group as that in default-network. How to obtain: Log in to the VPC console. In the navigation pane, choose Access Control > Security Groups. Click the target security group name and copy the ID on the Summary tab page. |
subnets |
Yes |
Array of subnetID Objects |
List of container subnet IDs. At least one subnet ID must be entered. The format is as follows: [{"subnetID":"27d95**"},{"subnetID":"827bb**"},{"subnetID":"bdd6b**"}] Subnet ID not used by the cluster in the same VPC. How to obtain: Log in to the VPC console. In the navigation pane, choose Virtual Private Cloud > Subnets. Click the target subnet name and copy the Subnet ID on the Summary tab page. |
kubectl create -f networkattachment-test.yaml
If information similar to the following is displayed, the NetworkAttachmentDefinition has been created.
networkattachmentdefinition.k8s.cni.cncf.io/example created
apiVersion: apps/v1 kind: Deployment metadata: name: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx yangtse.io/network: "example" # Name of the custom container network configuration, which can be used to obtain all pods associated with the container network configuration by label annotations: yangtse.io/network: "example" # Name of the custom container network configuration spec: containers: - name: container-0 image: nginx:alpine resources: limits: cpu: 100m memory: 200Mi requests: cpu: 100m memory: 200Mi imagePullSecrets: - name: default-secret
You can delete the new container network configuration or view its YAML file.
Before deleting a container network configuration, delete all pods using the configuration. Otherwise, the deletion will fail.
kubectl get po -A -o=jsonpath="{.items[?(@.metadata.annotations.cni\.yangtse\.io/network-status=='[{\"name\":\"example\"}]')]['metadata.namespace', 'metadata.name']}"
The command output contains the pod name and namespace associated with the configuration.