Skip to content

Instantly share code, notes, and snippets.

@exocode
Created March 31, 2019 01:14
Show Gist options
  • Save exocode/e5058c06faf02ba94f57b067b9b7f5a0 to your computer and use it in GitHub Desktop.
Save exocode/e5058c06faf02ba94f57b067b9b7f5a0 to your computer and use it in GitHub Desktop.
Rancher Gitlab

Connect Rancher 2 Kubernetes cluster with Gitlab

I am using VSCode with a Docker plugin

Create a Repo.

This example uses a React app as demo app.

Create a basic React app: npx create-react-app demo-app cd demo-app yarn install yarn start

Your browser pops up and shows you After everything is

image: node:latest # can be upgraded, depending on your node version used
pages:
  stage: deploy
  script:
    - npm install
    - npm run build
    - rm -rf public
    - mv build public
  artifacts:
    paths:
      - public # mandatory, other folder won't work
  only:
    - master # or dev, the branch you want to publish

Connect your Gitlab with your Rancher Server

This tutorial is based from that blog post. https://rancher.com/blog/2018/2018-08-07-cicd-pipeline-k8s-autodevops-rancher-and-gitlab/

TL;DR

Write a kubeconfig file

bash <(curl -s https://gist.githubusercontent.com/superseb/f6cd637a7ad556124132ca39961789a4/raw/4db7da97073ccb6049f797df1880dcd36850179e/get_kubeconfig_custom_cluster_rancher2.sh) YOUR_CLUSTER_NAME

Return your Server IP

cat kubeconfig |sed -n '/clusters/,/name:/s/.*server: \(.*\)/\1/p' |sed 's/"//g' or cat kubeconfig | grep server:

Get a cert

cat kubeconfig |sed -n '/clusters/,/name:/s/.*certificate-authority-data: \(.*\)/\1/p' |base64 --decode

Create ServiceAccount

Apply kubectl apply -f https://gist.githubusercontent.com/exocode/ec3583163ecd21f57d0a98729c04526f/raw/b4e4e1feaa65451cc0b64fa5de0a3add654cf841/rancher-gitlab-managed-apps.yml (optionally add –kubeconfig <kubeconfig> if you want to use a cluster other than the default one specified in your .kube/config file) This will create a service account and create a token for it, which is the token that we need to specify in the GitLab Kubernetes configuration pane.

Get the secret

kubectl describe secrets/gitlab-secret -n gitlab-managed-apps | grep token:

Custom Domains (optional)

Once you've set up the external endpoint, you should associate it with a wildcard DNS record such as *.example.com. in order to be able to reach your apps. If your external endpoint is an IP address, use an A record. If your external endpoint is a hostname, use a CNAME record.

Dockerize your app

Assuming that you already have installed Docker and your docker-machine is running. (if you use Visual Code Editor you can Use vscode-docker (https://github.com/microsoft/vscode-docker) which helps with Dockerfile templates.)

But most basic .Dockerfile looks like this:

FROM node:10.13-alpine
ENV NODE_ENV production
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm install --production --silent && mv node_modules ../
COPY . .
EXPOSE 3000
CMD npm start

and a docker-compose.yml to spin up:

version: '2.1'
services:
  demo-app:
    image: demo-app
    build: .
    environment:
      NODE_ENV: development
    ports:
      - 3000:3000
      - 9229:9229
    ## set your startup file here
    command: node --inspect index.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment