部署到gcloud上的CircleCI kubectl配置错误

时间:2019-06-26 02:30:21

标签: docker kubernetes gcloud kubectl circleci

我通常是docker / kubernetes / dev ops的新手,我正在学习将TravisGithub结合使用的课程,但是我使用了BitBucket,所以我试图通过CircleCI将CI部署到GKE。

大多数任务都可以正常工作,但是在涉及kubectl时出现错误(特别是在deploy.sh脚本上)。这是我遇到的错误:

unable to recognize "k8s/client-deployment.yml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
unable to recognize "k8s/database-persistent-volume-claim.yml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
unable to recognize "k8s/ingress-service.yml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
unable to recognize "k8s/postgres-cluster-ip-service.yml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
unable to recognize "k8s/postgres-deployment.yml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
unable to recognize "k8s/redis-cluster-ip-service.yml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
unable to recognize "k8s/redis-deployment.yml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
unable to recognize "k8s/server-cluster-ip-service.yml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
unable to recognize "k8s/server-deployment.yml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
unable to recognize "k8s/worker-deployment.yml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused

我已经设法解决了很多问题,但是我对此一无所知,因此可以提供任何帮助。

这是CircleCI的config.yml(在MyUser上,我实际上是在使用我的docker用户,而不是env或其他任何东西,只是不公开它):

version: 2
jobs:
  build:
    docker:
      - image: node

    working_directory: ~/app

    steps:
      - checkout
      - setup_remote_docker

      - run:
          name: Install Docker Client
          command: |
            set -x
            VER="18.09.2"
            curl -L -o /tmp/docker-$VER.tgz https://download.docker.com/linux/static/stable/x86_64/docker-$VER.tgz
            tar -xz -C /tmp -f /tmp/docker-$VER.tgz
            mv /tmp/docker/* /usr/bin

      - run:
          name: Build Client Docker Image
          command: docker build -t MY_USER/multi-docker-react -f ./client/Dockerfile.dev ./client

      - run:
          name: Run Tests
          command: docker run -e CI=true MY_USER/multi-docker-react npm run test -- --coverage

  deploy:
    working_directory: ~/app
    # Docker environment where we gonna run our build deployment scripts 
    docker:
    - image: google/cloud-sdk

    steps:
    - checkout
    - setup_remote_docker:
        docker_layer_caching: true

    # Set up Env
    - run:
        name: Setup Environment Variables
        command: |
          echo 'export GIT_SHA="$CIRCLE_SHA1"' >> $BASH_ENV
          echo 'export CLOUDSDK_CORE_DISABLE_PROMPTS=1' >> $BASH_ENV

          # Log in to docker CLI
    - run:
        name: Log in to Docker Hub
        command: |
          echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_ID" --password-stdin

    # !!! This installs gcloud !!!
    - run:
        name: Installing GCL
        working_directory: /
        command: |
          echo $GCLOUD_SERVICE_KEY | gcloud auth activate-service-account --key-file=-
          gcloud --quiet config set project ${GOOGLE_PROJECT_ID}
          gcloud --quiet config set compute/zone ${GOOGLE_COMPUTE_ZONE}

    # !!! This runs a deployment
    - run:
        name: Deploying
        command: bash ./deploy.sh

workflows:
  version: 2
  build:
    jobs:
    - deploy:
        filters:
          branches:
            only:
            - master

这里是deploy.sh

docker build -t MY_USER/multi-docker-client:latest -t MY_USER/multi-docker-client:$GIT_SHA -f ./client/Dockerfile ./client
docker build -t MY_USER/multi-docker-server:latest -t MY_USER/multi-docker-server:$GIT_SHA -f ./server/Dockerfile ./server
docker build -t MY_USER/multi-docker-worker:latest -t MY_USER/multi-docker-worker:$GIT_SHA -f ./worker/Dockerfile ./worker
docker push MY_USER/multi-docker-client:latest
docker push MY_USER/multi-docker-server:latest
docker push MY_USER/multi-docker-worker:latest
docker push MY_USER/multi-docker-client:$GIT_SHA
docker push MY_USER/multi-docker-server:$GIT_SHA
docker push MY_USER/multi-docker-worker:$GIT_SHA
kubectl apply -f k8s
kubectl set image deployments/client-deployment client=MY_USER/multi-docker-client:$GIT_SHA
kubectl set image deployments/server-deployment server=MY_USER/multi-docker-server:$GIT_SHA
kubectl set image deployments/worker-deployment worker=MY_USER/multi-docker-worker:$GIT_SHA

这是我的项目结构:

enter image description here

1 个答案:

答案 0 :(得分:1)

原来,我只是缺少下一个命令:

gcloud --quiet container clusters get-credentials multi-cluster

此任务的一部分:

    # !!! This installs gcloud !!!
    - run:
        name: Installing GCL
        working_directory: /
        command: |
          echo $GCLOUD_SERVICE_KEY | gcloud auth activate-service-account --key-file=-
          gcloud --quiet config set project ${GOOGLE_PROJECT_ID}
          gcloud --quiet config set compute/zone ${GOOGLE_COMPUTE_ZONE}
          gcloud --quiet container clusters get-credentials multi-cluster

向@DazWilkin大喊大叫