自定义创建阿里云镜像

阿里云

网址查看阿里云的AccessKey和AccessKey Secret

Packer

Packer

Packer_阿里文档

Packer_创建镜像

创建镜像

查看创建后上传的镜像
管理控制台 –> 云服务器ECS –> 快照和镜像 –> 镜像

Windows(PowerShell为例)

下载Packer Win x64
Windows_64-bit

添加环境变量
Advanced system settings --> Avanced --> Enviroment Variables... --> System... --> Path --> C:\Program Files\Packer

PowerShell查看版本
packer.exe -v

PowerShell添加临时变量

1
2
$env:ALICLOUD_ACCESS_KEY="Access Key ID"
$env:ALICLOUD_SECRET_KEY="Access Key Secret"

新建alicloud.json,放到packer.exe同级目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
"variables": {
"access_key": "{{env `Access Key ID`}}",
"secret_key": "{{env `Access Key Secret`}}"
},
"builders": [
{
"type":"alicloud-ecs",
"access_key":"{{user `Access Key ID`}}",
"secret_key":"{{user `Access Key Secret`}}",
"region":"cn-beijing",
"image_name":"Packer_test",
"source_image":"centos_7_04_64_20G_alibase_201701015.vhd",
"ssh_username":"root",
"instance_type":"ecs.n1.tiny",
"io_optimized":"true",
"image_force_delete":"true"
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"date +'%T'",
"yum install redis.x86_64 -y"
]
}
]
}

之后创建自定义镜像
packer.exe build alicloud.json

Linux

下载Packer Linux x64
Linux_64-bit

添加临时变量
vi ~/.bashrc

1
2
3
## 添加
export ALICLOUD_ACCESS_KEY="Access Key ID"
export ALICLOUD_SECRET_KEY="Access Key Secret"

或者直接在终端

1
2
export ALICLOUD_ACCESS_KEY="Access Key ID"
export ALICLOUD_SECRET_KEY="Access Key Secret"

创建welcome.txt

1
Welcome to use WordPress_Img!

创建create_database.sh
chmod +x create_database.sh

1
2
3
4
5
6
7
8
9
10
11
12
## Shell创建数据库
#!/bin/bash
echo ">>>>>>>>>>>>>> Create Wordpress db <<<<<<<<<<<<<<"

HOSTNAME="localhost"
PORT="3306"
USERNAME="root"
PASSWORD=""
DBNAME="wordpress"

create_db_sql="create database IF NOT EXISTS ${DBNAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}"

新建alicloud.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{
"variables": {
"access_key": "{{env `Access Key ID`}}",
"secret_key": "{{env `Access Key Secret`}}"
},
"builders": [
{
"type":"alicloud-ecs",
"access_key":"{{user `Access Key ID`}}",
"secret_key":"{{user `Access Key Secret`}}",
"region":"cn-beijing",
"image_name":"test_img",
"source_image":"centos_7_04_64_20G_alibase_201701015.vhd",
"ssh_username":"root",
"instance_type":"ecs.n1.tiny",
"io_optimized":"true",
"image_force_delete":"true"
}
],
"provisioners": [
{
"type": "file",
"source": "./welcome.txt", # 本地的文件,之后会上传到镜像里去
"destination": "/root/" # 镜像的里的路径(因为是root,所以文件夹是/root/)
},
{
"type": "shell",
"inline": [
"ls -la /root/",
"cat /root/welcome.txt"
]
},
{
"type": "shell",
"inline": [
"echo '>>>>>>>>>>>>>>>>>>> Start <<<<<<<<<<<<<<<<<<'",
"date +'%T'",
"rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm",
"yum install mysql mysql-server mysql-libs mysql-server -y",
"systemctl enable httpd.service",
"systemctl enable mysqld.service",
"yum install php -y",
"yum install php-mysql -y",
"yum install php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc -y",
"yum install zip unzip -y",
"wget https://cn.wordpress.org/wordpress-5.0-zh_CN.zip && unzip wordpress-5.0-zh_CN.zip",
"cp -rf wordpress/* /var/www/html",
"chmod 777 /var/www/html/wp-content/",
"systemctl start mysqld.service",
"echo '>>>>>>>>>>>>>>>>>>> End <<<<<<<<<<<<<<<<<<'",
"date +'%T'"
]
},
{
"type": "shell",
"script": "./create_database.sh"
}
]
}

创建镜像
packer build alicloud.json

0%