Jenkinsfile
15.6 KB
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
pipeline {
agent {
label 'jenkins-agent'
}
environment {
CLUSTER_NAME = "comic-website-prod"
AWS_REGION = "us-east-1"
NAMESPACE = "logging"
DOMAIN_NAME = "awsmpc.asia"
KIBANA_DOMAIN = "kibana.awsmpc.asia"
TF_STATE_BUCKET = "terraformstatefile090909"
TF_LOCK_TABLE = "terraform-locks"
}
options {
timeout(time: 1, unit: 'HOURS')
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '10'))
}
parameters {
choice(
name: 'DEPLOYMENT_TYPE',
choices: ['full', 'infrastructure-only', 'elk-only', 'dns-only'],
description: 'Select deployment type'
)
booleanParam(
name: 'DESTROY',
defaultValue: false,
description: 'Destroy resources instead of creating'
)
}
stages {
stage('Verify Environment') {
steps {
script {
echo "Starting ELK Stack Deployment on EKS"
echo "Deployment Type: ${params.DEPLOYMENT_TYPE}"
echo "Destroy Mode: ${params.DESTROY}"
// 使用 AWS 凭据
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'dev-user-aws-credentials',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]]) {
// 验证工具是否已安装
sh '''
echo "=== Verifying Required Tools ==="
terraform version
kubectl version --client
aws --version
jq --version
echo "All tools are available!"
'''
// 配置 kubectl 访问 EKS 集群
sh '''
echo "=== Configuring kubectl for EKS ==="
aws eks update-kubeconfig --region ${AWS_REGION} --name ${CLUSTER_NAME}
kubectl get nodes
'''
}
}
}
}
stage('Deploy Infrastructure') {
when {
expression {
params.DESTROY == false &&
(params.DEPLOYMENT_TYPE == 'full' || params.DEPLOYMENT_TYPE == 'infrastructure-only')
}
}
steps {
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'dev-user-aws-credentials',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]]) {
deployInfrastructure()
}
}
}
}
stage('Deploy ELK Stack') {
when {
expression {
params.DESTROY == false &&
(params.DEPLOYMENT_TYPE == 'full' || params.DEPLOYMENT_TYPE == 'elk-only')
}
}
steps {
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'dev-user-aws-credentials',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]]) {
deployELKStack()
}
}
}
}
stage('Update DNS') {
when {
expression {
params.DESTROY == false &&
(params.DEPLOYMENT_TYPE == 'full' || params.DEPLOYMENT_TYPE == 'dns-only')
}
}
steps {
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'dev-user-aws-credentials',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]]) {
updateDNS()
}
}
}
}
stage('Health Check') {
when {
expression { params.DESTROY == false }
}
steps {
script {
healthCheck()
}
}
}
// 将 Destroy 阶段移到最后
stage('Destroy Resources') {
when {
expression { params.DESTROY == true }
}
steps {
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'dev-user-aws-credentials',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]]) {
echo "Starting destruction of ELK Stack resources..."
destroyResources()
}
}
}
}
}
post {
always {
script {
echo "Build completed: ${currentBuild.result}"
echo "Build URL: ${env.BUILD_URL}"
}
}
success {
script {
if (params.DESTROY) {
echo "✅ ELK Stack resources destroyed successfully!"
} else {
echo "✅ ELK Stack deployment completed successfully!"
echo "🌐 Kibana URL: https://${env.KIBANA_DOMAIN}"
}
}
}
failure {
script {
if (params.DESTROY) {
echo "❌ ELK Stack destruction failed!"
} else {
echo "❌ ELK Stack deployment failed!"
}
echo "Please check the build logs for details: ${env.BUILD_URL}"
}
}
}
}
// Pipeline Functions
def deployInfrastructure() {
echo "🚀 Stage 1: Deploying Infrastructure (Route53 + ACM)"
// 部署 Route53
dir('terraform/route53') {
sh '''
echo "Initializing and applying Route53 Terraform..."
terraform init
terraform plan -out=tfplan
terraform apply -auto-approve tfplan
'''
// 获取输出
def route53ZoneId = sh(
script: 'terraform output -raw route53_zone_id',
returnStdout: true
).trim()
def nameServers = sh(
script: 'terraform output -json route53_name_servers',
returnStdout: true
).trim()
echo "Route53 Zone ID: ${route53ZoneId}"
echo "Name Servers: ${nameServers}"
env.ROUTE53_ZONE_ID = route53ZoneId
}
// 部署 ACM
dir('terraform/acm') {
sh '''
echo "Initializing and applying ACM Terraform..."
terraform init
terraform plan -out=tfplan
terraform apply -auto-approve tfplan
'''
// 获取 ACM 证书 ARN
def acmCertArn = sh(
script: 'terraform output -raw acm_certificate_arn',
returnStdout: true
).trim()
echo "ACM Certificate ARN: ${acmCertArn}"
env.ACM_CERT_ARN = acmCertArn
}
// 在根目录更新 Kibana Ingress
sh """
echo "Updating Kibana Ingress with ACM Certificate ARN..."
sed -i 's|alb.ingress.kubernetes.io/certificate-arn:.*|alb.ingress.kubernetes.io/certificate-arn: \"${env.ACM_CERT_ARN}\"|' k8s/kibana/kibana-ingress.yaml
echo "✅ Kibana Ingress updated with ACM Certificate ARN"
"""
echo "=== 🔔 IMPORTANT: Update Your Domain Nameservers ==="
echo "Please update your domain registrar for ${env.DOMAIN_NAME} to use the nameservers shown above"
echo "====================================================="
}
def deployELKStack() {
echo "🚀 Stage 2: Deploying ELK Stack"
// 应用 Kubernetes 资源
sh '''
echo "Creating logging namespace..."
kubectl apply -f k8s/namespaces/logging-namespace.yaml
echo "Deploying Elasticsearch..."
kubectl apply -f k8s/elasticsearch/ -n ${NAMESPACE}
'''
// 等待 Elasticsearch 就绪
sh '''
echo "Waiting for Elasticsearch to be ready..."
for i in {1..60}; do
if kubectl wait --for=condition=ready pod -l app=elasticsearch -n ${NAMESPACE} --timeout=60s 2>/dev/null; then
echo "✅ Elasticsearch is ready!"
break
fi
echo "⏳ Elasticsearch not ready yet, attempt $i/60..."
sleep 10
done
'''
// 部署其他组件
sh '''
echo "Deploying Kibana..."
kubectl apply -f k8s/kibana/ -n ${NAMESPACE}
echo "Deploying Filebeat..."
kubectl apply -f k8s/filebeat/ -n ${NAMESPACE}
echo "Deploying Logstash..."
kubectl apply -f k8s/logstash/ -n ${NAMESPACE}
'''
// 等待 Kibana pod 启动
echo "⏳ Waiting for Kibana to start..."
sh '''
for i in {1..30}; do
if kubectl get pods -n ${NAMESPACE} -l app=kibana --no-headers 2>/dev/null | grep -q Running; then
echo "✅ Kibana pod is running"
break
fi
echo "⏳ Kibana not running yet, attempt $i/30..."
sleep 10
done
'''
echo "✅ ELK Stack deployment completed!"
echo "📝 Note: ALB provisioning may take a few minutes. Kibana will be available once ALB is ready."
}
def updateDNS() {
echo "🚀 Stage 3: Updating DNS Records"
// 获取 ALB 信息
echo "📥 Retrieving ALB information..."
sh '''
for i in {1..30}; do
ALB_DNS_NAME=$(kubectl get ingress -n ${NAMESPACE} kibana-ingress -o jsonpath="{.status.loadBalancer.ingress[0].hostname}" 2>/dev/null || echo "")
if [ -n "$ALB_DNS_NAME" ]; then
echo "✅ ALB DNS Name found: $ALB_DNS_NAME"
break
fi
echo "⏳ ALB not ready yet, attempt $i/30..."
sleep 10
done
'''
def albDnsName = sh(
script: 'kubectl get ingress -n ${NAMESPACE} kibana-ingress -o jsonpath="{.status.loadBalancer.ingress[0].hostname}" 2>/dev/null || echo ""',
returnStdout: true
).trim()
if (!albDnsName) {
error "❌ ALB DNS name not found. ALB may not be provisioned yet. Please wait a few minutes and try again."
}
def albZoneId = sh(
script: "aws elbv2 describe-load-balancers --region ${env.AWS_REGION} --query \"LoadBalancers[?DNSName=='${albDnsName}'].CanonicalHostedZoneId\" --output text",
returnStdout: true
).trim()
echo "ALB DNS Name: ${albDnsName}"
echo "ALB Zone ID: ${albZoneId}"
env.ALB_DNS_NAME = albDnsName
env.ALB_ZONE_ID = albZoneId
// 更新 DNS 记录
dir('terraform/dns') {
sh """
echo "Initializing and applying DNS Terraform..."
terraform init
terraform plan \
-var="alb_dns_name=${env.ALB_DNS_NAME}" \
-var="alb_zone_id=${env.ALB_ZONE_ID}" \
-out=tfplan
terraform apply -auto-approve tfplan
"""
}
echo "✅ DNS records updated successfully!"
echo "🌐 Kibana will be available at: https://${env.KIBANA_DOMAIN}"
}
def healthCheck() {
echo "🔍 Performing Health Checks..."
sh '''
echo "=== 📊 ELK Stack Pods Status ==="
kubectl get pods -n ${NAMESPACE} -o wide
echo ""
echo "=== 🔗 Services Status ==="
kubectl get svc -n ${NAMESPACE}
echo ""
echo "=== 📈 Component Status ==="
# 检查 Elasticsearch
echo "Elasticsearch:"
ES_PODS=$(kubectl get pods -n ${NAMESPACE} -l app=elasticsearch --no-headers | wc -l)
ES_READY=$(kubectl get pods -n ${NAMESPACE} -l app=elasticsearch --no-headers | grep "Running" | wc -l)
echo " Pods: ${ES_READY}/${ES_PODS} ready"
# 检查 Kibana
echo "Kibana:"
KIBANA_PODS=$(kubectl get pods -n ${NAMESPACE} -l app=kibana --no-headers | wc -l)
KIBANA_READY=$(kubectl get pods -n ${NAMESPACE} -l app=kibana --no-headers | grep "Running" | wc -l)
echo " Pods: ${KIBANA_READY}/${KIBANA_PODS} ready"
# 检查 Logstash
echo "Logstash:"
LOGSTASH_PODS=$(kubectl get pods -n ${NAMESPACE} -l app=logstash --no-headers | wc -l)
LOGSTASH_READY=$(kubectl get pods -n ${NAMESPACE} -l app=logstash --no-headers | grep "Running" | wc -l)
echo " Pods: ${LOGSTASH_READY}/${LOGSTASH_PODS} ready"
# 检查 Filebeat
echo "Filebeat:"
FILEBEAT_PODS=$(kubectl get pods -n ${NAMESPACE} -l app=filebeat --no-headers | wc -l)
FILEBEAT_READY=$(kubectl get pods -n ${NAMESPACE} -l app=filebeat --no-headers | grep "Running" | wc -l)
echo " Pods: ${FILEBEAT_READY}/${FILEBEAT_PODS} ready"
echo ""
echo "=== 🌐 DNS and ALB Status ==="
ALB_DNS=$(kubectl get ingress -n ${NAMESPACE} kibana-ingress -o jsonpath="{.status.loadBalancer.ingress[0].hostname}" 2>/dev/null || echo "Not available")
echo "ALB DNS: ${ALB_DNS}"
if [ "${ALB_DNS}" != "Not available" ]; then
echo "✅ ALB is provisioned"
echo "🌐 Access Kibana at: https://${KIBANA_DOMAIN}"
else
echo "⏳ ALB is still provisioning..."
fi
'''
echo "✅ Health checks completed!"
}
def destroyResources() {
echo "🗑️ Destroying ELK Stack resources..."
// 删除 Kubernetes 资源
sh '''
echo "Deleting Kubernetes resources..."
kubectl delete -f k8s/logstash/ -n ${NAMESPACE} --ignore-not-found=true || true
kubectl delete -f k8s/filebeat/ -n ${NAMESPACE} --ignore-not-found=true || true
kubectl delete -f k8s/kibana/ -n ${NAMESPACE} --ignore-not-found=true || true
kubectl delete -f k8s/elasticsearch/ -n ${NAMESPACE} --ignore-not-found=true || true
kubectl delete -f k8s/namespaces/logging-namespace.yaml --ignore-not-found=true || true
echo "✅ Kubernetes resources deleted"
'''
// 销毁 DNS 记录
dir('terraform/dns') {
sh '''
echo "Destroying DNS records..."
terraform init
terraform destroy -auto-approve
echo "✅ DNS records destroyed"
'''
}
// 销毁 ACM 证书
dir('terraform/acm') {
sh '''
echo "Destroying ACM certificate..."
terraform init
terraform destroy -auto-approve
echo "✅ ACM certificate destroyed"
'''
}
// 销毁 Route53 区域
dir('terraform/route53') {
sh '''
echo "Destroying Route53 zone..."
terraform init
terraform destroy -auto-approve
echo "✅ Route53 zone destroyed"
'''
}
echo "✅ All ELK Stack resources have been destroyed successfully!"
}