Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kops #14

Open
wants to merge 33 commits into
base: add-vpc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
1e8a581
Add explicit backend to terraform scripts (#13)
thejsj May 18, 2017
674dbc1
WIP
thejsj Apr 27, 2017
2d65e0b
Add validated changes
thejsj Apr 27, 2017
ca648e2
Remove unsued variables
thejsj Apr 27, 2017
dd8093c
Fix database module
thejsj Apr 27, 2017
10cc7b0
Fix instance module
thejsj Apr 27, 2017
8083298
Start adding bastion
thejsj Apr 27, 2017
d6039e2
Add fixes
thejsj Apr 28, 2017
04bcd09
WIP: Adding VPC, subnets, route tables, etc
thejsj May 1, 2017
030e3cf
Add output and minor fixes
thejsj May 10, 2017
01e47a1
Change public key to file
thejsj May 11, 2017
928bf95
Add route53
thejsj May 11, 2017
58d7fe8
Add Route53 setup
thejsj May 11, 2017
e8797b8
Add new s3 bucket
thejsj May 11, 2017
26398fd
Add output and kops script
thejsj May 11, 2017
76c5ece
Start refactoring
thejsj May 12, 2017
f0ab6a8
WIP Addp K8 stuff
thejsj May 15, 2017
3e2439f
Add main terraform file
thejsj May 18, 2017
db43d0b
Fix setup of kops cluster. Fix Readme
thejsj May 18, 2017
4be7b86
Add security groups from cluster
thejsj May 18, 2017
ba68023
Remove main instance and related resources. Fix kops terraform apply
thejsj May 19, 2017
627ed03
Add default username and password
thejsj May 19, 2017
23007ed
Update README.md
thejsj May 19, 2017
a5e4344
Update bastion and database
thejsj Jun 1, 2017
cfb9204
Update README and cluster creation
thejsj Jun 2, 2017
0145db4
Fix node-count
thejsj Jun 5, 2017
0abf158
Start ASG with 0 instances
thejsj Jun 26, 2017
a8b743b
Ignore back files
thejsj Jun 26, 2017
5634b4c
Fix comments
thejsj Jul 5, 2017
ee8ead3
Change backend to S3. Change desired capacity to 0
thejsj Jul 6, 2017
d3fd612
Create the elastic ip, nat gateway, routing table for docks (#21)
henrymollman Jul 7, 2017
c23cdeb
Output dock subnet cidr range (#22)
henrymollman Jul 13, 2017
722e8c6
Small helpers for readme (#19)
Myztiq Jul 13, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Compiled files
*.tfstate
*.tfstate.backup
*.bak

# Module directory
.terraform/
Expand All @@ -9,3 +10,4 @@
environments/*
!environments/runnable-on-prem.example.tfvars

step-2-kops
118 changes: 115 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,121 @@
# Runnable On-Prem Terraform

### Dependencies

```
brew install terraform kops jq kubectl

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should also provide link to install brew.

```

[How to install homebrew](https://brew.sh/).

### Step 1: Obtaining AWS Access Tokens

1. https://console.aws.amazon.com/iam/home?region=us-east-2#/security_credential
2. Click create new access key

```
# TODO: Define permisisons
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
terraform get
terraform plan
terraform apply

```
[How to get access tokens](http://docs.aws.amazon.com/lambda/latest/dg/getting-started.html)

### Step 2: Populating Variables

Populate `environments/main.tfvars` with correct variables.

The following are the only required variables:

```
# Domain to be used by Runnable.
# Access to setting DNS nameservers is required.
# Multiple subdomains must be set for this domain
domain = "runnable.com"
# A Github organization id (See below of obtaining ID)
github_org_id = "2828361" # Github ID for organization
# Location of previously generationg configuation
# Should be generated using github.com/CodeNow/on-prem-devops-scripts
lc_user_data_file_location = "~/dock-runnable-on-prem.sh" # File must be already generated
# Path to a public key (See below of generating public key)
public_key_path = "~/.ssh/*.pub" # A public key
```

##### Obtaining A Github ID}

```
curl -sS "https://api.github.com/orgs/${ORGNAME}" | jq '.id'
```

##### Obtaining A Public Key From Private Key

```
ssh-keygen -y -f ~/.ssh/${NAME}.pem >> ~/.ssh/${NAME}.pem.pub
```

##### Creating a New Public Key
```
openssl req -newkey rsa:2048 -new -nodes -keyout key.pem
chmod 400 key.pem
ssh-keygen -y -f key.pem >> key.pem.pub
```

### Step 3: Init Terraform and Apply First Part

```
terraform init

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add cd step, or tell me what directory I need to be in to run this command

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're supposed to run it from this directory... cd ./on-prem-terraform?

# Bug in terraform requires explicitely requiring submodules https://github.com/hashicorp/terraform/issues/5190
terraform apply -target=module.step_1.module.key_pair -target=module.step_1.module.vpc -target=module.step_1.module.route53 -target=module.step_1.module.s3 -var-file="environments/main.tfvars"
```

### Step 4: Update DNS

Using the output of the command above you can see the DNS entry section, update your DNS to match these records. There should 4 entries. DNS nameservers need to be propagated before going on to the next step.

### Step 5: Create Kops configuration

[kops]() is a tool to automatically spin up

```
bash create-k8-cluster.bash environments/main.tfvars
```

### Step 6: Apply configuration

Finally, it's time to create the infrastructure. This includes the kuberentes cluster, the auto scaling group for the dock workers, and the RDS database.

If you want to review the resources to be created, first run `terraform plan -var-file="environments/main.tfvars"`.

When you're ready to apply changes, just run

```
terraform apply -var-file="environments/main.tfvars"
```

### Step 7: Confirm Cluster is Up

After finishing the setup, you can now test if the cluster is up by running the following command (This can take a few minutes).

```
kubectl get nodes
```

You should see something like this. It will take some time for nodes to appear as "Ready":

```
$ kubectl get nodes
NAME STATUS AGE VERSION
ip-10-10-34-129.us-west-2.compute.internal Ready,master 1h v1.5.7
ip-10-10-57-73.us-west-2.compute.internal Ready 1h v1.5.7
ip-10-10-61-76.us-west-2.compute.internal Ready 1h v1.5.7
```

### Step 8: Add dashboard

After cluster is ready, run the following command to run the dashboard:

```
kubectl create -f https://raw.githubusercontent.com/kubernetes/kops/master/addons/kubernetes-dashboard/v1.6.0.yaml
```

Then, run `kubectl proxy` and go to [`127.0.0.1:8001/ui/`](http://127.0.0.1:8001/ui) to test it.
30 changes: 30 additions & 0 deletions create-k8-cluster.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

# We need to run a refresh before we can run `terraform output`
terraform refresh -var-file=$1 > /dev/null

JSON=$(terraform output -json)
REGION=$(echo $JSON | jq --raw-output '.aws_region.value')
ENV=$(echo $JSON | jq --raw-output '.environment.value')
VPC_ID=$(echo $JSON | jq --raw-output '.vpc_id.value')
BUCKET_NAME=$(echo $JSON | jq --raw-output '.kops_config_bucket.value')
CLUSTER_NAME=$(echo $JSON | jq --raw-output '.cluster_name.value')
SSH_PUBLIC_KEY_PATH=$(echo $JSON | jq --raw-output '.ssh_public_key_path.value')

echo "Creating cluster in VPC $VPC_ID with name $CLUSTER_NAME"

kops create cluster \
--zones="${REGION}a" \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you add a ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be us-west-2a or us-west-2b (a zone, not a region). I added a arbitrarily.

--name=${CLUSTER_NAME} \
--vpc=${VPC_ID} \
--node-count=3 \
--cloud=aws \
--cloud-labels="Environment=${ENV}" \
--ssh-public-key=${SSH_PUBLIC_KEY_PATH} \
--state=s3://${BUCKET_NAME} \
--node-size=m4.large \
--master-size=m4.large \
--out=./step-2-kops --target=terraform

# Move file in order for it to be a valid module
mv ./step-2-kops/kubernetes.tf ./step-2-kops/main.tf
35 changes: 0 additions & 35 deletions database/main.tf

This file was deleted.

10 changes: 2 additions & 8 deletions environments/runnable-on-prem.example.tfvars
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
# All variables in this document should match
domain = ""
github_org_id = ""
public_key = ""
db_username = "" # Must start with a letter
db_password = ""
db_subnet_group_name = ""
main_host_vpc_id = ""
main_host_subnet_id = ""
main_host_private_ip = "10.4.0.100"
dock_subnet_id = ""
github_org_id = ""
key_name = ""
lc_user_data_file_location = "~/dock-runnable-on-prem.sh"
bastion_sg_id = ""
environment = "runnable-on-prem"
aws_region = "us-west-2"
Loading