如何構建Memcached Docker容器

jopen 9年前發布 | 10K 次閱讀 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的更多知識,請觀看免費培訓視頻(http://csphere.cn/training)。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!