使用 Packer 在 Amazon EC2 Linux 2 AMI 上使用 JDK 11 安装 Jenkins 失败
Failed to install Jenkins with JDK 11 on Amazon EC2 Linux 2 AMI with Packer
我尝试使用以下 Packer 配置安装 EC2 实例,并预装 Jenkins:
这里最重要的部分是setup.sh脚本,它安装Jenkins和Java:
#!/bin/bash
echo "Installing Amazon Linux extras"
amazon-linux-extras install epel -y
echo "Install Jenkins stable release"
yum remove -y java
yum install -y java-11-openjdk-devel
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install -y jenkins
chkconfig jenkins on
echo "Install git"
yum install -y git
echo "Setup SSH key"
mkdir /var/lib/jenkins/.ssh
touch /var/lib/jenkins/.ssh/known_hosts
chown -R jenkins:jenkins /var/lib/jenkins/.ssh
chmod 700 /var/lib/jenkins/.ssh
mv /tmp/id_rsa /var/lib/jenkins/.ssh/id_rsa
chmod 600 /var/lib/jenkins/.ssh/id_rsa
chown -R jenkins:jenkins /var/lib/jenkins/.ssh/id_rsa
echo "Configure Jenkins"
mkdir -p /var/lib/jenkins/init.groovy.d
mv /tmp/scripts/*.groovy /var/lib/jenkins/init.groovy.d/
chown -R jenkins:jenkins /var/lib/jenkins/init.groovy.d
mv /tmp/config/jenkins /etc/sysconfig/jenkins
chmod +x /tmp/config/install-plugins.sh
bash /tmp/config/install-plugins.sh
service jenkins start
通过这个link查看所有其他配置文件:
https://github.com/mlabouardy/pipeline-as-code-with-jenkins/tree/master/chapter4/distributed/master
当我运行:
packer build template.json
我得到以下异常:
==> amazon-ebs: Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details.
一切正常,如果我使用:
yum install -y java-1.8.0-openjdk
而不是:
yum install -y java-11-openjdk-devel
我可以启动 Jenkins,但我看到以下警告:
Using Java 8 with the latest Jenkins is not recommended
有谁知道如何使此配置与 Java 11 一起使用?
P.S。
作为 Packer 的源亚马逊机器映像,我使用:
Amazon Linux 2 AMI (HVM), SSD Volume Type - ami-02e136e904f3da870 (64-bit x86) / ami-0e341fcaad89c3650 (64-bit Arm)
它在“us-east-1”区域可用(“source_ami”:“ami-02e136e904f3da870" - Amazon Linux 2 AMI 的确切 ID 取决于地区)
这里是Packer template.json,用于烘焙Jenkins Image 来自Source图片:
{
"variables" : {
"region" : "us-east-1",
"aws_profile": "ops-account",
"source_ami" : "ami-02e136e904f3da870",
"instance_type": "t2.micro",
"ssh_key": "./jenkins_ssh"
},
"builders" : [
{
"type" : "amazon-ebs",
"profile" : "{{user `aws_profile`}}",
"region" : "{{user `region`}}",
"instance_type" : "{{user `instance_type`}}",
"source_ami" : "{{user `source_ami`}}",
"ssh_username" : "ec2-user",
"ami_name" : "jenkins-master-2.204.1",
"ami_description" : "Amazon Linux Image with Jenkins Server",
"run_tags" : {
"Name" : "packer-builder"
}
}
],
"provisioners" : [
{
"type" : "file",
"source" : "./scripts",
"destination" : "/tmp/"
},
{
"type" : "file",
"source" : "./config",
"destination" : "/tmp/"
},
{
"type" : "file",
"source" : "{{user `ssh_key`}}",
"destination" : "/tmp/id_rsa"
},
{
"type" : "shell",
"script" : "./setup.sh",
"execute_command" : "sudo -E -S sh '{{ .Path }}'"
}
]
}
将以下命令行添加到 setup.sh 解决了问题:
amazon-linux-extras install java-openjdk11 -y
看来,java-11-openjdk 软件包在 Amazon 中不可用 Linux 2 个主存储库 安装。
但它们现在 可用 在 Amazon Linux 2 extras 存储库
大多数人在 ec2-user 中安装 jenkins 时遇到问题。
Jenkins Redhat 包
要使用此存储库,运行 以下命令:
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install epel-release ( it will not work so use below command ) sudo amazon-linux-extras install epel
yum install java-11-openjdk-devel -y
yum install java-11-openjdk-devel
(它不会工作所以使用下面的命令)
yum search all "java"
之后:
yum install java-11-amazon-corretto.x86_64
java --version
enter image description here
enter image description here
yum install jenkins
systemctl start jenkins
systemctl enable jenkins
我尝试使用以下 Packer 配置安装 EC2 实例,并预装 Jenkins:
这里最重要的部分是setup.sh脚本,它安装Jenkins和Java:
#!/bin/bash
echo "Installing Amazon Linux extras"
amazon-linux-extras install epel -y
echo "Install Jenkins stable release"
yum remove -y java
yum install -y java-11-openjdk-devel
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install -y jenkins
chkconfig jenkins on
echo "Install git"
yum install -y git
echo "Setup SSH key"
mkdir /var/lib/jenkins/.ssh
touch /var/lib/jenkins/.ssh/known_hosts
chown -R jenkins:jenkins /var/lib/jenkins/.ssh
chmod 700 /var/lib/jenkins/.ssh
mv /tmp/id_rsa /var/lib/jenkins/.ssh/id_rsa
chmod 600 /var/lib/jenkins/.ssh/id_rsa
chown -R jenkins:jenkins /var/lib/jenkins/.ssh/id_rsa
echo "Configure Jenkins"
mkdir -p /var/lib/jenkins/init.groovy.d
mv /tmp/scripts/*.groovy /var/lib/jenkins/init.groovy.d/
chown -R jenkins:jenkins /var/lib/jenkins/init.groovy.d
mv /tmp/config/jenkins /etc/sysconfig/jenkins
chmod +x /tmp/config/install-plugins.sh
bash /tmp/config/install-plugins.sh
service jenkins start
通过这个link查看所有其他配置文件:
https://github.com/mlabouardy/pipeline-as-code-with-jenkins/tree/master/chapter4/distributed/master
当我运行:
packer build template.json
我得到以下异常:
==> amazon-ebs: Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details.
一切正常,如果我使用:
yum install -y java-1.8.0-openjdk
而不是:
yum install -y java-11-openjdk-devel
我可以启动 Jenkins,但我看到以下警告:
Using Java 8 with the latest Jenkins is not recommended
有谁知道如何使此配置与 Java 11 一起使用?
P.S。 作为 Packer 的源亚马逊机器映像,我使用:
Amazon Linux 2 AMI (HVM), SSD Volume Type - ami-02e136e904f3da870 (64-bit x86) / ami-0e341fcaad89c3650 (64-bit Arm)
它在“us-east-1”区域可用(“source_ami”:“ami-02e136e904f3da870" - Amazon Linux 2 AMI 的确切 ID 取决于地区)
这里是Packer template.json,用于烘焙Jenkins Image 来自Source图片:
{
"variables" : {
"region" : "us-east-1",
"aws_profile": "ops-account",
"source_ami" : "ami-02e136e904f3da870",
"instance_type": "t2.micro",
"ssh_key": "./jenkins_ssh"
},
"builders" : [
{
"type" : "amazon-ebs",
"profile" : "{{user `aws_profile`}}",
"region" : "{{user `region`}}",
"instance_type" : "{{user `instance_type`}}",
"source_ami" : "{{user `source_ami`}}",
"ssh_username" : "ec2-user",
"ami_name" : "jenkins-master-2.204.1",
"ami_description" : "Amazon Linux Image with Jenkins Server",
"run_tags" : {
"Name" : "packer-builder"
}
}
],
"provisioners" : [
{
"type" : "file",
"source" : "./scripts",
"destination" : "/tmp/"
},
{
"type" : "file",
"source" : "./config",
"destination" : "/tmp/"
},
{
"type" : "file",
"source" : "{{user `ssh_key`}}",
"destination" : "/tmp/id_rsa"
},
{
"type" : "shell",
"script" : "./setup.sh",
"execute_command" : "sudo -E -S sh '{{ .Path }}'"
}
]
}
将以下命令行添加到 setup.sh 解决了问题:
amazon-linux-extras install java-openjdk11 -y
看来,java-11-openjdk 软件包在 Amazon 中不可用 Linux 2 个主存储库 安装。
但它们现在 可用 在 Amazon Linux 2 extras 存储库
大多数人在 ec2-user 中安装 jenkins 时遇到问题。
Jenkins Redhat 包 要使用此存储库,运行 以下命令:
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install epel-release ( it will not work so use below command ) sudo amazon-linux-extras install epel
yum install java-11-openjdk-devel -y
yum install java-11-openjdk-devel
(它不会工作所以使用下面的命令)yum search all "java"
之后:
yum install java-11-amazon-corretto.x86_64
java --version
enter image description here
enter image description here
yum install jenkins
systemctl start jenkins
systemctl enable jenkins