In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to build a simple application stack and container Hello World access for Docker. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
System environment
Operating system version
$cat / etc/issueDebian GNU/Linux 8\ n\ l
Kernel version
$uname-r3.16.0-4-amd64
Docker version
Docker versionClient: Version: 1.12.1 API version: 1.24 Go version: go1.6.3 Git commit: 23cf638 Built: Thu Aug 18 05:02:53 2016 OS/Arch: linux/amd64Server: Version: 1.12.1 API version: 1.24 Go version: go1.6.3 Git commit: 23cf638 Built: Thu Aug 18 05:02:53 2016 OS/Arch: linux/amd64
Application stack construction
We will build a Docker application stack with six nodes, including a proxy node, two Web application nodes, a master database node and two slave database nodes. The specific structure of the application stack is shown in the figure:
Get the image required by the application stack node
According to the application stack structure, you need to obtain images of HAProxy, Redis and Django from Docker Hub:
# docker pull ubuntu:14.04# docker pull haproxy# docker pull redis# docker pull django# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEhaproxy latest 65599e2ea3f2 2 weeks ago 139.1 MBredis latest 0d1cbfaa41da 2 weeks ago 185 MBubuntu 14.04 4a725d3b3b1c 3 weeks ago 188 MBdjango latest 79d802ec2b6c 4 weeks ago 437.4 MB
Application stack container node interconnection
Docker run-link redis:redis-name console ubuntu bash
Here you will launch a container on the Ubuntu image and name it console, while connecting the newly launched console container to the container named redis. The-- link option of the docker run command is used to add connections to a container. The-- name option is also used here to specify a name for the container.
For more information on the usage of Docker link, please refer to this site's "how Docker uses link to establish connections between containers"
Application stack node starts
Organize the connection process of the application stack node before starting the application stack node:
Start the redis-master container node
Two redis-slave container nodes connect to redis-master at startup
Two APP container nodes connect to redis-master at startup
The HAProxy container node connects to two APP nodes when it starts.
In addition, in order to be able to access the application stack from the external network and access the APP in the application stack through the HAproxy node, the port is exposed to the host using the-p parameter when starting the HAProxy node.
To sum up, the container startup sequence is:
Redis-master-"redis slave -" APP-"HAProxy
Start the Redis container
# 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
Start the Django container
# docker run-it-- name APP1-- link redis-master:db-v / Projects/Django/APP1:/usr/src/app django / bin/bash# docker run-it-- name APP2-- link redis-master:db-v / Projects/Django/APP2:/usr/src/app django / bin/bash
Start the HAproxy container
# docker run-it-- name HAProxy-- link APP1:APP1-- link APP2:APP2-p 6301 link APP1:APP1 6301-v ~ / Projects/HAProxy:tmp haproxy / bin/bash
Description: each container needs to be assigned a terminal when it is started.
View the container startup information:
# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESab25650701f0 haproxy "/ docker-entrypoint.s" 3 hours ago Up 3 hours 0.0.0.0 hours ago Up 6301-> 6301/tcp HAProxyace790044e06 django "/ bin/bash" 3 hours ago Up 3 hours APP264963af16131 django "/ bin/bash" 3 hours ago Up 3 hours APP1aa77330aee2a redis "docker-entrypoint.sh" 3 hours ago Up 3 hours 6379/tcp redis-slave21fd72289d4f2 redis "docker-entrypoint.sh" 3 hours ago Up 3 hours 6379/tcp redis-slave1518b41200dab redis "docker-entrypoint.sh" 3 hours ago Up 3 hours 6379/tcp redis-master
Application stack container node configuration
Redis Master master database container node configuration
We know that data can be shared between hosts and containers through volume, so startup configuration files for Redis can be created and edited on the host machine. Use the docker inpect command to view the volume mount:
# docker inspect-f'{{.Mounts}} 'redis-master [{5920a23b5e230a449230bbd4807912793bbc3bab0a05ae085ff95423301f0d6c/ var/lib/docker/volumes/5920a23b5e230a449230bbd4807912793bbc3bab0a05ae085ff95423301f0d6c/_data / data local true}]
As you can see, the volume of redis-master is directory / var/lib/docker/volumes/5920a23b5e230a449230bbd4807912793bbc3bab0a05ae085ff95423301f0d6c/_data on the host machine and / data in the container.
Execute the following command to create the startup configuration file redis.conf for Redis:
# cd / var/lib/docker/volumes/5920a23b5e230a449230bbd4807912793bbc3bab0a05ae085ff95423301f0d6c/_data# cp / ~ / redis.conf. # vim redis.conf
For the redis master database, modify several parameters in the following template file:
Daemonize yespidfile / var/run/redis.pid
Download redis.conf template: https://github.com/yhsong-linux/docker-redis/blob/master/redis.conf
After creating the startup configuration file on the host, change to the / data directory in the container, copy the redis.conf to the execution working directory, and then start the redis service:
# cd / data# cp redis.conf / usr/local/bin# cd / usr/local/bin# redis-server redis.conf
Redis Slave is configured from the database container node
Similar to the Redis Master container node, after starting the Redis Slave container node, you need to view the vloume information and create a startup configuration file.
For the Redis slave database, you need to modify the following parameters:
Daemonize yespidfile / var/run/redis.pidslaveof master 6379
After creating the startup configuration file on the host, change to the / data directory in the container, copy the redis.conf to the execution working directory, and then start the redis service:
# cd / data# cp redis.conf / usr/local/bin# cd / usr/local/bin# redis-server redis.conf
Redis database container node test
After the configuration and service startup of the Redis Master and Redis Slave container nodes, you can test the database by starting the client program for Redis.
First, in the Redis Master container, start the client procedure of Redis and store a piece of data:
# redis-cli127.0.0.1:6379 > set master 518bOK127.0.0.1:6379 > get master "518b"
Then, in the two Redis Slave containers, start the client procedure of Redis to query the data previously stored in the Master database:
# redis-cli127.0.0.1:6379 > get master "518b"
According to the response, the data in the Master database has been synchronized to the Slave database. At this point, the database part of the application stack is built.
Configuration of APP container node (Django)
After the Django container starts, you need to use the Django framework to develop a simple Web program. To access the database, you need to install the Redis support package for Python in the container:
# pip install redis
"after the installation is complete, verify that the support pack is installed successfully:"
# pythonPython 3.4.5 (default, Aug 22 2016, 20:55:07) [GCC 4.9.2] on linuxType "help", "copyright", "credits" or "license" for more information. > > import redis > > print (redis.__file__) / usr/local/lib/python3.4/site-packages/redis/__init__.py
As shown in the output above, it means that you can now call the Redis database using the Python language. Next, create the Web program. Take APP1 as an example, enter the volume directory of the host to edit the new APP.
Under the container's volume directory / usr/src/app, start creating APP:
# cd / usr/src/app# mkdir dockerweb# cd dockerweb# django-admin.py startproject redisweb# lsredisweb# cd redisweb# lsmanage.py redisweb# python manager.py startapp helloworld# lshelloworld manage.py redisweb
After creating an APP in the container, change to the volume directory ~ / Projects/Django/App1 of the host:
# cd ~ / Projects/Django/App1# lsdockerweb
As you can see, the APP file created in the container is also visible in the host's volume directory. Then modify the view file views.py of the helloword application:
# cd dockerweb/redisweb/helloworld# lsadmin.py _ _ init__.py migrations models.py tests.py views.py# vim views.py
The modified views.py file is as follows:
From django.shortcuts import renderfrom django.http import HttpResponse# Create your views here.import redisdef hello (requset): 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)
Note that when connecting to the Redis database, use the-link parameter to create a db connection instead of a specific IP address; similarly, for APP2, use the desired db connection.
Next, modify the configuration file setiing.py of the redisweb project and add the new helloworld application:
# cd.. / redisweb# ls__init__.py _ _ pycache__ settings.py urls.py wsgi.py
Add helloworld under the INSTALLED_APPS option in the setting.py file:
# Application definitionINSTALLED_APPS = ['django.contrib.admin',' django.contrib.auth', 'django.contrib.contenttypes',' django.contrib.sessions', 'django.contrib.messages',' django.contrib.staticfiles', 'helloworld',]
Finally, modify the URL template file urls.py of the redisweb project, which sets the URL mode for accessing the application and the mapping table between the view functions called for the URL mode:
# vim urls.py
In the url.py file, introduce the hello view of the helloworld application and add a urlpatterns variable to the hello view. The modified urls.py file is as follows:
From django.conf.urls import * from django.contrib import adminadmin.autodiscover () from helloworld.views import hellourlpatterns = [url (r'^ admin/', include (admin.site.urls)), url (r'^ helloworld$', hello),]
After the above modifications are completed, enter the container again and generate the project under the directory / usr/src/app/dockerweb/redisweb:
# python manage.py makemigrationsNo changes detected# python manage.py migrateOperations to perform: Apply all migrations: admin, auth, contenttypes, sessionsRunning migrations: Rendering model states... DONE 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
At this point, the configuration of all APP1 containers is complete, and the configuration of APP2 containers is the same process. After configuring the containers for APP1 and APP2, you complete the configuration of the APP portion of the application stack.
When starting the Web server of the APP container, you can specify the port and IP address of the server. In order to access all the public IP addresses of the public network through the HAproxy container node and achieve load balancing, you need to specify the IP address and port of the server. Use port 8001 for APP1 and port 8002 for APP2, and both use the 0.0.0.0 address. Take APP1 as an example, the process of starting the server is as follows:
# python manage.py runserver 0.0.0.0 September 800mm python manage.py runserver 0.0.0.0:8001Performing system checks...System check identified no issues (0 silenced). September 20, 2016-23:16:44Django version 1.10, using settings' redisweb.settings'Starting development server at http://0.0.0.0:8001/Quit the server with CONTROL-C.
HAproxy container node configuration
All access to the application stack is achieved through the HAproxy load balancing proxy container node.
First, copy the startup configuration of HAProxy to the container, under the volumes directory ~ / Projects/HAProxy/ of the host:
# cd ~ / Projects/HAProxy/# vim haproxy.cfg
The modified haproxy.cfg file is as follows:
Global log 127.0.0.1 local0 maxconn 4096 chroot / usr/local/sbin daemon nbproc 4 pidfile / usr/local/sbin/haproxy.piddefaults 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 50000mslisten redis_proxy bind 0.0.0.0:6301 stats enable stats uri / haproxy-stats stats auth phil:NRG93012 server APP1 APP1:8001 check inter 2000 rise 2 fall 5 server APP2 APP2:8002 check inter 2000 rise 2 fall 5
Then, go to the container's volume directory / tmp and copy the startup configuration file of Haproxy to the working directory of HAproxy:
# cd / tmp# cp haproxy.cfg / usr/local/sbin# cd / usr/local/sbin# lshaproxy haproxy-systemd-wrapper haproxy.cfg
Then, start the HAProxy agent with the configuration file:
# haproxy-f haproxy.cfg
Application stack access test
When you access http://172.17.0.7:6301/helloworld in the browser, you can see the page of APP1 or APP2 (the local host accesses the application stack):
Description: 172.17.0.7 is the address of the HAProxy container.
After the local test passes, try to access the application stack APP, namely http://192.168.1.104:6301/helloworld, on other hosts through the IP address of the application stack entry address and port 6301, as shown below (other hosts in the public network access the application stack):
Description: 192.168.1.104 is the IP address of the host.
This is the end of the article on "how to build a simple application stack and container Hello World access for Docker". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.