Skip to content

Commit

Permalink
Merge pull request #16 from jugatsu/kubernetes-2
Browse files Browse the repository at this point in the history
Add full app deployment in the Kubernetes cluster
  • Loading branch information
jugatsu authored Dec 7, 2017
2 parents 71736b9 + 3320c7e commit 9cbb248
Show file tree
Hide file tree
Showing 15 changed files with 457 additions and 76 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ build_info.txt
# python
*.pyc
*.pyo

# terraform
*.tfstate
*.tfstate.*.backup
*.tfstate.backup
*.tfvars
.terraform/
47 changes: 47 additions & 0 deletions infra/gke/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Ensure GKE cluster is present and configured
resource "google_container_cluster" "cluster" {
description = "GKE Cluster for Reddit-app"
enable_legacy_abac = false
initial_node_count = "${var.gke_node_count}"
min_master_version = "${var.gke_version}"
name = "${var.gke_name}"
zone = "${var.gke_zone}"

addons_config {
kubernetes_dashboard {
disabled = "${var.gke_dashboard_disabled}"
}
}

node_config {
disk_size_gb = "${var.gke_node_size}"
image_type = "${var.gke_node_image}"
machine_type = "${var.gke_node_machine_type}"

oauth_scopes = [
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring",
]
}

// configure kubectl
provisioner "local-exec" {
command = "gcloud container clusters get-credentials ${var.gke_name} --zone ${var.gke_zone} --project ${var.google_project}"
}
}

// Ensure firewall rule for application access is present and configured
resource "google_compute_firewall" "firewall" {
name = "gke-reddit-app"
description = "Allow access to reddit-app deployed in the Kubernetes"
network = "default"

allow = {
protocol = "tcp"
ports = ["30000-32767"]
}

source_ranges = ["0.0.0.0/0"]
}
12 changes: 12 additions & 0 deletions infra/gke/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Configure the Google Cloud provider
provider "google" {
version = "~> 1.3.0"

project = "${var.google_project}"
region = "${var.google_region}"
}

// Configure the Kubernetes provider
provider "kubernetes" {
version = "~> 1.0.1"
}
5 changes: 5 additions & 0 deletions infra/gke/terraform.tfvars.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#################################
# Google Cloud Provider variables
#################################

google_project = "YOUR_PROJECT"
53 changes: 53 additions & 0 deletions infra/gke/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#################################
# Google Cloud Provider variables
#################################

variable "google_project" {
description = "The ID of the project"
}

variable "google_region" {
default = "europe-west1"
description = "The region to operate under"
}

#################################
# Kubernetes Provider variables
#################################

variable "gke_dashboard_disabled" {
default = true
}

variable "gke_name" {
default = "cluster-1"
description = "The name of GKE cluster"
}

variable "gke_node_count" {
default = 3
description = "The number of nodes in GKE cluster"
}

variable "gke_version" {
default = "1.8.3-gke.0"
}

variable "gke_zone" {
default = "europe-west1-c"
}

variable "gke_node_size" {
default = 20
description = "Size of the disk attached to each node"
}

variable "gke_node_machine_type" {
default = "n1-standard-1"
description = "The name of a Google Compute Engine machine type"
}

variable "gke_node_image" {
default = "COS"
description = "The image type to use for this node"
}
46 changes: 46 additions & 0 deletions kubernetes/app/comment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# ------------------- Comment Deployment ------------------- #
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: comment
labels:
app: reddit
component: comment
spec:
selector:
matchLabels:
app: reddit
component: comment
replicas: 3
template:
metadata:
name: comment
labels:
app: reddit
component: comment
spec:
containers:
- image: jugatsu/comment:latest
name: comment
env:
- name: COMMENT_DATABASE_HOST
value: comment-db

---
# ------------------- Comment Service ------------------- #
apiVersion: v1
kind: Service
metadata:
name: comment
labels:
app: reddit
component: comment
spec:
ports:
- port: 9292
protocol: TCP
targetPort: 9292
selector:
app: reddit
component: comment
75 changes: 75 additions & 0 deletions kubernetes/app/mongodb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
# ------------------- MongoDB Deployment ------------------- #
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: mongo
labels:
app: reddit
component: mongo
comment-db: "true"
post-db: "true"
spec:
replicas: 1
selector:
matchLabels:
app: reddit
component: mongo
template:
metadata:
name: mongo
labels:
app: reddit
component: mongo
comment-db: "true"
post-db: "true"
spec:
containers:
- image: mongo:3.2
name: mongo
volumeMounts:
- name: mongo-persistent-storage
mountPath: /data/db
volumes:
- name: mongo-persistent-storage
emptyDir: {}

---
# ------------------- Mongo-Comment Service ------------------- #
apiVersion: v1
kind: Service
metadata:
name: comment-db
labels:
app: reddit
component: mongo
comment-db: "true"
spec:
ports:
- port: 27017
protocol: TCP
targetPort: 27017
selector:
app: reddit
component: mongo
comment-db: "true"

---
# ------------------- Mongo-Post Service ------------------- #
apiVersion: v1
kind: Service
metadata:
name: post-db
labels:
app: reddit
component: mongo
post-db: "true"
spec:
ports:
- port: 27017
protocol: TCP
targetPort: 27017
selector:
app: reddit
component: mongo
post-db: "true"
46 changes: 46 additions & 0 deletions kubernetes/app/post.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# ------------------- Post Deployment ------------------- #
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: post
labels:
app: reddit
component: post
spec:
selector:
matchLabels:
app: reddit
component: post
replicas: 3
template:
metadata:
name: post
labels:
app: reddit
component: post
spec:
containers:
- image: jugatsu/post:latest
name: post
env:
- name: POST_DATABASE_HOST
value: post-db

---
# ------------------- Post Service ------------------- #
apiVersion: v1
kind: Service
metadata:
name: post
labels:
app: reddit
component: post
spec:
ports:
- port: 5000
protocol: TCP
targetPort: 5000
selector:
app: reddit
component: post
49 changes: 49 additions & 0 deletions kubernetes/app/ui.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
# ------------------- UI Deployment ------------------- #
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: ui
labels:
app: reddit
component: ui
spec:
replicas: 3
selector:
matchLabels:
app: reddit
component: ui
template:
metadata:
name: ui
labels:
app: reddit
component: ui
spec:
containers:
- image: jugatsu/ui:latest
name: ui
env:
- name: ENV
valueFrom:
fieldRef:
fieldPath: metadata.namespace

---
# ------------------- UI Service ------------------- #
apiVersion: v1
kind: Service
metadata:
name: ui
labels:
app: reddit
component: ui
spec:
type: NodePort
ports:
- port: 9292
protocol: TCP
targetPort: 9292
selector:
app: reddit
component: ui
19 changes: 0 additions & 19 deletions kubernetes/comment-deployment.yml

This file was deleted.

Loading

0 comments on commit 9cbb248

Please sign in to comment.