简体   繁体   English

如何将掌舵图从 gitlab 部署到 eks?

[英]how to deploy helm chart from gitlab to eks?

I created a kubernetes cluster and linked it with eks.我创建了一个 kubernetes 集群并将其与 eks 链接。

I created also an helm chart and.gitla-ci.yml.我还创建了一个舵图和.gitla-ci.yml。 I want to add a new step to deploy my app using helm to the cluster, but I don't find a recent tutorial.我想添加一个新步骤来使用 helm 将我的应用程序部署到集群,但我没有找到最近的教程。 All tutorials use gitlab-auto devops.所有教程都使用 gitlab-auto devops。

The image is hosted on gitlab.该图像托管在 gitlab 上。

How could I do to achieve this task?我该怎么做才能完成这项任务?

image: docker:latest
services:
    - docker:dind

variables:
    DOCKER_DRIVER: overlay
    SPRING_PROFILES_ACTIVE: test
    USER_GITLAB: kosted
    APP_NAME: mebooks
    REPO: gara-mebooks
    MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
    MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"


stages:
    - deploy



k8s-deploy:
  stage: deploy
  image: dtzar/helm-kubectl:3.1.2
  only:
    - develop
  script:
    # Read certificate stored in $KUBE_CA_PEM variable and save it in a new file
    - echo $KUBE_URL
    - kubectl config set-cluster gara-eks-cluster --server="$KUBE_URL" --certificate-authority="$KUBE_CA_PEM"
    - kubectl get pods

In the gitlab console I got在我得到的 gitlab 控制台中

The connection to the server localhost:8080 was refused - did you specify the right host or port?与服务器 localhost:8080 的连接被拒绝 - 您是否指定了正确的主机或端口? Running after_script 00:01 Uploading artifacts for failed job 00:02 ERROR: Job failed: exit code 1运行 after_script 00:01 为失败的作业上传工件 00:02 错误:作业失败:退出代码 1

1 - Create arn role or user on IAM from your aws console 2 - connect to your bastion and add the arn role/user in the ConfigMap aws-auth you can follow this to understand how it works (you are not the creator of the cluster paragraph): https://aws.amazon.com/fr/premiumsupport/knowledge-center/eks-api-server-unauthorized-error/ 3- In your gitlab ci you just have to add this if it is a user you have created: 1 - 从您的 aws 控制台在 IAM 上创建 arn 角色或用户 2 - 连接到您的堡垒并在 ConfigMap aws-auth 中添加 arn 角色/用户,您可以按照此操作了解其工作原理(您不是集群的创建者段): https://aws.amazon.com/fr/premiumsupport/knowledge-center/eks-api-server-unauthorized-error/ 3-在您的 gitlab ci 中,如果它是您拥有的用户,您只需添加它创建:

k8s-deploy:
  stage: deploy
  image: you need an image with aws + kubectl + helm
  only:
    - develop
  script:
    - aws --version
    - aws --profile default configure set aws_access_key_id "your access id"
    - aws --profile default configure set aws_secret_access_key "your secret"
    - helm version
    - aws eks update-kubeconfig --name NAME-OF-YOUR-CLUSTER --region eu-west-3
    - helm upgrade init
    - helm upgrade --install my-chart ./my-chart-folder

If you created a role note a user, you have just to do:如果您创建了角色注释用户,您只需执行以下操作:

   k8s-deploy:
      stage: deploy
      image: you need an image with aws + kubectl + helm
      only:
        - develop
      script:
        - aws --version
        - helm version
        - aws eks update-kubeconfig --name NAME-OF-YOUR-CLUSTER --region eu-west-3 -arn
        - helm upgrade init
        - helm upgrade --install my-chart ./my-chart-folder

Here I am adding my method, which is generic and can be used in any K8S environment without AWS CLI.在这里,我添加了我的方法,它是通用的,可以在没有 AWS CLI 的任何 K8S 环境中使用。

First, you need to convert your Kube Config to a base64 string:首先,您需要将 Kube 配置转换为 base64 字符串:

cat ~/.kube/config | base64

Add the result string as a variable to your CI/CD pipeline settings of the project/group.将结果字符串作为变量添加到项目/组的 CI/CD 管道设置中。 In my example I used kube_config .在我的示例中,我使用kube_config Read more on how to add variables here. 在此处阅读有关如何添加变量的更多信息。

Here is my CI YAML file:这是我的 CI YAML 文件:

stages:
  # - build
  # - test
  - deploy

variables:
  KUBEFOLDER: /root/.kube
  KUBECONFIG: $KUBEFOLDER/config


k8s-deploy-job: 
  stage: deploy
  image: dtzar/helm-kubectl:3.5.0
  before_script:
    - mkdir ${KUBEFOLDER}
    - echo ${kube_config} | base64 -d > ${KUBECONFIG}
    - helm version
    - helm repo update
  script:
    - echo "Deploying application..."
    - kubectl get pods
    #- helm upgrade --install my-chart ./my-chart-folder
    - echo "Application successfully deployed."

Inspired by: https://about.gitlab.com/blog/2017/09/21/how-to-create-ci-cd-pipeline-with-autodeploy-to-kubernetes-using-gitlab-and-helm/灵感来自: https://about.gitlab.com/blog/2017/09/21/how-to-create-ci-cd-pipeline-with-autodeploy-to-kubernetes-using-gitlab-and-helm/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM