CI/CD with Gitlab & Kubernetes

Today, after few experimentations, I’m able to show you how to deploy a complete CI/CD Pipeline based on Gitlab and Kubernetes with multi-environments and auto-deployment in the cluster.

Valentin Ouvrard
Feb 16, 2017 · 5 min read
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
FROM debian:jessieMAINTAINER NGINX Docker Maintainers “docker-maint@nginx.com”ENV NGINX_VERSION 1.11.9–1~jessieRUN apt-key adv — keyserver hkp://pgp.mit.edu:80 — recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 \
&& echo “deb http://nginx.org/packages/mainline/debian/ jessie nginx” >> /etc/apt/sources.list \
&& apt-get update \
&& apt-get install — no-install-recommends — no-install-suggests -y \
ca-certificates \
nginx=${NGINX_VERSION} \
nginx-module-xslt \
nginx-module-geoip \
nginx-module-image-filter \
nginx-module-perl \
nginx-module-njs \
gettext-base \
&& rm -rf /var/lib/apt/lists/*
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log
EXPOSE 80 443
COPY website /usr/share/nginx/html
CMD [“nginx”, “-g”, “daemon off;”]
variables:
DOCKER_DRIVER: overlay
IMAGE_NAME: “gilabregistry/user/tropicalhosting”
build:
image: docker:latest
services:
— docker:dind
stage: build
cache:
key: “builder”
paths:
— ./.build
script:
— docker version
— docker build — pull -t “$IMAGE_NAME:${CI_BUILD_REF_NAME}_${CI_BUILD_REF}” .
— docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN gitlabregistry
— docker push “$IMAGE_NAME:${CI_BUILD_REF_NAME}_${CI_BUILD_REF}”
k8s-deploy-Dev:
image: lwolf/kubectl_deployer:latest
services:
— docker:dind
stage: deploy
script:
— kubectl config set-cluster my-cluster — server=”$K8S_URL” — insecure-skip-tls-verify=true
— kubectl config set-credentials admin — token=”$K8S_TOKEN”
— kubectl config set-context default-context — cluster=my-cluster — user=admin
— kubectl config use-context default-context
— kubectl get cs
— /bin/sh deploy.sh ${CI_BUILD_REF_NAME}_${CI_BUILD_REF} dev dev.tropicalhosting.com
environment:
name: dev
url: https://dev.tropicalhosting.com
only:
— dev
k8s-deploy-Prod:
image: lwolf/kubectl_deployer:latest
services:
— docker:dind
stage: deploy
script:
— kubectl config set-cluster my-cluster — server=”$K8S_URL” — insecure-skip-tls-verify=true
— kubectl config set-credentials admin — token=”$K8S_TOKEN”
— kubectl config set-context default-context — cluster=my-cluster — user=admin
— kubectl config use-context default-context
— kubectl get cs
— /bin/sh deploy.sh ${CI_BUILD_REF_NAME}_${CI_BUILD_REF} default tropicalhosting.com
environment:
name: production
url: https://tropicalhosting.com
only:
— master
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: tropicalhosting
namespace: ${NAMESPACE}
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: tropicalhosting
spec:
terminationGracePeriodSeconds: 60
containers:
— image: gitlabregistry/user/tropicalhosting:${BUILD_NUMBER}
name: tropicalhosting
imagePullPolicy: Always
ports:
— containerPort: 80
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 30
timeoutSeconds: 1
imagePullSecrets:
— name: registrypullsecret
#!/usr/bin/env bashTAG=${1}
NS=${2}
URL=${3}
export BUILD_NUMBER=${TAG}
export NAMESPACE=${NS}
export ENV_URL=${URL}
for f in templates/*.yml
do
envsubst < $f > “.generated/$(basename $f)”
done
kubectl apply -f .generated/
Image for post
Image for post
Pipeline’s builds
Image for post
Image for post
Pipeline’s environments
Image for post
Image for post

DevOpsTricks

Some tips & tricks about Unix & DevOps.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store