使用 Packer 在 Amazon EC2 Linux 2 AMI 上安装 Jenkins 和 JDK 11 失败

我尝试使用以下 Packer 配置安装 EC2 实例,预安装 Jenkins:

这里最重要的部分是 setup.sh 脚本,它安装了 JenkinsJava

#!/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

通过此链接查看所有其他配置文件:

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 一起使用?

附言作为 Packer 的源 Amazon 系统映像,我使用:

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,用于烘焙来自 Source ImageJenkins Image

{
    "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 }}'"
        }
    ]
}
stack overflow Failed to install Jenkins with JDK 11 on Amazon EC2 Linux 2 AMI with Packer
原文答案
author avatar

接受的答案

将以下命令行添加到 setup.sh 解决了该问题:

amazon-linux-extras install java-openjdk11 -y

看来,java-11-openjdk 软件包在Amazon Linux 2 主存储库不可用 进行安装。

但它们现在在 Amazon Linux 2 extras 存储库中可用**


答案:

作者头像

大多数人在 ec2-user 中安装 jenkins 时遇到问题。

Jenkins Redhat 软件包 要使用此存储库,请运行以下命令:

  1. sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

  2. sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

  3. yum install epel-release ( it will not work so use below command ) sudo amazon-linux-extras install epel

  4. yum install java-11-openjdk-devel -y yum install java-11-openjdk-devel (它不会工作,所以使用下面的命令)

  5. yum search all "java"

在那之后:

yum install java-11-amazon-corretto.x86_64
java --version

enter image description here

enter image description here

  1. yum install jenkins
  2. systemctl start jenkins
  3. systemctl enable jenkins
作者头像

如果在安装 jdk 11 后启动 jenkins 时仍然遇到错误?检查java版本 java -version

如果它显示 java 1.8.* amazon ami linux ec2 默认附带的东西,我们需要更改为指向 jdk 11 版本。试试下面的命令

替代品 --config java --> 选择 2 有 2 个程序提供“java”。

选择命令

*+ 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.amzn2.0.1.x86_64/jre/bin/java) 2 java -11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.16.0.8-1.amzn2.0.1.x86_64/bin/java)

那么 java -version 应该显示 11 版本。现在安装 jenkins 并启动它应该不是问题。