Docker 搭建一个Docker应用栈
Docker应用栈结构图
Build Django容器
编写docker-file
FROM django
RUN pip install redis
build django-with-redis image
root@swarm-manager:~# docker build -t django-with-redis . Sending build context to Docker daemon 223.7kB Step 1/2 : FROM django ---> eb40dcf64078 Step 2/2 : RUN pip install redis ---> Running in 61fb045c3277 Collecting redis Downloading redis-2.10.6-py2.py3-none-any.whl (64kB) Installing collected packages: redis Successfully installed redis-2.10.6 You are using pip version 9.0.1, however version 9.0.3 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Removing intermediate container 61fb045c3277 ---> 60c26592c8e5 Successfully built 60c26592c8e5 Successfully tagged django-with-redis:latest
启动容器
启用Redis一个master和两个slave # docker run -itd --name redis-master redis # docker run -itd --name redis-slave1 --link redis-master:master redis # docker run -itd --name redis-slave2 --link redis-master:master redis 启动Django # docker run -itd --name app1 --link redis-master:db -v ~/Projects/Django/app1:/usr/src/app django-with-redis # docker run -itd --name app2 --link redis-master:db -v ~/Projects/Django/app2:/usr/src/app django-with-redis 启动HAproxy # docker run -itd --name HAproxy --link app1:app1 --link app2:app2 -p 8080:8080 -v ~/Projects/HAproxy:/usr/local/sbin haproxy
配置Redis
Redis-Master配置文件, 任意redis兼容的配置文件, 修改以下两个参数
daemonize yes pidfile /var/run/redis.pid
Redis-Slave配置文件,任意redis兼容的配置文件,修改以下三个参数
daemonize yes pidfile /var/run/redis.pid slaveof master 6379
将配置文件复制到Redis中,并启动Redis服务
# docker cp redis.conf redis-master:/usr/local/bin # docker exec -it redis-master /bin/bash # cd /usr/local/bin # redis-server redis.conf
Redis主从测试-Master测试
root@93cf64cdeedd:/data# redis-cli 127.0.0.1:6379> set hello world OK 127.0.0.1:6379> get hello "world" 127.0.0.1:6379>
Redis主从测试-Slave测试
root@c84e012871ae:/data# redis-cli 127.0.0.1:6379> get hello "world" 127.0.0.1:6379>
配置Django
在Django容器中,生成Django项目文件
# cd /usr/src/app # mkdir dockerweb # django-admin startproject redisweb # cd redisweb # python manage.py startapp helloworld
在Docker宿主机修改Django配置文件
root@swarm-manager:~/Projects/Django/App1/dockerweb/redisweb/helloworld# ls
admin.py apps.py __init__.py migrations models.py __pycache__ tests.py views.py
修改views.py
from django.shortcuts import render from django.http import HttpResponse # Create your views here. import redis def hello(request): str = redis.__file__ str += "<br>" r = redis.Redis(host="db", port=6379, db=0) info = r.info() str += ("Set Hi <br>") r.set("Hi", "HelloWorld-App1") str += ("Get Hi: %s <br>" % r.get("Hi")) str += ("Redis Info: <br>") str += ("Key: Info Value") for key in info: str += ("%s: %s <br>" % (key, info[key])) return HttpResponse(str)
修改settings.py
ALLOWED_HOSTS = ["*",] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'helloworld' ]
修改url.py
from django.conf.urls import url from django.contrib import admin from helloworld.views import hello urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'helloworld$', hello), ]
进入Django容器完成项目生成
# python manage.py makemigrations
# python manage.py migrate
启动Django
# python manage.py runserver 0.0.0.0:8000 &
配置HAProxy
创建配置文件
global log 127.0.0.1 local0 maxconn 4096 chroot /usr/local/sbin daemon nbproc 4 pidfile /usr/local/sbin/haproxy.pid defaults log 127.0.0.1 local3 mode http option dontlognull option redispatch retries 2 maxconn 2000 balance roundrobin timeout connect 5000ms timeout client 50000ms timeout server 50000ms listen redis_proxy bind 0.0.0.0:6301 stats enable stats uri /haproxy-stats server app1 app1:8000 check inter 2000 rise 2 fall 5 server app2 app2:8000 check inter 2000 rise 2 fall 5
启动HAproxy
# cd /usr/local/sbin
# haproxy -f haproxy.cfg
验证