Centos 搭建 Docker Swarm

容器 刘宇帅 6年前 阅读量: 1602

Centos docker-ce安装

一般安装步骤

安装依赖包

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

修改软件源

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 切回官方源
# $ sudo yum-config-manager \
#     --add-repo \
#     https://download.docker.com/linux/centos/docker-ce.repo

安装docker-ce

$ sudo yum makecache fast
$ sudo yum -y install docker-ce

快熟安装步骤

在测试或开发环境中 Docker 官方为了简化安装流程,提供了一套便捷的安装脚本,CentOS 系统上可以使用这套脚本安装

$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun

执行这个命令后,脚本就会自动的将一切准备工作做好,并且把 Docker CE 的 Edge 版本安装在系统中。

启动和配置

遵循一般安全考虑,这里不使用root直接操作docker服务,操作docker一般使用dokcer的Unix socket。添加用户操作权限

usermod -a -G docker Username

启动:

$ sudo systemctl enable docker
$ sudo systemctl start docker

测试docker是否成功

docker提供了一个hello world镜像,我们可以用它来测试,有以下输出则表示已经安装成功。

$docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
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://hub.docker.com/

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

docker-compose安装

直接下载github上编译好的linux二进制包

$ sudo curl -L https://github.com/docker/compose/releases/download/1.17.1/docker-compose-`uname -s`-`uname -m` > /usr/bin/docker-compose
$ chmod +x /usr/bin/docker-compose

检查

$ docker-compose --version
docker-compose version 1.17.1, build 6d101fb

docker swarm集群搭建

首先在机器01创建一个docker主机作为管理节点

$ docker swarm init
Swarm initialized: current node (uwqcanks3pzaruzhi6cv9mfft) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-5vatlkm1qpx61ys6kffpfs9qddyo2wkrr7rojf630m6zxmyner-9kfghlmvma7snynqy8zvyd2nb 111.109.23.28:1377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

然后可以在其他机器上执行如下命令,这样该机器就加入到集群里了

docker swarm join --token SWMTKN-1-5vatlkm1qpx61ys6kffpfs9qddyo2wkrr7rojf630m6zxmyner-9kfghlmvma7snynqy8zvyd2nb 111.109.23.28:1377

在机器01上查看集群状态

docker node ls

启动一个nginx service

$ docker service create --replicas 2 -p 8080:80 --name nginx nginx

查看服务

ID                  NAME                MODE                REPLICAS            IMAGE                 PORTS
pm3trtqb6d9z        nginx               replicated          3/3                 nginx:1.13.7-alpine   *:8080->80/tcp
相关推荐

提示

功能待开通!


暂无评论~