Gitlab CI/CD: deploy docker image to existing kubernetes cluster 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 &quot;$CI_REGISTRY_USER&quot; -p &quot;$CI_REGISTRY_PASSWORD&quot; $CI_REGISTRY
  script:
    - docker build --pull -t &quot;$CI_REGISTRY_IMAGE:prod&quot; .
    - docker push &quot;$CI_REGISTRY_IMAGE:prod&quot;
  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 &quot;$CI_REGISTRY_USER&quot; -p &quot;$CI_REGISTRY_PASSWORD&quot; $CI_REGISTRY
  script:
    - docker build --pull -t &quot;$CI_REGISTRY_IMAGE:dev&quot; .
    - docker push &quot;$CI_REGISTRY_IMAGE:dev&quot;
  only:
    - develop

I am getting below error.
Executing &quot;step_script&quot; 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 &quot;scheduler-deployment.yaml&quot;: 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: &quot;redis://redis-master &quot;
            - name: RABBITMQ_HOST
              value: &quot;amqp://rabbitmq&quot;
      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.
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.