Learning Docker Part 004 docker单机应用框架实战

今天我们来用docker实现如下架构应用:

Learning Docker Part 004 docker单机应用框架实战_第1张图片
应用框架
  1. 利用haproxy镜像做负载均衡
  2. 使用django镜像提供web应用
  3. 使用redis主从复制特性来保证数据的安全性

实验准备

  • docker pull ubuntu
  • docker pull django
  • docker pull haproxy
  • docker pull redis

实验步骤

# 1. 启动redis-master容器
fbo@fbo-virtual-machine:~$ sudo docker run -it --name redis-master redis /bin/bash
## 1.1 找到redis-master容器挂载的目录
root@fbo-virtual-machine:/home/fbo# docker inspect --format "{{.Mounts}}" redis-master
[{volume 468d2fd77c8abb2292c4bbde0f38445db29fc95814325a7d78b5eb3275bc0f28 /var/lib/docker/volumes/468d2fd77c8abb2292c4bbde0f38445db29fc95814325a7d78b5eb3275bc0f28/_data /data local  true }]
## 1.2 挂载redis配置文件到容器内 
root@fbo-virtual-machine:/home/fbo# cp redis.conf /var/lib/docker/volumes/468d2fd77c8abb2292c4bbde0f38445db29fc95814325a7d78b5eb3275bc0f28/_data/
## 1.3 修改redis配置文件并启动redis服务,'172.17.0.2'为redis-master容器
##     分配到的ip地址
root@6826f0490cac:/data# sed -i -e "s/^daemonize no/daemonize yes/g" \
    -e "s/^bind 127.0.0.1/bind 127.0.0.1 172.17.0.2/g" redis.conf
root@6826f0490cac:/data# redis-server /data/redis.conf
# 2. 启动redis-slave1容器
fbo@fbo-virtual-machine:~$ sudo docker run -it --name redis-slave1 --link redis-master:master redis /bin/bash
## 2.1 找到redis-slave1容器的挂载目录并拷贝配置文件到容器内
root@fbo-virtual-machine:/home/fbo# docker inspect --format "{{.Mounts}}" redis-slave1
[{volume 0ea7d8a1a95fb1623377e26c1cd2c421f9495502f36f474f6c9f3dd5ab216e28 /var/lib/docker/volumes/0ea7d8a1a95fb1623377e26c1cd2c421f9495502f36f474f6c9f3dd5ab216e28/_data /data local  true }]
root@fbo-virtual-machine:/home/fbo# cp redis.conf /var/lib/docker/volumes/0ea7d8a1a95fb1623377e26c1cd2c421f9495502f36f474f6c9f3dd5ab216e28/_data/
fbo@fbo-virtual-machine:~$ sudo docker run -it --name redis-slave2 --link redis-master:master redis /bin/bash
## 2.3 修改redis-slave1的配置文件,并启动redis服务
root@e66f071918f8:/data# sed -i -e "s/^daemonize no/daemonize yes/g" -e "s/^# slaveof.*/slaveof master 6379/g" redis.conf
root@e66f071918f8:/data# redis-server /data/redis.conf 
8:C 26 Sep 07:57:08.562 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8:C 26 Sep 07:57:08.562 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=8, just started
8:C 26 Sep 07:57:08.562 # Configuration loaded
# 3. 启动redis-slave2镜像,配置方式同redis-slave1
fbo@fbo-virtual-machine:~$ sudo docker run -it --name redis-slave2 --link redis-master:master redis /bin/bash
...
# 4. 在redis-master上检查主从同步情况
root@6826f0490cac:/data# redis-cli 
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2  # 此处slave应为2
slave0:ip=172.17.0.4,port=6379,state=online,offset=28,lag=0
slave1:ip=172.17.0.3,port=6379,state=online,offset=28,lag=0
master_replid:cec407ed87a259ddc1eadb16266949ff3a852e09
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
# 5. 启动并配置django App1
fbo@fbo-virtual-machine:~$ sudo docker run -it --name APP1 --link redis-master:db -v /home/fbo/Projects/Django/App1:/usr/src/app django /bin/bash
## 5.1 pip安装redis模块并验证
root@0cb68fb2c817:/# pip install redis
Collecting redis
  Downloading redis-2.10.6-py2.py3-none-any.whl (64kB)
    100% |████████████████████████████████| 71kB 103kB/s 
Installing collected packages: redis
Successfully installed redis-2.10.6
root@0cb68fb2c817:/# python 
Python 3.4.5 (default, Dec 14 2016, 18:54:20) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> print(redis.__file__)
/usr/local/lib/python3.4/site-packages/redis/__init__.py
>>> exit()
## 5.2 创建django项目
root@0cb68fb2c817:/# cd /usr/src/app/
root@0cb68fb2c817:/usr/src/app# mkdir dockerweb
root@0cb68fb2c817:/usr/src/app# cd dockerweb/
root@0cb68fb2c817:/usr/src/app/dockerweb# django-admin startproject redisweb
root@0cb68fb2c817:/usr/src/app/dockerweb# ls
redisweb
root@0cb68fb2c817:/usr/src/app/dockerweb# cd redisweb/
root@0cb68fb2c817:/usr/src/app/dockerweb/redisweb# ls
manage.py  redisweb
root@0cb68fb2c817:/usr/src/app/dockerweb/redisweb# python manage.py startapp helloworld
root@0cb68fb2c817:/usr/src/app/dockerweb/redisweb# ls
helloworld  manage.py  redisweb
## 5.3 在host上修改如下配置
root@fbo-virtual-machine:/home/fbo/Projects/Django/App1/dockerweb/redisweb/helloworld# pwd
/home/fbo/Projects/Django/App1/dockerweb/redisweb/helloworld
root@fbo-virtual-machine:/home/fbo/Projects/Django/App1/dockerweb/redisweb/helloworld# cat 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+="
" r = redis.Redis(host='db', port=6379, db=0) info = r.info() str+=("Set Hi
") r.set('Hi','HelloWorld-APP1') str+=("Get Hi: %s
" % r.get('Hi')) str+=("Redis Info:
") str+=("Key: Info Value") for key in info: str+=("%s: %s
" % (key, info[key])) return HttpResponse(str) root@fbo-virtual-machine:/home/fbo/Projects/Django/App1/dockerweb/redisweb/redisweb# pwd /home/fbo/Projects/Django/App1/dockerweb/redisweb/redisweb root@fbo-virtual-machine:/home/fbo/Projects/Django/App1/dockerweb/redisweb/redisweb# ll settings.py -rw-r--r-- 1 root root 3120 9月 26 16:57 settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'helloworld', ] root@fbo-virtual-machine:/home/fbo/Projects/Django/App1/dockerweb/redisweb/redisweb# pwd /home/fbo/Projects/Django/App1/dockerweb/redisweb/redisweb root@fbo-virtual-machine:/home/fbo/Projects/Django/App1/dockerweb/redisweb/redisweb# cat urls.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), ] ## 5.3 回到容器,执行如下配置 root@0cb68fb2c817:/usr/src/app/dockerweb/redisweb# python manage.py makemigrations No changes detected root@0cb68fb2c817:/usr/src/app/dockerweb/redisweb# python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying sessions.0001_initial... OK ## 5.4 启动web服务 root@0cb68fb2c817:/usr/src/app/dockerweb/redisweb# python manage.py runserver 0.0.0.0:8001 Performing system checks... System check identified no issues (0 silenced). September 26, 2017 - 09:20:02 Django version 1.10.4, using settings 'redisweb.settings' Starting development server at http://0.0.0.0:8001/ Quit the server with CONTROL-C. # 6. 启动django app2,配置过程同app1 fbo@fbo-virtual-machine:~$ sudo docker run -it --name APP2 --link redis-master:db -v /home/fbo/Projects/Django/App2:/usr/src/app django /bin/bash # 7. 启动HAProxy容器 fbo@fbo-virtual-machine:~$ sudo docker run -it --name HAProxy --link APP1:APP1 --link APP2:APP2 -p 6301:6301 -v /home/fbo/Projects/HAProxy:/tmp haproxy /bin/bash root@2cf6b97c837a:/# ## 7.1 haproxy配置文件 root@fbo-virtual-machine:/home/fbo/Projects/HAProxy# cat haproxy.cfg global log 127.0.0.1 local0 maxconn 4096 chroot /usr/local/bin 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:8001 check inter 2000 rise 2 fall 5 server APP2 APP2:8002 check inter 2000 rise 2 fall 5 ## 7.2 启动haproxy root@2cf6b97c837a:/usr/local/bin# cp /tmp/haproxy.cfg /usr/local/bin/ root@2cf6b97c837a:/usr/local/bin# haproxy -f haproxy.cfg [WARNING] 268/094157 (15) : Proxy 'redis_proxy': in multi-process mode, stats will be limited to process assigned to the current request. root@2cf6b97c837a:/usr/local/bin# ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 20252 3184 pts/0 Ss 09:25 0:00 /bin/bash root 16 0.0 0.0 33328 2860 ? Ss 09:41 0:00 haproxy -f haproxy.cfg root 17 0.0 0.0 33328 2860 ? Ss 09:41 0:00 haproxy -f haproxy.cfg root 18 0.0 0.0 33328 2860 ? Ss 09:41 0:00 haproxy -f haproxy.cfg root 19 0.0 0.0 33328 2860 ? Ss 09:41 0:00 haproxy -f haproxy.cfg root 20 0.0 0.0 17500 2072 pts/0 R+ 09:42 0:00 ps -aux

你可能感兴趣的:(Learning Docker Part 004 docker单机应用框架实战)