Add a floating IP to a pod

Big picture

Configure one or more floating IPs that can be used as additional IP addresses for reaching a Kubernetes pod.

Value

Like Kubernetes Services, a floating IP provides a stable IP address to reach some network service that might be backed by different pods at different times. The primary advantage over Kubernetes services is that floating IPs work on all protocols: not just TCP, UDP, and SCTP. Unlike Kubernetes services, a floating IP fronts a single pod at a time and cannot be used for load balancing.

Features

This how-to guide uses the following Calico features:

Calico CNI configuration file with floating_ips enabled

Concepts

A floating IP is an additional IP address assigned to a workload endpoint. These IPs “float” in the sense that they can be moved around the cluster and front different workload endpoints at different times. The workload itself is generally unaware of the floating IP; the host uses network address translation (NAT) on incoming traffic to change the floating IP to the workload’s real IP before delivering packets to the workload.

A Kubernetes Service assigns a cluster IP that allows other endpoints on the network (and may also assign a nodePort and/or an external load balancer IP) to access a set of pods, using network address translation. In many circumstances, a Kubernetes Service can handle similar use cases as a floating IP, and is generally recommended for Kubernetes users because it is a native Kubernetes concept. One thing you cannot do with Kubernetes Services is use protocols other than UDP, TCP, and SCTP (use of such protocols is fairly rare).

Before you begin…

The features in this How to require:

  • Calico CNI plugin

To verify, ssh to one of the Kubernetes nodes and look for at the CNI plugin configuration, usually located at /etc/cni/net.d/. If you see the file, 10-calico.conflist, you are using the Calico CNI plugin.

How to

Enable floating IPs

  • Operator
  • Manifest

Floating IPs for Kubernetes pods are not currently supported for operator-managed Calico clusters.

By default, floating IPs are disabled. To enable floating IPs, follow these steps.

Modify the calico-config ConfigMap in the kube-system namespace. In the cni_network_config section, add the following stanza to the “calico” plugin config section.

  1. "feature_control": {
  2. "floating_ips": true
  3. }

For example, your cni_network_config will look similar to the following after the update.

  1. cni_network_config: |-
  2. {
  3. "name": "k8s-pod-network",
  4. "cniVersion": "0.3.0",
  5. "plugins": [
  6. {
  7. "type": "calico",
  8. "log_level": "info",
  9. "datastore_type": "kubernetes",
  10. "nodename": "__KUBERNETES_NODE_NAME__",
  11. "mtu": __CNI_MTU__,
  12. "ipam": {
  13. "type": "calico-ipam"
  14. },
  15. "policy": {
  16. "type": "k8s"
  17. },
  18. "kubernetes": {
  19. "kubeconfig": "__KUBECONFIG_FILEPATH__"
  20. },
  21. "feature_control": {
  22. "floating_ips": true
  23. }
  24. },
  25. {
  26. "type": "portmap",
  27. "snat": true,
  28. "capabilities": {"portMappings": true}
  29. }
  30. ]
  31. }

Configure a pod to use a floating IP

  • Operator
  • Manifest

Floating IPs for Kubernetes pods are not currently supported for operator-managed Calico clusters.

Annotate the pod with the key cni.projectcalico.org/floatingIPs and the value set to a list of IP addresses enclosed in square brackets. For correct advertisement to the rest of the cluster, all floating IPs must be within the range of a configured IP pool.

For example:

  1. "cni.projectcalico.org/floatingIPs": "[\"10.0.0.1\"]"

Note the use of the escaped \" for the inner double quotes around the addresses.