Docker搭建应用栈

前提条件

    系统:centos7.2

    软件:Docker 1.12.6


一、获取镜像


docker pull ubuntu

docker pull django

docker pull haproxy

docker pull redis


此步如果无法正常拉取镜像,可通过阿里云镜像加速器或者Daocloud的镜像加速器进行设置。


二、容器启动

# redis

docker run -it --name redis-master redis /bin/bash
docker run -it --name redis-slave1 --link redis-master:master redis /bin/bash
docker run -it --name redis-slave2 --link redis-master:master redis /bin/bash

#  应用(注意:--privileged=true是为了保证存储共享后,容器中可以正常读取host机的文件,否则默认无法访问)
docker run -it --privileged=true --name APP1 --link redis-master:db -v ~/Projects/Django/App1:/usr/src/app django /bin/bash
docker run -it --privileged=true --name APP2 --link redis-master:db -v ~/Projects/Django/App2:/usr/src/app django /bin/bash

# 负载均衡
docker run -it --privileged=true --name HAProxy --link APP1:APP1 --link APP2:APP2 -p 6301:6301 -v ~/Projects/HAProxy:/tmp haproxy /bin/bash


三、Redis配置

1、redis-master配置并启动

1)修改redis-master配置文件

docker inspect redi-master

#找到如下样式的数据

"Mounts": [
            {
                "Name": "05d9136c164d80670528fcf054dfd6579daa7142c35d770693913f08e3b65356",
                "Source": "/var/lib/docker/volumes/05d9136c164d80670528fcf054dfd6579daa7142c35d770693913f08e3b65356/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
可知道host机和容器之间的共享目录对应关系,即可通过在host机上编辑文件。

通过本地安装redis,找到默认的redis配置文件,

cp REDIS_HOME/redis.conf /var/lib/docker/volumes/05d9136c164d80670528fcf054dfd6579daa7142c35d770693913f08e3b65356

cd /var/lib/docker/volumes/05d9136c164d80670528fcf054dfd6579daa7142c35d770693913f08e3b65356

vi redis.conf

---------------------

daemonnize yes

pidfile /var/run/redis.pid

---------------------


2)启动redis-master

docker attach redis-master

cp /data/redis.conf /usr/local/bin

/usr/local/bin/redis-server redis.conf


2、redis-slave1配置并启动

1)修改redis-slave配置文件

docker inspect redi-master

#找到如下样式的数据

-------------------------------------------------------

"Mounts": [
            {
                "Name": "0fe23ead8cc28a4339be007a3fea9629c948ffe46199a96f4cd31599adf13861",
                "Source": "/var/lib/docker/volumes/0fe23ead8cc28a4339be007a3fea9629c948ffe46199a96f4cd31599adf13861/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

-------------------------------------------------------

cp REDIS_HOME/redis.conf /var/lib/docker/volumes/0fe23ead8cc28a4339be007a3fea9629c948ffe46199a96f4cd31599adf13861

cd /var/lib/docker/volumes/0fe23ead8cc28a4339be007a3fea9629c948ffe46199a96f4cd31599adf13861

vi redis.conf

---------------------

daemonnize yes

pidfile /var/run/redis.pid

slaveof master 6379

---------------------


2)启动redis-slave1

docker attach redis-slave1

cp /data/redis.conf /usr/local/bin

/usr/local/bin/redis-server redis.conf


3、redis-slave2配置并启动(此步骤同上)


4、测试redis集群

docker attach redis-master

redis-cli

127.0.0.1:6379>set master 'test"

OK

127.0.0.1:6379>get master

"test"

Ctl+p+q

docker attach redis-slave1

redis-cli

127.0.0.1:6379>get master

"test"

可以取到“test”,说明redis集群搭建成功。


四、App配置(以下只以APP1为例,APP2步骤同样,只是名称和端口要对应修改)

1、环境准备

docker attach APP1

pip install redis

#验证

python

>>>import redis

>>>

没有报错,则说明redis安装成功。


2、创建工程

在容器内执行:

mkfir -p /usr/src/app/dockerweb

cd /usr/src/app/dockerweb

django-admin.py startproject redisweb

cd redisweb

python manage.py start app helloworld


3、代码编辑

退出容器,在host上执行文件编辑:

cd ~/Project/Django/App1/dockerweb/redisweb/helloworld/

vi view.py

-------------------------------------

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)

--------------------------------------

cd ../redisweb/

vi setting.py

在INSTALLED_APP里面的最后一行加入:

‘helloworld’,


另外,修改ALLOWED_HOSTS如下,否则外部访问不了:

ALLOWED_HOSTS = ['*']


vi urls.py

--------------------------------------

"""redisweb URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
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),
]

----------------------------------------


4、数据库同步

docker attach APP1

cd /usr/src/app/dockerweb/redisweb

python manage.py makeigrations

python manage.py migrate

如果是旧版本的django,上面那句命令可用

python manage.py syncdb

代替。


5、服务启动

python manage.py runserver 0.0.0.0:8001


五、HAProxy负载均衡配置

1、haproxy配置文件编写

cd ~/Project/HAProxy

vi haproxy.cfg

--------------------------------------

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:8001 check inter 2000 rise 2 fall 5
        server APP2 APP2:8002 check inter 2000 rise 2 fall 5

-----------------------------------


2、启动haproxy

docker attach HAProxy

cd /tmp

cp haproxy.cfg /usr/local/sbin/

/usr/local/sbin/haproxy -f haproxy.cfg



六、应用栈访问测试

本机或者其他机器通过浏览器访问:

假设在本机,浏览器中输入:

http://localhost:6301/helloworld


如果出现如下页面,则说明整个应用栈部署成功:

/usr/local/lib/python3.4/site-packages/redis/__init__.py

Set Hi

Get Hi:b'HelloWorld-APP1'

....

如果网页出现问题,则需要跟到对应的代码,修改代码调试。

恭喜你!用Docker部署了一个简单的应用栈。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值