From d0109219c77f04dfc5a2116570b47975bd4bab2e Mon Sep 17 00:00:00 2001 From: Alex Ouzounis Date: Fri, 8 May 2020 11:36:07 +0100 Subject: [PATCH] feat: refactor (#1) * feat: refactor * feat: rename repo * feat: fix ingress --- 01-docker-nginx/run.sh | 8 +- 02-docker-git-sync/run.sh | 6 +- 03-pod/configmap.sh | 2 +- 03-pod/configmap.yaml | 2 +- 03-pod/pod.yaml | 22 +++++- 04-deployment/configmap.yaml | 16 ++++ .../deployment-dogs.yaml | 21 +++++- .../deployment-kittens.yaml | 21 +++++- .../service-dogs.yaml | 5 +- .../service-kittens.yaml | 5 +- .../service.yaml | 5 +- 06-ingress/ingress.yaml | 29 +++++++ 07-ingress/ingress.yaml | 26 ------- 07-ingress/services.yaml | 49 ------------ OWNERS | 14 ++++ README.md | 75 +++++++++++++++++++ resources/cats/index.html | 2 +- resources/dogs/index.html | 2 +- utils/namespace.yaml | 2 +- 19 files changed, 210 insertions(+), 102 deletions(-) create mode 100644 04-deployment/configmap.yaml rename 06-loadbalancer/deployment.yaml => 04-deployment/deployment-dogs.yaml (71%) rename 05-deployment/deployment.yaml => 04-deployment/deployment-kittens.yaml (71%) rename 06-loadbalancer/service.yaml => 05-service/service-dogs.yaml (70%) rename 04-service/service.yaml => 05-service/service-kittens.yaml (69%) rename 06-loadbalancer/service-lb.yaml => 05-service/service.yaml (67%) create mode 100644 06-ingress/ingress.yaml delete mode 100644 07-ingress/ingress.yaml delete mode 100644 07-ingress/services.yaml create mode 100644 OWNERS create mode 100644 README.md diff --git a/01-docker-nginx/run.sh b/01-docker-nginx/run.sh index 9ec51b1..a013c36 100755 --- a/01-docker-nginx/run.sh +++ b/01-docker-nginx/run.sh @@ -1,9 +1,11 @@ #!/bin/bash +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + docker run --rm --name nginx \ - -v $HOME/solarstorm:/var/git/solarstorm:ro \ - -v $HOME/solarstorm/conf/default.conf:/var/conf/default.conf:ro \ + -v $DIR/../:/var/git/k8sprimitives1:ro \ + -v $DIR/../conf/default.conf:/var/conf/default.conf:ro \ -e CONF_TEMPLATE=/var/conf/default.conf \ - -e HTML_PATH=/var/git/solarstorm/resources/cats \ + -e HTML_PATH=/var/git/k8sprimitives1/resources/dogs \ -p 8080:80 \ -d belitre/nginx:0.1 \ No newline at end of file diff --git a/02-docker-git-sync/run.sh b/02-docker-git-sync/run.sh index 786f60e..20c4507 100755 --- a/02-docker-git-sync/run.sh +++ b/02-docker-git-sync/run.sh @@ -1,13 +1,13 @@ #!/bin/bash -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/.." mkdir -p $DIR/git_repo docker run --rm \ -v $DIR/git_repo:/git \ - -e GIT_SYNC_REPO=https://github.com/belitre/solarstorm \ + -e GIT_SYNC_REPO=https://github.com/ouzi-dev/k8sprimitives1 \ -e GIT_SYNC_BRANCH=master \ -e GIT_SYNC_ONE_TIME=true \ - -e GYT_SYNC_DEST=solarstorm \ + -e GYT_SYNC_DEST=k8sprimitives1 \ belitre/git-sync:0.1 \ No newline at end of file diff --git a/03-pod/configmap.sh b/03-pod/configmap.sh index e7ac4d3..795aa26 100755 --- a/03-pod/configmap.sh +++ b/03-pod/configmap.sh @@ -1,3 +1,3 @@ #!/bin/bash -kubectl create configmap nginx-template --from-file=default.conf --dry-run -o yaml -n miguelsantiago \ No newline at end of file +kubectl create configmap nginx-template --from-file=default.conf --dry-run -o yaml -n k8sprimitives1 \ No newline at end of file diff --git a/03-pod/configmap.yaml b/03-pod/configmap.yaml index b894293..b70aa6b 100644 --- a/03-pod/configmap.yaml +++ b/03-pod/configmap.yaml @@ -2,7 +2,7 @@ apiVersion: v1 kind: ConfigMap metadata: name: nginx-template - namespace: miguelsantiago + namespace: k8sprimitives1 data: default.conf: |- server { diff --git a/03-pod/pod.yaml b/03-pod/pod.yaml index ba63e55..6877dc9 100644 --- a/03-pod/pod.yaml +++ b/03-pod/pod.yaml @@ -2,7 +2,7 @@ kind: Pod apiVersion: v1 metadata: name: kittens-web-server - namespace: miguelsantiago + namespace: k8sprimitives1 labels: app: nginx release: kittens @@ -15,7 +15,7 @@ spec: - name: CONF_TEMPLATE value: '/var/conf/default.conf' - name: HTML_PATH - value: '/var/git/solarstorm/resources/cats' + value: '/var/git/k8sprimitives1/resources/cats' ports: - containerPort: 80 protocol: TCP @@ -27,21 +27,35 @@ spec: - name: nginx-conf readOnly: true mountPath: /var/conf + resources: + limits: + cpu: "0.1" + memory: "50M" + requests: + cpu: "0.1" + memory: "50M" - name: git-sync image: 'index.docker.io/belitre/git-sync:0.1' imagePullPolicy: IfNotPresent env: - name: GIT_SYNC_REPO - value: 'https://github.com/belitre/solarstorm' + value: 'https://github.com/ouzi-dev/k8sprimitives1' - name: GIT_SYNC_BRANCH value: master - name: GIT_SYNC_WAIT value: '300' - name: GIT_SYNC_DEST - value: solarstorm + value: k8sprimitives1 volumeMounts: - name: git-repo mountPath: /git + resources: + limits: + cpu: "0.1" + memory: "50M" + requests: + cpu: "0.1" + memory: "50M" volumes: - name: git-repo emptyDir: {} diff --git a/04-deployment/configmap.yaml b/04-deployment/configmap.yaml new file mode 100644 index 0000000..b70aa6b --- /dev/null +++ b/04-deployment/configmap.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: nginx-template + namespace: k8sprimitives1 +data: + default.conf: |- + server { + listen 80; + server_name localhost; + + location / { + root _HTML_PATH_; + index index.html index.htm; + } + } diff --git a/06-loadbalancer/deployment.yaml b/04-deployment/deployment-dogs.yaml similarity index 71% rename from 06-loadbalancer/deployment.yaml rename to 04-deployment/deployment-dogs.yaml index 3af08bc..6346453 100644 --- a/06-loadbalancer/deployment.yaml +++ b/04-deployment/deployment-dogs.yaml @@ -2,6 +2,7 @@ kind: Deployment apiVersion: apps/v1 metadata: name: dogs-deployment + namespace: k8sprimitives1 labels: app: nginx release: dogs @@ -25,7 +26,7 @@ spec: - name: CONF_TEMPLATE value: '/var/conf/default.conf' - name: HTML_PATH - value: '/var/git/solarstorm/resources/dogs' + value: '/var/git/k8sprimitives1/resources/dogs' ports: - containerPort: 80 protocol: TCP @@ -37,21 +38,35 @@ spec: - name: nginx-conf readOnly: true mountPath: /var/conf + resources: + limits: + cpu: "0.1" + memory: "50M" + requests: + cpu: "0.1" + memory: "50M" - name: git-sync image: 'index.docker.io/belitre/git-sync:0.1' imagePullPolicy: IfNotPresent env: - name: GIT_SYNC_REPO - value: 'https://github.com/belitre/solarstorm' + value: 'https://github.com/ouzi-dev/k8sprimitives1' - name: GIT_SYNC_BRANCH value: master - name: GIT_SYNC_WAIT value: '300' - name: GIT_SYNC_DEST - value: solarstorm + value: k8sprimitives1 volumeMounts: - name: git-repo mountPath: /git + resources: + limits: + cpu: "0.1" + memory: "50M" + requests: + cpu: "0.1" + memory: "50M" volumes: - name: git-repo emptyDir: {} diff --git a/05-deployment/deployment.yaml b/04-deployment/deployment-kittens.yaml similarity index 71% rename from 05-deployment/deployment.yaml rename to 04-deployment/deployment-kittens.yaml index b7e5e8e..eaf0e52 100644 --- a/05-deployment/deployment.yaml +++ b/04-deployment/deployment-kittens.yaml @@ -2,6 +2,7 @@ kind: Deployment apiVersion: apps/v1 metadata: name: kittens-deployment + namespace: k8sprimitives1 labels: app: nginx release: kittens @@ -25,7 +26,7 @@ spec: - name: CONF_TEMPLATE value: '/var/conf/default.conf' - name: HTML_PATH - value: '/var/git/solarstorm/resources/cats' + value: '/var/git/k8sprimitives1/resources/cats' ports: - containerPort: 80 protocol: TCP @@ -37,21 +38,35 @@ spec: - name: nginx-conf readOnly: true mountPath: /var/conf + resources: + limits: + cpu: "0.1" + memory: "50M" + requests: + cpu: "0.1" + memory: "50M" - name: git-sync image: 'index.docker.io/belitre/git-sync:0.1' imagePullPolicy: IfNotPresent env: - name: GIT_SYNC_REPO - value: 'https://github.com/belitre/solarstorm' + value: 'https://github.com/ouzi-dev/k8sprimitives1' - name: GIT_SYNC_BRANCH value: master - name: GIT_SYNC_WAIT value: '300' - name: GIT_SYNC_DEST - value: solarstorm + value: k8sprimitives1 volumeMounts: - name: git-repo mountPath: /git + resources: + limits: + cpu: "0.1" + memory: "50M" + requests: + cpu: "0.1" + memory: "50M" volumes: - name: git-repo emptyDir: {} diff --git a/06-loadbalancer/service.yaml b/05-service/service-dogs.yaml similarity index 70% rename from 06-loadbalancer/service.yaml rename to 05-service/service-dogs.yaml index 1b4f34d..e579e04 100644 --- a/06-loadbalancer/service.yaml +++ b/05-service/service-dogs.yaml @@ -1,12 +1,13 @@ apiVersion: v1 kind: Service metadata: - name: dogs-nodeport + name: dogs + namespace: k8sprimitives1 labels: app: nginx release: dogs spec: - type: NodePort + type: ClusterIP ports: - port: 80 name: http diff --git a/04-service/service.yaml b/05-service/service-kittens.yaml similarity index 69% rename from 04-service/service.yaml rename to 05-service/service-kittens.yaml index 9c283df..99073d9 100644 --- a/04-service/service.yaml +++ b/05-service/service-kittens.yaml @@ -1,12 +1,13 @@ apiVersion: v1 kind: Service metadata: - name: kittens-nodeport + name: kittens + namespace: k8sprimitives1 labels: app: nginx release: kittens spec: - type: NodePort + type: ClusterIP ports: - port: 80 name: http diff --git a/06-loadbalancer/service-lb.yaml b/05-service/service.yaml similarity index 67% rename from 06-loadbalancer/service-lb.yaml rename to 05-service/service.yaml index 6232c95..49f8dc0 100644 --- a/06-loadbalancer/service-lb.yaml +++ b/05-service/service.yaml @@ -1,11 +1,12 @@ apiVersion: v1 kind: Service metadata: - name: lb-nodeport + name: all + namespace: k8sprimitives1 labels: app: nginx spec: - type: NodePort + type: ClusterIP ports: - port: 80 name: http diff --git a/06-ingress/ingress.yaml b/06-ingress/ingress.yaml new file mode 100644 index 0000000..669d9b5 --- /dev/null +++ b/06-ingress/ingress.yaml @@ -0,0 +1,29 @@ +apiVersion: extensions/v1beta1 +kind: Ingress +metadata: + name: kitten-ingress + namespace: k8sprimitives1 + annotations: + nginx.ingress.kubernetes.io/rewrite-target: /$2 + cert-manager.io/cluster-issuer: letsencrypt-prod +spec: + rules: + - host: k8sprimitives1.k8sdemos.test-infra.ouzi.io + http: + paths: + - path: /kittens(/|$)(.*) + backend: + serviceName: kittens + servicePort: http + - path: /dogs(/|$)(.*) + backend: + serviceName: dogs + servicePort: http + - path: /random(/|$)(.*) + backend: + serviceName: all + servicePort: http + tls: + - hosts: + - k8sprimitives1.k8sdemos.test-infra.ouzi.io + secretName: k8sprimitives1-cert \ No newline at end of file diff --git a/07-ingress/ingress.yaml b/07-ingress/ingress.yaml deleted file mode 100644 index 5f4b290..0000000 --- a/07-ingress/ingress.yaml +++ /dev/null @@ -1,26 +0,0 @@ -apiVersion: extensions/v1beta1 -kind: Ingress -metadata: - name: kitten-ingress - annotations: - nginx.ingress.kubernetes.io/rewrite-target: / -spec: - backend: - serviceName: default-http-backend - servicePort: 80 - rules: - - host: myminikube.info - http: - paths: - - path: /cats - backend: - serviceName: kittens-service - servicePort: http - - path: /dogs - backend: - serviceName: dogs-service - servicePort: http - - path: /random - backend: - serviceName: random-service - servicePort: http \ No newline at end of file diff --git a/07-ingress/services.yaml b/07-ingress/services.yaml deleted file mode 100644 index 2a78524..0000000 --- a/07-ingress/services.yaml +++ /dev/null @@ -1,49 +0,0 @@ -apiVersion: v1 -kind: List -items: - - apiVersion: v1 - kind: Service - metadata: - name: kittens-service - labels: - app: nginx - release: kittens - spec: - type: ClusterIP - ports: - - port: 80 - name: http - targetPort: http - selector: - app: nginx - release: kittens - - apiVersion: v1 - kind: Service - metadata: - name: dogs-service - labels: - app: nginx - release: dogs - spec: - type: ClusterIP - ports: - - port: 80 - name: http - targetPort: http - selector: - app: nginx - release: dogs - - apiVersion: v1 - kind: Service - metadata: - name: random-service - labels: - app: nginx - spec: - type: ClusterIP - ports: - - port: 80 - name: http - targetPort: http - selector: - app: nginx \ No newline at end of file diff --git a/OWNERS b/OWNERS new file mode 100644 index 0000000..1d2813c --- /dev/null +++ b/OWNERS @@ -0,0 +1,14 @@ +# See the OWNERS docs at https://go.k8s.io/owners +approvers: + - belitre + - givanov + - alexouzounis +reviewers: + - belitre + - givanov + - alexouzounis +aliases: + ouzi-team: + - belitre + - givanov + - alexouzounis \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..12df408 --- /dev/null +++ b/README.md @@ -0,0 +1,75 @@ +# k8s primitives 1 workshop + +## Overview + +We will go through a journey running simple application which serves gifs of cats and dogs. +The application has two components; a web server serving the images and a file puller pulling gifs from somewhere, in this case a git repository. + +## Steps + +These are the steps we will go through during the workshop + +### 01 - Run nginx in docker serving local files + +In this step we will run our nginx server locally on docker serving gifs we have locally. + +Run: +``` +01-docker-nginx/run.sh +``` + +>Thats nice but how do I get my local files in the instance docker runs ? Enter step 2 + +### 02 - Run a git sync tool in docker cloning this repo + +In this step we will run a separate container that pulls files from a remote git repository. + + +Run: +``` +02-docker-git-sync/run.sh +``` + +>Nice! so now we have a way to get our files over to the instance docker runs. But I need to combine both containers somehow.. + +### 03 - Run both containers above in a k8s pod + +In this step we will show how you can combine multiple containers of single concern into a pod and leverage that capability. + +Run: +``` +kubectl apply -f 03-pod/ +``` + +> So now we can put both containers in a pod and leverage the pod semantics across shared network and volume. What happens if the pod dies though ? How can i maintain a number of pods always ? + +### 04 - Run a fixed number of replics for each of our pods, called deployment + +In this step we will create two deployment objects. One for cats and one for dogs. They will each have a fixed number of replicas such that we can ensure a fixed capacity irrespective of individual pods are dying. + +Run: +``` +kubectl apply -f 04-deployment/ +``` + +> How do I access all the pods you might ask ? Since i have more than 1, i need a way to do service discovery and load balance across all the available ones. + +### 05 - Create a service for each deployment that allows us to do service discovery across any number of pods running + +In this step we will create a Service object for each deployment. This will allow us to group all the pods of a deployment into a service discovery endpoint based on the labels we provided to the pods in the deployment. + +Run: +``` +kubectl apply -f 05-service/ +``` + +> How do i expose those to the outside world now ? + +### 06 - Create an ingress object that makes those pods available to the external world + +In this step we will create an Ingress object that exposes the pods under [k8sprimitives1.k8sdemos.test-infra.ouzi.io](k8sprimitives1.k8sdemos.test-infra.ouzi.io) + +Run: +``` +kubectl apply -f 06-ingress/ +``` \ No newline at end of file diff --git a/resources/cats/index.html b/resources/cats/index.html index 89d64d5..07ba05e 100644 --- a/resources/cats/index.html +++ b/resources/cats/index.html @@ -1,6 +1,6 @@
- Solarstorm + k8sprimitives1