Crossplane Tutorial Infrastructure as Code with Kubernetes

Zaheer Ahmad 5 min read min read
Python
Crossplane Tutorial Infrastructure as Code with Kubernetes

Introduction

Crossplane is an open-source Kubernetes extension that allows you to manage cloud infrastructure using the same declarative approach you use for Kubernetes workloads. In simple terms, instead of writing Terraform scripts or manually configuring AWS resources, you define infrastructure using Kubernetes YAML files.

A crossplane tutorial: infrastructure as code with kubernetes is essential for modern DevOps engineers because it teaches how to treat infrastructure like application code. With Crossplane, you can provision databases, virtual machines, networking components, and storage directly from Kubernetes clusters.

For Pakistani students learning DevOps in cities like Lahore, Karachi, and Islamabad, Crossplane is a powerful skill. Many local startups and international companies hiring remotely from Pakistan are moving toward Kubernetes-based infrastructure platforms. Learning Crossplane gives you an edge in cloud-native DevOps careers where salaries can reach from PKR 150,000 to PKR 500,000+ per month depending on experience.

Crossplane is especially useful when combined with kubernetes infrastructure automation and cloud providers like AWS.

Prerequisites

Before starting this Crossplane tutorial, you should have:

  • Basic understanding of Kubernetes (Pods, Deployments, Services)
  • Familiarity with YAML syntax
  • Basic cloud knowledge (AWS or similar platforms)
  • kubectl installed and configured
  • A running Kubernetes cluster (Minikube, Kind, or EKS)
  • AWS account for practicing crossplane aws provisioning

Recommended prior learning:

For Pakistani learners, even a local Minikube setup on a laptop (Intel i5, 8GB RAM) is enough to practice Crossplane concepts.


Core Concepts & Explanation

Kubernetes-Native Infrastructure as Code Model

Crossplane extends Kubernetes using Custom Resource Definitions (CRDs). Instead of writing infrastructure scripts, you define resources like:

  • RDS databases
  • S3 buckets
  • VPC networks

Example:

apiVersion: database.example.org/v1
kind: MySQLInstance
metadata:
  name: student-db
spec:
  storageGB: 20
  region: us-east-1

This YAML tells Kubernetes (via Crossplane) to create a managed database.

How it works:

  • apiVersion defines the API group
  • kind defines the type of infrastructure
  • spec defines desired state

Crossplane continuously reconciles this state.


Crossplane Compositions and Composite Resources

Crossplane introduces two powerful concepts:

Composite Resource Definition (XRD)

Defines what users can request.

Example:

apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
  name: xdatabases.example.org
spec:
  group: example.org
  names:
    kind: XDatabase
    plural: xdatabases
  claimNames:
    kind: Database
    plural: databases

Explanation:

  • CompositeResourceDefinition defines a new infrastructure API
  • XDatabase is the internal resource
  • Database is what developers use

Composition

Maps abstract resources to real cloud resources like AWS.

Example:

apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: aws-database
spec:
  resources:
    - name: rds-instance
      base:
        apiVersion: rds.aws.crossplane.io/v1beta1
        kind: DBInstance
        spec:
          forProvider:
            engine: mysql
            dbInstanceClass: db.t3.micro

Explanation:

  • Composition defines how infrastructure is built
  • DBInstance is AWS RDS resource
  • engine: mysql defines database type
  • dbInstanceClass defines compute size


Practical Code Examples

Example 1: Installing Crossplane on Kubernetes

apiVersion: v1
kind: Namespace
metadata:
  name: crossplane-system

Explanation:

  • Creates a namespace for Crossplane components
  • Keeps infrastructure tools isolated
helm repo add crossplane-stable https://charts.crossplane.io/stable

Explanation:

  • Adds Crossplane Helm repository
  • Helm manages Kubernetes package installation
helm install crossplane crossplane-stable/crossplane -n crossplane-system

Explanation:

  • Installs Crossplane in Kubernetes cluster
  • Deploys controllers and CRDs

Example 2: Real-World AWS Database Provisioning (Crossplane AWS)

apiVersion: aws.database.crossplane.io/v1beta1
kind: RDSInstance
metadata:
  name: ali-production-db
spec:
  forProvider:
    region: us-east-1
    dbInstanceClass: db.t3.micro
    engine: postgres
    masterUsername: admin
    allocatedStorage: 20
  writeConnectionSecretToRef:
    name: db-connection
    namespace: default

Explanation line by line:

  • apiVersion: AWS Crossplane provider API
  • kind: RDS instance resource type
  • metadata.name: name of database (Ali’s production DB example)
  • region: AWS region
  • dbInstanceClass: compute size
  • engine: PostgreSQL database
  • masterUsername: admin user
  • allocatedStorage: 20GB storage
  • writeConnectionSecretToRef: stores credentials securely in Kubernetes secrets

In a Pakistani startup scenario, Ahmad from Lahore can deploy a backend database for an e-commerce app without manually logging into AWS console.



Common Mistakes & How to Avoid Them

Mistake 1: Not Configuring Provider Credentials Properly

Many beginners forget to configure AWS credentials.

Wrong approach:

  • Installing Crossplane only
  • Trying to create AWS resources immediately

Fix:

apiVersion: aws.crossplane.io/v1beta1
kind: ProviderConfig
metadata:
  name: default
spec:
  credentials:
    source: Secret
    secretRef:
      name: aws-creds
      namespace: crossplane-system

Explanation:

  • ProviderConfig connects Kubernetes to AWS
  • Secret stores secure credentials
  • Without this, Crossplane cannot provision resources

Mistake 2: Confusing XRD and Composition

Students often mix:

  • XRD = defines API
  • Composition = defines implementation

Fix:

  • Always design XRD first (what user wants)
  • Then design Composition (how cloud resources are created)

Example analogy:

  • XRD = "Order Pizza"
  • Composition = "Pizza recipe (ingredients + cooking steps)"


Practice Exercises

Exercise 1: Create a Simple S3 Bucket

Task:
Create a Crossplane configuration that provisions an AWS S3 bucket for a student project in Karachi.

Solution:

apiVersion: storage.aws.crossplane.io/v1beta1
kind: Bucket
metadata:
  name: karachi-student-bucket
spec:
  forProvider:
    locationConstraint: us-east-1

Explanation:

  • Creates S3 bucket resource
  • locationConstraint defines AWS region
  • Useful for storing app files or backups

Exercise 2: Multi-Environment Database Setup

Task:
Create dev and prod database using Crossplane.

Solution:

  • Use XRD to define Database
  • Create two claims:
apiVersion: example.org/v1
kind: Database
metadata:
  name: dev-db
spec:
  size: small
apiVersion: example.org/v1
kind: Database
metadata:
  name: prod-db
spec:
  size: large

Explanation:

  • Same API used for multiple environments
  • Dev uses small resources
  • Production uses larger resources

Frequently Asked Questions

What is Crossplane in simple terms?

Crossplane is a Kubernetes extension that allows you to manage cloud infrastructure using YAML files instead of scripts. It makes infrastructure behave like Kubernetes applications.

How is Crossplane different from Terraform?

Terraform is a standalone tool, while Crossplane runs inside Kubernetes. Crossplane provides continuous reconciliation, meaning it constantly ensures infrastructure matches desired state.

Can I use Crossplane with AWS in Pakistan?

Yes, Crossplane fully supports AWS. Pakistani developers can use it with AWS accounts to deploy production-grade infrastructure for startups and freelance projects.

Do I need Kubernetes knowledge for Crossplane?

Yes, basic Kubernetes knowledge is required because Crossplane works using Kubernetes CRDs, controllers, and declarative YAML configuration.

Is Crossplane good for beginners?

Crossplane is advanced, but beginners who already know Kubernetes can learn it quickly. It is highly valuable for DevOps careers and cloud engineering roles.


Summary & Key Takeaways

  • Crossplane brings Infrastructure as Code directly into Kubernetes
  • It uses CRDs, XRDs, and Compositions to define infrastructure
  • AWS resources can be provisioned using Kubernetes YAML files
  • It is ideal for modern DevOps workflows and cloud-native systems
  • Pakistani students can use Crossplane to build real-world cloud projects
  • It improves scalability, automation, and infrastructure consistency

To strengthen your DevOps skills, continue learning with:

For Pakistani students, mastering Crossplane combined with Kubernetes infrastructure skills can open doors to remote DevOps jobs, freelancing opportunities, and high-paying cloud engineering roles globally.


Practice the code examples from this tutorial
Open Compiler
Share this tutorial:

Test Your Python Knowledge!

Finished reading? Take a quick quiz to see how much you've learned from this tutorial.

Start Python Quiz

About Zaheer Ahmad