Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DefrostedTuna committed Feb 21, 2019
0 parents commit 296d48e
Show file tree
Hide file tree
Showing 25 changed files with 1,570 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
files/*.yaml
Empty file added files/.gitkeep
Empty file.
114 changes: 114 additions & 0 deletions init-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/bin/bash

set -e

BASEDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )
source "${BASEDIR}/scripts/functions.sh"

# Set the backspace key here.
# Create a gif of the script?

# ------------------------------------------------------------------------------
# Welcome
# ------------------------------------------------------------------------------

echo -e "Welcome, this script will help ease the process of setting up a" \
"Kubernetes cluster from scratch on DigitalOcean."
echo -e "First, we’ll check for any necessary dependencies and install them if necessary."
echo -e "After this you will be guided through each step of the process, being prompted" \
"for the necessary information needed for configuration."
echo -e "Let’s start by checking those dependencies, here's what we're looking for:"
echo -e "Brew, Kubectl, Helm, and Doctl."
echo

read -p $'\033[33mPress enter to continue...\033[39m'
echo

# ------------------------------------------------------------------------------
# Check Dependencies: Brew, Kubectl, Helm, Doctl.
# ------------------------------------------------------------------------------

"${BASEDIR}"/scripts/dependency-check.sh

# ------------------------------------------------------------------------------
# Create Cluster
# ------------------------------------------------------------------------------

if ask "\033[33mWould you like to create a new Kubernetes cluster?\033[39m"; then
echo
"${BASEDIR}"/scripts/create-cluster.sh

else # Copy Config
echo
if ask "\033[33mWould you like to save an existing config?\033[39m" Y; then
echo
"${BASEDIR}"/scripts/copy-config.sh
fi
echo
fi

# ------------------------------------------------------------------------------
# Cluster Initializaton (Helm/Tiller)
# ------------------------------------------------------------------------------

if ask "\033[33mWould you like to initialize Helm/Tiller?\033[39m" Y; then
echo
"${BASEDIR}"/scripts/install-helm-tiller.sh
else
echo
fi

# ------------------------------------------------------------------------------
# Nginx Ingress Setup
# ------------------------------------------------------------------------------

if ask "\033[33mWould you like to install the Nginx Ingress?\033[39m" Y; then
echo
"${BASEDIR}"/scripts/install-nginx-ingress.sh
else
echo
fi

# ------------------------------------------------------------------------------
# Certificate Manager Setup
# ------------------------------------------------------------------------------

if ask "\033[33mWould you like to install Cert Manager?\033[39m" Y; then
echo
"${BASEDIR}"/scripts/install-cert-manager.sh
else
echo
fi

# ------------------------------------------------------------------------------
# Jenkins Setup
# ------------------------------------------------------------------------------

if ask "\033[33mWould you like to install and configure Jenkins?\033[39m" Y; then
echo
"${BASEDIR}"/scripts/install-jenkins.sh
else
echo
fi

# ------------------------------------------------------------------------------
# Harbor Setup
# ------------------------------------------------------------------------------

if ask "\033[33mWould you like to install and configure Harbor?\033[39m" Y; then
echo
"${BASEDIR}"/scripts/install-harbor.sh
else
echo
fi

# ------------------------------------------------------------------------------
# Dashboard Setup
# ------------------------------------------------------------------------------

if ask "\033[33mWould you like to install Kubernetes Dashboard?\033[39m" Y; then
echo
"${BASEDIR}"/scripts/install-dashboard.sh
else
echo
fi
61 changes: 61 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Kubernetes Scaffolding Script

## What is this?

While experimenting with Kubernetes on DigitalOcean, I found myself spinning up clusters fairly often to try new things. I got very familiar with the process of doing this and decided to [write a guide](https://gist.github.com/DefrostedTuna/1cf0367b3b121d82a0591e177d6887b8) covering how to set up a barebones cluster, along with common tools that I personally use. This bash script project is an extension of that guide, automating the process of setting up a cluster on DigitalOcean and installing a subset of software to get fresh cluster up and running quickly.

## What are the requirements?

* MacOS Mojave (Untested on other Unix based systems, or other versions of MacOS)
* Homebrew
* Kubectl
* Helm
* Doctl

## What does it install and configure?

The script will first and foremost check for the proper dependencies. These dependencies include:

* Homebrew
* Kubectl
* Helm
* Doctl

If these dependencies are not found on the system, the script will attempt to install them for you.

**Note:** The Homebrew installation may take a considerable amount of time if the X Code Development Tools are not found on your system.

Once these dependencies are present the script will configure the following:

* Creating a Kubernetes cluster on DigitalOcean
* Copying a kubeconfig file to your local machine (If opted against creating a cluster)
* Initializing Helm/Tiller
* Installing an Nginx Ingress
* Installing Cert Manager
* Installing and configuring Jenkins
* Installing and configuring Harbor
* Installing Kubernetes Dashboard

**Note:** Setting up the Ingress *WILL* configure a DigitalOcean Load Balancer. This is a service that DigitalOcean charges for, so keep that in mind.

## How do I use this script?

Clone the repository onto your local machine.

```
https://github.com/DefrostedTuna/kubernetes-scaffolding.git && cd kubernetes-scaffolding
```

Make the script executable.

```
chmod +x init-cluster.sh && chmod -R +x scripts
```

Run the script.

```
./init-cluster.sh
```

The script will guide you through the setup process, prompting for input when necessary. Follow the instructions in the script and you'll be good to go!
39 changes: 39 additions & 0 deletions scripts/copy-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

BASEDIR=$(dirname "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )")
source "${BASEDIR}/scripts/functions.sh"

# -----------------------------------------------------------------------------
# Copy Config
# -----------------------------------------------------------------------------

echo -e "\033[33mWhich cluster would you like to save the configuration for?\033[39m"

IFS=$'\n'
KUBE_CLUSTERS=($(doctl kubernetes cluster list | awk '{if(NR>1)print $0}'))
unset IFS

select_option "${KUBE_CLUSTERS[@]}"
choice=$?

TARGET_CLUSTER_ID=$(echo ${KUBE_CLUSTERS[$choice]} | awk '{print $1}')
TARGET_CLUSTER_NAME=$(echo ${KUBE_CLUSTERS[$choice]} | awk '{print $2}')
TARGET_CLUSTER_REGION=$(echo ${KUBE_CLUSTERS[$choice]} | awk '{print $3}')
TARGET_CLUSTER_VERSION=$(echo ${KUBE_CLUSTERS[$choice]} | awk '{print $4}')

# Save the config, throwing an error is something unexpected happens.
doctl kubernetes cluster kubeconfig save "${TARGET_CLUSTER_ID}" > /dev/null
if [[ $? -eq 0 ]]; then
echo -e "\033[32mThe kubeconfig has been successfully saved to $HOME/.kube/config.\033[39m"
else
echo -e "\033[31mThere was a problem saving the kubeconfig file.\033[39m"
exit 1
fi
# Set the new context, throwing an error is something unexpected happens.
kubectl config use-context "do-${TARGET_CLUSTER_REGION}-${TARGET_CLUSTER_NAME}" > /dev/null
if [[ $? -eq 0 ]]; then
echo -e "\033[32mKubectl has been configured to use ${TARGET_CLUSTER_NAME}.\033[39m"
else
echo -e "\033[31mThere was a problem setting the default context.\033[39m"
exit 1
fi
100 changes: 100 additions & 0 deletions scripts/create-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

BASEDIR=$(dirname "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )")
source "${BASEDIR}/scripts/functions.sh"

# ------------------------------------------------------------------------------
# Create Cluster
# ------------------------------------------------------------------------------
# In order to create a cluster we need the following:
# Cluster Name, Region, Version, Node Size, Node Count
# ------------------------------------------------------------------------------

# Cluster Name
# TODO: Add validation to this. No empty values, double check if spaces are allowed.
echo -e "\033[33mWhat would you like to name this cluster?\033[39m"
read -p "Cluster name: " CLUSTER_NAME
echo

echo "Fetching Kubernetes information, please wait..."
echo

REGIONS=($(doctl k8s options regions | awk '{if(NR>1)print $1}'))
VERSIONS=($(doctl k8s options versions | awk '{if(NR>1)print $1}'))
NODE_SIZES=($(doctl k8s options sizes | awk '{if(NR>1)print $1}'))

# Cluster Region
echo -e "\033[33mWhat region would you like to use for your cluster?\033[39m"
select_option "${REGIONS[@]}"
choice=$?
CLUSTER_REGION="${REGIONS[$choice]}"

# Kubernetes Version
echo -e "\033[33mWhich version of Kubernetes would you like to use?\033[39m"
select_option "${VERSIONS[@]}"
choice=$?
CLUSTER_VERSION="${VERSIONS[$choice]}"

# Node Size
echo -e "\033[33mPlease specify the node size that you would like to use for this cluster.\033[39m"
select_option "${NODE_SIZES[@]}"
choice=$?
NODE_SIZE="${NODE_SIZES[$choice]}"

# Node Count
echo -e "\033[33mHow many nodes would you like to provision for this cluster?\033[39m"

while true; do
read -p "Desired node count: " NODE_COUNT
# Make sure that the entry is a number that is between 1-10.
if [[ $NODE_COUNT -gt 0 && $NODE_COUNT -lt 11 ]]; then
break
else
echo -e "\033[31mError: Given value is not a valid entry. The count must be from 1 to 10. Please try again.\033[39m"
fi
done
echo

# Cluster Creation
echo -e "A Kubernetes cluster with the following configuration will be created.\033[39m"
echo "Name: kube-cluster"
echo "Region: nyc1"
echo "Version: 1.13.2-do.0"
echo "Node Size: s-1vcpu-2gb"
echo "Node Count: 1"
echo

echo "Creating a new Kubernetes cluster, please wait a few minutes..."
doctl kubernetes cluster create "${CLUSTER_NAME}" \
--region "${CLUSTER_REGION}" \
--version "${CLUSTER_VERSION}" \
--size "${NODE_SIZE}" \
--count "${NODE_COUNT}"

if [[ $? -eq 0 ]]; then
echo -e "\033[32mThe Kubernetes cluster has been successfully created!\033[39m"
else
echo -e "\033[31mThere was a problem creating the Kubernetes cluster.\033[39m"
exit 1
fi

# Set the proper context for the cluster.
CLUSTER_CONTEXT="do-${CLUSTER_REGION}-${CLUSTER_NAME}"
kubectl config use-context "${CLUSTER_CONTEXT}" > /dev/null

if [[ $? -eq 0 ]]; then
echo -e "\033[32mKubectl has been configured to use $CLUSTER_CONTEXT as the default context.\033[39m"
else
echo -e "\033[31mThere was a problem setting the default context for kubectl.\033[39m"
exit 1
fi

echo "Please wait until the nodes are up and running..."
until [[ $(kubectl get nodes 2>/dev/null | grep "\sReady\s") ]]; do
echo -n "."
sleep 1
done
echo

echo -e "\033[32mKubernetes is up and running!\033[39m"
echo
Loading

0 comments on commit 296d48e

Please sign in to comment.