这篇“Docker怎么创建centos容器集群并实现远程登录功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Docker怎么创建centos容器集群并实现远程登录功能”文章吧。
Docker 创建centos容器集群并实现远程登录
0.拉取Docker镜像(Centos7官方版)
拉取官方镜像(这个镜像里面几乎什么都没有,很多依赖库需要自己配置,实实在在的“纯净版”。关注我,下期教你用ISO镜像文件使用dockerfile制作究极完整版docker镜像)
docker pull centos:centos7
1.搭建网桥加入网络
创建docker bridge网桥
搭建网桥可以方便管理结点,并且让结点同时位于同一个网段下
sudo docker network create NodeNetWork
创建三个不同端口的结点容器
zwb@test-algo:~$ sudo docker run -itd --restart=always --hostname node01 --name Node01 -p 50001:22 -v /data/sda/sharedata:/share --network NodeNetWork --privileged=true centos:centos7 /sbin/initzwb@test-algo:~$ sudo docker run -itd --restart=always --hostname node02 --name Node02 -p 50002:22 -v /data/sda/sharedata:/share --network NodeNetWork --privileged=true centos:centos7 /sbin/initzwb@test-algo:~$ sudo docker run -itd --restart=always --hostname node03 --name Node03 -p 50003:22 -v /data/sda/sharedata:/share --network NodeNetWork --privileged=true centos:centos7 /sbin/init# 参数解释:# -itd# 选项选项简写说明# –detach-d在后台运行容器,并且打印容器id。# –interactive-i即使没有连接,也要保持标准输入保持打开状态,一般与 -t 连用。# –tty-t分配一个伪tty,一般与 -i 连用。# --restart=always机器启动时自启动# --hostname 初始化的hostname# -p 50001:22端口映射 宿主机端口:容器端口这里为22表示容器内ssh端口# --privileged=true通过特权模式进入docker,不仅可以使用systemctl命令(centos 7系统),还可以开启ssh服务# --network NodeNetWork 将容器结点加入网桥中# 注意:在 Linux Docker中无法使用 systemd(systemctl) 相关命令的原因是 1号进程不是 init ,而是其他例如 /bin/bash ,所以导致缺少相关文件无法运行。(System has not been booted with systemd as init system (PID 1). Can't operat#解决方案:/sbin/init并且--privilaged=true一定要加上
2.配置机器网络环境并加入ssh
以Node01为例子,进入结点容器并配置网络环境并加入ssh
(base) zwb@test-algo:~$ sudo docker exec -it Node01 /bin/bash[root@aa92cb71e3ab /]# yum -y install net-tools.x86_64Failed to set locale, defaulting to C.UTF-8CentOS Linux 8 - AppStream 26 B/s | 38 B 00:01 Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
可以看到我们在安装网络工具包的时候出错了,上面的报错信息意思是,从仓库 ‘appstream’ 下载元数据失败:由于镜像列表中没有 URL,不能准备内部镜像列表。
问题分析:
✨第一种可能的情况便是网络连接问题。检查是否可以连接外部网络,可以使用 ping baidu.com 查看是否有丢包情况。如果丢包,则进一步检查网络连接是否正常;如果没有丢包,继续阅读下文
✨那么第二种情况,便是 CentOS 已经停止维护的问题。2020 年 12 月 8 号,CentOS 官方宣布了停止维护 CentOS Linux 的计划,并推出了 CentOS Stream 项目,CentOS Linux 8 作为 RHEL 8 的复刻版本,生命周期缩短,于 2021 年 12 月 31 日停止更新并停止维护(EOL),更多的信息可以查看 CentOS 官方公告。如果需要更新 CentOS,需要将镜像从 mirror.centos.org 更改为 vault.centos.org
那么针对上面提到的第二种情况,给出的解决方法如下:
首先,进入到 yum 的 repos 目录
cd /etc/yum.repos.d/
其次,修改 centos 文件内容
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
然后,生成缓存更新(第一次更新,速度稍微有点慢,耐心等待两分钟左右)
yum makecache
最后,运行 yum update 并重新安装工具包、ssh网络环境和vim
yum update -yyum -y install net-tools.x86_64yum -y install openssh-serveryum install vim
安装passwd并修改root密码
yum install passwd[root@aa92cb71e3ab yum.repos.d]# passwdChanging password for user root.New password: Retype new password: passwd: all authentication tokens updated successfully.
重启docker
systemctl stop docker systemctl start docker
查看容器
(base) zwb@test-algo:~$ sudo docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2b8fa155e71f centos "/sbin/init" 17 minutes ago Up 9 seconds 0.0.0.0:50003->22/tcp, :::50003->22/tcp Node0397041252bf37 centos "/sbin/init" 17 minutes ago Up 9 seconds 0.0.0.0:50002->22/tcp, :::50002->22/tcp Node02aa92cb71e3ab centos "/sbin/init" 17 minutes ago Up 9 seconds 0.0.0.0:50001->22/tcp, :::50001->22/tcp Node01
开放宿主机防火墙
(base) zwb@test-algo:~$ firewall-cmd --add-port=50022/tcp --permanentYou're performing an operation over default zone ('public'),but your connections/interfaces are in zone 'docker' (see --get-active-zones)You most likely need to use --zone=docker option.Authorization failed. Make sure polkit agent is running or run the application as superuser.(base) zwb@test-algo:~$ sudo firewall-cmd --add-port=50022/tcp --permanentYou're performing an operation over default zone ('public'),but your connections/interfaces are in zone 'docker' (see --get-active-zones)You most likely need to use --zone=docker option.Warning: ALREADY_ENABLED: 50022:tcpsuccess(base) zwb@test-algo:~$ sudo firewall-cmd --reloadsuccess(base) zwb@test-algo:~$ sudo firewall-cmd --list-portYou're performing an operation over default zone ('public'),but your connections/interfaces are in zone 'docker' (see --get-active-zones)You most likely need to use --zone=docker option.50022/tcp
手动启动sshd
(base) zwb@test-algo:~$ sudo /usr/sbin/sshd(base) zwb@test-algo:~$ sudo netstat -antp | grep sshdtcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 929/sshd: /usr/sbin tcp 0 76 172.21.198.185:22 10.3.16.31:53836 ESTABLISHED 127529/sshd: zwb [p tcp 0 0 127.0.0.1:50522 127.0.0.1:38979 ESTABLISHED 127629/sshd: zwb@no tcp 0 0 127.0.0.1:50510 127.0.0.1:38979 ESTABLISHED 127629/sshd: zwb@no tcp 0 0 172.21.198.185:22 10.3.16.31:52932 ESTABLISHED 127260/sshd: zwb [p tcp6 0 0 :::22 :::* LISTEN 929/sshd: /usr/sbin
若发生以下问题
问题
[root@79a70e3d26cd /]# /usr/sbin/sshd Unable to load host key: /etc/ssh/ssh_host_rsa_keyUnable to load host key: /etc/ssh/ssh_host_ecdsa_keyUnable to load host key: /etc/ssh/ssh_host_ed25519_keysshd: no hostkeys available -- exiting.[root@79a70e3d26cd /]#
解决方案
执行:
# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ""# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""# ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
通过宿主机ip和端口远程连接容器
# 这是在windows上的shell远程连接,可以看到已经通过ssh连接上了node01,aa92cb71e3ab表示的是Node01的docker容器idPS C:\Users\99140> ssh root@172.21.198.185 -p 50001The authenticity of host '[172.21.198.185]:50001 ([172.21.198.185]:50001)' can't be established.ED25519 key fingerprint is SHA256:zqNzugPY6dYmLFlaDGFOfkxOF8qtY/a5mP0DXH7Vxbk.This key is not known by any other namesAre you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning: Permanently added '[172.21.198.185]:50001' (ED25519) to the list of known hosts.root@172.21.198.185's password:[root@aa92cb71e3ab ~]#
3. 查看容器中自启项内容
[root@79a70e3d26cd ~]# systemctl list-unit-files|grep enabledautovt@.service enabled getty@.service enabled kdump.service enabled nis-domainname.service enabled sshd.service enabled remote-fs.target enabled dnf-makecache.timer enabled [root@62435d2d7fd2 ~]#
容器在创建时通过----restart=always实现自启动 但还可以在使用on - failure策略时,指定Docker将尝试重新启动容器的最大次数
docker run --restart=on-failure:10 xxx
最后重启测试一下自启动是否成功
reboot
最后在远程主机上ssh连接三台centos结点并查看其网络ip情况
# Node01PS C:\Users\99140> ssh root@172.21.198.185 -p 50001root@172.21.198.185's password:Last login: Tue Mar 21 11:37:04 2023 from 10.3.16.31[root@aa92cb71e3ab ~]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.0.4 netmask 255.255.0.0 broadcast 172.18.255.255 ether 02:42:ac:12:00:04 txqueuelen 0 (Ethernet) RX packets 46 bytes 5625 (5.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 30 bytes 4929 (4.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet) RX packets 11 bytes 946 (946.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 # Node02PS C:\Users\99140> ssh root@172.21.198.185 -p 50002root@172.21.198.185's password:Last login: Tue Mar 21 11:37:16 2023 from 10.3.16.31[root@97041252bf37 ~]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.0.3 netmask 255.255.0.0 broadcast 172.18.255.255 ether 02:42:ac:12:00:03 txqueuelen 0 (Ethernet) RX packets 46 bytes 5625 (5.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 29 bytes 4819 (4.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet) RX packets 11 bytes 946 (946.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0# Node03PS C:\Users\99140> ssh root@172.21.198.185 -p 50003The authenticity of host '[172.21.198.185]:50003 ([172.21.198.185]:50003)' can't be established.ED25519 key fingerprint is SHA256:JdfhD5YG8cVOheu8diTuPlByz+KKdjYtQW8c6/XL28I.This key is not known by any other namesAre you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning: Permanently added '[172.21.198.185]:50003' (ED25519) to the list of known hosts.root@172.21.198.185's password:[root@2b8fa155e71f ~]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.0.2 netmask 255.255.0.0 broadcast 172.18.255.255 ether 02:42:ac:12:00:02 txqueuelen 0 (Ethernet) RX packets 47 bytes 6271 (6.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 27 bytes 5135 (5.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 11 bytes 946 (946.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
其他常用工具命令
# 安装ifconfigyum install net-tools# 修改hostnamehostnamectl set-hostname 想要的名字#会报错误:Could not set property: Failed to set static hostname: Device or resource busy,#exit退出重新进入容器即可
以上就是关于“Docker怎么创建centos容器集群并实现远程登录功能”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网行业资讯频道。