Centos7搭建swarm集群

  1. 准备

两台虚拟机,IP分别为:
192.168.1.104
192.168.1.105
保证能互相 ping

  1. 修改虚拟机的 host,分别任 c1、c2

192.168.1.105 中,设置其 hostc1

	hostnamectl --static set-hostname c1

查看:

	hostnamectl status

192.168.1.104 中,设置其 hostc2

	hostnamectl --static set-hostname c2

查看:

	hostnamectl status
  1. 修改网络配置:

192.168.1.105 中:
运行:vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
修改以下几项:

静态IP:BOOTPROTO=static
自启动网络:ONBOOT=yes
设置IP:IPADDR0=192.168.1.105
设置子网掩码:PREFIXO0=255.255.255.0
设置网关:192.168.1.1
设置DNS:192.168.1.1

192.168.1.104 中,同样设置;

重启网络配置:service network restart

  1. hostname 加入 /etc/hosts

192.168.1.104

vi /etc/hosts

加入 192.168.1.105 c1

192.168.1.105

vi /etc/hosts

加入 192.168.1.104 c2

此时:
192.168.1.104 中:ping c1ping
192.168.1.105 中:ping c2ping

  1. ssh 免密码登录:

c1 中:
运行:ssh-keygen
修改配置文件:vi /etc/ssh/sshd_config
去掉下面两行的注释:

	RSAAuthentication yes
	PubkeyAuthentication yes
将生成的密钥,复制到另一台:
	ssh-copy-id c1
	ssh-copy-id c2

c2 中类似;

验证:在 c1、c2 中:
运行:for n in $(seq 1 2); do ssh c$n hostname; done;
输出:

	c1
	c2
  1. 使用 swarm (将 c1 作为管理节点)

c1 中:
运行:docker swarm init --advertise-addr 192.168.1.105
输出:

Swarm initialized: current node (8e4unyeyr41lm9ydpd6m5h2ik) is now a manager.

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

    docker swarm join \
    --token SWMTKN-1-0mff0pmz2wj90pis58ism59518pifb7kytcgiaavi8gpbem4im-174dot24tr60ntiafv1rso40n \
    192.168.1.105:2377

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

c2 中,运行 c1 中输出的部分:

docker swarm join \
    --token SWMTKN-1-0mff0pmz2wj90pis58ism59518pifb7kytcgiaavi8gpbem4im-174dot24tr60ntiafv1rso40n \
    192.168.1.105:2377

输出:This node joined a swarm as a worker.
表示已经成为了一个工作节点。

c1 中:
运行:docker node ls
输出:

ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
		0zmhvma2s5km2pjn2mudutzru    c2        Ready   Active        
		8e4unyeyr41lm9ydpd6m5h2ik *  c1        Ready   Active        Leader

Leader 表示该节点为管理节点

集群建立成功。

posted @ 2017-10-07 17:55  美好人生shy  阅读(802)  评论(0编辑  收藏  举报