Skip to content

Commit

Permalink
add docs, helm-uninstall and clean up values.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
cooldragontattoo authored and willbarton committed Dec 12, 2024
1 parent c28b40c commit d7b3400
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ cfgov/regulations3k/jinja2/regulations3k/regulations3k-service-worker.js.map
cfgov/regulations3k/jinja2/regulations3k/workbox-*.js
cfgov/regulations3k/jinja2/regulations3k/workbox-*.js.map

# Helm Charts #
helm/charts/

# Apache #
##########
cfgov/apache/logs
Expand Down
106 changes: 106 additions & 0 deletions docs/K8s-deplyoment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Helm Deployment Guide

This document provides a comprehensive guide to setting up, deploying, and uninstalling your Helm deployment.

## Table of Contents

1. [Introduction](#introduction)
2. [Requirements](#requirements)
3. [Helm Chart](#helm-chart)
- [chart.yaml](#chartyaml)
- [values.yaml](#valuesyaml)
- [templates/NOTES.txt](#templatesnotestxt)
4. [Deploying the Helm Chart](#deploying-the-helm-chart)
5. [Uninstalling the Helm Deployment](#uninstalling-the-helm-deployment)

## Introduction

This guide explains the setup and deployment process for the CFPB Helm Chart, which includes PostgreSQL, OpenSearch, and the CFGOV application. The Helm chart is configured to support local deployments.

## Requirements

1. **Local Kubernetes Cluster**:
- Ensure you have a local Kubernetes cluster running. We only support [Docker Desktop](https://www.docker.com/products/docker-desktop) and [colima](https://github.com/abiosoft/colima).

2. **kubectl**:
- Install `kubectl`, the Kubernetes command-line tool, by following the [official installation guide](https://kubernetes.io/docs/tasks/tools/install-kubectl/).

3. **Helm**:
- Install Helm, the package manager for Kubernetes, by following the [official installation guide](https://helm.sh/docs/intro/install/).


## Helm Chart

### chart.yaml

In our `chart.yaml` we bring two Helm chart dependencies:

- Opensearch
- Postgresql

### values.yaml

The `values.yaml` file contains our local configuration for the deployment of CFGOV.

#### Init Containers
We have two init containers:

1. Busybox:
- Starts up once we have our Postgresql and Opensearch Pods running.
- Serves as a pre-cursor to our cfgov-migrations container.

2. cfgov-migrations
- Runs the django migrations and the `refresh-data.sh` script and populates it with test data `test.sql.gz`.
- Indexes our database to Opensearch.

#### Containers

1. cfgov:
- Uses the cfgov image, found in the repo's root `dockerfile`
- Serves up our Django application on Port 8000 of the cfgov pod

2. cfgov-apache:
- Built from `cfgov-apache` image built from the `apache/dockerfile`
- Serves as a webserver to proxy to our cfgov container


### notes.txt

Our [notes.txt](https://helm.sh/docs/chart_template_guide/notes_files/) is split to work for local deployments and deployments to production.

```txt
{{- if .Values.localDeployment }}
{{ .Files.Get "notes/NOTES-local.txt" }}
{{- else }}
{{ .Files.Get "notes/NOTES-production.txt" }}
{{- end }}
```

## Deploying the CFGOV Helm Chart

Deploying the CFGOV Helm chart is simple with the `helm-init.sh` script.

To deploy the CFGOV Helm Chart you must:
1. make sure you have a the cfgov and cfgov-apache image built
2. have a local K8s cluster running (docker desktop, colima)

If these criteria are not met, the `helm-init.sh` script will not run.

## Viewing the Helm Chart

You can either user a K8s IDE ([lens](https://k8slens.dev/), [k9s](https://k9scli.io)) or manually portfward to view the application running.

Review the output of your deployment for more information on manually portforwarding.

|Pod|Container|Port|
|-|-|-|
|cfgov|cfgov|8000|
|cfgov|cfogv-apache|80|
|postgresql|postgresql|5432|
|opensearch|opensearch|9200 (http)|
| | | 9300 (transport)|
| | | 9600 (metrics)

## Uninstall the CFGOV Helm Chart

Running `helm-uninstall.sh` will uninstall the helm deploymennt of the CFGOV Helm Chart, as well as remove Persistent Volume Claims for the Open Search Pod and the Postgresql Pod.
92 changes: 47 additions & 45 deletions helm-init.sh
Original file line number Diff line number Diff line change
@@ -1,86 +1,88 @@
#!/bin/bash

# Function to display usage
usage() {
echo "Usage: $0 [--release <release-name>]"
exit 1
}

# Parse command-line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--release)
RELEASE_NAME="$2"
shift
;;
*)
usage
;;
esac
shift
done
# Define color codes for pretty output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

OUTPUT_FILE=$(mktemp)

# Check if helm is installed
if ! command -v helm &> /dev/null; then
echo "helm could not be found. Please install it before running this script."
echo -e "${RED}Helm could not be found. Please install it before running this script.${NC}"
exit 1
fi


# Check if kubectl is installed
if ! command -v kubectl &> /dev/null; then
echo "kubectl could not be found. Please install it before running this script."
echo -e "${RED}Kubectl could not be found. Please install it before running this script.${NC}"
exit 1
fi

if ! docker images | grep -q "cfgov"; then
echo -e "${RED}Docker images 'cfgov' could not be found."
echo -e "Please run:{$NC} docker build . -t cfgov:latest"
exit 1
fi

# Get the current Kubernetes cluster context
CLUSTER=$(kubectl config current-context)

# Check if a Kubernetes cluster is configured
if [ -z "$CLUSTER" ]; then
echo "No Kubernetes cluster is currently configured. Please configure a cluster before running this script."
echo -e "${RED}No Kubernetes cluster is currently configured. Please configure a cluster before running this script.${NC}"
exit 1
fi

echo $CLUSTER
echo -e "${GREEN}Current Kubernetes cluster: $CLUSTER${NC}"

# Check if the current cluster is a local cluster (docker-desktop or colima)
if [[ "$CLUSTER" != "docker-desktop" && "$CLUSTER" != "colima" ]]; then
echo ""
echo -e "This script is intended to be run on a local Kubernetes cluster (i.e. docker-desktop or colima). The current cluster is $CLUSTER."
echo -e "Your current cluster is $CLUSTER."
echo -e "${YELLOW}"
echo -e "This script is intended to be run on a local Kubernetes cluster (i.e. docker-desktop or colima)."
echo -e "Your current cluster is ${RED}$CLUSTER${YELLOW}."
echo -e "Please switch to docker-desktop or colima before running this script."
echo ""
echo ""
echo "Exiting helm-init"
echo -e "${NC}"
echo -e "${RED}Exiting helm-init${NC}"
exit 1
fi

# Define the Helm directory
HELM_DIR="./helm"

if [ -n "$RELEASE_NAME" ]; then
echo "Release name: $RELEASE_NAME"
else
echo "No release name provided."
echo "Using default release name: 'cfgov'"
RELEASE_NAME="cfgov"
fi

# Check if the ./cfgov/apache directory exists
if [ -d "./cfgov/apache" ]; then
echo "Moving ./cfgov/apache to $HELM_DIR"
echo -e "${GREEN}Moving ./cfgov/apache to $HELM_DIR${NC}"
cp -r ./cfgov/apache $HELM_DIR
else
echo "Directory ./cfgov/apache does not exist."
echo -e "${RED}Directory ./cfgov/apache does not exist.${NC}"
exit 1
fi

if [! -d ./helm/charts]; then
echo "Building Helm charts..."
helm repo update
# Check if the ./helm/charts directory exists
if [ ! -d "./helm/charts" ]; then
echo -e "${GREEN}Building Helm charts...${NC}"
helm dependency update $HELM_DIR
helm dependency build $HELM_DIR
else
if [ -z $SKIP_DEP_UPDATE ]; then
echo "Updating Helm dependencies..."
# Update Helm dependencies if SKIP_DEP_UPDATE is not set
if [ -z "$SKIP_DEP_UPDATE" ]; then
echo -e "${GREEN}Updating Helm dependencies...${NC}"
helm dependency update $HELM_DIR
fi
fi

helm upgrade --install $RELEASE_NAME $HELM_DIR -f $HELM_DIR/values.yaml
# Upgrade or install the Helm release
echo -e "${GREEN}Upgrading/Installing Helm release...${NC}"
helm upgrade --install cfgov $HELM_DIR -f $HELM_DIR/values.local.yaml >> $OUTPUT_FILE 2>&1

# Remove the copied apache directory
echo -e "${GREEN}Cleaning up...${NC}"
rm -r $HELM_DIR/apache

echo -e "${GREEN}Helm initialization script completed successfully.${NC}"

cat $OUTPUT_FILE
rm $OUTPUT_FILE
38 changes: 38 additions & 0 deletions helm-uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Define color codes for pretty output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# Check if helm is installed
if ! command -v helm &> /dev/null; then
echo -e "${RED}Helm could not be found. Please install it before running this script.${NC}"
exit 1
fi

# Define the release name and namespace
RELEASE_NAME="cfgov"
NAMESPACE="default"

# Uninstall the Helm release
echo -e "${GREEN}Uninstalling Helm release ${RELEASE_NAME}...${NC}"
helm uninstall $RELEASE_NAME --namespace $NAMESPACE

# Uninstall the PVCs for the PostgreSQL and OpenSearch pods
kubectl delete pvc data-cfgov-postgresql-0 --namespace $NAMESPACE
kubectl delete pvc opensearch-cluster-master-opensearch-cluster-master-0 --namespace $NAMESPACE

# Check if the uninstall was successful
if [ $? -eq 0 ]; then
echo -e "${GREEN}Helm release ${RELEASE_NAME} uninstalled successfully.${NC}"
else
echo -e "${RED}Failed to uninstall Helm release ${RELEASE_NAME}.${NC}"
exit 1
fi

# Optionally, delete the namespace if it was created specifically for this release
# echo -e "${GREEN}Deleting namespace ${NAMESPACE}...${NC}"
# kubectl delete namespace $NAMESPACE

echo -e "${GREEN}Helm uninstallation script completed successfully.${NC}"
7 changes: 2 additions & 5 deletions helm/Chart.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ dependencies:
- name: postgresql
repository: https://charts.bitnami.com/bitnami
version: 16.2.3
- name: pgadmin4
repository: https://helm.runix.net
version: 1.33.3
- name: opensearch
repository: https://opensearch-project.github.io/helm-charts/
version: 1.31.3
digest: sha256:a9576d12585500d4f1e276d7d35b077f45b734cc2972183d519b60c0283980da
generated: "2024-12-10T07:53:25.026703-07:00"
digest: sha256:9a8a22b89a2f2c3c3d9ab178dc0b751c75d95e37215baf42a27b80982469efe0
generated: "2024-12-10T15:35:50.786246-07:00"
3 changes: 0 additions & 3 deletions helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ dependencies:
- name: postgresql
version: 16.2.3
repository: https://charts.bitnami.com/bitnami
- name: pgadmin4
version: 1.33.3
repository: https://helm.runix.net
- name: opensearch
version: 1.31.3
repository: https://opensearch-project.github.io/helm-charts/
Binary file removed helm/charts/pgadmin4-1.33.3.tgz
Binary file not shown.
37 changes: 37 additions & 0 deletions helm/notes/NOTES-local.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
The following pods have been deployed to your cluster:

PostgreSQL:
- Service Name: postgres
- Internal Port: 5432

To access PostgreSQL, run the following commands:
export POSTGRES_POD=$(kubectl get pods -l "statefulset.kubernetes.io/pod-name=cfgov-postgresql-0" -o jsonpath="{.items[0].metadata.name}")
export POSTGRES_PORT=$(kubectl get pod $POSTGRES_POD -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "PostgreSQL is running on internal port $POSTGRES_PORT"
kubectl port-forward $POSTGRES_POD 5432:$POSTGRES_PORT
Visit PostgreSQL at: http://127.0.0.1:5432

OpenSearch:
- Service Name: opensearch
- Http Internal Port: 9200
- Transport Internal Port: 9300
- Metrics Internal Port: 9600

To access OpenSearch Metrics, run the following commands:
export OPENSEARCH_POD=$(kubectl get pods -l "statefulset.kubernetes.io/pod-name=opensearch-cluster-master-0" -o jsonpath="{.items[0].metadata.name}")
export OPENSEARCH_PORT=$(kubectl get pod $OPENSEARCH_POD -o jsonpath="{.spec.containers[0].ports[2].containerPort}")
echo "OpenSearch Metrics is running on internal port $OPENSEARCH_PORT"
kubectl port-forward $OPENSEARCH_POD 9600:$OPENSEARCH_PORT
Visit OpenSearch at: http://127.0.0.1:9600

cfgov:
- Service Name: cfgov
- Applicaiton Port: 8000
- Apache Port: 80

To access cfgov, run the following commands:
export CFGOV_POD=$(kubectl get pods -l "app.kubernetes.io/name=cfgov" -o jsonpath="{.items[0].metadata.name}")
export CFGOV_PORT=$(kubectl get pod $CFGOV_POD -o jsonpath="{.spec.containers[1].ports[0].containerPort}")
echo "cfgov is running on internal port $CFGOV_PORT"
kubectl port-forward $CFGOV_POD 8080:$CFGOV_PORT
Visit cfgov at: http://127.0.0.1:8080
1 change: 1 addition & 0 deletions helm/notes/NOTES-production.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Uh-oh, we're not in production yet. :P
27 changes: 5 additions & 22 deletions helm/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }}
{{- range .paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }}
{{- end }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "cfgov.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch its status by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "cfgov.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "cfgov.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "cfgov.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT
{{- end }}
{{- if .Values.localDeployment }}
{{ .Files.Get "notes/NOTES-local.txt" }}
{{- else }}
{{ .Files.Get "notes/NOTES-production.txt" }}
{{- end }}
2 changes: 2 additions & 0 deletions helm/templates/tests/test-connection.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{- if .Values.tests.enabled }}
apiVersion: v1
kind: Pod
metadata:
Expand All @@ -13,3 +14,4 @@ spec:
command: ['wget']
args: ['{{ include "cfgov.fullname" . }}:{{ .Values.service.port }}']
restartPolicy: Never
{{- end }}
Loading

0 comments on commit d7b3400

Please sign in to comment.