0

I have node.js application that I need to deploy to exising kubernetes cluster.

The cluster is setup using kops on AWS.

I have created .gitlab-ci.yml file for building docker images.

So, whenever a change is pushed to either master or develop branch. It will build the docker image.

Now, I have to deploy it to exisitng kubernetes cluster. I have already followed steps mentioned here to add existing cluster.

For now, I am using shared runner.

How can I integrate kubernetes deployment after image is built with gitlab ci/cd to deploy on aws (cluster is created with kops)?

For registry I am using gitlab's container registry not docker hub.

I have tried below gitlab-ci.yaml file,

gitlab-ci.yaml

stages:
  - docker-build
  - deploy

docker-build-master:
  image: docker:latest
  stage: docker-build
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE:prod" .
    - docker push "$CI_REGISTRY_IMAGE:prod"
  only:
    - master

deploy-prod:
  stage: deploy
  image: roffe/kubectl
  script:
    - kubectl apply -f scheduler-deployment.yaml
  only:
    - master


docker-build-dev:
  image: docker:latest
  stage: docker-build
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE:dev" .
    - docker push "$CI_REGISTRY_IMAGE:dev"
  only:
    - develop

I am getting below error.

Executing "step_script" stage of the job script
00:01
Using docker image sha256:c8d24d490701efec4c8d544978b3e4ecc4855475a221b002a8f9e5e473398805 for roffe/kubectl with digest roffe/kubectl@sha256:ba13f8ffc55c83a7ca98a6e1337689fad8a5df418cb160fa1a741c80f42979bf ...
$ kubectl apply -f scheduler-deployment.yaml
error: unable to recognize "scheduler-deployment.yaml": Get http://localhost:8080/api?timeout=32s: dial tcp [::1]:8080: connect: connection refused
Cleaning up file based variables
00:00
ERROR: Job failed: exit code 1

File scheduler-deployment.yaml is a valid yaml file and it works when I apply manually.

Below is the deployment file.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sheduler-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: scheduler
  template:
    metadata:
      labels:
        app: scheduler
    spec:
      containers:
        - name: scheduler
          image: registry.gitlab.com/user/schedulingapis:dev
          env:
            - name: REDIS_HOST
              value: "redis://redis-master "
            - name: RABBITMQ_HOST
              value: "amqp://rabbitmq"
      imagePullSecrets:
        - name: registry-credentials

If someone has any better idea to automate deployment to a k8s cluster with gitlab.

Pease do suggest. Any link or reference will also work.

1 Answer 1

1

Your error is most likely from referencing the wrong directory in your script block of your job.

When GitLab executes your job in a container, it mounts the project directory to the /builds directory in the container. You can reference this either by specifying /builds/scheduler-deployment.yaml or by using the predefined environment variable $CI_PROJECT_DIR with $CI_PROJECT_DIR/scheduler-deployment.yaml.

1
  • I have updated the question. I was able to solve that issue. There were some mistake from my side. Commented Mar 16, 2021 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.