From ce83fb3981618b510e803701ad12e592eafc3949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Fri, 19 May 2017 14:38:29 +0200 Subject: [PATCH] Added k8s deployment --- .gitlab-ci.yml | 28 ++++++++++++++++++---------- Makefile | 35 ++++++++++++++++++++++++++++------- k8s/senpy-deployment.yaml | 24 ++++++++++++++++++++++++ k8s/senpy-ingress.yaml | 14 ++++++++++++++ k8s/senpy-svc.yaml | 12 ++++++++++++ 5 files changed, 96 insertions(+), 17 deletions(-) create mode 100644 k8s/senpy-deployment.yaml create mode 100644 k8s/senpy-ingress.yaml create mode 100644 k8s/senpy-svc.yaml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 48f4d84..9719070 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,18 +1,22 @@ -image: gsiupm/dockermake:latest - - +# Uncomment if you want to use docker-in-docker +# image: gsiupm/dockermake:latest +# services: +# - docker:dind # When using dind, it's wise to use the overlayfs driver for # improved performance. + variables: - DOCKER_DRIVER: overlay - DOCKERFILE: Dockerfile IMAGENAME: $CI_REGISTRY_IMAGE stages: - test - push + - deploy - clean +before_script: + - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY + .test: &test_definition stage: test script: @@ -28,11 +32,8 @@ test-2.7: variables: PYTHON_VERSION: "2.7" - .image: &image_definition stage: push - before_script: - - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY script: - make -e push-$PYTHON_VERSION only: @@ -57,9 +58,16 @@ push-latest: - master - triggers +deploy: + stage: deploy + environment: test + script: + - make -e deploy + only: + - master + clean : stage: clean script: - make -e clean - only: - - master \ No newline at end of file + when: manual \ No newline at end of file diff --git a/Makefile b/Makefile index 91fcd5c..3e260ca 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,15 @@ IMAGEWTAG=$(IMAGENAME):$(VERSION) DEVPORT=5000 action="test-${PYMAIN}" +KUBE_CA_PEM_FILE="" +KUBE_URL="" +KUBE_TOKEN="" +KUBECTL=docker run --rm -v $(KUBE_CA_PEM_FILE):/tmp/ca.pem -v $$PWD:/tmp/cwd/ -i lachlanevenson/k8s-kubectl --server="$(KUBE_URL)" --token="$(KUBE_TOKEN)" --certificate-authority="/tmp/ca.pem" -n $(KUBE_NAMESPACE) +CI_REGISTRY=docker.io +CI_REGISTRY_USER=gitlab +CI_BUILD_TOKEN="" + + all: build run .FORCE: @@ -37,7 +46,7 @@ quick_build: $(addprefix build-, $(PYMAIN)) build: $(addprefix build-, $(PYVERSIONS)) build-%: version Dockerfile-% - docker build -t '$(IMAGEWTAG)-python$*' -f Dockerfile-$* .; + docker build -t '$(IMAGEWTAG)-python$*' --cache-from $(IMAGENAME):python$* -f Dockerfile-$* .; quick_test: $(addprefix test-,$(PYMAIN)) @@ -53,8 +62,8 @@ dev: dev-$(PYMAIN) test-all: $(addprefix test-,$(PYVERSIONS)) -test-%: build-% - docker run --rm --entrypoint /usr/local/bin/python -w /usr/src/app $(IMAGEWTAG)-python$* setup.py test +test-%: + docker run --rm --entrypoint /usr/local/bin/python -w /usr/src/app $(IMAGENAME):python$* setup.py test test: test-$(PYMAIN) @@ -71,8 +80,8 @@ pip_test-%: sdist pip_test: $(addprefix pip_test-,$(PYVERSIONS)) clean: - @docker ps -a | awk '/$(REPO)\/$(NAME)/{ split($$2, vers, "-"); if(vers[0] != "${VERSION}"){ print $$1;}}' | xargs docker rm -v 2>/dev/null|| true - @docker images | awk '/$(REPO)\/$(NAME)/{ split($$2, vers, "-"); if(vers[0] != "${VERSION}"){ print $$1":"$$2;}}' | xargs docker rmi 2>/dev/null|| true + @docker ps -a | grep $(IMAGENAME) | awk '{ split($$2, vers, "-"); if(vers[0] != "${VERSION}"){ print $$1;}}' | xargs docker rm -v 2>/dev/null|| true + @docker images | grep $(IMAGENAME) | awk '{ split($$2, vers, "-"); if(vers[0] != "${VERSION}"){ print $$1":"$$2;}}' | xargs docker rmi 2>/dev/null|| true @docker stop $(addprefix $(NAME)-dev,$(PYVERSIONS)) 2>/dev/null || true @docker rm $(addprefix $(NAME)-dev,$(PYVERSIONS)) 2>/dev/null || true @@ -93,12 +102,17 @@ run-%: build-% run: run-$(PYMAIN) -push-latest: build-$(PYMAIN) +push-latest: $(addprefix push-latest-,$(PYVERSIONS)) docker tag '$(IMAGEWTAG)-python$(PYMAIN)' '$(IMAGEWTAG)' docker tag '$(IMAGEWTAG)-python$(PYMAIN)' '$(IMAGENAME)' docker push '$(IMAGENAME):latest' docker push '$(IMAGEWTAG)' +push-latest-%: build-% + docker tag $(IMAGENAME):$(VERSION)-python$* $(IMAGENAME):python$* + docker push $(IMAGENAME):$(VERSION)-python$* + docker push $(IMAGENAME):python$* + push-%: build-% docker push $(IMAGENAME):$(VERSION)-python$* @@ -109,4 +123,11 @@ push: $(addprefix push-,$(PYVERSIONS)) ci: gitlab-runner exec docker --docker-volumes /var/run/docker.sock:/var/run/docker.sock --env CI_PROJECT_NAME=$(NAME) ${action} -.PHONY: test test-% test-all build-% build test pip_test run yapf push-main push-% dev ci version .FORCE +deploy: + $(KUBECTL) delete -n senpy secret $(CI_REGISTRY) || true + @$(KUBECTL) create -n $(NAME) secret docker-registry $(CI_REGISTRY) --docker-server=$(CI_REGISTRY) --docker-username=$(CI_REGISTRY_USER) --docker-email=$(CI_REGISTRY_USER) --docker-password=$(CI_BUILD_TOKEN) + $(KUBECTL) apply -f /tmp/cwd/k8s/ + + + +.PHONY: test test-% test-all build-% build test pip_test run yapf push-main push-% dev ci version .FORCE deploy diff --git a/k8s/senpy-deployment.yaml b/k8s/senpy-deployment.yaml new file mode 100644 index 0000000..a34a780 --- /dev/null +++ b/k8s/senpy-deployment.yaml @@ -0,0 +1,24 @@ +--- +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: senpy-latest +spec: + replicas: 1 + template: + metadata: + labels: + role: senpy-latest + spec: + containers: + - name: senpy-latest + image: gsiupm/senpy:latest + args: + - "--default-plugins" + resources: + limits: + memory: "512Mi" + cpu: "1000m" + ports: + - name: web + containerPort: 5000 diff --git a/k8s/senpy-ingress.yaml b/k8s/senpy-ingress.yaml new file mode 100644 index 0000000..908af47 --- /dev/null +++ b/k8s/senpy-ingress.yaml @@ -0,0 +1,14 @@ +--- +apiVersion: extensions/v1beta1 +kind: Ingress +metadata: + name: senpy-ingress +spec: + rules: + - host: latest.senpy.cluster.gsi.dit.upm.es + http: + paths: + - path: / + backend: + serviceName: senpy-latest + servicePort: 5000 diff --git a/k8s/senpy-svc.yaml b/k8s/senpy-svc.yaml new file mode 100644 index 0000000..5d5c21e --- /dev/null +++ b/k8s/senpy-svc.yaml @@ -0,0 +1,12 @@ +--- +apiVersion: v1 +kind: Service +metadata: + name: senpy-latest +spec: + type: ClusterIP + ports: + - port: 5000 + protocol: TCP + selector: + role: senpy-latest