前面在部署容器,使用docker容器作为jenkins的Slave节点时,会发现在使用centos作为镜像源拉去容器,不能正常连接,最后是因为centos的sshd的问题
下面专门是centos容器安装sshd的实验
1. 拉去centos的镜像
[root@node6 ~]# docker pull centos
[root@node6 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 775349758637 3 days ago 64.2MB jenkins/jenkins lts fac78e370c0b 6 days ago 568MB centos latest 0f3e07c0138f 4 weeks ago 220MB
2. 运行一个容器
[root@node6 ~]# docker run -it centos /bin/bash
安装相关软件
[root@f2320c5d3c54 /]# yum -y update
[root@f2320c5d3c54 /]# yum -y install net-tools passwd cracklib-dicts
3. 创建一个用户,并设置密码
[root@f2320c5d3c54 /]# useradd jenkins
[root@f2320c5d3c54 /]# echo "p@ssw0rd"|passwd --stdin jenkins
Changing password for user jenkins. passwd: all authentication tokens updated successfully
4. 安装sshd
[root@f2320c5d3c54 /]# yum provides sshd
Last metadata expiration check: 0:15:20 ago on Mon Nov 4 17:33:23 2019. openssh-server-7.8p1-4.el8.i686 : An open source SSH server daemon Repo : BaseOS Matched from: Filename : /usr/sbin/sshd openssh-server-7.8p1-4.el8.x86_64 : An open source SSH server daemon Repo : BaseOS Matched from: Filename : /usr/sbin/sshd
[root@f2320c5d3c54 /]# yum -y install openssh-server-7.8p1-4.el8.x86_64
5. 安装一些其他应用
安装maven
[root@f2320c5d3c54 /]# yum -y install maven
yum安装maven会自己安装Java作为依赖
[root@f2320c5d3c54 /]# java -version
openjdk version "1.8.0_232" OpenJDK Runtime Environment (build 1.8.0_232-b09) OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)
[root@f2320c5d3c54 /]# mvn -v
Apache Maven 3.5.4 (Red Hat 3.5.4-5) Maven home: /usr/share/maven Java version: 1.8.0_232, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el8_0.x86_64/jre Default locale: en_US, platform encoding: ANSI_X3.4-1968 OS name: "linux", version: "3.10.0-957.27.2.el7.x86_64", arch: "amd64", family: "unix"
安装git
[root@f2320c5d3c54 /]# yum -y install git
6. 尝试启动sshd
[root@f2320c5d3c54 /]# /usr/sbin/sshd -D
Could not load host key: /etc/ssh/ssh_host_rsa_key Could not load host key: /etc/ssh/ssh_host_ecdsa_key Could not load host key: /etc/ssh/ssh_host_ed25519_key sshd: no hostkeys available -- exiting.
[root@f2320c5d3c54 /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
[root@f2320c5d3c54 /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@f2320c5d3c54 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''
Generating public/private dsa key pair. Your identification has been saved in /etc/ssh/ssh_host_ed25519_key. Your public key has been saved in /etc/ssh/ssh_host_ed25519_key.pub. The key fingerprint is: SHA256:uE0sGjrBYgq2t30d0ICWRP3o4Mn+ZhnAH4TX/1riVsA root@f2320c5d3c54 The key's randomart image is: +---[DSA 1024]----+ | oo= . | | = = . | | o o = o | | . + * o E | |ooo + O S o | |=..o * O . . + | |..o.o . = o = | | ..o. = . + | | . .=. . | +----[SHA256]-----+
[root@f2320c5d3c54 /]# /usr/sbin/sshd -D &
[root@f2320c5d3c54 /]# ps -a
PID TTY TIME CMD 507 pts/0 00:00:00 sshd 508 pts/0 00:00:00 ps
7. 已经启动,sshd安装完成
退出,并保存到镜像
[root@f2320c5d3c54 /]# exit
[root@node6 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 37e4f2be0a41 5f2bf26e3524 "/bin/bash" 19 minutes ago Up 19 minutes sweet_hypatia f2320c5d3c54 centos "/bin/bash" 40 minutes ago Exited (0) 12 seconds ago compassionate_elbakyan b119e7f28b50 jenkins/jenkins:lts "/sbin/tini -- /usr/…" 4 hours ago Exited (143) 3 hours ago jenkins_prod
[root@node6 ~]# docker commit f2320c5d3c54 centos_jenkins
sha256:deca1dcb74bebefb77f9f9ca4328131e52332815d766cace00a16702351e1f8a
[root@node6 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE centos_jenkins latest deca1dcb74be 3 minutes ago 703MB dftbcros.mirror.aliyuncs.com/library/ubuntu 16.04 5f2bf26e3524 3 days ago 123MB jenkins/jenkins lts fac78e370c0b 6 days ago 568MB centos latest 0f3e07c0138f 4 weeks ago 220MB
ssh安装完成