CircleCI kubectl config error on deployment to gcloud

6/26/2019

I'm new to docker/kubernetes/dev ops in general and I was following a course that used Travis with Github, however I use BitBucket so I'm trying to implement a CI deployment to GKE with CircleCI.

Most of the tasks are working just fine but I'm reaching an error when it comes to kubectl (especifically on the deploy.sh script). Here's the error I'm getting:

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

I've managed to get this far tackling through problems, however I'm lost on this one so any help is appreciated.

Here's the config.yml for CircleCI (on MyUser I'm actually using my docker user, not an env or anything, it's simply to not disclose it):

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

Here's the 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

And here's my project structure:

enter image description here

-- deathandtaxes
circleci
docker
gcloud
kubectl
kubernetes

1 Answer

6/26/2019

So turns out I was only missing the next command:

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

As part of this task:

    # !!! 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

Shout out to @DazWilkin for shedding a light

-- deathandtaxes
Source: StackOverflow