Answers: <p>Your error is most likely from referencing the wrong directory in your script block of your job.</p>
  <p>When GitLab executes your job in a container, it mounts the project directory to the <code>/builds</code> directory in the container. You can reference this either by specifying <code>/builds/scheduler-deployment.yaml</code> or by using the predefined environment variable <code>$CI_PROJECT_DIR</code> with <code>$CI_PROJECT_DIR/scheduler-deployment.yaml</code>.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-03-16T02:39:27.413Z
Answers: I have updated the question. I was able to solve that issue. There were some mistake from my side.
Answers: confusedWarrior
Answers: CC BY-SA 4.0
Answers: 2021-03-15T16:12:59.923Z
Answers: 2021-03-15T16:31:55.027Z
Answers: 2021-03-15T16:31:55.027Z
Answers: Preston Martin
Body: <p>I have node.js application that I need to deploy to exising kubernetes cluster.</p>
  <p>The cluster is setup using <code>kops</code> on AWS.</p>
  <p>I have created <code>.gitlab-ci.yml</code> file for building docker images.</p>
  <p>So, whenever a change is pushed to either <code>master</code> or <code>develop</code> branch. It will build the docker image.</p>
  <p>Now, I have to deploy it  to exisitng kubernetes cluster. I have already followed steps mentioned <a href="https://docs.gitlab.com/ee/user/project/clusters/add_remove_clusters.html#add-existing-cluster" rel="nofollow noreferrer">here</a> to add existing cluster.</p>
  <p>For now, I am using shared runner.</p>
  <p><em>How can I integrate kubernetes deployment after image is built with gitlab ci/cd to deploy on aws (cluster is created with kops)?</em></p>
  <p><em>For registry I am using gitlab's container registry not docker hub.</em></p>
  <p>I have tried below <code>gitlab-ci.yaml</code> file,</p>
  <p><em>gitlab-ci.yaml</em></p>
  <pre><code>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
  </code></pre>
  <p>I am getting below error.</p>
  <pre><code>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
  
  </code></pre>
  <p>File <code>scheduler-deployment.yaml</code> is a valid yaml file and it works when I apply manually.</p>
  <p>Below is the <code>deployment</code> file.</p>
  <pre><code>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
  
  </code></pre>
  <p>If someone has any better idea to automate deployment to a k8s cluster with gitlab.</p>
  <p><em>Pease do suggest.
  Any link or reference will also work.</em></p>
  
Tags: <docker><kubernetes><amazon-web-services><continuous-integration><gitlab>
Title: Gitlab CI/CD: deploy docker image to existing kubernetes cluster
UserDisplayName: confusedWarrior
ThreadText: # Gitlab CI/CD: deploy docker image to existing kubernetes cluster
  
  (Asked by: username_0 on 2021-03-15)
  
  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](https://docs.gitlab.com/ee/user/project/clusters/add_remove_clusters.html#add-existing-cluster) 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.*
  
  **Tags:** docker, kubernetes, amazon-web-services, continuous-integration, gitlab
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-03-15. Score: 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`.
  
  > **Comments:**
  > **username_0**: I have updated the question. I was able to solve that issue. There were some mistake from my side.  
  
  ---
  
  