Commit 232f9950 authored by Jason Plum's avatar Jason Plum

Merge branch 'add-eks-cluster-creation' into 'master'

Create EKS bootstrap script

See merge request charts/gitlab!706
parents be43870e ad621b33
Pipeline #50620345 failed with stages
in 62 minutes and 35 seconds
---
title: Add eks_bootstrap_script
merge_request: 706
author:
type: added
......@@ -5,6 +5,54 @@ deploying the `gitlab` chart.
## Creating the EKS cluster
To get started easier, a script is provided to automate the cluster creation.
Alternatively, a cluster can be created manually as well.
### Scripted cluster creation
A [bootstrap script](https://gitlab.com/charts/gitlab/blob/master/scripts/eks_bootstrap_script)
has been created to automate much of the setup process for users on EKS.
The script will:
1. Create a new EKS cluster.
1. Setup `kubectl`, and connect it to the cluster.
1. Initialize Helm and install Tiller.
The script uses [eksctl](https://eksctl.io) to initialize the cluster. If it cannot locate it in your PATH, it will install it
to a temporary location.
To authenticate, eksctl uses the same options as the aws command line. See the AWS documentation for how to
use [environment variables](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html), or [configuration files](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html).
The script reads various parameters from environment variables, or command line arguments and an argument
`up` or `down` for bootstrap and clean up respectively.
The table below describes all variables.
| Variable | Description | Default value |
|-----------------|-----------------------------------------------------------------------------|----------------------------------|
| REGION | The region where your cluster lives | us-east-2 |
| CLUSTER_NAME | The name of the cluster | gitlab-cluster |
| CLUSTER_VERSION | The version of your EKS cluster | 1.10 |
| NUM_NODES | The number of nodes required | 2 |
| MACHINE_TYPE | The type of nodes to deploy | m5.xlarge |
| SERVICE_ACCOUNT | The service account name to use for helm/tilller | tiller |
Run the script, by passing in your desired parameters. It can work with the
default parameters.
```bash
./scripts/eks_bootstrap_script up
```
The script can also be used to clean up the created EKS resources:
```bash
./scripts/eks_bootstrap_script down
```
### Manual cluster creation
For the most up to date instructions, follow Amazon's
[EKS getting started guide](https://docs.aws.amazon.com/eks/latest/userguide/getting-started.html).
......
......@@ -2,6 +2,29 @@
# Checks that appropriate gke params are set and
# that gcloud and kubectl are properly installed and authenticated
function need_tool(){
local tool="${1}"
local url="${2}"
echo >&2 "${tool} is required. Please follow ${url}"
exit 1
}
function need_gcloud(){
need_tool "gcloud" "https://cloud.google.com/sdk/downloads"
}
function need_kubectl(){
need_tool "kubectl" "https://kubernetes.io/docs/tasks/tools/install-kubectl"
}
function need_helm(){
need_tool "helm" "https://github.com/kubernetes/helm/blob/master/docs/install.md"
}
function need_eksctl(){
need_tool "eksctl" "https://exkctl.io"
}
function validate_required_tools(){
if [ -z "$PROJECT" ]; then
......@@ -9,9 +32,10 @@ function validate_required_tools(){
exit 1;
fi
command -v gcloud >/dev/null 2>&1 || { echo >&2 "gcloud is required please follow: https://cloud.google.com/sdk/downloads"; exit 1; }
command -v kubectl >/dev/null 2>&1 || { echo >&2 "kubectl is required please follow: https://kubernetes.io/docs/tasks/tools/install-kubectl"; exit 1; }
command -v helm >/dev/null 2>&1 || { echo >&2 "helm is required please follow: https://github.com/kubernetes/helm/blob/master/docs/install.md"; exit 1; }
for comm in gcloud kubectl helm
do
command -v "${comm}" > /dev/null 2>&1 || "need_${comm}"
done
gcloud container clusters list >/dev/null 2>&1 || { echo >&2 "Gcloud seems to be configured incorrectly or authentication is unsuccessfull"; exit 1; }
......@@ -20,3 +44,10 @@ function validate_required_tools(){
function cluster_admin_password_gke(){
gcloud container clusters describe $CLUSTER_NAME --zone $ZONE --project $PROJECT --format='value(masterAuth.password)';
}
function validate_eks_required_tools(){
for comm in eksctl kubectl helm
do
command -v "${comm}" > /dev/null 2>&1 || "need_${comm}"
done
}
#!/bin/sh
# This script will
# 1. create an EKS cluster
# 2. create an external IP
# 3. add a context to $HOME/.kube/config
# 5. Install tiller with a dedicated service account
set -e
# MacOS does not support readlink, but it does have perl
KERNEL_NAME=$(uname -s)
if [ "${KERNEL_NAME}" = "Darwin" ]; then
SCRIPT_PATH=$(perl -e 'use Cwd "abs_path";use File::Basename;print dirname(abs_path(shift))' "$0")
else
SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
fi
source "${SCRIPT_PATH}/common.sh"
# Set default values
cluster_name=${CLUSTER_NAME-gitlab-cluster}
nodes=${NUM_NODES-2}
kubernetes_version=${CLUSTER_VERSION-1.10}
region="${REGION-us-east-2}"
node_type="${MACHINE_TYPE-m5.xlarge}"
service_account="${SERVICE_ACCOUNT-tiller}"
function print_help(){
cat <<EOF
Usage: ${0} ARGS (up|down)
-c NAME - Name of the cluster to use. Defaults to gitlab-cluster
-n NODES - number of nodes to use. Defaults to 2
-k VERSION - Kubernetes version to use. Defaults to 1.10
-r REGION - Region to install the cluster in. Defaults to us-east-2
-m TYPE - Type of nodes to use. Default m5.xlarge
-s SA - Service account to use for helm/tiller. Default tiller
up - create a cluster
down - delete a cluster
list - display information about running clusters
EOF
}
function do_up(){
local cluster_name="${1}"
local nodes="${2}"
local kubernetes_version="${3}"
local region="${4}"
local node_type="${5}"
local sa="${6}"
eksctl create cluster --name="${cluster_name}" --nodes="${nodes}" --version="${kubernetes_version}" --region="${region}" --node-type="${node_type}"
kubectl create serviceaccount "${sa}" --namespace kube-system
kubectl create clusterrolebinding "${sa}" --clusterrole=cluster-admin --serviceaccount="kube-system:${sa}"
helm init --service-account "${sa}"
}
function do_down(){
local cluster_name="${1}"
local region="${2}"
eksctl delete cluster --wait --region "${region}" --name ${cluster_name}
}
function do_list(){
local cluster_name="${1}"
local region="${2}"
eksctl get cluster -r "${region}" "${cluster_name}"
}
while getopts c:n:kr:m:s: opt
do
case "${opt}" in
c)
cluster_name="${OPTARG}"
;;
n)
nodes="${OPTARG}"
;;
k)
kubernetes_version="${OPTARG}"
;;
m)
node_type="${OPTARG}"
;;
r)
region="${OPTARG}"
;;
s)
service_account="${OPTARG}"
;;
[?])
print_help
;;
esac
done
shift $((OPTIND -1))
validate_eks_required_tools
# Ensure we can connect to aws
if ! eksctl get cluster -v 0 > /dev/null
then
>&2 echo "Cannot connect to AWS. Ensure credentials are configured"
exit 1
fi
case ${1} in
up)
do_up "${cluster_name}" "${nodes}" "${kubernetes_version}" "${region}" "${node_type}" "${service_account}"
;;
down)
do_down "${cluster_name}" "${region}"
;;
list)
do_list "${2}" "${region}"
;;
*)
print_help
exit 1
esac
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment