Skip to content

Commit 4494d3f

Browse files
committed
init commit
0 parents  commit 4494d3f

File tree

91 files changed

+1470
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+1470
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.vagrant
2+
cookbooks
3+
tmp
4+
web/public/exports
5+
web/vendor/assets/bower_components
6+
web/public/assets

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Kubernetes-Rails
2+
3+
**NOTE** This is not production ready. The env is set to "production" as a proof of concept to show the asset pipeline is working in prod.
4+
5+
## Run locally
6+
1. `docker-machine create --driver virtualbox --virtualbox-disk-size "30000" --virtualbox-memory "8096" kubernetes-rails`
7+
2. `docker-machine start kubernetes-rails`
8+
3. `eval "$(docker-machine env kubernetes-rails)"`
9+
4. `docker-compose build && docker-compose up -d`
10+
11+
## Build images
12+
You will need to change **foxio-rnd** to your GCE project name.
13+
14+
1. `docker build -t gcr.io/foxio-rnd/rails-image:v1 web/.`
15+
2. `gcloud docker push gcr.io/foxio-rnd/rails-image:v1` **Note** pushing can take a long time and use a lot of bandwidth. This actually times out sometimes. Just run it again if it does that.
16+
3. `docker build -t gcr.io/foxio-rnd/nginx-image:v1 nginx/.`
17+
4. `gcloud docker push gcr.io/foxio-rnd/nginx-image:v1`
18+
19+
20+
## GCE deploy
21+
1. `gcloud container clusters create kubernetes-rails --num-nodes 2 --machine-type g1-small`
22+
2. `kubectl run db --image=postgres --port=5432`
23+
3. `kubectl expose rc db`
24+
4. Update kubernetes/web-controller.yaml to use your GCE project name instead of **foxio-rnd**
25+
5. `kubectl create -f kubernetes/web-controller.yaml`
26+
6. Using `kubectl get pods` wait for the 2 pods to change to `Running`. This can take a few minutes.
27+
7. `kubectl create -f kubernetes/web-service.json`
28+
29+
## DB Setup
30+
1. `kubectl get pods`
31+
2. `kubectl exec -it [pod id] -c web bash`
32+
3. run `rake db:create` and `rake db:migrate`
33+
34+
## Teardown
35+
1. `gcloud container clusters delete kubernetes-rails`
36+
2. Manually delete the Load balancer from the Network -> Network load balancer
37+
38+
## TODO
39+
1. automate the DB setup

docker-compose.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
version: '2'
2+
services:
3+
web:
4+
restart: always
5+
dns:
6+
- 8.8.8.8
7+
build:
8+
context: ./web
9+
environment:
10+
RAILS_ENV: production
11+
WEB_DATABASE_HOST: db
12+
SECRET_KEY_BASE: a964ebdd62805aeff7659781ae0e017e94b9fb8a90a8187dd01f68fefa3791cd1d72ea36f469bc3ad3da1efa36981147afdffe2461afac518ee2a672fb201948
13+
WEB_DATABASE_PASSWORD: postgres
14+
expose:
15+
- "8080"
16+
volumes:
17+
- ./web:/my_project
18+
links:
19+
- db
20+
nginx:
21+
build: ./nginx
22+
links:
23+
- web
24+
# - api
25+
ports:
26+
- "80:80"
27+
expose:
28+
- "80"
29+
volumes:
30+
- "/var/run/docker.sock:/tmp/docker.sock"
31+
volumes_from:
32+
- web
33+
db:
34+
image: postgres:latest
35+
environment:
36+
POSTGRES_USER: "postgres"
37+
POSTGRES_PASSWORD: "postgres"
38+
ports:
39+
- "5432:5432"

kubernetes/db-service.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: db
5+
labels:
6+
name: db
7+
spec:
8+
ports:
9+
- port: 5432
10+
selector:
11+
run: db

kubernetes/web-controller.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
apiVersion: v1
2+
kind: ReplicationController
3+
metadata:
4+
name: www-v1
5+
labels:
6+
app: www
7+
spec:
8+
replicas: 2
9+
selector:
10+
app: www
11+
version: v1
12+
template:
13+
metadata:
14+
labels:
15+
app: www
16+
version: v1
17+
spec:
18+
volumes:
19+
- name: web-assets
20+
emptyDir: {}
21+
- name: web-sock
22+
emptyDir: {}
23+
containers:
24+
- name: web
25+
image: gcr.io/foxio-rnd/rails-image:v1
26+
ports:
27+
- name: web-server
28+
containerPort: 8080
29+
env:
30+
- name: RAILS_ENV
31+
value: production
32+
- name: WEB_DATABASE_HOST
33+
value: db
34+
- name: SECRET_KEY_BASE
35+
value: 4fb2a451674dd7c5641577a0031847d82247bd137fedb0ba91c6d1a6ccbc8d2da370ffa164503f50c2f2c121f46f1f21b89dc946633924e0c464bdb69b368415
36+
volumeMounts:
37+
- mountPath: /assets
38+
name: web-assets
39+
- mountPath: /tmp
40+
name: web-sock
41+
lifecycle:
42+
postStart:
43+
exec:
44+
command:
45+
- /bin/bash
46+
- -c
47+
- /my_project/kubernetes-post-start.sh
48+
- name: nginx
49+
image: gcr.io/foxio-rnd/nginx-image:v1
50+
ports:
51+
- name: http-server
52+
containerPort: 80
53+
- name: https-server
54+
containerPort: 443
55+
volumeMounts:
56+
- mountPath: /my_project/public
57+
name: web-assets
58+
readOnly: true
59+
- mountPath: /tmp
60+
name: web-sock

kubernetes/web-service.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"kind":"Service",
3+
"apiVersion":"v1",
4+
"metadata":{
5+
"name":"www",
6+
"labels":{
7+
"app":"www"
8+
}
9+
},
10+
"spec":{
11+
"ports": [
12+
{
13+
"name": "http",
14+
"port":80,
15+
"targetPort":"http-server"
16+
},
17+
{
18+
"name": "https",
19+
"port":443,
20+
"targetPort":"https-server"
21+
}
22+
],
23+
"selector":{
24+
"app":"www"
25+
},
26+
"type": "LoadBalancer"
27+
}
28+
}

nginx/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Set nginx base image
2+
FROM nginx
3+
4+
# Copy custom configuration file from the current directory
5+
COPY nginx.conf /etc/nginx/nginx.conf

nginx/nginx.conf

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
worker_processes 4;
2+
3+
events { worker_connections 1024; }
4+
5+
http {
6+
7+
upstream unicorn {
8+
least_conn;
9+
#server web:8080 weight=10 max_fails=3 fail_timeout=30s;
10+
server unix:/tmp/unicorn.sock fail_timeout=0;
11+
}
12+
13+
server {
14+
listen 80;
15+
16+
root /my_project/public;
17+
18+
# serve static (compiled) assets directly if they exist (for rails production)
19+
location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
20+
include /etc/nginx/mime.types;
21+
try_files $uri @unicorn;
22+
23+
access_log off;
24+
gzip_static on; # to serve pre-gzipped version
25+
26+
expires max;
27+
add_header Cache-Control public;
28+
29+
# Some browsers still send conditional-GET requests if there's a
30+
# Last-Modified header or an ETag header even if they haven't
31+
# reached the expiry date sent in the Expires header.
32+
add_header Last-Modified "";
33+
add_header ETag "";
34+
break;
35+
}
36+
37+
# send non-static file requests to the app server
38+
location / {
39+
try_files $uri @unicorn;
40+
}
41+
42+
location @unicorn {
43+
#proxy_set_header X-Real-IP $remote_addr;
44+
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
45+
#proxy_set_header Host $http_host;
46+
#proxy_redirect off;
47+
#proxy_pass http://unicorn;
48+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
49+
proxy_set_header Host $http_host;
50+
proxy_redirect off;
51+
52+
# If you don't find the filename in the static files
53+
# Then request it from the unicorn server
54+
if (!-f $request_filename) {
55+
proxy_pass http://unicorn;
56+
break;
57+
}
58+
}
59+
60+
error_page 500 502 503 504 /500.html;
61+
location = /500.html {
62+
root /my_project/public;
63+
}
64+
}
65+
}

web/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile '~/.gitignore_global'
6+
7+
# Ignore bundler config.
8+
/.bundle
9+
10+
# Ignore the default SQLite database.
11+
/db/*.sqlite3
12+
/db/*.sqlite3-journal
13+
14+
# Ignore all logfiles and tempfiles.
15+
/log/*
16+
/tmp/*
17+
!/log/.keep
18+
!/tmp/.keep
19+
20+
# Ignore Byebug command history file.
21+
.byebug_history

web/.rvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rvm --create use ruby-2.3.0@kubernetes-rails

0 commit comments

Comments
 (0)