zoukankan      html  css  js  c++  java
  • Docker容器与容器云之Docker单机集群部署案例

    准备工作:

    CentOS 7安装docker:

    #yum -y install docker

    1、获取节点所需镜像

    --主机执行

    #docker pull django

    #docker pull haproxy

    #docker pull redis

    # docker images

    REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE

    docker.io/haproxy       latest              d2deb2e11e09        6 days ago          135.5 MB

    docker.io/redis         latest              4e482b286430        13 days ago         98.95 MB

    docker.io/django        latest              eb40dcf64078        6 months ago        436 MB

    2、启动各个容器

    --主机执行

    # 启动redis容器

    sudo docker run -it --name redis-master redis /bin/bash

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

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

    # 启动django容器

    sudo docker run -it --name APP1 --link redis-master:db -v ~/Projects/Django/App1:/usr/src/app django /bin/bash

    sudo docker run -it --name APP2 --link redis-master:db -v ~/Projects/Django/App2:/usr/src/app django /bin/bash

    # 启动haproxy容器

    sudo docker run -it --name HAProxy --link APP1:APP1 --link APP2:APP2 -p 6301:6301 -v ~/Projects/HAProxy:/tmp haproxy /bin/bash

    # docker ps

    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES

    61ceae4c7c85        haproxy             "/docker-entrypoint.s"   8 hours ago         Up 8 hours          0.0.0.0:6301->6301/tcp   HAProxy

    7b11baf7b2fd        django              "/bin/bash"              8 hours ago         Up 8 hours                                   app2

    bdb9ddacefdb        django              "/bin/bash"              8 hours ago         Up 8 hours                                   app1

    2877a5829a6d        redis               "docker-entrypoint.sh"   8 hours ago         Up 8 hours          6379/tcp                 redis-slave2

    a39b414d570c        redis               "docker-entrypoint.sh"   8 hours ago         Up 8 hours          6379/tcp                 redis-slave1

    c7c22a1a4123        redis               "docker-entrypoint.sh"   8 hours ago         Up 8 hours          6379/tcp                 redis-master

    3、redis节点配置

    主机操作,切换到容器挂载目录下,进行master节点配置:

    # docker inspect --format "{{ .Config.Volumes }}" c7c22a1a4123

    map[/data:{}]

    # docker inspect c7c22a1a4123 | grep Source

                    "Source": "/var/lib/docker/volumes/4e823da40e15260dc5865060ce54d11b34f7527c37f1f55cbd4457f411ac7905/_data",

    #cd /var/lib/docker/volumes/4e823da40e15260dc5865060ce54d11b34f7527c37f1f55cbd4457f411ac7905/_data

    #cp <your-own-redis-dir>/redis.conf redis.conf

    #vim redis.conf

    修改以下内容:

    bind 0.0.0.0

    daemonize yes

    pidfile /var/run/redis.pid

    容器内操作,启动redis master节点服务:

    #cd /data

    #cp redis.conf /usr/local/bin

    #cd /usr/local/bin

    #redis-server redis.conf

    4、配置redis slave节点

    主机操作配置redis配置文件:

    # docker inspect --format "{{ .Config.Volumes }}" a39b414d570c

    map[/data:{}]

    # docker inspect a39 | grep Source

                    "Source": "/var/lib/docker/volumes/3c4ae73e1457456daf67ee0c03c33a41f855edc9c1c5d7226ef8119e3804561b/_data",

    #cd /var/lib/docker/volumes/3c4ae73e1457456daf67ee0c03c33a41f855edc9c1c5d7226ef8119e3804561b/_data

    #cp <your-own-redis-dir>/redis.conf redis.conf

    #vim redis.conf

    修改以下内容:

    bind 0.0.0.0

    daemonize yes

    pidfile /var/run/redis.pid

    slaveof master 6379

    切换到redis slave1节点容器下执行:

    #cd /data

    #cp redis.conf /usr/local/bin

    #cd /usr/local/bin

    #redis-server redis.conf

    同理启动redis另一个从节点。

    至此redis配置完成,进行redis功能测试:

    以redis-cli方式登录redis master节点:

    # redis-cli

    127.0.0.1:6379> set master c7c2

    OK

    127.0.0.1:6379> get master

    "c7c2"

    127.0.0.1:6379> quit

    以redis-cli方式登录redis slave节点:

    # redis-cli

    127.0.0.1:6379> get master

    "c7c2"

    127.0.0.1:6379> quit

    Slave节点能正常接收主节点设置,说明redis配置成功。

    5、app容器节点配置

    App容器内安装python语言redis支持包:

    #pip install redis

    测试:

    # 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

    >>> quit()

    6、创建app:

    容器内:

    #cd /usr/src/app

    #mkdir dockerapp    ##注意,这里需要关闭主机的selinux功能,关闭方式:setenforce 0

    #cd dockerapp

    # django-admin.py startproject redisweb

    # ls

    redisweb

    # cd redisweb/

    # ls

    manage.py  redisweb

    # python manage.py startapp helloworld

    # ls

    helloworld  manage.py  redisweb

    主机内:

    # cd ~/Projects/Django/App1/

    # ls

    dockerweb

    # cd dockerweb/redisweb/helloworld/

    # ls

    admin.py  apps.py  __init__.py  migrations  models.py  __pycache__  tests.py  views.py

    # vim 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文件:

    # ls

    __init__.py  __pycache__  settings.py  urls.py  wsgi.py

    # vim 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',                       --添加此处

    ]

    最后再修改urls.py文件:

    # vim urls.py

    修改后内容如下:

    from django.conf.urls import include,url

    from django.contrib import admin

    from helloworld.views import hello

    urlpatterns = [

        url(r'^admin/', include(admin.site.urls)),

        url(r'^helloworld$',hello),

    ]

    进入app容器,完成项目的生成:

    # python manage.py makemigrations

    No changes detected

    # 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

    # python manage.py createsuperuser

    Username (leave blank to use 'root'):

    Email address:

    Password:

    Password (again):

    This password is too short. It must contain at least 8 characters.

    Password:

    Password (again):

    Superuser created successfully.

    至此,app节点配置完成,按照相同步骤配置app节点2.

    容器内启动两个app节点:

    # python manage.py runserver 0.0.0.0:8001

    # python manage.py runserver 0.0.0.0:8002

    7、haproxy节点配置

    主机操作:

    # cd ~/Projects/HAProxy/

    #vim 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

    进入容器,启动haproxy负载均衡服务:

    #cd /usr/local/sbin

    # cp /tmp/haproxy.cfg haproxy.cfg

    # haproxy -f haproxy.cfg

    至此,代理服务配置完成。

    应用测试:

    客户机登录:http://192.168.100.235:6301/helloworld

    wps2E24.tmp

    客户机也可以登录后台管理界面:http://192.168.100.235:6301/haproxy-stats

    wps2EE0.tmp

  • 相关阅读:
    docker 入门5
    docker 入门4
    Machine概念和获取帮助 【翻译】
    docker 入门3
    docker 入门2
    docker 入门1
    在生产环境中使用Compose 【翻译】
    docker常用命令整理
    ASP.NET WebAPI 06 HttpMessageHandler管道
    ASP.NET WebAPI 05 参数绑定
  • 原文地址:https://www.cnblogs.com/wcwen1990/p/7128651.html
Copyright © 2011-2022 走看看