Flux CD is an open-source GitOps tool that enables continuous delivery for Kubernetes applications. One of the powerful features of Flux CD is its support for Custom Resource Definitions (CRDs). CRDs allow you to extend the Kubernetes API with custom resources that are specific to your application. In this blog post, we will explore how to use CRDs with Flux CD to manage your application’s configuration.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- A Kubernetes cluster with Flux CD installed.
- The
kubectl
command-line tool installed on your local machine.
Creating a Custom Resource Definition
The first step is to create a Custom Resource Definition (CRD) for your application. A CRD defines the schema for your custom resource and allows you to create, update, and delete instances of that resource using the Kubernetes API.
Create a new file named myapp.yaml
with the following content:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata: name: myapps.example.com
spec: group: example.com names: kind: MyApp listKind: MyAppList plural: myapps singular: myapp scope: Namespaced subresources: status: {} version: v1alpha1
This YAML file defines a CustomResourceDefinition
resource that creates a new custom resource named MyApp
in the example.com
API group. The plural
field specifies the name of the resource in the Kubernetes API, and the kind
field specifies the kind of the resource.
Applying the Custom Resource Definition
Next, we need to apply the myapp.yaml
file to our Kubernetes cluster using the kubectl
command-line tool:
kubectl apply -f myapp.yaml
This will create the MyApp
custom resource in our Kubernetes cluster.
Creating a Custom Resource
Now that we have created the MyApp
custom resource, we can create an instance of that resource. Create a new file named myapp-instance.yaml
with the following content:
apiVersion: example.com/v1alpha1
kind: MyApp
metadata: name: my-app
spec: replicas: 3 image: my-app:latest
This YAML file defines a MyApp
resource that specifies the desired state of our application. The replicas
field specifies the number of replicas of our application, and the image
field specifies the Docker image for our application.
Applying the Custom Resource
Next, we need to apply the myapp-instance.yaml
file to our Kubernetes cluster using the kubectl
command-line tool:
kubectl apply -f myapp-instance.yaml
This will create a new MyApp
resource in our Kubernetes cluster.
Using Flux CD to Manage Custom Resources
Now that we have created a custom resource, we can use Flux CD to manage that resource. Create a new file named flux-config.yaml
with the following content:
apiVersion: fluxcd.io/v1
kind: GitRepository
metadata: name: my-app-repo
spec: url: https://github.com/my-org/my-app-repo ref: branch: main interval: 1m
---
apiVersion: fluxcd.io/v1
kind: Kustomization
metadata: name: my-app-kustomization
spec: sourceRef: kind: GitRepository name: my-app-repo path: "kustomize" prune: true interval: 1m dependsOn: - name: my-app-repo
This YAML file defines a GitRepository
resource that references our Git repository, and a Kustomization
resource that specifies the path to our Kustomize configuration.
Applying the Flux CD Configuration
Next, we need to apply the flux-config.yaml
file to our Kubernetes cluster using the kubectl
command-line tool:
kubectl apply -f flux-config.yaml
This will create a new GitRepository
resource and a new Kustomization
resource in our Kubernetes cluster.
Creating a Kustomize Configuration
Now that we have configured Flux CD to manage our custom resource, we need to create a Kustomize configuration that references our custom resource. Create a new directory named kustomize
in your Git repository, and create a new file named kustomization.yaml
with the following content:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- myapp-instance.yaml
This YAML file defines a Kustomization
resource that references our myapp-instance.yaml
file.
Committing the Kustomize Configuration
Next, we need to commit the kustomize
directory to our Git repository:
git add kustomize
git commit -m "Add Kustomize configuration"
git push
This will push the kustomize
directory to our Git repository.
Verifying the Custom Resource
Finally, we can verify that our custom resource is being managed by Flux CD. Run the following command to view the status of our MyApp
resource:
kubectl get myapps.example.com
This should display the status of our MyApp
resource, including the number of replicas and the Docker image.
Conclusion
Custom Resource Definitions (CRDs) are a powerful feature of Kubernetes that allow you to extend the Kubernetes API with custom resources that are specific to your application. By using Flux CD to manage your custom resources, you can ensure that your application’s configuration is kept in sync with your Git repository. In this blog post, we explored how to create a custom resource definition, create an instance of that resource, and use Flux CD to manage that resource. By following the steps outlined in this blog post, you can use CRDs with Flux CD to manage your application’s configuration.
Discover more from Coursity
Subscribe to get the latest posts sent to your email.