1. 程式人生 > >ubuntu虛擬機器搭建docker內的redis叢集

ubuntu虛擬機器搭建docker內的redis叢集

安裝Ubuntu虛擬機器

全程預設安裝不用操心別的事情,安裝完成之後做下面幾件事情:

安裝ssh-server客戶端(如果你想遠端連線它的話)

$ sudo apt-get update
$ sudo apt-get install openssh-server

設定root使用者密碼

$ passwd root

安裝docker(gitbook步驟)

安裝可選核心模組(ubuntu14以上需要)

$ sudo apt-get update

$ sudo apt-get install \
    linux-image-extra-$(uname -r
) \ linux-image-extra-virtual

使用apt安裝

新增https傳輸軟體包和CA證書

$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

新增軟體源的GPG金鑰

$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add
-

向 source.list 中新增 Docker 軟體源

$ sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"


# 官方源--慢
# $ sudo add-apt-repository \
#    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
#    $(lsb_release -cs) \
#    stable"

安裝docker ce

$ sudo apt-get update

$ sudo apt-get install docker-ce

啟動docke ce

$ sudo service docker start

測試是否正確安裝

$ docker run hello-world

正確輸出:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

簡單解釋

  • 需要使用root使用者或者擁有root許可權的使用者,原因:docker 命令會使用 Unix socket 與 Docker 引擎通訊
  • hello-world是一個用來測試的映象,測試完成可以刪除

建立redis叢集

流程

  • 分別建立使用7001到7006作為埠的redis-docker映象,並且啟動他們
  • 在容器外安裝redis 並且安裝 ruby 啟動叢集

建立映象

在任意一個目錄建立docker 映象檔案: Dockerfile,編輯內容如下:

FROM redis:3.2
MAINTAINER xiaowei

ENV REDIS_HOME /usr/local

RUN mkdir $REDIS_HOME/conf
WORKDIR $REDIS_HOME/conf
WORKDIR $REDIS_HOME/conf

RUN echo "cluster-enabled yes" >> redis.conf \
    && echo "loglevel notice" >> redis.conf \
        && echo "timeout 1000" >> redis.conf \
        && echo "cluster-config-file nodes.conf" >> redis.conf \
        && echo "port 7001" >> redis.conf \
        && echo "#bind 127.0.0.1" >> redis.conf 

EXPOSE 7001

CMD ["redis-server","/usr/local/conf/redis.conf"]

註釋:
前三行分別是:
redis 版本
主要管理者:修改成自己的名字
redis_home在docker中的位置

剩下的是是建立redis.conf檔案並且修改對應的埠和叢集模式。這裡沒有寫密碼,需要的自己加。

然後根據這個檔案建立映象:

$ docker build -t xiaowei:cluter7001 .

注意:在image名字的後面有一個空格和英文句號,必須有,否則報錯
如果建立成功,你可以用命令檢視這個映象:

$ docker image ls -a

然後啟動這個映象:

$ docker run -d --name redis01 -p 7001:7001 --net=host xiaowei:cluster7001

注意: –name 後面是container的名字,最後是image的名字
同樣,建立成功的話可以檢視這個容器:

$ docker container ls -a

如果建立失敗,那麼可以檢視日誌,使用命令:

$ docker logs redis01

如果沒有問題,那麼可以繼續建立剩下的5個docker,總結步驟:
- 修改Dockerfile檔案的兩項:”port 7001”和”EXPOSE 7001”,修改為需要使用的埠
- build一個image,然後啟動它
- 重複5次

安裝redis-trib.rb

下載redis-3.2.11.tar.gz(其他版本也可以),然後解壓到當前資料夾

$ wget http://download.redis.io/releases/redis-3.2.11.tar.gz
$ tar zxvf redis-3.2.11.tar.gz

然後安裝ruby

$ sudo apt-get update
$ sudo apt-get install ruby

然後使用gem安裝redis

$ gem  install  redis

建立叢集

到這裡基本大功告成,還差最後一點。進入 redis-3.2.11/src 目錄,然後執行:

$ ./redis-trib.rb  create --replicas 1 192.168.240.137:7001 192.168.240.137:7002 192.168.240.137:7003 192.168.240.137:7004 192.168.240.137:7005 192.168.240.137:7006 

叢集搭建完成,可以隨便玩了。


轉載請保持署名和註明原地址