Skip to content

Cloud Provider Interface (CPI)

The Cloud Provider Interface (CPI) is a Kubernetes standard that allows Kubernetes clusters to integrate with cloud providers for managing cloud-specific infrastructure. It enables provisioning of resources like nodes, load balancers, and persistent storage by abstracting cloud platform details.

Below is an example of integrating a Kubernetes cluster with a Cloud Provider Interface (CPI) for managing a Load Balancer on a cloud provider.


Example Use Case: Exposing a Service Using Cloud Load Balancer

This example demonstrates how the Cloud Provider Interface enables Kubernetes to provision a cloud-based load balancer to expose a service.


1. Prerequisites

  • A Kubernetes cluster running on a supported cloud provider (AWS, GCP, Azure).
  • The Cloud Controller Manager for the cloud provider must be deployed and configured.

2. Deployment and Service Configuration

Sample Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 80

Service with Load Balancer

The following service definition provisions a cloud load balancer using the CPI:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: "nlb" # AWS-specific annotation (optional)
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Explanation:

  • Type: LoadBalancer: Tells Kubernetes to provision an external cloud load balancer.
  • Annotations: Optional cloud-specific configurations (e.g., specifying the load balancer type in AWS).
  • Selector: Targets pods labeled with app: nginx.

3. How It Works

  1. When the service is created with type: LoadBalancer, the CPI Cloud Controller Manager interacts with the cloud provider’s API.
  2. A cloud load balancer (e.g., AWS Elastic Load Balancer, Azure Load Balancer, or GCP Load Balancer) is provisioned automatically.
  3. The load balancer routes external traffic to the Kubernetes service, which forwards requests to the backend pods.

4. Verifying the Load Balancer

After creating the service, run the following command to verify the provisioned load balancer:

kubectl get services

Output Example:

NAME            TYPE           CLUSTER-IP     EXTERNAL-IP       PORT(S)        AGE
nginx-service   LoadBalancer   10.0.0.1       a1b2c3d4e5.elb.amazonaws.com   80:30080/TCP   5m
  • EXTERNAL-IP: Shows the address of the provisioned cloud load balancer.
  • Traffic sent to this external IP will be forwarded to the service and its backend pods.

Supported Cloud Providers

Cloud Provider CPI Implementation Features
AWS AWS Cloud Controller Manager Load balancers, EBS storage, node management
GCP GCE Cloud Controller Manager Load balancers, PD storage, node scaling
Azure Azure Cloud Controller Manager Load balancers, Disk storage, scaling

Conclusion

The Cloud Provider Interface (CPI) allows Kubernetes to integrate seamlessly with cloud providers for provisioning cloud infrastructure like load balancers, persistent volumes, and nodes. By defining a Service with type: LoadBalancer, Kubernetes leverages the CPI to interact with the cloud provider’s API and automatically provision a load balancer for external traffic.