Infrastructure GitOps: A Step-by-Step Guide to Setting Up Your Test Cluster Locally using minikube for Seamless Exploration
Tags: crossplane, gitops, aws, devops
Published: 2023-12-21 22:26:46

## Introduction
Explore the power of Crossplane Infrastructure through GitOps as I guide you in setting up a Crossplane Management Cluster on a local Minikube cluster. Follow this comprehensive tutorial to create and manage AWS resources seamlessly within your Kubernetes environment.

## Prerequisites for setting up Crossplane
- A Kubernetes cluster with at least 2 GB of RAM

- Permissions to create pods and secrets in the Kubernetes cluster

- Helm version v3.2.0 or later

- An AWS account with permissions to create an S3 storage bucket

- AWS access keys

**Step 1: A Kubernetes cluster with at least 2 GB of RAM** 
We will use Minikube to set up a Kubernetes cluster locally on Ubuntu - x86-64. If your setup is different, visit [https://minikube.sigs.k8s.io/docs/start/](https://minikube.sigs.k8s.io/docs/start/)  to download the binary.

**Install the latest minikube stable release:**
```
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
```
**Start minikube:**
```
minikube start
```
Add the current user to the docker group and then start a new shell with the updated group membership using the command below
```
sudo usermod -aG docker $USER && newgrp docker
```
If minikube fails to start, setup docker as your driver using the commands below:

**Start a cluster using the docker driver:**
```
minikube start --driver=docker
```
To make docker the default driver
```
minikube config set driver docker
```
**Interact with the cluster:**
If you already have kubectl installed to get pods
```
kubectl get po -A
```
If you don’t have kubectl installed, use the command below to set it up and get pods:
```
minikube kubectl -- get po -A
```
**Step 2: Install Helm**
Use the commands below to install helm locally using a bash script:
```
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
```
**Step 3: Install Crossplane**
Add the Crossplane Helm repository:
```
helm repo add crossplane-stable https://charts.crossplane.io/stable
```
**Update the local Helm chart cache:**
```
helm repo update
```
Install the Crossplane helm chart:
```
helm install crossplane \
--namespace crossplane-system \
--create-namespace crossplane-stable/crossplane
```
Run the Helm dry-run to see all the Crossplane components Helm installs.
```
helm install crossplane \
crossplane-stable/crossplane \
--dry-run --debug \
--namespace crossplane-system \
--create-namespace
```
**Install the Crossplane components:**
```
helm install crossplane \
crossplane-stable/crossplane \
--namespace crossplane-system \
--create-namespace
```
**Verify if Crossplane is installed:**
```
kubectl get pods -n crossplane-system
```
**View all the end-points created when Crossplane is installed:**
```
kubectl api-resources | grep crossplane
```
**Install the AWS provider** 
Install the AWS provider into the Kubernetes cluster with a Kubernetes configuration file. You can choose your provider based on the managed resource you want o provision. We will install AWS S3 provider which provisions S3 managed resource
```
cat <<EOF | kubectl apply -f -
apiVersion: pkg.crossplane.io/v1
kind: Provider
metadata:
  name: provider-aws-s3
spec:
  package: xpkg.upbound.io/upbound/provider-aws-s3:v0.37.0
EOF
```
Verify the provider installed with kubectl get providers.
```
kubectl get providers
```
Add your access key and secret to a text file aws-credentials.txt
```
[default]
aws_access_key_id = <access key>
aws_secret_access_key = <secret key>
```
Create a Kubernetes secret with the AWS credentials
```
kubectl create secret \
generic aws-secret \
-n crossplane-system \
--from-file=creds=./aws-credentials.txt
```
View kubernetes secret
```
kubectl describe secret aws-secret -n crossplane-system
```
Create a ProviderConfig: a ProviderConfig customizes the settings of the AWS Provider. Apply the ProviderConfig with this Kubernetes configuration file:
```
cat <<EOF | kubectl apply -f -
apiVersion: aws.upbound.io/v1beta1
kind: ProviderConfig
metadata:
  name: default
spec:
  credentials:
    source: Secret
    secretRef:
      namespace: crossplane-system
      name: aws-secret
      key: creds
EOF
```
**Create a managed resource:**
```
bucket=$(echo "crossplane-bucket-"$(head -n 4096 /dev/urandom | openssl sha1 | tail -c 10))
cat <<EOF | kubectl apply -f -
apiVersion: s3.aws.upbound.io/v1beta1
kind: Bucket
metadata:
  name: $bucket
spec:
  forProvider:
    region: us-east-2
  providerConfigRef:
    name: default
EOF
```
A managed resource is anything Crossplane creates and manages outside of the Kubernetes cluster.
**Verify crossplane has created an S3 bucket:**
```
kubectl get buckets
```
## References

- Minikube: https://minikube.sigs.k8s.io/docs/start/  

- Crossplane: https://docs.crossplane.io/v1.14/software/install/  

- Providers: https://marketplace.upbound.io/providers  
Getting started with AWS provider: https://docs.crossplane.io/v1.13/getting-started/provider-aws/ 

- Crossplane youtube playlist by Viktor Farcic: https://www.youtube.com/watch?v=AtbS1u2j7po&list=PLyicRj904Z9_X62k6_XM_xlJkSyoQDkS2&ab_channel=DevOpsToolkit 

- Upbound official crossplane documentation: https://docs.upbound.io/