Public IP operator for Kubernetes

While experimenting with ExternalDNS, I found that I needed a way to use my public IP address in custom resources without having to manually keep them up to date.

To solve this problem, I created a new Kubernetes operator called pubip-operator that automatically fetches your public IP address and updates resources in the cluster. The operator monitors your IP address and automatically updates any resources you specify when it changes. This is useful for Kubernetes clusters running on a home network or other networks that have a dynamic IP address.

How it Works

The operator is built using Kubebuilder, a popular framework for building Kubernetes operators. It introduces a new custom resource called PublicIPUpdater that defines which sources to get the public IP from and which resources to update.

The operator supports several popular IP lookup services including Akamai, AWS CheckIP, ipify, and ipinfo. By using multiple sources, the operator can continue working even if one service is temporarily unavailable.

Once the operator detects a change in your public IP, it automatically updates all the resources you've specified in your PublicIPUpdater configuration. This happens seamlessly in the background without any manual intervention.

Installation

The operator can be installed with Helm in a single command:

helm install pubip-operator oci://ghcr.io/olav-st/pubip-operator/charts/pubip-operator \
  --namespace pubip-operator --create-namespace --version <VERSION>

Replace <VERSION> with the most recent version from this page.

Configuration

After installing the operator, you configure its behavior by creating a PublicIPUpdater resource. Here's a simple example that updates a ConfigMap with your current public IP:

The sources field specifies which IP lookup services to query. By default, the operator will use the first source that returns a successful response, but it can also be configured to query all the sources and only update the cluster if they all return the same IP. The targets field defines which resources should be updated and where to store the IP address within those resources.

The fieldPath uses a dot-notation to specify exactly where in the resource the IP should be stored. In this example, data.PUBLIC_IP means the IP will be stored in the PUBLIC_IP key within the ConfigMap's data section.

The operator isn't limited to ConfigMaps. You can update any Kubernetes resource, making it flexible enough to integrate with various use cases. For example, you could update a Service annotation, a custom resource definition, or any other field in your cluster.

Use Cases

In my Homelab, I use pubip-operator to keep my DNSEndpoint resources up to date. When my ISP changes my public IP, the operator automatically updates the DNSEndpoint resources and ExternalDNS in turn updates my publicly facing DNS records.

Other potential use cases include:

  • Configuring firewall rules that need to know your current IP
  • Updating webhook endpoints that external services use to reach your cluster
  • Maintaining IP allowlists in security policies
Implementation Details

The operator is built to be extensible, so adding support for new IP sources should be fairly straightforward. See the cmd/fetcher/sources folder for how the existing sources are implemented. Contributions of new sources are welcome!

The project is open source and available on GitHub under the Apache 2.0 license. The repository includes documentation, examples, and contribution guidelines.

Conclusion

For anyone running Kubernetes on a network with a dynamic IP address, pubip-operator provides a simple, declarative way to keep your cluster resources synchronized with your current public IP. It eliminates manual updates and integrates seamlessly with the Kubernetes ecosystem.

Please give it a try if you're also facing challenges with dynamic IP addresses in your Kubernetes environment!