您好,登錄后才能下訂單哦!
這篇“Docker怎么創(chuàng)建centos容器集群并實現(xiàn)遠程登錄功能”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Docker怎么創(chuàng)建centos容器集群并實現(xiàn)遠程登錄功能”文章吧。
拉取官方鏡像(這個鏡像里面幾乎什么都沒有,很多依賴庫需要自己配置,實實在在的“純凈版”。關(guān)注我,下期教你用ISO鏡像文件使用dockerfile制作究極完整版docker鏡像)
docker pull centos:centos7
創(chuàng)建docker bridge網(wǎng)橋
搭建網(wǎng)橋可以方便管理結(jié)點,并且讓結(jié)點同時位于同一個網(wǎng)段下
sudo docker network create NodeNetWork
創(chuàng)建三個不同端口的結(jié)點容器
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 # 選項 選項簡寫 說明 # –detach -d 在后臺運行容器,并且打印容器id。 # –interactive -i 即使沒有連接,也要保持標準輸入保持打開狀態(tài),一般與 -t 連用。 # –tty -t 分配一個偽tty,一般與 -i 連用。 # --restart=always 機器啟動時自啟動 # --hostname 初始化的hostname # -p 50001:22 端口映射 宿主機端口:容器端口這里為22表示容器內(nèi)ssh端口 # --privileged=true 通過特權(quán)模式進入docker,不僅可以使用systemctl命令(centos 7系統(tǒng)),還可以開啟ssh服務(wù) # --network NodeNetWork 將容器結(jié)點加入網(wǎng)橋中 # 注意:在 Linux Docker中無法使用 systemd(systemctl) 相關(guān)命令的原因是 1號進程不是 init ,而是其他例如 /bin/bash ,所以導(dǎo)致缺少相關(guān)文件無法運行。(System has not been booted with systemd as init system (PID 1). Can't operat #解決方案:/sbin/init并且--privilaged=true一定要加上
以Node01為例子,進入結(jié)點容器并配置網(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ò)工具包的時候出錯了,上面的報錯信息意思是,從倉庫 ‘appstream’ 下載元數(shù)據(jù)失?。河捎阽R像列表中沒有 URL,不能準備內(nèi)部鏡像列表。
問題分析:
?第一種可能的情況便是網(wǎng)絡(luò)連接問題。檢查是否可以連接外部網(wǎng)絡(luò),可以使用 ping baidu.com 查看是否有丟包情況。如果丟包,則進一步檢查網(wǎng)絡(luò)連接是否正常;如果沒有丟包,繼續(xù)閱讀下文
?那么第二種情況,便是 CentOS 已經(jīng)停止維護的問題。2020 年 12 月 8 號,CentOS 官方宣布了停止維護 CentOS Linux 的計劃,并推出了 CentOS Stream 項目,CentOS Linux 8 作為 RHEL 8 的復(fù)刻版本,生命周期縮短,于 2021 年 12 月 31 日停止更新并停止維護(EOL),更多的信息可以查看 CentOS 官方公告。如果需要更新 CentOS,需要將鏡像從 mirror.centos.org 更改為 vault.centos.org
那么針對上面提到的第二種情況,給出的解決方法如下:
首先,進入到 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-*
然后,生成緩存更新(第一次更新,速度稍微有點慢,耐心等待兩分鐘左右)
yum makecache
最后,運行 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
開放宿主機防火墻
(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 ""
通過宿主機ip和端口遠程連接容器
# 這是在windows上的shell遠程連接,可以看到已經(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 ~]#
[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實現(xiàn)自啟動 但還可以在使用on - failure策略時,指定Docker將嘗試重新啟動容器的最大次數(shù)
docker run --restart=on-failure:10 xxx
最后重啟測試一下自啟動是否成功
reboot
最后在遠程主機上ssh連接三臺centos結(jié)點并查看其網(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 eth2: 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 eth2: 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 eth2: 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
其他常用工具命令
# 安裝ifconfig yum install net-tools # 修改hostname hostnamectl set-hostname 想要的名字 #會報錯誤:Could not set property: Failed to set static hostname: Device or resource busy, #exit退出重新進入容器即可
以上就是關(guān)于“Docker怎么創(chuàng)建centos容器集群并實現(xiàn)遠程登錄功能”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。