安裝虛擬機
准備一台Centos7的VM,名為Centos7-1
具體過程可以參考:
安裝Docker
下載rpm包:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/
執行命令:
yum install docker-ce-17.09.0.ce-1.el7.centos.x86_64.rpm
在安裝過程中會安裝“container-selinux-2.281.git8ce147.el7.noarch”
具體過程參考:
啟動Docker:
systemctl start docker
驗證是否安裝正確:
docker run hello-world
定義一個容器並部署一個應用
創建 Dockerfile 文件用於定義容器:
# Use an official Python runtime as a parent image FROM python:2.7-slim # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app ADD . /app # Install any needed packages specified in requirements.txt RUN pip install -r requirements.txt # Make port 80 available to the world outside this container EXPOSE 80 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"]
創建 requirements.txt文件羅列依賴:
Flask
Redis
創建應用 app.py :
from flask import Flask from redis import Redis, RedisError import os import socket # Connect to Redis redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2) app = Flask(__name__) @app.route("/") def hello(): try: visits = redis.incr("counter") except RedisError: visits = "<i>cannot connect to Redis, counter disabled</i>" html = "<h3>Hello {name}!</h3>" \ "<b>Hostname:</b> {hostname}<br/>" \ "<b>Visits:</b> {visits}" return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits) if __name__ == "__main__": app.run(host='0.0.0.0', port=80)
將這三個文件隨意存儲在一個目錄中,此處我存儲在/home/luwei/dockerfile目錄下,然后在該目錄下執行命令:
docker build -t friendlyhello .
注意后面的點不要漏掉,表示當前目錄,執行完成之后使用如下命令可以查看創建的app:
docker images
結果:
REPOSITORY TAG IMAGE ID CREATED SIZE friendlyhello latest a0a4a49f2713 9 minutes ago 150MB
執行一下命令可以運行該應用:
docker run -p 4000:80 friendlyhello
其中4000表示將容器中的80端口映射到主機上的4000端口,因此可以在外部使用如下地址訪問:
http://192.168.0.100:4000
返回結果為:
此外,還可以以后台的形式運行:
docker run -d -p 4000:80 friendlyhello
可以使用如下命令查看:
docker container ls
結果如下:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0316cf74320f friendlyhello "python app.py" 4 minutes ago Up 4 minutes 0.0.0.0:4000->80/tcp sad_swartz
使用一下命令可以終止:
docker container stop 0316cf74320f
其中末尾的一串字符就是上面的CONTAINER ID
將應用上傳到遠程倉庫
登錄dockerID:
docker login
輸入用戶名密碼即可(在Docker官網上注冊)
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: weilu2 Password: Login Succeeded
標記鏡像:
語法: docker tag image username/repository:tag 例子: docker tag friendlyhello weilu2/myrep:demo1
使用docker images查看:
REPOSITORY TAG IMAGE ID CREATED SIZE friendlyhello latest a0a4a49f2713 28 minutes ago 150MB weilu2/myrep demo1 a0a4a49f2713 28 minutes ago 150MB
上傳鏡像到倉庫:
語法: docker push username/repository:tag 例子: docker push weilu2/myrep:demo1
從遠程倉庫中拉取並運行:
語法: docker run -p 4000:80 username/repository:tag 例子: docker run -p 4000:80 weilu2/myrep:demo1
安裝docker-compose
從官網上下載該文件,並將其放置在/usr/local/bin下:
sudo curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
修改該文件的權限,增加執行權限:
chmod +x /usr/local/bin/docker-compose
部署單節點swarm和service
編寫 docker-compose.yml文件:
version: "3" services: web: # replace username/repo:tag with your name and image details image: weilu2/myrep:demo1 deploy: replicas: 5 resources: limits: cpus: "0.1" memory: 50M restart_policy: condition: on-failure ports: - "80:80" networks: - webnet networks: webnet:
這個文件可以放在任意位置。
初始化swarm:
docker swarm init
部署應用:
docker stack deploy -c docker-compose.yml getstartedlab
這樣,我們的服務就運行在五個容器實例中,可以使用如下命令查看:
docker service ls
結果:
ID NAME MODE REPLICAS IMAGE PORTS hh1j4kg92pd2 getstartedlab_web replicated 5/5 weilu2/myrep:demo1 *:80->80/tcp
一個容器中運行的一個服務稱為一個任務,查看這些任務可以使用命令:
docker service ps getstartedlab_web
結果:
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS mrd5ptud2ojy getstartedlab_web.1 weilu2/myrep:demo1 localhost.localdomain Running Running 5 minutes ago 17mr5bja58k1 getstartedlab_web.2 weilu2/myrep:demo1 localhost.localdomain Running Running 5 minutes ago 8oitjksmqtsc getstartedlab_web.3 weilu2/myrep:demo1 localhost.localdomain Running Running 5 minutes ago xylsrsu2yjid getstartedlab_web.4 weilu2/myrep:demo1 localhost.localdomain Running Running 5 minutes ago nx8r17ow9hb8 getstartedlab_web.5 weilu2/myrep:demo1 localhost.localdomain Running Running 5 minutes ago
擴展應用規模
可以通過修改docker-compose.yml文件中的replicas值來修改應用的規模,然后使用如下命令進行部署:
docker stack deploy -c docker-compose.yml getstartedlab
停止應用:
docker stack rm getstartedlab
離開swarm:
docker swarm leave --force
部署swarm集群
初始化swarm(注意,如果是承接上面的操作,那么在初始化之前要用docker swarm leave --force命令使當前節點離開現有的一個swarm)
docker swarm init --advertise-addr 192.168.0.100
通過這個命令初始化時,會得到兩個命令提示:
Swarm initialized: current node (joly7cvvnunrhoh961bqg2tur) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-0xrs078ymj1evlv57ue86xo06idn9c3qfaomyjo89p1r59zquk-d4ln63p34msselmzasv1qsv0g 192.168.0.100:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
表示當前節點是一個manager,如果要在這個swarm中增加worker,可以通過在對應節點上執行 docker swarm join的命令來實現。
現在我另外部署了一台CentOS7的虛擬機,稱為CentOS7-2,IP為192.168.0.101,這台虛擬機的環境配置與CentOS7-1完全一致,然后在其中執行命令:
docker swarm join --token SWMTKN-1-0xrs078ymj1evlv57ue86xo06idn9c3qfaomyjo89p1r59zquk-d4ln63p34msselmzasv1qsv0g 192.168.0.100:2377
就可以將CentOS7-2作為worker加入到這個swarm中。在首次執行該命令時,可能會碰到一個錯誤,請參考附錄一。
添加成功之后會有相應的這個worker節點成功添加到swarm中的提示信息。
在manager節點上運行命令:
docker node ls
可以查看節點信息:
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS joly7cvvnunrhoh961bqg2tur * localhost.localdomain Ready Active Leader yjkfjamz95ic5azroxrox2i58 localhost.localdomain Ready Active
在swarm集群上部署應用
還使用與之前在單節點上部署應用相同的方法在manager節點上部署,進入docker-compose.yml文件所在目錄,執行命令:
docker stack deploy -c docker-compose.yml getstartedlab
可以看到輸出結果:
Creating network getstartedlab_webnet
Creating service getstartedlab_web
使用如下命令查看部署的服務:
docker stack ps getstartedlab
根據結果中的Node一列,可以很直觀的發現應用被部署在集群上了:
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS nui1gvfjd8i8 getstartedlab_web.1 weilu2/myrep:demo1 centos7-2 Running Running 5 minutes ago i75oapmobxp3 getstartedlab_web.2 weilu2/myrep:demo1 centos7-1 Running Running 5 minutes ago jit8geun7h15 getstartedlab_web.3 weilu2/myrep:demo1 centos7-2 Running Running 5 minutes ago zga7xgf6tjup getstartedlab_web.4 weilu2/myrep:demo1 centos7-1 Running Running 5 minutes ago lidvf7jsucif getstartedlab_web.5 weilu2/myrep:demo1 centos7-2 Running Running 5 minutes ago vpqkyxv4ebn9 getstartedlab_web.6 weilu2/myrep:demo1 centos7-1 Running Running 5 minutes ago 3jlyg6edjz5s getstartedlab_web.7 weilu2/myrep:demo1 centos7-2 Running Running 5 minutes ago fh2p24jrqhvv getstartedlab_web.8 weilu2/myrep:demo1 centos7-1 Running Running 5 minutes ago
附錄一:worker加入swarm出現rpc錯誤
錯誤信息:
Error response from daemon: rpc error: code = Unavailable desc = grpc: the connection is unavailable
這個是由於HOST的端口沒有打開的問題,導致被防火牆攔截了,使用如下命令打開相關端口即可:
firewall-cmd --add-port=2376/tcp --permanent firewall-cmd --add-port=2377/tcp --permanent firewall-cmd --add-port=7946/tcp --permanent firewall-cmd --add-port=7946/udp --permanent firewall-cmd --add-port=4789/udp --permanent
添加端口之后要重啟防火牆才生效:
systemctl restart firewalld.service
可以使用命令查看端口開放情況:
firewall-cmd --list-ports
結果如下:
2376/tcp 2377/tcp 7946/tcp 7946/udp 4789/udp
參考內容:
Open protocols and ports between the hosts