Docker?創(chuàng)建centos容器集群并實(shí)現(xiàn)遠(yuǎn)程登錄功能
Docker 創(chuàng)建centos容器集群并實(shí)現(xiàn)遠(yuǎn)程登錄
0.拉取Docker鏡像(Centos7官方版)
拉取官方鏡像(這個鏡像里面幾乎什么都沒有,很多依賴庫需要自己配置,實(shí)實(shí)在在的“純凈版”。關(guān)注我,下期教你用ISO鏡像文件使用dockerfile制作究極完整版docker鏡像)
docker pull centos:centos7
1.搭建網(wǎng)橋加入網(wǎng)絡(luò)
創(chuàng)建docker bridge網(wǎng)橋
搭建網(wǎng)橋可以方便管理結(jié)點(diǎn),并且讓結(jié)點(diǎn)同時位于同一個網(wǎng)段下
sudo docker network create NodeNetWork
創(chuàng)建三個不同端口的結(jié)點(diǎn)容器
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/init zwb@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/init zwb@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 # 參數(shù)解釋: # -itd # 選項(xiàng) 選項(xiàng)簡寫 說明 # –detach -d 在后臺運(yùn)行容器,并且打印容器id。 # –interactive -i 即使沒有連接,也要保持標(biāo)準(zhǔn)輸入保持打開狀態(tài),一般與 -t 連用。 # –tty -t 分配一個偽tty,一般與 -i 連用。 # --restart=always 機(jī)器啟動時自啟動 # --hostname 初始化的hostname # -p 50001:22 端口映射 宿主機(jī)端口:容器端口這里為22表示容器內(nèi)ssh端口 # --privileged=true 通過特權(quán)模式進(jìn)入docker,不僅可以使用systemctl命令(centos 7系統(tǒng)),還可以開啟ssh服務(wù) # --network NodeNetWork 將容器結(jié)點(diǎn)加入網(wǎng)橋中 # 注意:在 Linux Docker中無法使用 systemd(systemctl) 相關(guān)命令的原因是 1號進(jìn)程不是 init ,而是其他例如 /bin/bash ,所以導(dǎo)致缺少相關(guān)文件無法運(yùn)行。(System has not been booted with systemd as init system (PID 1). Can't operat #解決方案:/sbin/init并且--privilaged=true一定要加上
2.配置機(jī)器網(wǎng)絡(luò)環(huán)境并加入ssh
以Node01為例子,進(jìn)入結(jié)點(diǎn)容器并配置網(wǎng)絡(luò)環(huán)境并加入ssh
(base) zwb@test-algo:~$ sudo docker exec -it Node01 /bin/bash [root@aa92cb71e3ab /]# yum -y install net-tools.x86_64 Failed to set locale, defaulting to C.UTF-8 CentOS 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
可以看到我們在安裝網(wǎng)絡(luò)工具包的時候出錯了,上面的報(bào)錯信息意思是,從倉庫 ‘appstream’ 下載元數(shù)據(jù)失敗:由于鏡像列表中沒有 URL,不能準(zhǔn)備內(nèi)部鏡像列表。
??問題分析:
?第一種可能的情況便是網(wǎng)絡(luò)連接問題。檢查是否可以連接外部網(wǎng)絡(luò),可以使用 ping baidu.com 查看是否有丟包情況。如果丟包,則進(jìn)一步檢查網(wǎng)絡(luò)連接是否正常;如果沒有丟包,繼續(xù)閱讀下文
?那么第二種情況,便是 CentOS 已經(jīng)停止維護(hù)的問題。2020 年 12 月 8 號,CentOS 官方宣布了停止維護(hù) CentOS Linux 的計(jì)劃,并推出了 CentOS Stream 項(xiàng)目,CentOS Linux 8 作為 RHEL 8 的復(fù)刻版本,生命周期縮短,于 2021 年 12 月 31 日停止更新并停止維護(hù)(EOL),更多的信息可以查看 CentOS 官方公告。如果需要更新 CentOS,需要將鏡像從 mirror.centos.org 更改為 vault.centos.org
??那么針對上面提到的第二種情況,給出的解決方法如下:
?? 首先,進(jìn)入到 yum 的 repos 目錄
cd /etc/yum.repos.d/
??其次,修改 centos 文件內(nèi)容
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-*
?? 然后,生成緩存更新(第一次更新,速度稍微有點(diǎn)慢,耐心等待兩分鐘左右)
yum makecache
?? 最后,運(yùn)行 yum update 并重新安裝工具包、ssh網(wǎng)絡(luò)環(huán)境和vim
yum update -y yum -y install net-tools.x86_64 yum -y install openssh-server yum install vim
安裝passwd并修改root密碼
yum install passwd [root@aa92cb71e3ab yum.repos.d]# passwd Changing 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 -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2b8fa155e71f centos "/sbin/init" 17 minutes ago Up 9 seconds 0.0.0.0:50003->22/tcp, :::50003->22/tcp Node03 97041252bf37 centos "/sbin/init" 17 minutes ago Up 9 seconds 0.0.0.0:50002->22/tcp, :::50002->22/tcp Node02 aa92cb71e3ab centos "/sbin/init" 17 minutes ago Up 9 seconds 0.0.0.0:50001->22/tcp, :::50001->22/tcp Node01
開放宿主機(jī)防火墻
(base) zwb@test-algo:~$ firewall-cmd --add-port=50022/tcp --permanent You'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 --permanent You'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:tcp success (base) zwb@test-algo:~$ sudo firewall-cmd --reload success (base) zwb@test-algo:~$ sudo firewall-cmd --list-port You'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 sshd tcp 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
若發(fā)生以下問題
問題
[root@79a70e3d26cd /]# /usr/sbin/sshd Unable to load host key: /etc/ssh/ssh_host_rsa_key Unable to load host key: /etc/ssh/ssh_host_ecdsa_key Unable to load host key: /etc/ssh/ssh_host_ed25519_key sshd: no hostkeys available -- exiting. [root@79a70e3d26cd /]#
解決方案
執(zhí)行:
# 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 ""
通過宿主機(jī)ip和端口遠(yuǎn)程連接容器
# 這是在windows上的shell遠(yuǎn)程連接,可以看到已經(jīng)通過ssh連接上了node01,aa92cb71e3ab表示的是Node01的docker容器id PS C:\Users\99140> ssh root@172.21.198.185 -p 50001 The 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 names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '[172.21.198.185]:50001' (ED25519) to the list of known hosts. root@172.21.198.185's password: [root@aa92cb71e3ab ~]#
3. 查看容器中自啟項(xiàng)內(nèi)容
[root@79a70e3d26cd ~]# systemctl list-unit-files|grep enabled autovt@.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 ~]#
容器在創(chuàng)建時通過----restart=always實(shí)現(xiàn)自啟動 但還可以在使用on - failure策略時,指定Docker將嘗試重新啟動容器的最大次數(shù)
docker run --restart=on-failure:10 xxx
最后重啟測試一下自啟動是否成功
reboot
最后在遠(yuǎn)程主機(jī)上ssh連接三臺centos結(jié)點(diǎn)并查看其網(wǎng)絡(luò)ip情況
# Node01 PS C:\Users\99140> ssh root@172.21.198.185 -p 50001 root@172.21.198.185's password: Last login: Tue Mar 21 11:37:04 2023 from 10.3.16.31 [root@aa92cb71e3ab ~]# ifconfig eth0: 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 0 eth1: 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 0 lo: 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 # Node02 PS C:\Users\99140> ssh root@172.21.198.185 -p 50002 root@172.21.198.185's password: Last login: Tue Mar 21 11:37:16 2023 from 10.3.16.31 [root@97041252bf37 ~]# ifconfig eth0: 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 0 eth1: 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 0 lo: 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 # Node03 PS C:\Users\99140> ssh root@172.21.198.185 -p 50003 The 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 names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '[172.21.198.185]:50003' (ED25519) to the list of known hosts. root@172.21.198.185's password: [root@2b8fa155e71f ~]# ifconfig eth0: 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 0 eth1: 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 0 lo: 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
三臺結(jié)點(diǎn)都能遠(yuǎn)程連接并且ip在同一網(wǎng)段下,完美,可以躺在寢室完美運(yùn)行學(xué)校服務(wù)器上的結(jié)點(diǎn)集群了!
4. 參考文章:
https://zhuanlan.zhihu.com/p/212772001
https://blog.csdn.net/chj_1224365967/article/details/109286763
https://www.cnblogs.com/davis12/p/14392125.html
https://blog.csdn.net/rjszz1314/article/details/112948993
其他常用工具命令
# 安裝ifconfig yum install net-tools # 修改hostname hostnamectl set-hostname 想要的名字 #會報(bào)錯誤:Could not set property: Failed to set static hostname: Device or resource busy, #exit退出重新進(jìn)入容器即可
到此這篇關(guān)于Docker 創(chuàng)建centos容器集群并實(shí)現(xiàn)遠(yuǎn)程登錄的文章就介紹到這了,更多相關(guān)Docker centos容器遠(yuǎn)程登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA通過Docker插件部署SpringBoot項(xiàng)目的過程詳解
在idea中如何通過Docker插件部署SpringBoot項(xiàng)目呢?很多朋友在配置過程中走了很多誤區(qū),今天小編給大家分享一篇教程關(guān)于IDEA通過Docker插件部署SpringBoot項(xiàng)目的過程,感興趣的朋友一起看看吧2021-11-11使用Docker的NFS-Ganesha鏡像搭建nfs服務(wù)器的詳細(xì)過程
這篇文章主要介紹了使用Docker的NFS-Ganesha鏡像搭建nfs服務(wù)器,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08在Windows系統(tǒng)下安裝docker窗口的配置過程
相信大家都知道Docker有很多種安裝的選擇,其中支持最好的是Ubuntu系統(tǒng)。而且docker如果想在windows上運(yùn)行必須借助docker-machine,這篇文章將給大家詳細(xì)的介紹在Windows系統(tǒng)上安裝docker窗口的配置過程,有需要的朋友們可以參考借鑒。2016-10-10使用Docker搭建MySQL主從數(shù)據(jù)庫的方法步驟
本文主要介紹了使用Docker搭建MySQL主從數(shù)據(jù)庫的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01docker生產(chǎn)環(huán)境jvm性能優(yōu)化方式
這篇文章主要介紹了docker生產(chǎn)環(huán)境jvm性能優(yōu)化方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08