Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Network Policies

Updated on 2024-01-26 GMT+08:00

Network policies are designed by Kubernetes to restrict pod access. It is equivalent to a firewall at the application layer to enhance network security. The capabilities supported by network policies depend on the capabilities of the network add-ons of the cluster.

By default, if a namespace does not have any policy, pods in the namespace accept traffic from any source and send traffic to any destination.

Network policies are classified into the following types:

  • namespaceSelector: selects particular namespaces for which all pods should be allowed as ingress sources or egress destinations.
  • podSelector: selects particular pods in the same namespace as the network policy which should be allowed as ingress sources or egress destinations.
  • ipBlock: selects particular IP blocks to allow as ingress sources or egress destinations. (Only egress rules support IP blocks.)

Constraints

  • Only clusters that use the tunnel network model support network policies. Network policies are classified into the following types:
    • Ingress: All versions support this type.
    • Egress: Only the following OSs and cluster versions support egress rules.

      OS

      Cluster Version

      Verified Kernel Version

      CentOS

      v1.23 or later

      3.10.0-1062.18.1.el7.x86_64

      3.10.0-1127.19.1.el7.x86_64

      3.10.0-1160.25.1.el7.x86_64

      EulerOS 2.5

      v1.23 or later

      3.10.0-862.14.1.5.h591.eulerosv2r7.x86_64

      3.10.0-862.14.1.5.h687.eulerosv2r7.x86_64

      EulerOS 2.9

      v1.23 or later

      4.18.0-147.5.1.6.h541.eulerosv2r9.x86_64

      4.18.0-147.5.1.6.h766.eulerosv2r9.x86_64

  • Network isolation is not supported for IPv6 addresses.
  • If upgrade to a cluster version that supports egress rules is performed in in-place mode, you cannot use egress rules because the node OS is not upgraded. In this case, reset the node.

Using Ingress Rules

  • Using podSelector to specify the access scope
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: test-network-policy
      namespace: default
    spec:
      podSelector:                  # The rule takes effect for pods with the role=db label.
        matchLabels:
          role: db
      ingress:                      # This is an ingress rule.
      - from:
        - podSelector:              # Only traffic from the pods with the "role=frontend" label is allowed.
            matchLabels:
              role: frontend
        ports:                      # Only TCP can be used to access port 6379.
        - protocol: TCP
          port: 6379

    The following figure shows how podSelector works.

    Figure 1 podSelector
  • Using namespaceSelector to specify the access scope
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: test-network-policy
    spec:
      podSelector:                  # The rule takes effect for pods with the role=db label.
        matchLabels:
          role: db
      ingress:                      # This is an ingress rule.
      - from:
        - namespaceSelector:        # Only traffic from the pods in the namespace with the "project=myproject" label is allowed.
            matchLabels:
              project: myproject
        ports:                      # Only TCP can be used to access port 6379.
        - protocol: TCP
          port: 6379

    The following figure shows how namespaceSelector works.

    Figure 2 namespaceSelector

Using Egress Rules

Egress supports not only podSelector and namespaceSelector, but also ipBlock.

NOTE:

Only clusters of version 1.23 or later support egress rules. Currently, nodes running EulerOS 2.5, EulerOS 2.9, and CentOS 7.X are supported.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-client-a-via-except-cidr-egress-rule
  namespace: default
spec:
  policyTypes:                  # Must be specified for an egress rule.
    - Egress
  podSelector:                  # The rule takes effect for pods with the role=db label.
    matchLabels:
      role: db
  egress:                       # Egress rule
  - to:
    - ipBlock:
        cidr: 172.16.0.16/16    # Allow access to this CIDR block.
        except:
        - 172.16.0.40/32        # This CIDR block cannot be accessed. This value must fall within the range specified by cidr.

The following figure shows how ipBlock works.

Figure 3 ipBlock

You can define ingress and egress in the same rule.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: test-network-policy
  namespace: default
spec:
  policyTypes:
  - Ingress
  - Egress
  podSelector:                  # The rule takes effect for pods with the role=db label.
    matchLabels:
      role: db
  ingress:                      # This is an ingress rule.
  - from:
    - podSelector:              # Only traffic from the pods with the "role=frontend" label is allowed.
        matchLabels:
          role: frontend
    ports:                      # Only TCP can be used to access port 6379.
    - protocol: TCP
      port: 6379
  egress:                       # Egress rule
  - to:
    - podSelector:              # Only pods with the role=web label can be accessed.
        matchLabels:
          role: web

The following figure shows how to use ingress and egress together.

Figure 4 Using both ingress and egress

Creating a Network Policy on the Console

  1. Log in to the CCE console and click the cluster name to access the cluster console.
  2. Choose Networking in the navigation pane, click the Network Policies tab, and click Create Network Policy in the upper right corner.

    • Policy Name: Specify a network policy name.
    • Namespace: Select a namespace in which the network policy is applied.
    • Selector: Enter a label, select the pod to be associated, and click Confirm. You can also click Reference Workload Label to reference the label of an existing workload.
    • Inbound Rule: Click to add an inbound rule. For details about parameter settings, see Table 1.

      Table 1 Adding an inbound rule

      Parameter

      Description

      Protocol & Port

      Select the protocol type and port. Currently, TCP and UDP are supported.

      Source Namespace

      Select a namespace whose objects can be accessed. If this parameter is not specified, the object belongs to the same namespace as the current policy.

      Source Pod Label

      Allow accessing the pods with this label. If this parameter is not specified, all pods in the namespace can be accessed.

  3. Click OK.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback