CircleCI错误信息: "error: exec插件:apiVersion无效"client.authentication.k8s.io/v1alpha1""。

74
我在CircleCI部署时遇到了一个错误。请查看下面的配置文件。
当运行kubectl CLI时,我们在kubectl和aws-cli的EKS工具之间遇到了一个错误。
version: 2.1
orbs:
  aws-ecr: circleci/aws-ecr@6.3.0
  docker: circleci/docker@0.5.18
  rollbar: rollbar/deploy@1.0.1
  kubernetes: circleci/kubernetes@1.3.0
  deploy:
    version: 2.1
    orbs:
      aws-eks: circleci/aws-eks@1.0.0
      kubernetes: circleci/kubernetes@1.3.0
    executors:
      default:
        description: |
          The version of the circleci/buildpack-deps Docker container to use
          when running commands.
        parameters:
          buildpack-tag:
            type: string
            default: buster
        docker:
          - image: circleci/buildpack-deps:<<parameters.buildpack-tag>>
    description: |
      A collection of tools to deploy changes to AWS EKS in a declarative
      manner where all changes to templates are checked into version control
      before applying them to an EKS cluster.
    commands:
      setup:
        description: |
          Install the gettext-base package into the executor to be able to run
          envsubst for replacing values in template files.
          This command is a prerequisite for all other commands and should not
          have to be run manually.
        parameters:
          cluster-name:
            default: ''
            description: Name of the EKS Cluster.
            type: string
          aws-region:
            default: 'eu-central-1'
            description: Region where the EKS Cluster is located.
            type: string
          git-user-email:
            default: "deploy@mail.com"
            description: Email of the git user to use for making commits
            type: string
          git-user-name:
            default: "CircleCI Deploy Orb"
            description:  Name of the git user to use for making commits
            type: string
        steps:
          - run:
              name: install gettext-base
              command: |
                if which envsubst > /dev/null; then
                  echo "envsubst is already installed"
                  exit 0
                fi
                sudo apt-get update
                sudo apt-get install -y gettext-base
          - run:
              name: Setup GitHub access
              command: |
                mkdir -p ~/.ssh
                echo 'github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==' >> ~/.ssh/known_hosts
                git config --global user.email "<< parameters.git-user-email >>"
                git config --global user.name "<< parameters.git-user-name >>"
          - aws-eks/update-kubeconfig-with-authenticator:
              aws-region: << parameters.aws-region >>
              cluster-name: << parameters.cluster-name >>
              install-kubectl: true
              authenticator-release-tag: v0.5.1
      update-image:
        description: |
          Generates template files with the specified version tag for the image
          to be updated and subsequently applies that template after checking it
          back into version control.
        parameters:
          cluster-name:
            default: ''
            description: Name of the EKS Cluster.
            type: string
          aws-region:
            default: 'eu-central-1'
            description: Region where the EKS Cluster is located.
            type: string
          image-tag:
            default: ''
            description: |
              The tag of the image, defaults to the  value of `CIRCLE_SHA1`
              if not provided.
            type: string
          replicas:
            default: 3
            description: |
              The replica count for the deployment.
            type: integer
          environment:
            default: 'production'
            description: |
              The environment/stage where the template will be applied. Defaults
              to `production`.
            type: string
          template-file-path:
            default: ''
            description: |
              The path to the source template which contains the placeholders
              for the image-tag.
            type: string
          resource-name:
            default: ''
            description: |
              Resource name in the format TYPE/NAME e.g. deployment/nginx.
            type: string
          template-repository:
            default: ''
            description: |
              The fullpath to the repository where templates reside. Write
              access is required to commit generated templates.
            type: string
          template-folder:
            default: 'templates'
            description: |
              The name of the folder where the template-repository is cloned to.
            type: string
          placeholder-name:
            default: IMAGE_TAG
            description: |
              The name of the placeholder environment variable that is to be
              substituted with the image-tag parameter.
            type: string
          cluster-namespace:
            default: sayway
            description: |
              Namespace within the EKS Cluster.
            type: string
        steps:
          - setup:
              aws-region: << parameters.aws-region >>
              cluster-name: << parameters.cluster-name >>
              git-user-email: dev@sayway.com
              git-user-name: deploy
          - run:
              name: pull template repository
              command: |
                [ "$(ls -A << parameters.template-folder >>)" ] && \
                  cd << parameters.template-folder >> && git pull --force && cd ..
                [ "$(ls -A << parameters.template-folder >>)" ] || \
                  git clone << parameters.template-repository >> << parameters.template-folder >>
          - run:
              name: generate and commit template files
              command: |
                cd << parameters.template-folder >>
                IMAGE_TAG="<< parameters.image-tag >>"
                ./bin/generate.sh --file << parameters.template-file-path >> \
                  --stage << parameters.environment >> \
                  --commit-message "Update << parameters.template-file-path >> for << parameters.environment >> with tag ${IMAGE_TAG:-$CIRCLE_SHA1}" \
                  << parameters.placeholder-name >>="${IMAGE_TAG:-$CIRCLE_SHA1}" \
                  REPLICAS=<< parameters.replicas >>
          - kubernetes/create-or-update-resource:
              get-rollout-status: true
              namespace: << parameters.cluster-namespace >>
              resource-file-path: << parameters.template-folder >>/<< parameters.environment >>/<< parameters.template-file-path >>
              resource-name: << parameters.resource-name >>
jobs:
  test:
    working_directory: ~/say-way/core
    parallelism: 1
    shell: /bin/bash --login
    environment:
      CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
      CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
      KONFIG_CITUS__HOST: localhost
      KONFIG_CITUS__USER: postgres
      KONFIG_CITUS__DATABASE: sayway_test
      KONFIG_CITUS__PASSWORD: ""
      KONFIG_SPEC_REPORTER: true
    docker:
    - image: 567567013174.dkr.ecr.eu-central-1.amazonaws.com/core-ci:test-latest
      aws_auth:
        aws_access_key_id: $AWS_ACCESS_KEY_ID_STAGING
        aws_secret_access_key: $AWS_SECRET_ACCESS_KEY_STAGING
    - image: circleci/redis
    - image: rabbitmq:3.7.7
    - image: circleci/mongo:4.2
    - image: circleci/postgres:10.5-alpine
    steps:
    - checkout
    - run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS
    # This is based on your 1.0 configuration file or project settings
    - restore_cache:
        keys:
        - v1-dep-{{ checksum "Gemfile.lock" }}-
        # any recent Gemfile.lock
        - v1-dep-
    - run:
        name: install correct bundler version
        command: |
          export BUNDLER_VERSION="$(grep -A1 'BUNDLED WITH' Gemfile.lock | tail -n1 | tr -d ' ')"
          echo "export BUNDLER_VERSION=$BUNDLER_VERSION" >> $BASH_ENV
          gem install bundler --version $BUNDLER_VERSION
    - run: 'bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3'
    - run:
        name: copy test.yml.sample to test.yml
        command: cp config/test.yml.sample config/test.yml
    - run:
        name: Precompile and clean assets
        command: bundle exec rake assets:precompile assets:clean
    # Save dependency cache
    - save_cache:
        key: v1-dep-{{ checksum "Gemfile.lock" }}-{{ epoch }}
        paths:
        - vendor/bundle
        - public/assets
    - run:
        name: Audit bundle for known security vulnerabilities
        command: bundle exec bundle-audit check --update
    - run:
        name: Setup Database
        command: bundle exec ruby ~/sayway/setup_test_db.rb
    - run:
        name: Migrate Database
        command: bundle exec rake db:citus:migrate
    - run:
        name: Run tests
        command: bundle exec rails test -f
    # By default, running "rails test" won't run system tests.
    - run:
        name: Run system tests
        command: bundle exec rails test:system
    # Save test results
    - store_test_results:
        path: /tmp/circleci-test-results
    # Save artifacts
    - store_artifacts:
        path: /tmp/circleci-artifacts
    - store_artifacts:
        path: /tmp/circleci-test-results
  build-and-push-image:
    working_directory: ~/say-way/
    parallelism: 1
    shell: /bin/bash --login
    executor: aws-ecr/default
    steps:
      - checkout
      - run:
          name: Pull latest core images for cache
          command: |
            $(aws ecr get-login --no-include-email --region $AWS_REGION)
            docker pull "${AWS_ECR_ACCOUNT_URL}/core:latest"
      - docker/build:
          image: core
          registry: "${AWS_ECR_ACCOUNT_URL}"
          tag: "latest,${CIRCLE_SHA1}"
          cache_from: "${AWS_ECR_ACCOUNT_URL}/core:latest"
      - aws-ecr/push-image:
          repo: core
          tag: "latest,${CIRCLE_SHA1}"
  deploy-production:
    working_directory: ~/say-way/
    parallelism: 1
    shell: /bin/bash --login
    executor: deploy/default
    steps:
      - kubernetes/install-kubectl:
          kubectl-version: v1.22.0
      - rollbar/notify_deploy_started:
          environment: report
      - deploy/update-image:
          resource-name: deployment/core-web
          template-file-path: core-web-pod.yml
          cluster-name: report
          environment: report
          template-repository: git@github.com:say-way/sw-k8s.git
          replicas: 3
      - deploy/update-image:
          resource-name: deployment/core-worker
          template-file-path: core-worker-pod.yml
          cluster-name: report
          environment: report
          template-repository: git@github.com:say-way/sw-k8s.git
          replicas: 4
      - deploy/update-image:
          resource-name: deployment/core-worker-batch
          template-file-path: core-worker-batch-pod.yml
          cluster-name: report
          environment: report
          template-repository: git@github.com:say-way/sw-k8s.git
          replicas: 1
      - rollbar/notify_deploy_finished:
          deploy_id: "${ROLLBAR_DEPLOY_ID}"
          status: succeeded
  deploy-demo:
    working_directory: ~/say-way/
    parallelism: 1
    shell: /bin/bash --login
    executor: deploy/default
    steps:
      - kubernetes/install-kubectl:
          kubectl-version: v1.22.0
      - rollbar/notify_deploy_started:
          environment: demo
      - deploy/update-image:
          resource-name: deployment/core-web
          template-file-path: core-web-pod.yml
          cluster-name: demo
          environment: demo
          template-repository: git@github.com:say-way/sw-k8s.git
          replicas: 2
      - deploy/update-image:
          resource-name: deployment/core-worker
          template-file-path: core-worker-pod.yml
          cluster-name: demo
          environment: demo
          template-repository: git@github.com:say-way/sw-k8s.git
          replicas: 1
      - deploy/update-image:
          resource-name: deployment/core-worker-batch
          template-file-path: core-worker-batch-pod.yml
          cluster-name: demo
          environment: demo
          template-repository: git@github.com:say-way/sw-k8s.git
          replicas: 1
      - rollbar/notify_deploy_finished:
          deploy_id: "${ROLLBAR_DEPLOY_ID}"
          status: succeeded
workflows:
  version: 2.1
  build-n-test:
    jobs:
      - test:
          filters:
            branches:
              ignore: master
  build-approve-deploy:
    jobs:
      - build-and-push-image:
          context: Core
          filters:
            branches:
              only: master
      - approve-report-deploy:
          type: approval
          requires:
            - build-and-push-image
      - approve-demo-deploy:
          type: approval
          requires:
            - build-and-push-image
      - deploy-production:
          context: Core
          requires:
            - approve-report-deploy
      - deploy-demo:
          context: Core
          requires:
            - approve-demo-deploy

嗨yass,欢迎来到SO。请充分利用广泛的搜索功能,以便在堆栈交换网络中获得最大的收益。祝你好运! - mdaniel
如果本地机器上出现相同的错误,请检查带有上下文授权的 ~/.kube/ 配置,或者如果您想重新开始,则可以删除整个目录。 - Vladimir Vukanac
24个回答

129

aws-cli 出现了问题,但已经修复。


在我的情况下,更新aws-cli + 更新~/.kube/config有所帮助。
1. 更新aws-cli(按照文档进行操作
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install --update
  • 更新 kube 配置
  • mv ~/.kube/config ~/.kube/config.bk
    aws eks update-kubeconfig --region ${AWS_REGION}  --name ${EKS_CLUSTER_NAME}
    

    1
    在运行aws eks update-kubeconfig --region ${AWS_REGION} --name ${EKS_CLUSTER_NAME}之前,我必须删除~/.kube/config。如果不删除它,apiVersion就不会更改为v1beta1。 - Amit Itzkovitch
    当使用选项2时,我在运行 kubectl get svc 时遇到了超时。 - Gregor
    如上所述,在运行aws eks之前,请尝试删除('mv ~/.kube/config ~/.kube/config.bk')。 - Pav K.
    我必须执行这两个选项。 - barnhillec
    1
    哇,太棒了,非常出色。非常感谢您提供这个。我们需要同时执行两个选项。 - geert3
    只有选项1可行,选项2无效。 - Borislav Markov

    45

    我们已经有了解决方案: https://github.com/aws/aws-cli/issues/6920#issuecomment-1119926885

    更新包含修复的aws-cli(aws cli v1)版本:

    pip3 install awscli --upgrade --user
    

    要查看aws cli v2,请参见此文档
    之后,别忘了使用以下命令重新编写kube-config:

    aws eks update-kubeconfig --name ${EKS_CLUSTER_NAME} --region ${REGION}
    

    这个命令应该更新kube的apiVersionv1beta1


    2
    顺便提一下,update-kubeconfig 更新 ~/.kube/config,因此它只是本地的,不会影响远程服务器。 - Dorian
    aws eks... 这行代码会产生以下错误信息:"aws: error: argument --region: expected one argument"。 - sh37211
    1
    也许你的环境中未定义REGION变量,你可以尝试指定--region us-east-1 - bigLucas

    28

    在我的情况下,将kube配置文件中的apiVersion更改为v1beta1有所帮助:

    apiVersion: client.authentication.k8s.io/v1beta1
    

    配置文件的(字面)名称是什么? - Peter Mortensen
    1
    配置文件名为config,位于.kube文件夹内。 - Kiran Thorat
    我的遗失的拼图块,谢谢! - Yedidya Schwartz

    6

    最简单的解决方案:(虽然表述复杂,但是在这里呈现出来)

    打开您的kube config文件,并将所有alpha实例替换为beta。 (建议使用查找和替换编辑器:Atom、Sublime等)。

    Nano示例:

    nano  ~/.kube/config
    

    或者使用 Atom:

    atom ~/.kube/config
    

    那么你应该搜索alpha实例并将其替换为beta,然后保存文件。


    6

    最新版的 kubectl 存在一个故障。

    目前,您可以按照以下步骤解决此问题:

    1. curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.23.6/bin/linux/amd64/kubectl
    2. chmod +x ./kubectl
    3. sudo mv ./kubectl /usr/local/bin/kubectl
    4. sudo kubectl version

    3

    另一种方法是更新AWS命令行界面(AWS cli),这对于我来说起作用了。

    其余的指示来自bigLucas所提供的答案

    将aws-cli(aws cli v2)更新到最新版本:

    winget install Amazon.AWSCLI
    

    在此之后,别忘了使用以下命令重写 kube-config 文件:

    aws eks update-kubeconfig --name ${EKS_CLUSTER_NAME} --region ${REGION}
    

    该命令应该会将kube的apiVersion更新为v1beta1。

    3

    我曾经遇到和你一样的问题,以下是解决方法:

    1. 备份现有的配置文件 mv ~/.kube/config ~/.kube/config.bk

    2. 运行下面的命令:

    aws eks update-kubeconfig --name ${EKS_CLUSTER_NAME} --region ${REGION}
    
    1. 然后在任何文本编辑器中打开配置文件~/.kube/config,将v1apiVersion1更新为v1beta1,然后再尝试一次。

    3

    我将 alpha1 的值更改为 beta1 的值,在配置文件下它对我起作用了。


    “在配置文件下”是什么意思?最好请通过编辑(更改)您的答案来回复,而不是在评论中回复(不要添加“编辑:”,“更新:”或类似内容 - 答案应该看起来像今天写的)。 - Peter Mortensen

    3

    1
    你能在回答中总结一下吗?包括版本信息、日期、后续发展等(但不包括“编辑:”、“更新:”或类似内容——回答应该看起来像是今天写的)。 - Peter Mortensen

    2
    1. 打开~/.kube/config
    2. 在你遇到问题的集群中搜索user,并将client.authentication.k8s.io/v1alpha1替换为client.authentication.k8s.io/v1beta1

    你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接