Fork me on GitHub

Packer之实战Packer创建Jenkins镜像

Packer之实战Packer创建Jenkins镜像

准备内容

  • google cloud账号一个
  • 本地服务器安装Packer,请安装Packer

实践操作

创建Service Account

google云允许你创建并下载一个认证文件,我们可以使用Packer在任何地方使用googlecompute进行构建操作。根据以下步骤获取认证文件:

  • 登录Google Developers Console选择一个项目
  • 点击IAM和管理–服务账号
  • 点击创建服务账号,输入服务账号–>给服务账号授予项目权限(可选)–>给用户授予访问服务账号权限(可选)
  • 最后选择刚创建的服务账号,点击操作部分选择创建密钥下载json或者P12格式密钥文件

编写模板文件

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
# cat jenkins.json
{
"builders": [{
"type": "googlecompute",
"account_file": "account.json",
"project_id": "axiomatic-port-221011",
"source_image_family": "centos-7",
"ssh_username": "packer",
"machine_type": "n1-standard-1",
"image_name": "centos-4-jenkins-{{ timestamp }}", # 自定义images名称,默认为packer-{{ timestamp }}
"zone": "asia-east2-a"
}],
"provisioners": [{
"type": "shell",
"inline": [
"sudo yum update -y",
"sudo yum install -y java-1.8.0-openjdk wget",
"wget https://pkg.jenkins.io/redhat-stable/jenkins.repo",
"sudo cp jenkins.repo /etc/yum.repos.d/jenkins.repo",
"sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key",
"sudo yum install -y jenkins",
"sudo /etc/init.d/jenkins start",
"sudo systemctl enable jenkins"
]
}
]
}

执行构建命令

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# /usr/local/terraform/bin/packer validate jenkins.json   # 执行前检查下语法,避免无用功
Template validated successfully.
# /usr/local/terraform/bin/packer build jenkins.json
googlecompute output will be in this color.

==> googlecompute: Checking image does not exist...
==> googlecompute: Creating temporary SSH key for instance...
==> googlecompute: Using image: centos-7-v20181011 # 选择源镜像
==> googlecompute: Creating instance...
googlecompute: Loading zone: asia-east2-a # 指定的可用区
googlecompute: Loading machine type: n1-standard-1 # 指定的配置型号
googlecompute: Requesting instance creation... # 开始创建实例
googlecompute: Waiting for creation operation to complete...
googlecompute: Instance has been created!
==> googlecompute: Waiting for the instance to become running...
googlecompute: IP: 35.241.127.139
==> googlecompute: Using ssh communicator to connect: 35.241.127.139
==> googlecompute: Waiting for SSH to become available...
==> googlecompute: Connected to SSH!
==> googlecompute: Provisioning with shell script: /tmp/packer-shell044651127 # 开始进行自定义软件安装及配置
googlecompute: Loaded plugins: fastestmirror
googlecompute: Determining fastest mirrors
googlecompute: * base: mirror.fileplanet.com
googlecompute: * epel: d2lzkl7pfhq30w.cloudfront.net
googlecompute: * extras: mirror.fileplanet.com
googlecompute: * updates: mirror.fileplanet.com
googlecompute: Resolving Dependencies
googlecompute: --> Running transaction check
googlecompute: ---> Package google-cloud-sdk.noarch 0:220.0.0-1.el7 will be updated
googlecompute: ---> Package google-cloud-sdk.noarch 0:223.0.0-1.el7 will be an update
googlecompute: ---> Package google-compute-engine.noarch 0:2.8.6-1.el7 will be updated
googlecompute: ---> Package google-compute-engine.noarch 0:2.8.8-1.el7 will be an update
googlecompute: ---> Package python-google-compute-engine.noarch 0:2.8.6-1.el7 will be updated
googlecompute: ---> Package python-google-compute-engine.noarch 0:2.8.8-1.el7 will be an update
googlecompute: ---> Package tzdata.noarch 0:2018e-3.el7 will be updated
googlecompute: ---> Package tzdata.noarch 0:2018f-2.el7 will be an update
googlecompute: --> Finished Dependency Resolution
googlecompute:
googlecompute: Dependencies Resolved
googlecompute:
googlecompute: ================================================================================
googlecompute: Package Arch Version Repository Size
googlecompute: ================================================================================
googlecompute: Updating:
googlecompute: google-cloud-sdk noarch 223.0.0-1.el7 google-cloud-sdk 29 M
googlecompute: google-compute-engine noarch 2.8.8-1.el7 google-cloud-compute 16 k
googlecompute: python-google-compute-engine noarch 2.8.8-1.el7 google-cloud-compute 100 k
googlecompute: tzdata noarch 2018f-2.el7 updates 484 k
googlecompute:
googlecompute: Transaction Summary
googlecompute: ================================================================================
googlecompute: Upgrade 4 Packages
googlecompute:
googlecompute: Total download size: 29 M
googlecompute: Downloading packages:
googlecompute: Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
googlecompute: --------------------------------------------------------------------------------
googlecompute: Total 21 MB/s | 29 MB 00:01
googlecompute: Running transaction check
googlecompute: Running transaction test
googlecompute: Transaction test succeeded
googlecompute: Running transaction
googlecompute: Updating : python-google-compute-engine-2.8.8-1.el7.noarch 1/8
googlecompute: Updating : google-compute-engine-2.8.8-1.el7.noarch 2/8
googlecompute: Updating : tzdata-2018f-2.el7.noarch 3/8
googlecompute: Updating : google-cloud-sdk-223.0.0-1.el7.noarch 4/8
googlecompute: Cleanup : google-compute-engine-2.8.6-1.el7.noarch 5/8
googlecompute: Cleanup : python-google-compute-engine-2.8.6-1.el7.noarch 6/8
googlecompute: Cleanup : tzdata-2018e-3.el7.noarch 7/8
googlecompute: Cleanup : google-cloud-sdk-220.0.0-1.el7.noarch 8/8
googlecompute: Verifying : google-compute-engine-2.8.8-1.el7.noarch 1/8
googlecompute: Verifying : python-google-compute-engine-2.8.8-1.el7.noarch 2/8
googlecompute: Verifying : google-cloud-sdk-223.0.0-1.el7.noarch 3/8
googlecompute: Verifying : tzdata-2018f-2.el7.noarch 4/8
googlecompute: Verifying : google-cloud-sdk-220.0.0-1.el7.noarch 5/8
googlecompute: Verifying : python-google-compute-engine-2.8.6-1.el7.noarch 6/8
googlecompute: Verifying : tzdata-2018e-3.el7.noarch 7/8
googlecompute: Verifying : google-compute-engine-2.8.6-1.el7.noarch 8/8
googlecompute:
googlecompute: Updated:
googlecompute: google-cloud-sdk.noarch 0:223.0.0-1.el7
googlecompute: google-compute-engine.noarch 0:2.8.8-1.el7
googlecompute: python-google-compute-engine.noarch 0:2.8.8-1.el7
googlecompute: tzdata.noarch 0:2018f-2.el7
googlecompute:
googlecompute: Complete!
googlecompute: Loaded plugins: fastestmirror
googlecompute: Loading mirror speeds from cached hostfile
googlecompute: * base: mirror.fileplanet.com
googlecompute: * epel: d2lzkl7pfhq30w.cloudfront.net
googlecompute: * extras: mirror.fileplanet.com
googlecompute: * updates: mirror.fileplanet.com
googlecompute: Resolving Dependencies
googlecompute: --> Running transaction check
googlecompute: ---> Package java-1.8.0-openjdk.x86_64 1:1.8.0.191.b12-0.el7_5 will be installed
googlecompute: --> Processing Dependency: java-1.8.0-openjdk-headless(x86-64) = 1:1.8.0.191.b12-0.el7_5 for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: xorg-x11-fonts-Type1 for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libpng15.so.15(PNG15_0)(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libjvm.so(SUNWprivate_1.1)(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libjava.so(SUNWprivate_1.1)(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: fontconfig(x86-64) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libpng15.so.15()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libjvm.so()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libjpeg.so.62()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libjava.so()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libgif.so.4()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libXtst.so.6()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libXrender.so.1()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libXi.so.6()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libXext.so.6()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libXcomposite.so.1()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: libX11.so.6()(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: ---> Package wget.x86_64 0:1.14-15.el7_4.1 will be installed
googlecompute: --> Running transaction check
googlecompute: ---> Package fontconfig.x86_64 0:2.10.95-11.el7 will be installed
googlecompute: --> Processing Dependency: fontpackages-filesystem for package: fontconfig-2.10.95-11.el7.x86_64
googlecompute: --> Processing Dependency: font(:lang=en) for package: fontconfig-2.10.95-11.el7.x86_64
googlecompute: ---> Package giflib.x86_64 0:4.1.6-9.el7 will be installed
googlecompute: --> Processing Dependency: libSM.so.6()(64bit) for package: giflib-4.1.6-9.el7.x86_64
googlecompute: --> Processing Dependency: libICE.so.6()(64bit) for package: giflib-4.1.6-9.el7.x86_64
googlecompute: ---> Package java-1.8.0-openjdk-headless.x86_64 1:1.8.0.191.b12-0.el7_5 will be installed
googlecompute: --> Processing Dependency: tzdata-java >= 2015d for package: 1:java-1.8.0-openjdk-headless-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: copy-jdk-configs >= 2.2 for package: 1:java-1.8.0-openjdk-headless-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: lksctp-tools(x86-64) for package: 1:java-1.8.0-openjdk-headless-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: --> Processing Dependency: jpackage-utils for package: 1:java-1.8.0-openjdk-headless-1.8.0.191.b12-0.el7_5.x86_64
googlecompute: ---> Package libX11.x86_64 0:1.6.5-1.el7 will be installed
googlecompute: --> Processing Dependency: libX11-common >= 1.6.5-1.el7 for package: libX11-1.6.5-1.el7.x86_64
googlecompute: --> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.6.5-1.el7.x86_64
googlecompute: ---> Package libXcomposite.x86_64 0:0.4.4-4.1.el7 will be installed
googlecompute: ---> Package libXext.x86_64 0:1.3.3-3.el7 will be installed
googlecompute: ---> Package libXi.x86_64 0:1.7.9-1.el7 will be installed
googlecompute: ---> Package libXrender.x86_64 0:0.9.10-1.el7 will be installed
googlecompute: ---> Package libXtst.x86_64 0:1.2.3-1.el7 will be installed
googlecompute: ---> Package libjpeg-turbo.x86_64 0:1.2.90-5.el7 will be installed
googlecompute: ---> Package libpng.x86_64 2:1.5.13-7.el7_2 will be installed
googlecompute: ---> Package xorg-x11-fonts-Type1.noarch 0:7.5-9.el7 will be installed
googlecompute: --> Processing Dependency: ttmkfdir for package: xorg-x11-fonts-Type1-7.5-9.el7.noarch
googlecompute: --> Processing Dependency: ttmkfdir for package: xorg-x11-fonts-Type1-7.5-9.el7.noarch
googlecompute: --> Processing Dependency: mkfontdir for package: xorg-x11-fonts-Type1-7.5-9.el7.noarch
googlecompute: --> Processing Dependency: mkfontdir for package: xorg-x11-fonts-Type1-7.5-9.el7.noarch
googlecompute: --> Running transaction check
googlecompute: ---> Package copy-jdk-configs.noarch 0:3.3-10.el7_5 will be installed
googlecompute: ---> Package fontpackages-filesystem.noarch 0:1.44-8.el7 will be installed
googlecompute: ---> Package javapackages-tools.noarch 0:3.4.1-11.el7 will be installed
googlecompute: --> Processing Dependency: python-javapackages = 3.4.1-11.el7 for package: javapackages-tools-3.4.1-11.el7.noarch
googlecompute: --> Processing Dependency: libxslt for package: javapackages-tools-3.4.1-11.el7.noarch
googlecompute: ---> Package libICE.x86_64 0:1.0.9-9.el7 will be installed
googlecompute: ---> Package libSM.x86_64 0:1.2.2-2.el7 will be installed
googlecompute: ---> Package libX11-common.noarch 0:1.6.5-1.el7 will be installed
googlecompute: ---> Package libxcb.x86_64 0:1.12-1.el7 will be installed
googlecompute: --> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.12-1.el7.x86_64
googlecompute: ---> Package lksctp-tools.x86_64 0:1.0.17-2.el7 will be installed
googlecompute: ---> Package lyx-fonts.noarch 0:2.2.3-1.el7 will be installed
googlecompute: ---> Package ttmkfdir.x86_64 0:3.0.9-42.el7 will be installed
googlecompute: ---> Package tzdata-java.noarch 0:2018f-2.el7 will be installed
googlecompute: ---> Package xorg-x11-font-utils.x86_64 1:7.5-20.el7 will be installed
googlecompute: --> Processing Dependency: libfontenc.so.1()(64bit) for package: 1:xorg-x11-font-utils-7.5-20.el7.x86_64
googlecompute: --> Processing Dependency: libXfont.so.1()(64bit) for package: 1:xorg-x11-font-utils-7.5-20.el7.x86_64
googlecompute: --> Running transaction check
googlecompute: ---> Package libXau.x86_64 0:1.0.8-2.1.el7 will be installed
googlecompute: ---> Package libXfont.x86_64 0:1.5.2-1.el7 will be installed
googlecompute: ---> Package libfontenc.x86_64 0:1.1.3-3.el7 will be installed
googlecompute: ---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed
googlecompute: ---> Package python-javapackages.noarch 0:3.4.1-11.el7 will be installed
googlecompute: --> Processing Dependency: python-lxml for package: python-javapackages-3.4.1-11.el7.noarch
googlecompute: --> Running transaction check
googlecompute: ---> Package python-lxml.x86_64 0:3.2.1-4.el7 will be installed
googlecompute: --> Finished Dependency Resolution
googlecompute:
googlecompute: Dependencies Resolved
googlecompute:
googlecompute: ================================================================================
googlecompute: Package Arch Version Repository
googlecompute: Size
googlecompute: ================================================================================
googlecompute: Installing:
googlecompute: java-1.8.0-openjdk x86_64 1:1.8.0.191.b12-0.el7_5 updates 252 k
googlecompute: wget x86_64 1.14-15.el7_4.1 base 547 k
googlecompute: Installing for dependencies:
googlecompute: copy-jdk-configs noarch 3.3-10.el7_5 updates 21 k
googlecompute: fontconfig x86_64 2.10.95-11.el7 base 229 k
googlecompute: fontpackages-filesystem noarch 1.44-8.el7 base 9.9 k
googlecompute: giflib x86_64 4.1.6-9.el7 base 40 k
googlecompute: java-1.8.0-openjdk-headless x86_64 1:1.8.0.191.b12-0.el7_5 updates 32 M
googlecompute: javapackages-tools noarch 3.4.1-11.el7 base 73 k
googlecompute: libICE x86_64 1.0.9-9.el7 base 66 k
googlecompute: libSM x86_64 1.2.2-2.el7 base 39 k
googlecompute: libX11 x86_64 1.6.5-1.el7 base 606 k
googlecompute: libX11-common noarch 1.6.5-1.el7 base 164 k
googlecompute: libXau x86_64 1.0.8-2.1.el7 base 29 k
googlecompute: libXcomposite x86_64 0.4.4-4.1.el7 base 22 k
googlecompute: libXext x86_64 1.3.3-3.el7 base 39 k
googlecompute: libXfont x86_64 1.5.2-1.el7 base 152 k
googlecompute: libXi x86_64 1.7.9-1.el7 base 40 k
googlecompute: libXrender x86_64 0.9.10-1.el7 base 26 k
googlecompute: libXtst x86_64 1.2.3-1.el7 base 20 k
googlecompute: libfontenc x86_64 1.1.3-3.el7 base 31 k
googlecompute: libjpeg-turbo x86_64 1.2.90-5.el7 base 134 k
googlecompute: libpng x86_64 2:1.5.13-7.el7_2 base 213 k
googlecompute: libxcb x86_64 1.12-1.el7 base 211 k
googlecompute: libxslt x86_64 1.1.28-5.el7 base 242 k
googlecompute: lksctp-tools x86_64 1.0.17-2.el7 base 88 k
googlecompute: lyx-fonts noarch 2.2.3-1.el7 epel 159 k
googlecompute: python-javapackages noarch 3.4.1-11.el7 base 31 k
googlecompute: python-lxml x86_64 3.2.1-4.el7 base 758 k
googlecompute: ttmkfdir x86_64 3.0.9-42.el7 base 48 k
googlecompute: tzdata-java noarch 2018f-2.el7 updates 186 k
googlecompute: xorg-x11-font-utils x86_64 1:7.5-20.el7 base 87 k
googlecompute: xorg-x11-fonts-Type1 noarch 7.5-9.el7 base 521 k
googlecompute:
googlecompute: Transaction Summary
googlecompute: ================================================================================
googlecompute: Install 2 Packages (+30 Dependent packages)
googlecompute:
googlecompute: Total download size: 37 M
googlecompute: Installed size: 117 M
googlecompute: Downloading packages:
googlecompute: --------------------------------------------------------------------------------
googlecompute: Total 7.2 MB/s | 37 MB 00:05
googlecompute: Running transaction check
googlecompute: Running transaction test
googlecompute: Transaction test succeeded
googlecompute: Running transaction
googlecompute: Installing : libfontenc-1.1.3-3.el7.x86_64 1/32
googlecompute: Installing : libxslt-1.1.28-5.el7.x86_64 2/32
googlecompute: Installing : fontpackages-filesystem-1.44-8.el7.noarch 3/32
googlecompute: Installing : libjpeg-turbo-1.2.90-5.el7.x86_64 4/32
googlecompute: Installing : libICE-1.0.9-9.el7.x86_64 5/32
googlecompute: Installing : libSM-1.2.2-2.el7.x86_64 6/32
googlecompute: Installing : lyx-fonts-2.2.3-1.el7.noarch 7/32
googlecompute: Installing : fontconfig-2.10.95-11.el7.x86_64 8/32
googlecompute: Installing : python-lxml-3.2.1-4.el7.x86_64 9/32
googlecompute: Installing : python-javapackages-3.4.1-11.el7.noarch 10/32
googlecompute: Installing : javapackages-tools-3.4.1-11.el7.noarch 11/32
googlecompute: Installing : libXfont-1.5.2-1.el7.x86_64 12/32
googlecompute: Installing : 1:xorg-x11-font-utils-7.5-20.el7.x86_64 13/32
googlecompute: Installing : libXau-1.0.8-2.1.el7.x86_64 14/32
googlecompute: Installing : libxcb-1.12-1.el7.x86_64 15/32
googlecompute: Installing : libX11-common-1.6.5-1.el7.noarch 16/32
googlecompute: Installing : libX11-1.6.5-1.el7.x86_64 17/32
googlecompute: Installing : libXext-1.3.3-3.el7.x86_64 18/32
googlecompute: Installing : libXi-1.7.9-1.el7.x86_64 19/32
googlecompute: Installing : libXtst-1.2.3-1.el7.x86_64 20/32
googlecompute: Installing : giflib-4.1.6-9.el7.x86_64 21/32
googlecompute: Installing : libXrender-0.9.10-1.el7.x86_64 22/32
googlecompute: Installing : libXcomposite-0.4.4-4.1.el7.x86_64 23/32
googlecompute: Installing : lksctp-tools-1.0.17-2.el7.x86_64 24/32
googlecompute: Installing : tzdata-java-2018f-2.el7.noarch 25/32
googlecompute: Installing : copy-jdk-configs-3.3-10.el7_5.noarch 26/32
googlecompute: Installing : 1:java-1.8.0-openjdk-headless-1.8.0.191.b12-0.el7_5.x86_ 27/32
googlecompute: Installing : ttmkfdir-3.0.9-42.el7.x86_64 28/32
googlecompute: Installing : xorg-x11-fonts-Type1-7.5-9.el7.noarch 29/32
googlecompute: Installing : 2:libpng-1.5.13-7.el7_2.x86_64 30/32
googlecompute: Installing : 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64 31/32
googlecompute: Installing : wget-1.14-15.el7_4.1.x86_64 32/32
googlecompute: Verifying : libXext-1.3.3-3.el7.x86_64 1/32
googlecompute: Verifying : libX11-1.6.5-1.el7.x86_64 2/32
googlecompute: Verifying : libXi-1.7.9-1.el7.x86_64 3/32
googlecompute: Verifying : libICE-1.0.9-9.el7.x86_64 4/32
googlecompute: Verifying : giflib-4.1.6-9.el7.x86_64 5/32
googlecompute: Verifying : libjpeg-turbo-1.2.90-5.el7.x86_64 6/32
googlecompute: Verifying : libXrender-0.9.10-1.el7.x86_64 7/32
googlecompute: Verifying : lyx-fonts-2.2.3-1.el7.noarch 8/32
googlecompute: Verifying : libXfont-1.5.2-1.el7.x86_64 9/32
googlecompute: Verifying : python-lxml-3.2.1-4.el7.x86_64 10/32
googlecompute: Verifying : 2:libpng-1.5.13-7.el7_2.x86_64 11/32
googlecompute: Verifying : fontpackages-filesystem-1.44-8.el7.noarch 12/32
googlecompute: Verifying : ttmkfdir-3.0.9-42.el7.x86_64 13/32
googlecompute: Verifying : 1:java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64 14/32
googlecompute: Verifying : copy-jdk-configs-3.3-10.el7_5.noarch 15/32
googlecompute: Verifying : python-javapackages-3.4.1-11.el7.noarch 16/32
googlecompute: Verifying : 1:java-1.8.0-openjdk-headless-1.8.0.191.b12-0.el7_5.x86_ 17/32
googlecompute: Verifying : libXcomposite-0.4.4-4.1.el7.x86_64 18/32
googlecompute: Verifying : libXtst-1.2.3-1.el7.x86_64 19/32
googlecompute: Verifying : tzdata-java-2018f-2.el7.noarch 20/32
googlecompute: Verifying : lksctp-tools-1.0.17-2.el7.x86_64 21/32
googlecompute: Verifying : libxcb-1.12-1.el7.x86_64 22/32
googlecompute: Verifying : wget-1.14-15.el7_4.1.x86_64 23/32
googlecompute: Verifying : libxslt-1.1.28-5.el7.x86_64 24/32
googlecompute: Verifying : xorg-x11-fonts-Type1-7.5-9.el7.noarch 25/32
googlecompute: Verifying : javapackages-tools-3.4.1-11.el7.noarch 26/32
googlecompute: Verifying : libX11-common-1.6.5-1.el7.noarch 27/32
googlecompute: Verifying : libXau-1.0.8-2.1.el7.x86_64 28/32
googlecompute: Verifying : libSM-1.2.2-2.el7.x86_64 29/32
googlecompute: Verifying : fontconfig-2.10.95-11.el7.x86_64 30/32
googlecompute: Verifying : libfontenc-1.1.3-3.el7.x86_64 31/32
googlecompute: Verifying : 1:xorg-x11-font-utils-7.5-20.el7.x86_64 32/32
googlecompute:
googlecompute: Installed:
googlecompute: java-1.8.0-openjdk.x86_64 1:1.8.0.191.b12-0.el7_5
googlecompute: wget.x86_64 0:1.14-15.el7_4.1
googlecompute:
googlecompute: Dependency Installed:
googlecompute: copy-jdk-configs.noarch 0:3.3-10.el7_5
googlecompute: fontconfig.x86_64 0:2.10.95-11.el7
googlecompute: fontpackages-filesystem.noarch 0:1.44-8.el7
googlecompute: giflib.x86_64 0:4.1.6-9.el7
googlecompute: java-1.8.0-openjdk-headless.x86_64 1:1.8.0.191.b12-0.el7_5
googlecompute: javapackages-tools.noarch 0:3.4.1-11.el7
googlecompute: libICE.x86_64 0:1.0.9-9.el7
googlecompute: libSM.x86_64 0:1.2.2-2.el7
googlecompute: libX11.x86_64 0:1.6.5-1.el7
googlecompute: libX11-common.noarch 0:1.6.5-1.el7
googlecompute: libXau.x86_64 0:1.0.8-2.1.el7
googlecompute: libXcomposite.x86_64 0:0.4.4-4.1.el7
googlecompute: libXext.x86_64 0:1.3.3-3.el7
googlecompute: libXfont.x86_64 0:1.5.2-1.el7
googlecompute: libXi.x86_64 0:1.7.9-1.el7
googlecompute: libXrender.x86_64 0:0.9.10-1.el7
googlecompute: libXtst.x86_64 0:1.2.3-1.el7
googlecompute: libfontenc.x86_64 0:1.1.3-3.el7
googlecompute: libjpeg-turbo.x86_64 0:1.2.90-5.el7
googlecompute: libpng.x86_64 2:1.5.13-7.el7_2
googlecompute: libxcb.x86_64 0:1.12-1.el7
googlecompute: libxslt.x86_64 0:1.1.28-5.el7
googlecompute: lksctp-tools.x86_64 0:1.0.17-2.el7
googlecompute: lyx-fonts.noarch 0:2.2.3-1.el7
googlecompute: python-javapackages.noarch 0:3.4.1-11.el7
googlecompute: python-lxml.x86_64 0:3.2.1-4.el7
googlecompute: ttmkfdir.x86_64 0:3.0.9-42.el7
googlecompute: tzdata-java.noarch 0:2018f-2.el7
googlecompute: xorg-x11-font-utils.x86_64 1:7.5-20.el7
googlecompute: xorg-x11-fonts-Type1.noarch 0:7.5-9.el7
googlecompute:
googlecompute: Complete!
googlecompute: --2018-11-06 07:42:41-- https://pkg.jenkins.io/redhat-stable/jenkins.repo
googlecompute: Resolving pkg.jenkins.io (pkg.jenkins.io)... 52.202.51.185
googlecompute: Connecting to pkg.jenkins.io (pkg.jenkins.io)|52.202.51.185|:443... connected.
googlecompute: HTTP request sent, awaiting response... 200 OK
googlecompute: Length: 85
googlecompute: Saving to: ‘jenkins.repo’
googlecompute:
googlecompute: 0K 100% 3.16M=0s
googlecompute:
googlecompute: 2018-11-06 07:42:42 (3.16 MB/s) - ‘jenkins.repo’ saved [85/85]
googlecompute:
googlecompute: Loaded plugins: fastestmirror
googlecompute: Loading mirror speeds from cached hostfile
googlecompute: * base: mirror.fileplanet.com
googlecompute: * epel: d2lzkl7pfhq30w.cloudfront.net
googlecompute: * extras: mirror.fileplanet.com
googlecompute: * updates: mirror.fileplanet.com
googlecompute: Resolving Dependencies
googlecompute: --> Running transaction check
googlecompute: ---> Package jenkins.noarch 0:2.138.2-1.1 will be installed
googlecompute: --> Finished Dependency Resolution
googlecompute:
googlecompute: Dependencies Resolved
googlecompute:
googlecompute: ================================================================================
googlecompute: Package Arch Version Repository Size
googlecompute: ================================================================================
googlecompute: Installing:
googlecompute: jenkins noarch 2.138.2-1.1 jenkins 72 M
googlecompute:
googlecompute: Transaction Summary
googlecompute: ================================================================================
googlecompute: Install 1 Package
googlecompute:
googlecompute: Total download size: 72 M
googlecompute: Installed size: 72 M
googlecompute: Downloading packages:
googlecompute: Running transaction check
googlecompute: Running transaction test
googlecompute: Transaction test succeeded
googlecompute: Running transaction
googlecompute: Installing : jenkins-2.138.2-1.1.noarch 1/1
googlecompute: Verifying : jenkins-2.138.2-1.1.noarch 1/1
googlecompute:
googlecompute: Installed:
googlecompute: jenkins.noarch 0:2.138.2-1.1
googlecompute:
googlecompute: Complete!
googlecompute: Starting jenkins (via systemctl): [ OK ]
googlecompute: jenkins.service is not a native service, redirecting to /sbin/chkconfig.
googlecompute: Executing /sbin/chkconfig jenkins on
==> googlecompute: Deleting instance...
googlecompute: Instance has been deleted!
==> googlecompute: Creating image...
==> googlecompute: Deleting disk...
googlecompute: Disk has been deleted!
Build 'googlecompute' finished.

==> Builds finished. The artifacts of successful builds are:
--> googlecompute: A disk image was created: centos-4-jenkins-1541491568 # 输出镜像名称

验证镜像可用性

利用terraform调用刚刚生成的镜像创建Jenkins服务

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# cat main.tf
resource "google_compute_instance" "vm_instance" {
name = "jenkins-instance"
machine_type = "${var.machine_type}"
tags = ["instance"]

boot_disk {
initialize_params {
image = "${var.image}"
}
}

network_interface {
# A default network is created for all GCP projects
network = "${google_compute_network.vpc_network.self_link}"
access_config = {
}
}
}

#-----------------------------------------------------------------------
resource "google_compute_network" "vpc_network" {
name = "terraform-network"
auto_create_subnetworks = "true"
}

#-----------------------------------------------------------------------
resource "google_compute_firewall" "vpc_firewall" {
name = "terraform-firewall"
network = "${google_compute_network.vpc_network.name}"

allow {
protocol = "icmp"
}

allow {
protocol = "tcp"
ports = ["22", "80", "443", "3389", "8080"]
}

source_ranges = ["0.0.0.0/0"]
source_tags = ["web"]
}
#-----------------------------------------------------------------------
output "lan_ip" {
value = "${google_compute_instance.vm_instance.network_interface.0.address}"
}
output "wan_ip" {
value = "${google_compute_instance.vm_instance.network_interface.0.access_config.0.nat_ip}"
}

# terraform apply
google_compute_network.vpc_network: Refreshing state... (ID: terraform-network)
google_compute_firewall.vpc_firewall: Refreshing state... (ID: terraform-firewall)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

+ google_compute_instance.vm_instance
id: <computed>
boot_disk.#: "1"
boot_disk.0.auto_delete: "true"
boot_disk.0.device_name: <computed>
boot_disk.0.disk_encryption_key_sha256: <computed>
boot_disk.0.initialize_params.#: "1"
boot_disk.0.initialize_params.0.image: "centos-4-jenkins-1541491568"
boot_disk.0.initialize_params.0.size: <computed>
boot_disk.0.initialize_params.0.type: <computed>
can_ip_forward: "false"
cpu_platform: <computed>
create_timeout: "4"
deletion_protection: "false"
guest_accelerator.#: <computed>
instance_id: <computed>
label_fingerprint: <computed>
machine_type: "n1-standard-1"
metadata_fingerprint: <computed>
name: "jenkins-instance"
network_interface.#: "1"
network_interface.0.access_config.#: "1"
network_interface.0.access_config.0.assigned_nat_ip: <computed>
network_interface.0.access_config.0.nat_ip: <computed>
network_interface.0.access_config.0.network_tier: <computed>
network_interface.0.address: <computed>
network_interface.0.name: <computed>
network_interface.0.network: "https://www.googleapis.com/compute/v1/projects/axiomatic-port-221011/global/networks/terraform-network"
network_interface.0.network_ip: <computed>
network_interface.0.subnetwork_project: <computed>
project: <computed>
scheduling.#: <computed>
self_link: <computed>
tags.#: "1"
tags.1110487518: "instance"
tags_fingerprint: <computed>
zone: <computed>


Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

google_compute_instance.vm_instance: Creating...
boot_disk.#: "" => "1"
boot_disk.0.auto_delete: "" => "true"
boot_disk.0.device_name: "" => "<computed>"
boot_disk.0.disk_encryption_key_sha256: "" => "<computed>"
boot_disk.0.initialize_params.#: "" => "1"
boot_disk.0.initialize_params.0.image: "" => "centos-4-jenkins-1541491568"
boot_disk.0.initialize_params.0.size: "" => "<computed>"
boot_disk.0.initialize_params.0.type: "" => "<computed>"
can_ip_forward: "" => "false"
cpu_platform: "" => "<computed>"
create_timeout: "" => "4"
deletion_protection: "" => "false"
guest_accelerator.#: "" => "<computed>"
instance_id: "" => "<computed>"
label_fingerprint: "" => "<computed>"
machine_type: "" => "n1-standard-1"
metadata_fingerprint: "" => "<computed>"
name: "" => "jenkins-instance"
network_interface.#: "" => "1"
network_interface.0.access_config.#: "" => "1"
network_interface.0.access_config.0.assigned_nat_ip: "" => "<computed>"
network_interface.0.access_config.0.nat_ip: "" => "<computed>"
network_interface.0.access_config.0.network_tier: "" => "<computed>"
network_interface.0.address: "" => "<computed>"
network_interface.0.name: "" => "<computed>"
network_interface.0.network: "" => "https://www.googleapis.com/compute/v1/projects/axiomatic-port-221011/global/networks/terraform-network"
network_interface.0.network_ip: "" => "<computed>"
network_interface.0.subnetwork_project: "" => "<computed>"
project: "" => "<computed>"
scheduling.#: "" => "<computed>"
self_link: "" => "<computed>"
tags.#: "" => "1"
tags.1110487518: "" => "instance"
tags_fingerprint: "" => "<computed>"
zone: "" => "<computed>"
google_compute_instance.vm_instance: Still creating... (10s elapsed)
google_compute_instance.vm_instance: Still creating... (20s elapsed)
google_compute_instance.vm_instance: Creation complete after 29s (ID: jenkins-instance)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

lan_ip = 10.170.0.2
wan_ip = 35.241.127.139

访问服务:http://35.241.127.139:8080/
可以在镜像制作过程中直接将jenkins服务配置更完善,这边直接应用服务

验证完毕,销毁资源

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
terraform destroy
google_compute_network.vpc_network: Refreshing state... (ID: terraform-network)
google_compute_firewall.vpc_firewall: Refreshing state... (ID: terraform-firewall)
google_compute_instance.vm_instance: Refreshing state... (ID: jenkins-instance)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy

Terraform will perform the following actions:

- google_compute_firewall.vpc_firewall

- google_compute_instance.vm_instance

- google_compute_network.vpc_network


Plan: 0 to add, 0 to change, 3 to destroy.

Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.

Enter a value: yes

google_compute_firewall.vpc_firewall: Destroying... (ID: terraform-firewall)
google_compute_instance.vm_instance: Destroying... (ID: jenkins-instance)
google_compute_firewall.vpc_firewall: Still destroying... (ID: terraform-firewall, 10s elapsed)
google_compute_instance.vm_instance: Still destroying... (ID: jenkins-instance, 10s elapsed)
google_compute_firewall.vpc_firewall: Destruction complete after 11s
google_compute_instance.vm_instance: Still destroying... (ID: jenkins-instance, 20s elapsed)
google_compute_instance.vm_instance: Destruction complete after 26s
google_compute_network.vpc_network: Destroying... (ID: terraform-network)
google_compute_network.vpc_network: Still destroying... (ID: terraform-network, 10s elapsed)
google_compute_network.vpc_network: Still destroying... (ID: terraform-network, 20s elapsed)
google_compute_network.vpc_network: Still destroying... (ID: terraform-network, 30s elapsed)
google_compute_network.vpc_network: Still destroying... (ID: terraform-network, 40s elapsed)
google_compute_network.vpc_network: Still destroying... (ID: terraform-network, 50s elapsed)
google_compute_network.vpc_network: Still destroying... (ID: terraform-network, 1m0s elapsed)
google_compute_network.vpc_network: Destruction complete after 1m9s

Destroy complete! Resources: 3 destroyed.

参考资料

======================================================
希望各位朋友支持一下

本文作者:dongsheng
本文地址https://mds1455975151.github.io/archives/6a8f9b85.html
版权声明:转载请注明出处!

坚持技术分享,您的支持将鼓励我继续创作!