From 2313b133241964c5b3c2194ab99c9c92c0cf18d1 Mon Sep 17 00:00:00 2001 From: Dmitriy Creed Date: Tue, 15 Nov 2022 23:31:30 +0700 Subject: [PATCH 1/4] Upload the Lab 12 assignment Signed-off-by: Dmitriy Creed --- LAB10.md | 54 ------------------------------------------------------ LAB12.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 54 deletions(-) delete mode 100644 LAB10.md create mode 100644 LAB12.md diff --git a/LAB10.md b/LAB10.md deleted file mode 100644 index 6274f1f42..000000000 --- a/LAB10.md +++ /dev/null @@ -1,54 +0,0 @@ -# 11 - -## K8s secrets and resources - -### 10 points - -In this lab you will figure out how to manage sensitive data such as a password, a token, or a key in kubernetes. Then you will set up CPU and memory limits for your app. - -1. Create a secret in your cluster using `kubctl`. - - * [Secrets](https://kubernetes.io/docs/concepts/configuration/secret/) - * [Managing secrets](https://kubernetes.io/docs/tasks/configmap-secret/managing-secret-using-kubectl/#decoding-secret) - -2. Verify and decode your secret, then create 11.md in the k8s folder and provide the output of the commands inside. - -3. So we'll skip configuration file for secret, let's use helm. - - * You need to create `secrets.yaml` file in `templates` folder. - * Create a `secret` object inside. - * Add `env` field to your `Deployment`. Path that you will need: `spec.template.spec.containers.env.` - - > [Helm secrets](https://www.youtube.com/watch?v=hRSlKRvYe1A) - - * Update your helm deployment (instructions are present in the video). Get pods: `kubectl get po.` Use the name of the pod to provide a proof of your success. Check your secret inside pod, example: `kubectl exec demo- 5f898f5f4c-2gpnd -- printenv | grep MY_PASS.` - -4. Read about Resources management: - - * [Resource management](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/) - -5. Set up requests and limits for CPU and memory for your application and test that it works. - -6. Create a PR to the forked repo lab11 branch, ask your teammates to review it and review PRs of your teammates. - -7. Create a PR in your own repository from the lab11 branch to the lab10 one. It will help us with grading. - -### List of requirements - -* proof of work with a secret in 11.md for the step 2 -* secrets.yaml -* proof of work with a secret in 11.md for the step 3 -* resource requests and limits for cpu and memory - -## Bonus - -### 2 points - -1. Add environment variables for your containers: - - * Read about it: - - * [K8s env vars](https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/) - - * Update your helm chart with several environment variables, but use named templates, move them to `_helpers.tpl` file: - * [Helm named templates](https://helm.sh/docs/chart_template_guide/named_templates/) diff --git a/LAB12.md b/LAB12.md new file mode 100644 index 000000000..0d28f1c68 --- /dev/null +++ b/LAB12.md @@ -0,0 +1,54 @@ +# 12 + +## K8s ConfigMaps + +In this lab you will figure out how to manage non-confidential data in kubernetes. + +### 10 points + +1. Read about `ConfigMaps` objects: + * [ConfigMaps](https://kubernetes.io/docs/concepts/configuration/configmap/) + +2. We tried env vars, so let's mount a config file to our container. Create a folder `files` with `config.json` +file inside. Put some data in json format inside to check your success in the end. + +3. Use helm and mount config.json: + + * Create a `configMap` manifest. Get data from your `config.json`, use `.Files.Get`. + * Then update your `deployment.yaml`. Add `Volumes` and `VolumeMounts`. + > [Example](https://carlos.mendible.com/2019/02/10/kubernetes-mount-file-pod-with-configmap/) + * Install your updated helm chart and test the result. Get pods: `kubectl get po`. Use the name of the pod to provide a proof of your success. Check your config map inside pod, example: - `kubectl exec demo-758cc4d7c4-cxnrn -- cat /config.json`.`.Files.Get`. + +4. Create 12.md in the k8s folder and provide the output of the commands inside. + +5. You need to upgrade your application. For future labs your app must do something persistent. You can follow next steps or suggest any your logic. + + 1. Your application must write to a file the time when the root path `/` was accessed by the client. + 2. It must have a new endpoint with path `/visits`. + 3. Return the content via new endpoint, like you saw it for `/metrics`, but in any format. + 4. Test it: + 1. Update your `docker-compose.yml`. + 2. Add a new volume for your file. + 3. Test that it works. + 4. Update your README.md for your application. + +6. Create a PR to the forked repo lab12 branch, ask your teammates to review it and review PRs of your teammates. + +7. Create a PR in your own repository from the lab12 branch to the lab11 one. It will help us with grading. + +### List of requirements + +* `config.json` file in `files` folder +* `configMap` that takes data from `config.json` using `.Files.Get` +* `volume`s and `volumeMount`s exist in deployments.yml +* `12.md` file with results of commands + +## Bonus + +### 2 points + +1. Upgrade your bonus app in the same way, it must do something persistent. +2. Read about `StatefulSet, Headless service, Persistent Volumes`. Describe how did you understand them, and why do we need them. + * https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/ + * https://kubernetes.io/docs/concepts/services-networking/service/#headless-services + * https://kubernetes.io/docs/concepts/storage/persistent-volumes/ From b96746a0b44759fb248c307bbb752f287051bedf Mon Sep 17 00:00:00 2001 From: Alexey Posikera Date: Mon, 21 Nov 2022 21:49:27 +0300 Subject: [PATCH 2/4] update requirements --- app_python/timeweb/requirements.txt | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/app_python/timeweb/requirements.txt b/app_python/timeweb/requirements.txt index 5277a15cd..4873fc27d 100644 --- a/app_python/timeweb/requirements.txt +++ b/app_python/timeweb/requirements.txt @@ -1,11 +1,4 @@ -click==8.1.3 -Flask==2.2.2 -importlib-metadata==4.12.0 -itsdangerous==2.1.2 -Jinja2==3.1.2 -MarkupSafe==2.1.1 -Werkzeug==2.2.2 -zipp==3.8.1 +Flask gunicorn pytz requests From cb1c6487779b2f20145f1658640028cd17ccd01d Mon Sep 17 00:00:00 2001 From: Alexey Posikera Date: Mon, 21 Nov 2022 21:57:25 +0300 Subject: [PATCH 3/4] update requirements --- app_python/timeweb/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/app_python/timeweb/requirements.txt b/app_python/timeweb/requirements.txt index 4873fc27d..f36ec6a82 100644 --- a/app_python/timeweb/requirements.txt +++ b/app_python/timeweb/requirements.txt @@ -1,3 +1,4 @@ +setuptools==65.5.1 Flask gunicorn pytz From 4c611f13d5d30586a8177f427c3d83ca440fa0ad Mon Sep 17 00:00:00 2001 From: Alexey Posikera Date: Mon, 21 Nov 2022 22:55:26 +0300 Subject: [PATCH 4/4] lab12 --- app_python/Dockerfile | 1 + app_python/docker-compose.yml | 12 ++++++++++++ app_python/timeweb/timeweb.py | 23 +++++++++++++++++++++++ k8s/12.md | 5 +++++ k8s/timeweb/files/config.json | 1 + k8s/timeweb/templates/config.yaml | 9 +++++++++ k8s/timeweb/templates/deployment.yaml | 8 ++++++++ 7 files changed, 59 insertions(+) create mode 100644 app_python/docker-compose.yml create mode 100644 k8s/12.md create mode 100644 k8s/timeweb/files/config.json create mode 100644 k8s/timeweb/templates/config.yaml diff --git a/app_python/Dockerfile b/app_python/Dockerfile index 1d0fc1eec..2030b1116 100644 --- a/app_python/Dockerfile +++ b/app_python/Dockerfile @@ -9,6 +9,7 @@ COPY --chown=python:python timeweb/requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY --chown=python:python timeweb/timeweb.py timeweb/wsgi.py /app/ +RUN mkdir /data && chown python:python /data USER python diff --git a/app_python/docker-compose.yml b/app_python/docker-compose.yml new file mode 100644 index 000000000..e01cc13df --- /dev/null +++ b/app_python/docker-compose.yml @@ -0,0 +1,12 @@ +version: '3.4' +services: + timeweb: + image: passkeyra/timeweb:latest + restart: unless-stopped + ports: + - '50000:50000' + volumes: + - data:/data + +volumes: + data: {} diff --git a/app_python/timeweb/timeweb.py b/app_python/timeweb/timeweb.py index e5e0de0d7..5b87487be 100644 --- a/app_python/timeweb/timeweb.py +++ b/app_python/timeweb/timeweb.py @@ -2,6 +2,8 @@ """This web app shows Moscow time""" from datetime import datetime import pytz +import sqlite3 +import json from flask import Flask UTC = pytz.utc @@ -9,6 +11,23 @@ app = Flask(__name__) +con = sqlite3.connect("/data/visits.db") +cur = con.cursor() +cur.execute('CREATE TABLE IF NOT EXISTS visits (visit_time TEXT)') +con.commit() +cur.close() + +@app.route('/visits') +def visits(): + cur = con.cursor() + data = cur.execute('SELECT * FROM visits') + output = [] + for d in data.fetchall(): + output.append(d[0]) + cur.close() + return json.dumps(output) + + @app.route('/') def index(): """ @@ -19,6 +38,10 @@ def index(): """ current_time = datetime.now(timez_msk) current_time = current_time.strftime('%H:%M:%S') + cur = con.cursor() + cur.execute(f'INSERT INTO visits (visit_time) VALUES (\'{current_time}\')') + con.commit() + cur.close() return f'Hello! Current time in Moscow is: {current_time}' if __name__ == '__main__': diff --git a/k8s/12.md b/k8s/12.md new file mode 100644 index 000000000..1916242c9 --- /dev/null +++ b/k8s/12.md @@ -0,0 +1,5 @@ +# K8s ConfigMaps + +* Add config and upgrade the helm package + +![](https://i.ibb.co/jWryK3L/1.png) diff --git a/k8s/timeweb/files/config.json b/k8s/timeweb/files/config.json new file mode 100644 index 000000000..ccb8ecab6 --- /dev/null +++ b/k8s/timeweb/files/config.json @@ -0,0 +1 @@ +{"mydata":"hereismydata"} diff --git a/k8s/timeweb/templates/config.yaml b/k8s/timeweb/templates/config.yaml new file mode 100644 index 000000000..26ab299b5 --- /dev/null +++ b/k8s/timeweb/templates/config.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "timeweb.fullname" . }}-config + labels: + {{- include "timeweb.labels" . | nindent 4 }} +data: + config.json: |- + {{ .Files.Get "files/config.json" }} diff --git a/k8s/timeweb/templates/deployment.yaml b/k8s/timeweb/templates/deployment.yaml index dca3136a6..b4f92e4ad 100644 --- a/k8s/timeweb/templates/deployment.yaml +++ b/k8s/timeweb/templates/deployment.yaml @@ -53,6 +53,14 @@ spec: port: 50000 resources: {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - name: config-volume + mountPath: /config.json + subPath: config.json + volumes: + - name: config-volume + configMap: + name: {{ include "timeweb.fullname" . }}-config {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }}