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

Posted

技术标签:

【中文标题】使用 Packer 在 Amazon EC2 Linux 2 AMI 上安装 Jenkins 和 JDK 11 失败【英文标题】:Failed to install Jenkins with JDK 11 on Amazon EC2 Linux 2 AMI with Packer 【发布时间】:2021-12-15 21:01:33 【问题描述】:

我尝试使用以下 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 Image 烘焙 Jenkins 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 '"
        
    ]

【问题讨论】:

【参考方案1】:

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

amazon-linux-extras install java-openjdk11 -y

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

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

【讨论】:

以上是关于使用 Packer 在 Amazon EC2 Linux 2 AMI 上安装 Jenkins 和 JDK 11 失败的主要内容,如果未能解决你的问题,请参考以下文章

Amazon 实例 Ec2 连接超时

使用 Ansible playbook 在 AWS (Amazon) ec2 中部署 Play Framework 应用程序

amazon ec2 是个啥东西

Amazon EC2云端服务器的使用方法

在 java 中使用 amazon sdk 创建 amazon ec2 windows AMI

使用“实例存储”停止 Amazon EC2 实例