Canal: A Deep Dive into Kubernetes Network Plug-in

Shingai Zivuku
11 min readOct 27, 2023

Canal, a powerful network plug-in based on the MySQL protocol, serves as a bridge between the MySQL database and the Kubernetes cluster. It synchronizes binlog events from the MySQL database to various resources within the Kubernetes cluster, including Pods, Services, and more.

This synchronization capability of Canal paves the way for a seamless integration between your MySQL database and Kubernetes cluster. Whether you’re updating your database or scaling your applications within Kubernetes, Canal ensures that these changes are reflected across both platforms in real-time.

Photo by Milind Shah on Unsplash

In essence, Canal is not just a network plug-in; it’s a vital tool that enhances the interoperability of your MySQL databases and Kubernetes clusters, thereby optimizing your data management and application deployment processes. Let’s delve deeper into its workings and benefits.

The key concepts of Canal include :

Data flow: The core of Canal is the flow of data. It listens to the binlog events of the MySQL database converts these events into a data format that Kubernetes can understand and then sends the data to the specified Kubernetes resource.

Event conversion: Canal provides a complete set of event conversion rules that can convert MySQL binlog events into Kubernetes resource objects. In this way, we can manage and operate these resources through the Kubernetes API.

Data persistence: In order to ensure data integrity and consistency, Canal provides data persistence function. It will store the received data locally. When needed, the data can be read from local or written to the MySQL database.

Network functions: Canal can provide network functions for Kubernetes clusters, including network interconnection, network isolation, and routing. It implements a software-defined network (SDN) solution that allows containers and Pods to communicate with each other and connect to networks outside the cluster by creating virtual networks and network policies.

Network isolation: Canal has powerful network isolation capabilities and can isolate different namespaces or different Pod groups in the Kubernetes cluster to prevent communication between them. This isolation capability can help users achieve security and performance isolation in a multi-tenant environment.

Network routing: Canal can provide flexible network routing functions for Kubernetes clusters. It supports load balancing based on IP addresses and ports and can direct traffic to different Pods or services based on user-defined rules to achieve high availability and load balancing.

Environmental adaptability: Canal can adapt to different network environments, including cloud environments and local environments. It supports multiple network models such as Virtual LAN (VLAN), VXLAN, Overlay, etc., and can be integrated with various network devices and cloud service providers.

Container network access: Canal can provide network access capabilities for containers, allowing containers to communicate with other containers or external services through the network. It supports different network protocols and technologies such as IP, IPv6, VLAN, VXLAN, UDP/TCP, etc.

Plug-in ecosystem: Canal is an open plug-in architecture that provides a rich plug-in ecosystem. Users can choose suitable plug-ins according to their own needs, such as network policy plug-ins, load balancing plug-ins, and log plug-ins, to meet different usage scenarios.

Implementation

Canal operates on the principle of MySQL master-slave replication. In this setup, Canal emulates the interaction protocol of a MySQL slave. It masquerades as a MySQL slave and communicates with the MySQL master using the dump protocol.

Upon receiving the dump request from Canal, the MySQL master begins to transmit binary logs to Canal. Canal, in turn, parses these binary logs and forwards them to the designated storage destination. This could be another MySQL database, a Kafka stream, an Elastic Search index, or any other data storage system.

Canal’s workflow is as follows:

  1. The client initiates a connection request to the Canal Server cluster.
  2. Upon receiving the request, a node within the Canal Server cluster acknowledges the successful establishment of the connection.
  3. The client then proceeds to establish individual connections with each node in the Canal Server cluster.
  4. Upon receiving a start command from the client, every node in the Canal Server cluster issues a dump command to the MySQL database and forwards the resulting binary log events back to the client.
  5. The client parses these binary log events and transforms them into JSON-formatted data.
  6. Finally, the client dispatches this converted data to a designated storage destination, which could be MySQL, Kafka, Elastic Search, or others.

Architecture Introduction

Canal, a network plug-in built upon the foundations of the Calico and Flannel projects, is utilized to implement container network functions and isolation.

Here’s a more detailed breakdown of Canal’s architecture:

  • Control Plane: This is the brain of the network, responsible for making decisions about where traffic is sent.
  • Data Plane: This component is responsible for processing and forwarding packets to their destinations based on decisions made by the Control Plane.
  • Component Interaction: This refers to how various parts of the system communicate and work together to provide seamless network functionality.

Each of these components plays a crucial role in ensuring efficient network operations within Canal.

Control Plane

  • etcd Storage: Canal uses etcd as a distributed key-value store for preserving network status and configuration data. The high availability and consistency provided by etcd ensure the reliability of the cluster’s network status and configuration.
  • Flannel Controller: As a part of the control plane, Canal utilizes the Flannel controller. This controller is tasked with monitoring events from the Kubernetes API Server, such as Pod creation, deletion, and updates, along with network policy alterations. It translates these events into network configuration updates and commits them to etcd storage.
  • Network Policy Controller: Another integral component of the Canal control plane is the Network Policy Controller. It manages the configuration and updates of network policies. By listening to network policy events on the Kubernetes API Server, it converts them into network policy configuration data and stores it in etcd storage.

Data Plane

  • Network Proxy: Canal operates one or more network proxy components on each node. These components are responsible for managing network connections and data forwarding. They monitor the network status and configuration information in etcd and carry out operations accordingly. The network proxy component manages the network interfaces and routing tables on nodes and handles the routing and forwarding of network traffic.
  • Calico Data Plane: Canal utilizes Calico’s data plane to deliver high-performance network forwarding and security features. The Calico data plane comprises components such as routers (Route Reflectors), BGP Peering, routing tables, and ACLs (Access Control Lists). These components facilitate IP address management and network traffic forwarding.

Component interaction

The interaction between the control plane and the data plane is facilitated through etcd storage. The control plane records network configuration and status information into etcd. In response, the data plane keeps track of change events in etcd and executes operations based on these changes.

The control plane communicates directly with the Kubernetes API Server via the API. This communication allows the control plane to access Pod and network policy information, as well as monitor related events.

Leveraging the above architecture, Canal effectively implements network functions and isolation of Pods within the Kubernetes cluster. It employs Calico’s high-performance data plane to deliver robust network forwarding and security features.

Simultaneously, through its integration with the Flannel controller and network policy controller, Canal is capable of executing functions such as IP address management, network policy configuration, and dynamic updates.

In essence, Canal caters to the networking requirements in containerized application scenarios and offers a reliable solution for container networking.

Usage scenarios

Canal provides management capabilities for network functions and policies.

It is suitable for various scenarios, including network isolation in multi-tenant environments, high-availability and load-balancing network routing, and container network access.

Whether in the cloud or on-premises, Canal provides reliable networking.

Advantages of Canal

When discussing the pros and cons of Canal, it is important to note that it is a specific network plugin, so its pros and cons are relative to other network plugins.

The following are the advantages of Canal:

  1. Comprehensive network functions: Canal provides a wide range of network functions, including network interconnection, network isolation, and routing. It supports multiple network models and protocols and has flexible network routing capabilities that can manage traffic according to user-defined rules. This enables users to build complex network topologies and policies based on user needs.
  2. Multi-tenant support: Canal has strong network isolation capabilities to provide security and performance isolation for multi-tenant environments. It can isolate different namespaces or Pod groups to prevent communication between them, thereby protecting tenant data and applications.
  3. Environmental adaptability: Canal can adapt to different network environments, including cloud environments and local environments. It supports multiple network models and technologies and can integrate with a variety of network devices and cloud service providers. This enables users to use Canal in different deployment environments without changing the network architecture.
  4. Plug-in ecosystem: Canal provides a rich plug-in ecosystem, and users can choose suitable plug-ins according to their needs. This allows users to extend Canal’s functionality, including network policy, load balancing, and logging.

Disadvantages of Canal

  1. Learning curve: Since Canal is a relatively complex network plug-in, using it may require a certain cost of learning and understanding. You need to be familiar with Canal concepts, configuration, and management methods to ensure that it is deployed and used correctly.
  2. Configuration complexity: Canal can be relatively complex to configure, especially when advanced network features and policies need to be implemented. You may need to dig deeper into Canal’s configuration options and parameters to ensure the network is configured and managed correctly.
  3. Performance overhead: Like other network plug-ins, Canal also introduces certain performance overhead. Especially when dealing with large amounts of network traffic and complex network routing, performance may be affected. You need to evaluate and optimize network configurations to meet application performance requirements.

You should note that the advantages and disadvantages of Canal are not absolute, and they may vary depending on user needs, deployment environment, and other factors.

Before choosing and using Canal, I recommend you carefully evaluate its pros and cons and compare it with other network plug-ins to choose the solution that best suits you.

Installation and use

Step 1: Prepare the Kubernetes cluster

First, make sure you have a running Kubernetes cluster. This can be a local cluster or a cloud provider’s hosted cluster.

Step 2: Download Canal

Download the latest version of Canal from the official Canal GitHub repository.

Step 3: Deploy Canal

Use Kubectl or other Kubernetes deployment tools to deploy Canal to your Kubernetes cluster. Canal can be deployed as a Kubernetes Deployment using the following command:

kubectl apply -f canal.yaml

Among them, canal.yaml is a YAML file containing Canal deployment configuration. You can adjust the parameters in the configuration file as needed.

Step 4: Wait for deployment to complete

Wait for Canal to complete deployment in the Kubernetes cluster. You can check the status of a Pod using the following command:

kubectl get pods -n kube-system

Ensure that Canal-related Pods are running properly and in ready state.

Step 5: Verify the network plug-in

Once Canal deployment is complete, you can verify that the network plug-in is working properly. You can create a simple test Pod and try to access it from other Pods or from outside the cluster. If network communication is normal, Canal has been successfully installed and configured.

Step 6: Configure network policy (optional)

Depending on your needs, you can configure Canal’s network policies. Network policies can define rules that allow or deny communication between Pods to achieve stricter network isolation and security. You can write a network policy YAML file and apply it to your Kubernetes cluster using the kubectl apply command.

Verify that the network plug-in is installed successfully

To verify that Cabal is installed successfully and works properly, you can do the following:

Check the running status of the plug-in:

Use the following command to check whether the Pod related to the plug-in is running:

kubectl get pods -n kube-system

Make sure that the Pod associated with Canal is running and that the number of replicas in the READY column is the required number. If there are any errors or exceptions, check the relevant Pod’s logs for more details.

Create a test Pod:

Create a simple test Pod to verify network communication with other Pods or outside the cluster. A simple Nginx Pod can be created using the following YAML file:

apiVersion: v1
kind: Pod
metadata:
name: nginx-test
spec:
containers:
- name: nginx
image: nginx

Specific steps:

Save the above YAML file as nginx-test.yaml and create the Pod using the following command:

kubectl apply -f nginx-test.yaml

After the Pod is created, you can check its status using the following command:

kubectl get pods

Make sure the test pod is running and the number of replicas in the READY column is 1.

Test network connectivity: Use the following command to perform a network connectivity test in the test pod:

kubectl exec -it nginx-test -- curl <Target IP or domain>

Replace <target IP or domain name> with the target IP address or domain name you want to test. If network communication is normal, you will see a response from the target.

You can also test network connectivity to the test Pod by executing the same command in other Pods.

By performing the above steps, you can verify that the network plug-in installation was successful and ensure that network communication between Pods is functioning properly.

If you encounter any problems, you can check the relevant logs, configurations, and documents to further troubleshoot and solve the problem.

FAQ

During the use of Canal, you may encounter some common problems. Here are some possible problems and their solutions:

Canal Pod is in a non-running state or cannot be started:

  • Check whether the network of the Kubernetes cluster is normal and ensure that the dependent components of the network plug-in (such as etcd) are running normally.
  • Check the Canal Pod’s logs for more details. Use the following command to view the logs: kubectl logs <canal-pod-name> -n kube-system
  • Ensure that the Pod’s resource allocation (such as CPU, and memory) is sufficient to meet operating requirements.

Network communication is blocked or external services cannot be accessed:

  • Check the network policy is configured correctly to ensure that communication between Pods or with external services is allowed.
  • Check the routing rules of the network plug-in to ensure that traffic is correctly forwarded to the target Pod or service.
  • Make sure that the network plug-in’s network isolation feature is not blocking the required network traffic

The Pod cannot obtain an IP address or is not reachable over the network:

  • Check whether there is an IP address allocation problem and ensure that Canal correctly allocates IP addresses to Pods.
  • Ensure that the node’s network interface (such as the CNI interface) is properly configured and compatible with the network plug-in.
  • Check the node’s network configuration to ensure that the network plug-in can correctly manage the Pod’s network connections.

Problems occurred after upgrading the Canal version:

  • Before upgrading your Canal version, read the release notes and documentation carefully to learn about possible compatibility issues and configuration changes.
  • Review the configuration requirements and key points for the new version and ensure that appropriate configuration changes are made as needed.
  • Back up and restore critical data during the upgrade process to prevent data loss or irrecoverable damage.

Performance issues or excessive load:

  • Evaluate the resource configuration of the cluster to ensure that nodes have sufficient CPU, memory, and network bandwidth.
  • Adjust Canal’s configuration parameters according to actual load conditions, such as the number of concurrent connections, connection timeout, etc.
  • Use performance monitoring tools (such as Prometheus + Grafana) to monitor and analyze the performance indicators of the cluster, identify bottlenecks, and optimize.

Conclusion

Canal is a powerful network plug-in that provides network functions and container network isolation for Kubernetes clusters.

Its design is flexible and scalable to meet containerized application scenarios of different sizes and needs.

--

--

Shingai Zivuku

Passionate about technology and driven by a love for learning and sharing knowledge