阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

如何构建Memcached Docker容器

158次阅读
没有评论

共计 4591 个字符,预计需要花费 12 分钟才能阅读完成。

理论 + 实战,精彩详述“如何构建 Memcached Docker 容器”!

如何把 Memcached 运行到 Docker 容器中?

如何构建 Memcached Docker 容器

Docker

Docker 为容器(应用程序)提供运行环境。使用 Docker 镜像创建容器,既可以通过人工执行命令,也可以通过 cSphere 平台可视化操作。

Memcached 简介

Memcached 是一个分布式,开源的数据存储引擎。它被设计用来在 RAM(替换了低速的传统硬盘)中存储特定种类的数据,供应用程序进行快速检索。减少了处理申请所花费的时间,通过减少查询的次数来抵消沉重缓慢的数据集或者 API,比如传统的数据库(MySQL 等)。

通过引进一个灵巧的,精心设计并经过最优化的缓存机制,它变得可以处理更大的请求量,执行更多的程序。这是 Memcached 最重要的应用实例,因为它也是这样缓存其他应用或内容的。

可以深度依赖,并被用在网站或者其他应用的生产中,Memcached 已经成为一个即时提升性能的工具,而不必使用更好的硬件条件(比如更多的服务器或者服务资源)。

Memcached 的工作方式是将关键词和他们对应的值(最大能达到 1MB)保存在一个关联矩阵中(比如哈希表),延展和分布在大量的虚拟服务器中。

开始创建 Memcached 镜像

基于我们之前学习的 Docker 系列文章里面的知识,我们直接深入到创建 Dockerfile 来实现自动构建安装 Mamcached 功能的镜像(将可以用来运行沙盒化的 Memcached 实例)。

快速回顾:什么是 Dockerfile?
Dockerfile 是包含可执行的声明的命令的脚本,将以给定的顺序执行,来让 Docker 自动的创建一个新的 Docker 镜像。这给部署工作带来了极大的帮助。

这些文件(Dockerfile)使用 FROM 命令,总是以对基础镜像的描述开头。从那开始,构建进程开始运行,向主机提交(保存镜像的状态)的每一步的操作形成了最终的镜像。

用法:

 # Build an image using the Dockerfile at current location
 # Tag the final image with [name] (e.g. *nginx*)
 # Example: sudo docker build -t [name] .
 sudo docker build -t memcached_img .

创建 Memcached 镜像的 Dockerfile

通过熟悉文本编辑器,创建一个新的 Dockerfile:

首先让我们定义一下 Dockerfile 的目标,并声明需要使用的基础镜像。

 ############################################################
 # Dockerfile to run Memcached Containers
 # Based on Ubuntu Image
 ############################################################

 # Set the base image to use to Ubuntu
 FROM ubuntu

 # Set the file maintainer (your name – the file’s author)
 MAINTAINER cSphere

然后我们就可以开始安装 Memcached

 
 # Install Memcached
 RUN apt-get install -y memcached

设置默认对外开放的容器端口:

 # Port to expose (default: 11211)
 EXPOSE 11211

设置默认的执行命令和入口(例如 Memcached 进程):

 # Set the user to run Memcached daemon
 USER daemon
 
 # Set the entrypoint to memcached binary
 ENTRYPOINT memcached

 # Default Memcached run command arguments
 CMD [“-u”, “root”, “-m”, “128”]

### 最终的 Dockfile

 ############################################################
 # Dockerfile to run Memcached Containers
 # Based on Ubuntu Image
 ############################################################
 
 # Set the base image to use to Ubuntu
 FROM ubuntu
 
 # Set the file maintainer (your name – the file’s author)
 MAINTAINER Maintaner Name
 
 # Install Memcached
 RUN apt-get install -y memcached
 
 # Port to expose (default: 11211)
 EXPOSE 11211
 
 # Set the user to run Memcached daemon
 USER daemon
 
 # Set the entrypoint to memcached binary
 ENTRYPOINT memcached

    # Default Memcached run command arguments
 CMD [“-m”, “128”]

    Dockerfile 准备完毕!

创建 Memcached 容器

构建 memcached 镜像:“csphere-memcached”

 sudo docker build -t  csphere-memcached.

**Note**:不要遗漏了最后的“.”,Docker 需要它来找到 Dockerfile。

启动 memcached 容器

使用下面的命令来创建一个新容器,可以根据你的需求修改这个例子。

 # sudo docker run -name csphere-memcached -d -p 45001:11211 csphere-memcached

“csphere-memcached”容器,已启动,可使用 45001 端口连接使用。

限制 Memcached 容器的内存

如果想要限制一个 Docker 容器进程可以使用的内存量,只要设置 `-m [memory amount]` 并标上限制就 ok。

运行一个内存限制为 256MB 的容器:

  ` # sudo docker run -name csphere-memcached -m 256m -d -p 45001:11211 csphere-memcached`

检查此容器内存限制是否设置成功,执行以下命令:

 `# Example: docker inspect [container ID] | grep Memory
sudo docker inspect csphere-memcached | grep Memory`

测试 Memcached 容器

我们使用一个简单的 Python CLI 程序来测试。

确保你的主机拥有为 Python/Memcached 准备的必要库文件:

  ` sudo apt-get update && sudo apt-get -y upgrade
 sudo apt-get install -y python-pip
 pip install python-memcached`

创建一个简单的 Python 脚本,名为 cache.py

把下面的内容复制粘贴进去:

 ` # Import python-memcache and sys for arguments
 import memcache
 import sys
 
 # Set address to access the Memcached instance
 addr = ‘localhost’
 
 # Get number of arguments
 # Expected format: python cache.py [memcached port] [key] [value]
 len_argv = len(sys.argv)
 
 # At least the port number and a key must be supplied
 if len_argv < 3:
    sys.exit(“Not enough arguments.”)
 
 # Port is supplied and a key is supplied – let’s connect!
 port  = sys.argv[1]
 cache = memcache.Client([“{0}:{1}”.format(addr, port)])
 
 # Get the key
 key  = str(sys.argv[2])
 
 # If a value is also supplied, set the key-value pair
 if len_argv == 4:

    value = str(sys.argv[3])
    cache.set(key, value)
 
    print “Value for {0} set!”.format(key)
 
 # If a value is not supplied, return the value for the key
 else:
 
    value = cache.get(key)
 
    print “Value for {0} is {1}.”.format(key, value)`
 

      测试 Docker 的 Memcached 实例:
   
  # Example: python cache.py [port] [key] [value]
 python cache.py 45001 my_test_key test_value
 
 # Return: Value for my_test_key set
 
 # See if the key is set:
 python cache.py 45001 my_test_key
 
 # Return: Value for my_test_key is test_value.

更多 Docker 相关教程见以下内容

Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm 

Ubuntu 14.04 安装 Docker  http://www.linuxidc.com/linux/2014-08/105656.htm 

Ubuntu 使用 VNC 运行基于 Docker 的桌面系统  http://www.linuxidc.com/Linux/2015-08/121170.htm

阿里云 CentOS 6.5 模板上安装 Docker http://www.linuxidc.com/Linux/2014-11/109107.htm 

Ubuntu 15.04 下安装 Docker  http://www.linuxidc.com/Linux/2015-07/120444.htm 

在 Ubuntu Trusty 14.04 (LTS) (64-bit)安装 Docker http://www.linuxidc.com/Linux/2014-10/108184.htm 

在 Ubuntu 15.04 上如何安装 Docker 及基本用法 http://www.linuxidc.com/Linux/2015-09/122885.htm

Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-12/125818.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计4591字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中