本篇文章是基于Docker搭建大数据集群系列的开篇之作
主要内容
- docker搭建
- docker部署CentOS
- 容器免密钥通信
- 容器保存成镜像
- docker镜像发布
环境
- Linux 7.6
一、Docker安装
安装工具包
yum install -y yum-utils #安装工具包,缺少这些依赖将无法完成
设置远程仓库
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装
yum install docker-ce
启动
systemctl start docker
查看版本
docker version
校验
docker run hello-world
二、docker部署CentOS
拉取镜像
docker pull centos
查看是否拉取成功
docker imsages
设置docker网桥,用于分配固定IP
docker network create --subnet=172.15.0.0/16 netgroup
创建容器
--name 容器名称
-h 容器主机名
--net 设置网桥
--ip 指定容器IP
master
docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-master -h cluster-master --net netgroup --ip 172.15.0.2 centos /usr/sbin/init
slave1
docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-slave1-h cluster-slave1--net netgroup --ip 172.15.0.3 centos /usr/sbin/init
slave2
docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-slave2-h cluster-slave2--net netgroup --ip 172.15.0.4 centos /usr/sbin/init
slave3
docker run -d --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup --name cluster-slave3-h cluster-slave3--net netgroup --ip 172.15.0.5 centos /usr/sbin/init
查看运行中的容器
docker ps
查看所有容器,包括未运行的容器
docker pa -a
运行容器
docker run 容器名或容器ID
进入容器
docker run exec -ti 容器名或容器ID bash
配置hosts文件
#安装VIM编辑工具 yum install vim vim /etc/hosts #追加以下内容 172.15.0.2 cluster-master 172.15.0.3 cluster-slave1 172.15.0.4 cluster-slave2 172.15.0.5 cluster-slave3 #将配置文件发送到其它节点 scp /etc/hosts cluster-slave1:/etc/ scp /etc/hosts cluster-slave2:/etc/ scp /etc/hosts cluster-slave3:/etc/• 1
三、容器免密钥通信
更换阿里YUM源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo• 1
部署OpenSSH
yum -y install openssh openssh-server openssh-clients systemctl start sshd• 1
生成密钥
敲出下面命令后连续回车三次
ssh-keygen -t rsa
设置用户密码
passwd root
公钥分发
在所有结点都生成密钥之后再进行这步
master
ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-master ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave1 ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave2 ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave3• 1
slave1
ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-master ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave1 ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave2 ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave3• 1
slave2
ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-master ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave1 ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave2 ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave3• 1
slave3
ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-master ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave1 ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave2 ssh-copy-id -f -i ~/.ssh/id_rsa.pub cluster-slave3• 1
测试免密钥
第一次敲下面命令时,会向你询问,直接打
yes
,然后输入对应主机的访问密码第二次敲
ssh
命令时,应当直接能够访问,无需输入密码退出当前用户使用命令
exit
master
ssh cluster-master ssh cluster-slave1 ssh cluster-slave1 ssh cluster-slave1• 1
slave1
ssh cluster-master ssh cluster-slave1 ssh cluster-slave1 ssh cluster-slave1• 1
slave2
ssh cluster-master ssh cluster-slave1 ssh cluster-slave1 ssh cluster-slave1• 1
slave3
ssh cluster-master ssh cluster-slave1 ssh cluster-slave1 ssh cluster-slave1• 1
四、容器保存成镜像
docker commit -m '提交文字说明' -a '作者' 容器名 提交后的镜像名:提交后的镜像tag名
五、镜像推送到远程仓库
新建账号
创建一个镜像仓库
给需要推送的镜像打标签
docker tag 镜像id 要推入的仓库的用户名/要推入的仓库名:新定义的tag
推送镜像到仓库
docker push 要推入的仓库的用户名/要推入的仓库名:镜像标签