Skip to content

Commit

Permalink
Initial Commit (#1)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
shahariaazam authored Dec 17, 2023
1 parent aa38b1f commit a2149a4
Show file tree
Hide file tree
Showing 15 changed files with 5,653 additions and 0 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/kind-cluster.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Create a kind cluster

on:
pull_request:
branches:
- master
push:
branches:
- master

jobs:
build:
name: Create a kind cluster
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up kind cluster
run: |
chmod +x ./create_cluster.sh
./create_cluster.sh
- name: Verify kind cluster
run: |
kubectl cluster-info
kubectl get nodes
- name: install helmfile
run: |
curl -LO https://github.com/helmfile/helmfile/releases/download/v0.159.0/helmfile_0.159.0_linux_amd64.tar.gz
tar -xzvf helmfile_0.159.0_linux_amd64.tar.gz
sudo mv helmfile /usr/local/bin
chmod +x /usr/local/bin/helmfile
helmfile --version
- name: apply helmfile
run: |
command_retry=0
until [ $command_retry -ge 2 ]
do
helmfile --file ./helm sync && break
command_retry=$((command_retry+1))
if [ $command_retry -eq 2 ]; then
echo "Command failed after 2 attempts"
exit 1
fi
sleep 1
done
- name: Check localhost response
run: |
response=$(curl --write-out "%{http_code}" --silent --output /dev/null http://localhost)
if [ "$response" -ne 404 ]; then
echo "Error: expected a 404 response, got $response"
exit 1
fi
33 changes: 33 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
name: Lint

on: # yamllint disable-line rule:truthy
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
name: Lint
runs-on: ubuntu-latest

permissions:
contents: read
packages: read
# To report GitHub Actions status checks
statuses: write

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Super-linter
uses: super-linter/super-linter@v5
env:
DEFAULT_BRANCH: master
# To report GitHub Actions status checks
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea
/.git
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# k8s-dev-cluster

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`

<img src="https://kind.sigs.k8s.io/logo/logo.png" width="160" height="100">
<img src="https://helm.sh/img/helm.svg" width="100" height="100">

## Prerequisites

- [Docker](https://docs.docker.com/get-docker/)
- [Kind](https://kind.sigs.k8s.io/docs/user/quick-start/#installation)
- [Helmfile](https://github.com/helmfile/helmfile)

## Usage

### Create Cluster

```bash
chmod +x ./create_cluster.sh
./create_cluster.sh
```

### Get Kubeconfig

```bash
➜ kind get kubeconfig --name local-k8s > ~/.kube/config
```

If you want to access the cluster from another machine, you need to change your kubeconfig file a little bit:

```yaml
clusters:
- name: kind-local-k8s
cluster:
# need to remove "certificate-authority-data" otherwise "insecure-skip-tls-verify" will not work
server: https://x.x.x.x:6443 # change this to your IP address where "Kind" cluster is running
insecure-skip-tls-verify: true # add this
```
### Install Necessary Tools using Helmfile
```bash
helmfile --file ./helm deps
helmfile --file ./helm sync
```

After that, you can access the cluster using `kubectl`:

```bash
➜ kubectl get nodes
NAME STATUS ROLES AGE VERSION
local-k8s-control-plane Ready control-plane 27m v1.25.3
```

## Contributing

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.

## Create Issue

If you have any questions or issues, please create an issue [here](https://github.com/shaharia-lab/k8s-dev-cluster/issues)

## Disclaimer

This repository is only for development purpose. Do not use it in production.
210 changes: 210 additions & 0 deletions create_cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
#!/bin/bash

# Set the desired configuration
KIND_VERSION="v0.20.0"
CLUSTER_NAME="local-k8s"
NODES=2

# Function to delete an existing Kind cluster
delete_cluster() {
local cluster_name=$1
if kind get clusters | grep -q "^$cluster_name$"; then
echo "Kind cluster '$cluster_name' is already running. Deleting the cluster..."
kind delete cluster --name "$cluster_name"
fi
}

# Function to install Kind if not already installed
install_kind() {
if ! command -v kind &> /dev/null; then
echo "Kind not found. Installing Kind..."
curl -Lo ./kind "https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-linux-amd64"
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
fi
}

# Function to create the Kind cluster
create_cluster() {
local cluster_name=$1
local nodes=$2
echo "Creating Kind cluster: $cluster_name with $nodes nodes..."
cat <<EOF | kind create cluster --name "$cluster_name" --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
apiServerAddress: "0.0.0.0"
apiServerPort: 6443
kubeadmConfigPatches:
- |-
kind: ClusterConfiguration
# configure controller-manager bind address
controllerManager:
extraArgs:
bind-address: 0.0.0.0
# configure etcd metrics listen address
etcd:
local:
extraArgs:
listen-metrics-urls: http://0.0.0.0:2381
# configure scheduler bind address
scheduler:
extraArgs:
bind-address: 0.0.0.0
- |-
kind: KubeProxyConfiguration
# configure proxy metrics bind address
metricsBindAddress: 0.0.0.0
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
listenAddress: "0.0.0.0"
- containerPort: 443
hostPort: 443
protocol: TCP
listenAddress: "0.0.0.0"
EOF
}

# Function to verify cluster status
verify_cluster_status() {
echo "Verifying cluster status..."
kubectl cluster-info
}

# Function to wait until all nodes are ready
wait_for_nodes_ready() {
echo "Waiting for all nodes to be ready..."
kubectl wait --for=condition=ready nodes --all --timeout=300s
}

# Function to install and configure Ingress controller
install_ingress_controller() {
echo "Installing ingress controller"
kubectl create ns ingress-nginx
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml -n ingress-nginx
}

# Function to wait until Ingress controller is ready
wait_for_ingress_ready() {
echo "Waiting for ingress controller to be ready..."
kubectl wait --namespace ingress-nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=90s
}

# Function to deploy test app
deploy_test_app() {
echo "Deploying test app..."
kubectl create deployment test-app --image=nginx
kubectl expose deployment test-app --type=NodePort --port=80 --target-port=80
echo "Test app deployed and exposed."
}

# Function to print URL for accessing the test app
print_test_app_url() {
local cluster_ip
cluster_ip=$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[0].address}')
local node_port
node_port=$(kubectl get service test-app -o jsonpath='{.spec.ports[0].nodePort}')
echo "You can access the test app at: http://$cluster_ip:$node_port"
}

# Function to install PostgreSQL in Kind cluster using Helm chart
install_postgresql() {
local chart_name="postgresql"
local chart_repo="https://charts.bitnami.com/bitnami"
local namespace="$1"
local release_name="postgresql"
local admin_username="app"
local admin_password="pass"
local admin_database="app"

echo "Installing PostgreSQL using Helm chart..."

# Add the Bitnami Helm repository
helm repo add bitnami "$chart_repo"

# Create the PostgreSQL namespace
kubectl create namespace "$namespace"

# Install PostgreSQL using the Helm chart and override admin credentials
helm upgrade --install "$release_name" bitnami/"$chart_name" \
--namespace "$namespace" \
--set auth.username="$admin_username" \
--set auth.password="$admin_password" \
--set auth.database="$admin_database"

echo "PostgreSQL installation completed."
}

# Function to deploy kube-prometheus-stack Helm chart to Kind cluster
# Function to deploy kube-prometheus-stack Helm chart to Kind cluster
deploy_kube_prometheus_stack() {
local cluster_name=$1
local chart_name="kube-prometheus-stack"
local chart_repo="https://prometheus-community.github.io/helm-charts"
local namespace="$2"
local release_name="kube-prometheus"

echo "Deploying kube-prometheus-stack Helm chart..."

# Add the Prometheus Community Helm repository
helm repo add prometheus-community "$chart_repo"

# Create the namespace if it doesn't exist
kubectl create namespace "$namespace" --dry-run=client -o yaml | kubectl apply -f -

# Install the kube-prometheus-stack chart with desired configurations
helm upgrade --install "$release_name" prometheus-community/"$chart_name" \
--namespace "$namespace" \
--kubeconfig "$KUBECONFIG" \
--set prometheus.enabled="true" \
--set prometheus.serviceAccount.name="kube-prometheus" \
--set prometheus.ingress.annotations."kubernetes\.io/ingress\.class"="nginx" \
--set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues="false" \
--set prometheus.prometheusSpec.serviceMonitorSelector.matchExpressions[0].key="prometheus" \
--set prometheus.prometheusSpec.serviceMonitorSelector.matchExpressions[0].operator="In" \
--set prometheus.prometheusSpec.serviceMonitorSelector.matchExpressions[0].values[0]="kube-prometheus" \
--set prometheus.prometheusSpec.podMonitorSelectorNilUsesHelmValues="false" \
--set prometheus.prometheusSpec.ruleSelectorNilUsesHelmValues="false"

echo "kube-prometheus-stack deployment completed."
}



# Main script

# Function to prepare the Kind cluster
prepare_kind_cluster() {
local cluster_name=$1
local nodes=$2

# Delete existing Kind cluster if running
delete_cluster "$cluster_name"

# Install Kind if not already installed
install_kind

# Create the Kind cluster
create_cluster "$cluster_name" "$nodes"

# Verify cluster status
verify_cluster_status

# Wait until all nodes are ready
wait_for_nodes_ready
}

prepare_kind_cluster $CLUSTER_NAME $NODES
16 changes: 16 additions & 0 deletions helm/Helmfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: v0.144.0
dependencies:
- name: cloudflare-tunnel
repository: https://cloudflare.github.io/helm-charts
version: 0.3.0
- name: ingress-nginx
repository: https://kubernetes.github.io/ingress-nginx
version: 4.7.1
- name: kube-prometheus-stack
repository: https://prometheus-community.github.io/helm-charts
version: 48.1.2
- name: loki-stack
repository: https://grafana.github.io/helm-charts
version: 2.9.10
digest: sha256:78768f4f4e9413b7738d26341511b00a07ece61a4ac624923625aba63717d002
generated: "2023-12-13T19:12:56.454820474+01:00"
Loading

0 comments on commit a2149a4

Please sign in to comment.