Skip to content

Commit

Permalink
Fix #57 multiple containers in one pod
Browse files Browse the repository at this point in the history
  • Loading branch information
dimus committed Jul 19, 2017
1 parent 6ae5303 commit 1e45727
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 40 deletions.
123 changes: 123 additions & 0 deletions :w
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
apiVersion: v1
kind: Service
metadata:
name: resolver
namespace: gn
labels:
app: resolver
tier: frontend
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: resolver
tier: frontend

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: resolver
namespace: gn
spec:
replicas: 1
template:
metadata:
namespace: gn
name: resolver
labels:
app: resolver
tier: frontend
spec:
volumes:
- name: app-vol
emptyDir: {}
- name: tmp-vol
persistentVolumeClaim:
claimName: resolver-tmp-pvc
containers:
- name: resolver
image: gnames/resolver:0.0.15

env:
- name: RAILS_ENV
value: production
- name: GNI_UNICORN_WORKER_PROCESSES
valueFrom:
configMapKeyRef:
name: resolver
key: unicorn.workers
- name: GNI_BASE_URL
valueFrom:
configMapKeyRef:
name: resolver
key: server.url
- name: GNI_DATABASE_HOST
valueFrom:
configMapKeyRef:
name: resolver
key: db
- name: GNI_DATABASE
valueFrom:
configMapKeyRef:
name: resolver
key: db.database
- name: GNI_SOLR_URL
valueFrom:
configMapKeyRef:
name: resolver
key: solr.url
- name: GNI_DATABASE_USER
valueFrom:
secretKeyRef:
name: resolver
key: db.user
- name: GNI_DATABASE_PASSWORD
valueFrom:
secretKeyRef:
name: resolver
key: db.user.password
ports:
- containerPort: 8080
name: unicorn
command:
- /app/exe/unicorn-start
volumeMounts:
- name: app-vol
mountPath: /var/www
# - name: tmp-vol
# mountPath: /var/www/app/tmp/name_resolvers

- name: redis
image: redis
ports:
- containerPort: 6379
name: redis

- name: nginx
image: nginx
livenessProbe:
httpGet:
path: /name_resolvers.json?names=Plantago+major&data_source_ids=1
port: 80
initialDelaySeconds: 180
timeoutSeconds: 3
readinessProbe:
httpGet:
path: /name_resolvers.json?names=Plantago+major&data_source_ids=1
port: 80
initialDelaySeconds: 20
timeoutSeconds: 3
ports:
- containerPort: 80
name: http
volumeMounts:
- name: app-vol
mountPath: /var/www
command:
- bash
- -c
- "sleep 4 && var/www/app/exe/nginx-start"
19 changes: 4 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:14.04.4
FROM ubuntu:16.04
MAINTAINER Dmitry Mozzherin
ENV LAST_FULL_REBUILD 2016-03-06

Expand All @@ -7,14 +7,9 @@ RUN apt-get update && \
apt-get install -y software-properties-common && \
apt-add-repository ppa:brightbox/ruby-ng && \
apt-get update && \
apt-get install -y ruby2.1 ruby2.1-dev ruby-switch \
curl redis-server zlib1g-dev liblzma-dev libxml2-dev \
libxslt-dev libmysqlclient-dev supervisor build-essential nodejs && \
add-apt-repository -y ppa:nginx/stable && \
apt-get update && \
apt-get install -qq -y nginx && \
echo "\ndaemon off;" >> /etc/nginx/nginx.conf && \
chown -R www-data:www-data /var/lib/nginx && \
apt-get install -y ruby2.3 ruby2.3-dev locales \
curl zlib1g-dev liblzma-dev libxml2-dev \
libxslt-dev libmysqlclient-dev build-essential nodejs && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Expand All @@ -23,7 +18,6 @@ ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

RUN ruby-switch --set ruby2.1
RUN echo 'gem: --no-rdoc --no-ri >> "$HOME/.gemrc"'

# Configure Bundler to install everything globally
Expand All @@ -35,8 +29,6 @@ RUN gem install bundler && \
bundle config --global bin "$GEM_HOME/bin" && \
mkdir /app


COPY config/docker/files/nginx-sites.conf /etc/nginx/sites-enabled/default
COPY config/docker/files/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

WORKDIR /app
Expand All @@ -49,6 +41,3 @@ RUN bundle install

COPY . /app
RUN bundle exec rake assets:precompile RAILS_ENV=production

# CMD ["unicorn", "-c", "/app/config/docker/files/unicorn.rb"]
CMD /usr/bin/supervisord
5 changes: 2 additions & 3 deletions config/docker/files/nginx-sites.conf
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
upstream unicorn_server {
server unix:/tmp/unicorn.sock fail_timeout=0;
server localhost:8080 fail_timeout=0;
}

server {
listen 80;

root /app/public;
root /var/www/app/public;
try_files $uri @unicorn_server;

add_header Access-Control-Allow-Origin "*";

location @unicorn_server {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
#proxy_set_header X-Forwarded-Proto https; # if use ssl
proxy_redirect off;
proxy_pass http://unicorn_server;
client_max_body_size 1G;
Expand Down
6 changes: 1 addition & 5 deletions config/docker/files/supervisord.conf
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
[supervisord]
nodaemon=true

[program: redis]
command=redis-server
umask=022

[program: nginx]
command=/usr/sbin/nginx -c /etc/nginx/nginx.conf
umask=022

[program: unicorn]
command=/bin/bash -c "cd /app && bundle exec unicorn -c /app/config/docker/files/unicorn.rb"
command=/bin/bash -c "ln -s /app /var/www/app && cd /var/www/app && bundle exec unicorn -c /var/www/app/config/docker/files/unicorn.rb"
umask=022

6 changes: 3 additions & 3 deletions config/docker/files/unicorn.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
app_dir = "/app"
app_dir = "/var/www/app"
ENV["RACK_ENV"] = ENV["RAILS_ENV"] = "production"

working_directory app_dir
Expand All @@ -9,9 +9,9 @@
stdout_path "#{app_dir}/log/unicorn.stdout.log"

workers_num = ENV["GNI_UNICORN_WORKER_PROCESSES"].to_i
workers_num = 10 if workers_num == 0
workers_num = 1 if workers_num == 0

worker_processes workers_num

listen "/tmp/unicorn.sock", :backlog => 64
listen 8080, tcp_nopush: true
timeout 240
11 changes: 11 additions & 0 deletions exe/nginx-start
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
set -euo pipefail

while [[ ! -d /var/www/app ]]; do
echo "Waiting for resolver directory"
sleep 0.1
done

cp /var/www/app/config/docker/files/nginx-sites.conf /etc/nginx/conf.d/default.conf

nginx -g "daemon off;"
12 changes: 12 additions & 0 deletions exe/unicorn-start
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -euo pipefail

cp -r /app /var/www

if [ ! -e /var/www/app/tmp/name_resolvers ]; then
mkdir -p /var/www/app/tmp/name_resolvers
fi

cd /var/www/app
rm -rf /var/www/tmp/name_resolvers/*
bundle exec unicorn -c ./config/docker/files/unicorn.rb
55 changes: 41 additions & 14 deletions k8s/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ metadata:
name: resolver
namespace: gn
spec:
replicas: 3
replicas: 1
template:
metadata:
namespace: gn
Expand All @@ -32,18 +32,12 @@ spec:
app: resolver
tier: frontend
spec:
# volumes:
# - name: app-vol
# emptyDir: {}
volumes:
- name: app-vol
emptyDir: {}
containers:
- name: resolver
image: gnames/resolver:0.0.1
# livenessProbe:
# httpGet:
# path: /
# port: 80
# initialDelaySeconds: 180
# timeoutSeconds: 3
image: gnames/resolver:2017-07-18-c6150bd

env:
- name: RAILS_ENV
Expand Down Expand Up @@ -83,9 +77,42 @@ spec:
secretKeyRef:
name: resolver
key: db.user.password
ports:
- containerPort: 8080
name: unicorn
command:
- /app/exe/unicorn-start
volumeMounts:
- name: app-vol
mountPath: /var/www

- name: redis
image: redis
ports:
- containerPort: 6379
name: redis

- name: nginx
image: nginx
livenessProbe:
httpGet:
path: /name_resolvers.json?names=Plantago+major&data_source_ids=1
port: 80
initialDelaySeconds: 180
timeoutSeconds: 3
readinessProbe:
httpGet:
path: /name_resolvers.json?names=Plantago+major&data_source_ids=1
port: 80
initialDelaySeconds: 20
timeoutSeconds: 3
ports:
- containerPort: 80
name: http
# volumeMounts:
# - name: app-vol
# mountPath: /app
volumeMounts:
- name: app-vol
mountPath: /var/www
command:
- bash
- -c
- "sleep 4 && var/www/app/exe/nginx-start"

0 comments on commit 1e45727

Please sign in to comment.