如何使用 Jenkins 将 Docker 容器从 Amazon ECR 自动部署到 Kubernetes [英] How to auto deploy Docker containers from Amazon ECR to Kubernetes using Jenkins

查看:20
本文介绍了如何使用 Jenkins 将 Docker 容器从 Amazon ECR 自动部署到 Kubernetes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我们的环境:

  1. 我有一个在 Amazon 上运行的 Kubernetes 集群.
  2. 在 Amazon 上运行的 Jenkins CI/CD,它连接到私有 GitLab 并将我们的服务构建为 Docker 映像.
  3. 存储我们的 Docker 镜像的 Amazon ECR.

我的问题:

  1. 一旦 Jenkins 管道将新建的映像推送到 ECR,我如何将映像从 ECR 自动部署到 Kubernetes(作为 Pod)?
  2. 我可以在 Jenkins 管道中做到这一点吗?我已经阅读了很多材料,但我找不到应该怎么做.

还有一个像 Keel 这样的 3rd 方工具,但它不支持 Amazon ECR (ECR 的 Webhook 问题).

There is also a 3rd party tool like Keel but it doesn't support Amazon ECR (Webhook problem with ECR).

任何帮助将不胜感激.

推荐答案

我有一个类似的工作流程,我希望这能帮助你找到方向.我正在为 CI 使用 bitbucket 管道,但我相信 Jenkins 也能正常工作.

I have a similar workflow and I hope this helps you get some direction. I am using bitbucket pipeline for CI, but I am sure Jenkins will work fine as well.

这就是我在 CI 流程中所做的:

This is what I do in my CI flow :

  • 构建我的代码并安装依赖项
  • 创建一个带有唯一标签( commit-id )的容器> my-cntnr:12
  • 推送到 ECR
  • Curl Rancher API for my-pod > set(image:my-cntnr:12)
  • Kubernates 更新 Pod 并从 ECR 中拉取带有标签 12 的容器

这里是参考脚本:

      - composer install --no-interaction
      - docker build -t cms .
      - docker tag myrepo:latest 123456789.dkr.ecr.my-region.amazonaws.com/myrepo:$BITBUCKET_BUILD_NUMBER
      - aws ecr get-login --no-include-email --region my-region >> login.sh
      - sh login.sh
      - docker push 123456799.dkr.ecr.my-region.amazonaws.com/myrepo:$BITBUCKET_BUILD_NUMBER
      - sh .docker/workload-update.sh // my curl script calling rancher API

注意:由于我使用的是 Rancher,所以我可以使用 Rancher API 来更新 Pod 和他们的配置.

note: Since I am using Rancher, I can use Rancher API to update pods and their configuration.

现在对于 Kubernetes 的 ECR 凭证部分,您必须创建一个密钥(仅限 Kubernetes 的实体),该密钥是使用您的 AWS ECR 详细信息创建的.然后你可以在你的 pod.yml 中使用这个秘密作为 image-pull-secret.这将告诉 k8 使用秘密并从 ECR 中提取图像

Now for the ECR credentials part for Kubernetes, you have to create a secret ( a Kubernetes only entity), this secret is created using your AWS ECR details. Then you can use this secret in your pod.yml as image-pull-secret. This will tell k8 to use the secret and pull the image from ECR

我有一个简单的脚本可以快速做到这一点.

I have a simple script to quickly do that.

#
# RUN me where kubectl is available,& make sure to replace account,region etc
#
ACCOUNT=123456789
REGION=my-region
SECRET_NAME=${REGION}-ecr-registry
EMAIL=email@email.com ( can be anything)

#
# Fetch token (which will expire in 12 hours)
#

TOKEN=`aws ecr --region=$REGION get-authorization-token --output text --query authorizationData[].authorizationToken | base64 -d | cut -d: -f2`

#
# Create or replace registry secret
#

kubectl delete secret --ignore-not-found $SECRET_NAME
kubectl create secret docker-registry $SECRET_NAME 
 --docker-server=https://${ACCOUNT}.dkr.ecr.${REGION}.amazonaws.com 
 --docker-username=AWS 
 --docker-password="${TOKEN}" 
 --docker-email="${EMAIL}"

这就是你如何在你的 pod.yml 中使用它

And this is how you can use it in your pod.yml

apiVersion: v1
kind: Pod                                            
metadata:
  name: my-app   
  labels:
    app: my-app                              
spec:                                                
  containers:
    - image: 123456789.dkr.ecr.my-region.amazonaws.com/my-repo
      name: -cntnr                            
      ports:
        - containerPort: 8080    
  imagePullSecrets:
  - name: my-secret-name ( this will be same as name of secret we created earlier)

<小时>

我也写了一篇关于这个过程的详细文章.请在这里找到它.

这篇关于如何使用 Jenkins 将 Docker 容器从 Amazon ECR 自动部署到 Kubernetes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆