- 使用帶密碼的密鑰,通過配置ssh-agent,使用docker context訪問遠(yuǎn)程Docker Engine
環(huán)境
Docker Engine: 192.168.17.128 EngineHost
Docker Cli: 192.168.17.129 CliHost
以非root用戶身份管理Docker
- 在EngineHost上創(chuàng)建普通用戶并添加進(jìn)docker組
[root@EngineHost ~]# useradd testuser
[root@EngineHost ~]# usermod -aG docker testuser
- 在CliHost上生成ssh key,EngineHost配置密鑰登陸
[root@CliHost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 【此處輸入密碼】
Enter same passphrase again: 【此處輸入密碼】
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:1K5jexu43UckpNhRy9WtICiOtmgavysFhm90P+oib+Q root@CliHost
The key's' randomart image is:
+---[RSA 3072]----+
| . .. ...|
| . ..o.oo o|
|. o ..o.=o. . |
|o.. + ....o . o |
|.+ + o S . o |
|. B . o o . |
| X . .= . . |
|+ E . . =.o . |
| =+=. o.o... |
+----[SHA256]-----+
[root@CliHost ~]# ssh-copy-id testuser@EngineHost
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'enginehost (192.168.17.128)' can't be established.
ED25519 key fingerprint is SHA256:gpuL4EePK1eJYGtKQ72MAuRzqiQGywQUXia7953sdbw.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
testuser@enginehost's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'testuser@EngineHost'"
and check to make sure that only the key(s) you wanted were added.
[root@CliHost ~]#
創(chuàng)建docker context
[root@CliHost ~]# docker context create --docker host=ssh://testuser@EngineHost --description="Remote engine" my-remote-engine
my-remote-engine
Successfully created context "my-remote-engine"
[root@CliHost ~]# docker context use my-remote-engine
my-remote-engine
Current context is now "my-remote-engine"
創(chuàng)建ssh-agent
[root@CliHost ~]# ssh-keyscan EngineHost >> ~/.ssh/known_hosts
# EngineHost:22 SSH-2.0-OpenSSH_8.7
# EngineHost:22 SSH-2.0-OpenSSH_8.7
# EngineHost:22 SSH-2.0-OpenSSH_8.7
# EngineHost:22 SSH-2.0-OpenSSH_8.7
# EngineHost:22 SSH-2.0-OpenSSH_8.7
[root@CliHost ~]# cat .ssh/config
Host EngineHost
HostName EngineHost
User testuser
Port 22
IdentityFile ~/.ssh/id_rsa
ControlMaster auto
ControlPath ~/.ssh/control-%C
ControlPersist yes
[root@CliHost ~]# eval `ssh-agent`
Agent pid 13254
[root@CliHost ~]# ssh-add -l
The agent has no identities.
[root@CliHost ~]# ssh-add ~/.ssh/id_rsa
Enter passphrase for /root/.ssh/id_rsa:
Identity added: /root/.ssh/id_rsa (root@CliHost)
使用ssh登錄遠(yuǎn)程Docker Engine
[root@CliHost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@CliHost ~]# systemctl status docker.service
Unit docker.service could not be found.
[root@CliHost ~]# docker info
Client: Docker Engine - Community
Version: 26.1.4
Context: my-remote-engine
Debug Mode: false
Tips
[root@EngineHost ~]# egrep -v "^#|^$" /usr/lib/systemd/system/docker.service
[Service]
…………
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:8888 -H fd:// --containerd=/run/containerd/containerd.sock
…………
[root@EngineHost ~]# systemctl daemon-reload
[root@EngineHost ~]# systemctl restart docker.service
[root@EngineHost ~]# ss -anpt | grep dockerd
LISTEN 0 4096 *:8888 *:* users:(("dockerd",pid=2273,fd=3))
[root@CliHost ~]# export DOCKER_HOST="tcp://EngineHost:8888"
[root@CliHost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。