In this article we are going to cover How to Use Kustomize with Remote Git Repositories.
Managing Kubernetes configurations can become complex as your deployments grow. Kustomize simplifies this process by allowing you to customize YAML configurations effectively. In this guide, we’ll walk through how to use Kustomize with remote Git repositories and troubleshoot common issues. We’ll also cover creating a GitHub repository to store your configurations.
Table of Contents
Prerequisites
Before you begin, ensure the following prerequisites are met:
- Git: Installed and configured on your system.
- Kustomize: Installed on your machine. See the installation steps below.
- kubectl: Installed and configured to interact with your Kubernetes cluster.
- GitHub Account: An active GitHub account to create and store repositories.
Step #1:Create GitHub Repository
To store your Kubernetes configurations, create a GitHub repository:
- Log in to GitHub: Visit GitHub and log in with your credentials.
- Create a New Repository:
- Click the + icon in the top-right corner and select New repository.
- Name your repository, e.g.,
k8s-config. - Choose visibility (Public or Private).
- Click Create repository.
- Clone the Repository Locally:
- Copy the repository’s URL (HTTPS or SSH).
- Run the following commands in your terminal:
git clone https://github.com/your-username/k8s-config.git
cd k8s-config
Step #2:Install Kustomize
Ensure Kustomize is installed on your system. If it isn’t installed, follow these steps:
1. Install Kustomize:
The below-mentioned script automatically detects the OS and installs the Kustomize.
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
after using above command use below command
sudo install -o root -g root -m 0755 kustomize /usr/local/bin/kustomize
2. Verify the Installation:
kustomize version
Step #3:Organize Kubernetes Configurations with kustomize
Directory structure:
.
├── base
│ ├── deployment.yaml
│ └── kustomization.yaml
└── overlays
├── dev
│ └── kustomization.yaml
└── prod
Set Up Your Configuration Structure:
mkdir -p base overlays/dev overlays/prod
Add Base Configuration: Create a deployment.yaml file in the base directory:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:latest
Add Base Kustomization: Create a kustomization.yaml in the base directory:
resources:
- deployment.yaml
Reference the Base in Overlays: In overlays/dev/kustomization.yaml, reference the base configuration stored in your GitHub repository:
resources:
- https://github.com/harish981/k8s-config.git//base/
Commit and Push Changes:
git add .
git commit -m "Add Kustomize configurations"
git push origin main
Step #4:Apply Kustomize Configurations
Build and Apply Configurations: Run the following command to apply configurations to your Kubernetes cluster:
kustomize build overlays/dev | kubectl apply -f -
Verify the Deployment: Check the deployment status:
kubectl get deployments
Conclusion:
By following this guide, you can effectively use Kustomize with remote Git repositories to manage Kubernetes configurations. This setup enables seamless customization and deployment across multiple environments, making your Kubernetes workflow more efficient and scalable.
Related Articles:
Kubernetes Configurations with Kustomize Transformers with Examples