1. jenkins 添加SSH Server ,把k8s的master主机添加进k8s。
2. 将pipeline文件添加进gitab,并且增加tag标签
3. 修改jenkinsfile文件stage步骤。
4. 配置ssh 无密码登入,生成公钥和秘钥
[root@localhost .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:dEOIy7GOvXLWhwb3r4o3gtcGJmX+iyqMGlSMviBJqvY root@localhost
The key's randomart image is:
+---[RSA 2048]----+
| . .. |
| o o .. |
| o o . +. o |
|+.. *. . . |
|=o B S |
|= . o B . |
|o= + O o |
|o.+ o *oX o |
|o E.*o=o=.o. |
+----[SHA256]-----+
4. 把 /root/.ssh中的id_rsa.pub 拷贝到需要登入的目标服务器中的/root/.ssh/ authorized_keys
[root@localhost .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyrgSRxWn5kuTtKMbVVEM/PITxbkkRoulrymPlz4CJ9fNwXgRul0v0hIy1vR6W0Y+7IfpmBnp1b8gwVjQskUvIEQUfy243G5Oca5E1M5aRFS/qqtGZaQNYqIiS+kk/3oFJm7xn8vGnpqjGOgPdeVhnrHYzAO37q+ZtXxZNbsiGSmsVM+6ykmPeAyZg7sTDYWvD8aovTFL67wgWTA8lKDbx8yBoZWIxPHFbh4T0np
l8S5dLZhGfoPIIFaji56KHixueOjWmlgcJIdjorIKtMfCHSkaWMaD4nfUTbdwzZuFPjMLjW5z1oHS8r49/bf7Wc+7MI5sbiJNgJ1yh2jwp7W9v
[root@k8s-master01 .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyrgSRxWn5kuTtKMbVVEM/PITxbkkRoulrymPlz4CJ9fNwXgRul0v0hIy1vR6W0Y+7IfpmBnp1b8gwVjQskUvIEQUfy243G5Oca5E1M5aRFS/qqtGZaQNYqIiS+kk/3oFJm7xn8vGnpqjGOgPdeVhnrHYzAO37q+ZtXxZNbsiGSmsVM+6ykmPeAyZg7sTDYWvD8aov
TFL67wgWTA8lKDbx8yBoZWIxPHFbh4T0npl8S5dLZhGfoPIIFaji56KHixueOjWmlgcJIdjorIKtMfCHSkaWMaD4nfUTbdwzZuFPjMLjW5z1oHS8r49/bf7Wc+7MI5sbiJNgJ1yh2jwp7W9v
5.测试是否成功。
[root@localhost .ssh]# ssh [email protected] ls /usr/local/k8s
pipeline.yml
6.生成pipeline流水线脚本
7.修改jenkinfile,添加步骤。
stage('远程执行k8s-master的kubectl命令') {
steps {
sh 'ssh [email protected] kubectl apply -f /usr/local/k8s/pipeline.yml'
}
}
8. 统一jenkinsfile和pipeline文件中的版本。
9. 编译后报错,故障原因为
首先确认
- Jenkins的Git使用的ssh, 不需要预先记录known_hosts, 配置好就可以直接用
- SSH Agent必须要先在命令行下访问一下对方IP, 记录了known_hosts后, 才能正常使用因为安装为服务的Jenkins使用的是jenkins用户,而且是/bin/false的用户, 不能像普通用户那样登录然后ssh一下对方IP, 你需要按以下方式处理
[root@localhost .ssh]# su -s /bin/bash jenkins
bash-4.2$ cd /var/lib/jenkins/.ssh/
bash-4.2$ ssh [email protected]
Last login: Sat Oct 15 19:28:07 2022 from 192.168.30.107
# 查看.ssh下是否已经有known_hosts, 有的话, 是什么内容, 正常情况下, 这时候是没有对应IP记录的
[root@localhost .ssh]# cat known_hosts
192.168.30.117 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNjhibYp5lVtJpJePhjiSNrbA4xqJh7VvZ+2tZnPGHHCitf1Qh6w3IklhugTPe/03
10 编译后第二个报错
因为使用ssh执行命令和登录后执行Linux所加载的环境变量不同,在使用ssh执行命令时没有加载/etc/profile的环境变量。
vim /root/.bashrc 添加如下行。
标签:rsa,ssh,整合,jenkins,k8s,root,id From: https://www.cnblogs.com/fenghua001/p/16795076.html