Skip to content

Commit a2149a4

Browse files
authored
Initial Commit (#1)
* Initial Commit * Update the README file of k8s-dev-cluster Renamed the title and revised the content in README for clearer instructions. The update includes prerequisites, usage guidelines, and contributing protocols. Also added disclaimer to specify the usage of this dev cluster. * Add logos to k8s-dev-cluster README Added the logos of `Kind` and `Helm` to the README of the `k8s-dev-cluster`. The images help in enhancing the visual appeal and provide better brand recognition for the tools used in the repository. * Additionally, updates have been made to the Helmfile.yaml to include comments for each repository and to add a new Cloudflare repository for managing Cloudflare service-related Helm charts. * Update kind-cluster workflow trigger conditions The conditions to trigger the 'kind-cluster' workflow have been modified. This workflow now executes only on pull requests on the 'main' branch and push events on the 'master' branch. Furthermore, the setup of Go has been removed from the workflow steps. * Remove comments from kind-cluster workflow file The leading comments in the "kind-cluster.yml" file have been removed. This change simplifies the code by eliminating unnecessary comments. Better readability and overall code cleanliness is expected as a result. * Add lint workflow to k8s dev cluster A new GitHub Actions workflow was introduced for linting in the k8s-dev-cluster. This configuration will trigger the linting process on "push" and "pull request" events to the master branch. This is hoped to enhance the code quality by enforcing code standards. * Remove unused KUBERNETES_VERSION and kubeconfig context setup The KUBERNETES_VERSION variable and the set_kubeconfig_context function have been removed from create_cluster.sh since they were no longer in use. The README.md file was also updated with image placements for better visual presentation of the project. These changes contribute to cleaner, more efficient code. * The commit message does not fit the code differences provided. According to the code provided, the correct commit message would be: Change target branch from 'main' to 'master' in kind-cluster.yml In the GitHub workflows for the k8s-dev-cluster project, the target branch for pull requests has been changed from 'main' to 'master'. This change aligns with the project's branching strategy and ensures that pull requests are correctly targeted. * Add helmfile installation and application to k8s workflow The kind-cluster workflow within GitHub workflows has been updated to include the steps of installing and applying a helmfile. The README file was also updated to reflect the proper Helmfile repository link. This addition ensures helmfile's availability and improves the workflow's functionality in the k8s-dev-cluster project. * Also, in the kind-cluster.yml workflow file, the helmfile download URL was corrected from darwin to linux version, and added file permission change step to make helmfile executable after being moved to /usr/local/bin directory. * Improve helmfile sync retry mechanism in kind-cluster.yml The updated code in the `.github/workflows/kind-cluster.yml` file now includes a retry mechanism for the `helmfile sync` command. This helps to mitigate issues where the initial execution of the command fails, by attempting to run it twice more before exiting the script with an error. * Add localhost response check in kind-cluster.yml workflow This update augments the GitHub actions in the `.github/workflows/kind-cluster.yml` file by adding a localhost response check. The new section of the code verifies the correct response from the localhost endpoint, enhancing the robustness and the reliability of the workflow. If the returned HTTP status code is not 404, an error message will be printed and the action will fail. * Correct arithmetic syntax in kind-cluster.yml workflow This commit adjusts the syntax for an arithmetic operation in the `.github/workflows/kind-cluster.yml` file. Previously, the code utilized the deprecated syntax `$[$command_retry+1]`, but this has been updated to `$((command_retry+1))` which provides more predictable results and compatibility in increasing the `command_retry` count. * Update kubeconfig reference in create_cluster.sh script This commit adjusts the kubeconfig reference in the `create_cluster.sh` script from `$KUBECONFIG` to `"$KUBECONFIG"`. This modification in the syntax ensures that the value of the variable KUBECONFIG is properly quoted, preventing potential issues arising from spaces or special characters in the value.
1 parent aa38b1f commit a2149a4

15 files changed

+5653
-0
lines changed

.github/workflows/kind-cluster.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Create a kind cluster
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
push:
8+
branches:
9+
- master
10+
11+
jobs:
12+
build:
13+
name: Create a kind cluster
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Check out code
18+
uses: actions/checkout@v2
19+
20+
- name: Set up kind cluster
21+
run: |
22+
chmod +x ./create_cluster.sh
23+
./create_cluster.sh
24+
25+
- name: Verify kind cluster
26+
run: |
27+
kubectl cluster-info
28+
kubectl get nodes
29+
30+
- name: install helmfile
31+
run: |
32+
curl -LO https://github.com/helmfile/helmfile/releases/download/v0.159.0/helmfile_0.159.0_linux_amd64.tar.gz
33+
tar -xzvf helmfile_0.159.0_linux_amd64.tar.gz
34+
sudo mv helmfile /usr/local/bin
35+
chmod +x /usr/local/bin/helmfile
36+
helmfile --version
37+
38+
- name: apply helmfile
39+
run: |
40+
command_retry=0
41+
until [ $command_retry -ge 2 ]
42+
do
43+
helmfile --file ./helm sync && break
44+
command_retry=$((command_retry+1))
45+
if [ $command_retry -eq 2 ]; then
46+
echo "Command failed after 2 attempts"
47+
exit 1
48+
fi
49+
sleep 1
50+
done
51+
52+
- name: Check localhost response
53+
run: |
54+
response=$(curl --write-out "%{http_code}" --silent --output /dev/null http://localhost)
55+
if [ "$response" -ne 404 ]; then
56+
echo "Error: expected a 404 response, got $response"
57+
exit 1
58+
fi

.github/workflows/lint.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
name: Lint
3+
4+
on: # yamllint disable-line rule:truthy
5+
push:
6+
branches:
7+
- master
8+
pull_request:
9+
branches:
10+
- master
11+
12+
jobs:
13+
build:
14+
name: Lint
15+
runs-on: ubuntu-latest
16+
17+
permissions:
18+
contents: read
19+
packages: read
20+
# To report GitHub Actions status checks
21+
statuses: write
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v4
26+
27+
- name: Super-linter
28+
uses: super-linter/super-linter@v5
29+
env:
30+
DEFAULT_BRANCH: master
31+
# To report GitHub Actions status checks
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
...

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.idea
2+
/.git

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# k8s-dev-cluster
2+
3+
Deploy a local kubernetes cluster for development purpose. This repository contains all the necessary tools to create a local kubernetes cluster using `Kind` and `Helmfile`
4+
5+
<img src="https://kind.sigs.k8s.io/logo/logo.png" width="160" height="100">
6+
<img src="https://helm.sh/img/helm.svg" width="100" height="100">
7+
8+
## Prerequisites
9+
10+
- [Docker](https://docs.docker.com/get-docker/)
11+
- [Kind](https://kind.sigs.k8s.io/docs/user/quick-start/#installation)
12+
- [Helmfile](https://github.com/helmfile/helmfile)
13+
14+
## Usage
15+
16+
### Create Cluster
17+
18+
```bash
19+
chmod +x ./create_cluster.sh
20+
./create_cluster.sh
21+
```
22+
23+
### Get Kubeconfig
24+
25+
```bash
26+
➜ kind get kubeconfig --name local-k8s > ~/.kube/config
27+
```
28+
29+
If you want to access the cluster from another machine, you need to change your kubeconfig file a little bit:
30+
31+
```yaml
32+
clusters:
33+
- name: kind-local-k8s
34+
cluster:
35+
# need to remove "certificate-authority-data" otherwise "insecure-skip-tls-verify" will not work
36+
server: https://x.x.x.x:6443 # change this to your IP address where "Kind" cluster is running
37+
insecure-skip-tls-verify: true # add this
38+
```
39+
40+
### Install Necessary Tools using Helmfile
41+
42+
```bash
43+
helmfile --file ./helm deps
44+
helmfile --file ./helm sync
45+
```
46+
47+
After that, you can access the cluster using `kubectl`:
48+
49+
```bash
50+
➜ kubectl get nodes
51+
NAME STATUS ROLES AGE VERSION
52+
local-k8s-control-plane Ready control-plane 27m v1.25.3
53+
```
54+
55+
## Contributing
56+
57+
If you want to contribute to this repository, please create an issue first, then create a pull request with your changes. If the changes can help other developers, we can proceed with the pull request.
58+
59+
## Create Issue
60+
61+
If you have any questions or issues, please create an issue [here](https://github.com/shaharia-lab/k8s-dev-cluster/issues)
62+
63+
## Disclaimer
64+
65+
This repository is only for development purpose. Do not use it in production.

create_cluster.sh

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
#!/bin/bash
2+
3+
# Set the desired configuration
4+
KIND_VERSION="v0.20.0"
5+
CLUSTER_NAME="local-k8s"
6+
NODES=2
7+
8+
# Function to delete an existing Kind cluster
9+
delete_cluster() {
10+
local cluster_name=$1
11+
if kind get clusters | grep -q "^$cluster_name$"; then
12+
echo "Kind cluster '$cluster_name' is already running. Deleting the cluster..."
13+
kind delete cluster --name "$cluster_name"
14+
fi
15+
}
16+
17+
# Function to install Kind if not already installed
18+
install_kind() {
19+
if ! command -v kind &> /dev/null; then
20+
echo "Kind not found. Installing Kind..."
21+
curl -Lo ./kind "https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-linux-amd64"
22+
chmod +x ./kind
23+
sudo mv ./kind /usr/local/bin/kind
24+
fi
25+
}
26+
27+
# Function to create the Kind cluster
28+
create_cluster() {
29+
local cluster_name=$1
30+
local nodes=$2
31+
echo "Creating Kind cluster: $cluster_name with $nodes nodes..."
32+
cat <<EOF | kind create cluster --name "$cluster_name" --config=-
33+
kind: Cluster
34+
apiVersion: kind.x-k8s.io/v1alpha4
35+
networking:
36+
apiServerAddress: "0.0.0.0"
37+
apiServerPort: 6443
38+
kubeadmConfigPatches:
39+
- |-
40+
kind: ClusterConfiguration
41+
# configure controller-manager bind address
42+
controllerManager:
43+
extraArgs:
44+
bind-address: 0.0.0.0
45+
# configure etcd metrics listen address
46+
etcd:
47+
local:
48+
extraArgs:
49+
listen-metrics-urls: http://0.0.0.0:2381
50+
# configure scheduler bind address
51+
scheduler:
52+
extraArgs:
53+
bind-address: 0.0.0.0
54+
- |-
55+
kind: KubeProxyConfiguration
56+
# configure proxy metrics bind address
57+
metricsBindAddress: 0.0.0.0
58+
nodes:
59+
- role: control-plane
60+
kubeadmConfigPatches:
61+
- |
62+
kind: InitConfiguration
63+
nodeRegistration:
64+
kubeletExtraArgs:
65+
node-labels: "ingress-ready=true"
66+
extraPortMappings:
67+
- containerPort: 80
68+
hostPort: 80
69+
protocol: TCP
70+
listenAddress: "0.0.0.0"
71+
- containerPort: 443
72+
hostPort: 443
73+
protocol: TCP
74+
listenAddress: "0.0.0.0"
75+
EOF
76+
}
77+
78+
# Function to verify cluster status
79+
verify_cluster_status() {
80+
echo "Verifying cluster status..."
81+
kubectl cluster-info
82+
}
83+
84+
# Function to wait until all nodes are ready
85+
wait_for_nodes_ready() {
86+
echo "Waiting for all nodes to be ready..."
87+
kubectl wait --for=condition=ready nodes --all --timeout=300s
88+
}
89+
90+
# Function to install and configure Ingress controller
91+
install_ingress_controller() {
92+
echo "Installing ingress controller"
93+
kubectl create ns ingress-nginx
94+
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml -n ingress-nginx
95+
}
96+
97+
# Function to wait until Ingress controller is ready
98+
wait_for_ingress_ready() {
99+
echo "Waiting for ingress controller to be ready..."
100+
kubectl wait --namespace ingress-nginx \
101+
--for=condition=ready pod \
102+
--selector=app.kubernetes.io/component=controller \
103+
--timeout=90s
104+
}
105+
106+
# Function to deploy test app
107+
deploy_test_app() {
108+
echo "Deploying test app..."
109+
kubectl create deployment test-app --image=nginx
110+
kubectl expose deployment test-app --type=NodePort --port=80 --target-port=80
111+
echo "Test app deployed and exposed."
112+
}
113+
114+
# Function to print URL for accessing the test app
115+
print_test_app_url() {
116+
local cluster_ip
117+
cluster_ip=$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[0].address}')
118+
local node_port
119+
node_port=$(kubectl get service test-app -o jsonpath='{.spec.ports[0].nodePort}')
120+
echo "You can access the test app at: http://$cluster_ip:$node_port"
121+
}
122+
123+
# Function to install PostgreSQL in Kind cluster using Helm chart
124+
install_postgresql() {
125+
local chart_name="postgresql"
126+
local chart_repo="https://charts.bitnami.com/bitnami"
127+
local namespace="$1"
128+
local release_name="postgresql"
129+
local admin_username="app"
130+
local admin_password="pass"
131+
local admin_database="app"
132+
133+
echo "Installing PostgreSQL using Helm chart..."
134+
135+
# Add the Bitnami Helm repository
136+
helm repo add bitnami "$chart_repo"
137+
138+
# Create the PostgreSQL namespace
139+
kubectl create namespace "$namespace"
140+
141+
# Install PostgreSQL using the Helm chart and override admin credentials
142+
helm upgrade --install "$release_name" bitnami/"$chart_name" \
143+
--namespace "$namespace" \
144+
--set auth.username="$admin_username" \
145+
--set auth.password="$admin_password" \
146+
--set auth.database="$admin_database"
147+
148+
echo "PostgreSQL installation completed."
149+
}
150+
151+
# Function to deploy kube-prometheus-stack Helm chart to Kind cluster
152+
# Function to deploy kube-prometheus-stack Helm chart to Kind cluster
153+
deploy_kube_prometheus_stack() {
154+
local cluster_name=$1
155+
local chart_name="kube-prometheus-stack"
156+
local chart_repo="https://prometheus-community.github.io/helm-charts"
157+
local namespace="$2"
158+
local release_name="kube-prometheus"
159+
160+
echo "Deploying kube-prometheus-stack Helm chart..."
161+
162+
# Add the Prometheus Community Helm repository
163+
helm repo add prometheus-community "$chart_repo"
164+
165+
# Create the namespace if it doesn't exist
166+
kubectl create namespace "$namespace" --dry-run=client -o yaml | kubectl apply -f -
167+
168+
# Install the kube-prometheus-stack chart with desired configurations
169+
helm upgrade --install "$release_name" prometheus-community/"$chart_name" \
170+
--namespace "$namespace" \
171+
--kubeconfig "$KUBECONFIG" \
172+
--set prometheus.enabled="true" \
173+
--set prometheus.serviceAccount.name="kube-prometheus" \
174+
--set prometheus.ingress.annotations."kubernetes\.io/ingress\.class"="nginx" \
175+
--set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues="false" \
176+
--set prometheus.prometheusSpec.serviceMonitorSelector.matchExpressions[0].key="prometheus" \
177+
--set prometheus.prometheusSpec.serviceMonitorSelector.matchExpressions[0].operator="In" \
178+
--set prometheus.prometheusSpec.serviceMonitorSelector.matchExpressions[0].values[0]="kube-prometheus" \
179+
--set prometheus.prometheusSpec.podMonitorSelectorNilUsesHelmValues="false" \
180+
--set prometheus.prometheusSpec.ruleSelectorNilUsesHelmValues="false"
181+
182+
echo "kube-prometheus-stack deployment completed."
183+
}
184+
185+
186+
187+
# Main script
188+
189+
# Function to prepare the Kind cluster
190+
prepare_kind_cluster() {
191+
local cluster_name=$1
192+
local nodes=$2
193+
194+
# Delete existing Kind cluster if running
195+
delete_cluster "$cluster_name"
196+
197+
# Install Kind if not already installed
198+
install_kind
199+
200+
# Create the Kind cluster
201+
create_cluster "$cluster_name" "$nodes"
202+
203+
# Verify cluster status
204+
verify_cluster_status
205+
206+
# Wait until all nodes are ready
207+
wait_for_nodes_ready
208+
}
209+
210+
prepare_kind_cluster $CLUSTER_NAME $NODES

helm/Helmfile.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: v0.144.0
2+
dependencies:
3+
- name: cloudflare-tunnel
4+
repository: https://cloudflare.github.io/helm-charts
5+
version: 0.3.0
6+
- name: ingress-nginx
7+
repository: https://kubernetes.github.io/ingress-nginx
8+
version: 4.7.1
9+
- name: kube-prometheus-stack
10+
repository: https://prometheus-community.github.io/helm-charts
11+
version: 48.1.2
12+
- name: loki-stack
13+
repository: https://grafana.github.io/helm-charts
14+
version: 2.9.10
15+
digest: sha256:78768f4f4e9413b7738d26341511b00a07ece61a4ac624923625aba63717d002
16+
generated: "2023-12-13T19:12:56.454820474+01:00"

0 commit comments

Comments
 (0)