Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions LAB11.md

This file was deleted.

54 changes: 54 additions & 0 deletions LAB12.md
Original file line number Diff line number Diff line change
@@ -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/
1 change: 1 addition & 0 deletions app_python/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions app_python/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.4'
services:
timeweb:
image: passkeyra/timeweb:latest
restart: unless-stopped
ports:
- '50000:50000'
volumes:
- data:/data

volumes:
data: {}
10 changes: 2 additions & 8 deletions app_python/timeweb/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
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
setuptools==65.5.1
Flask
gunicorn
pytz
requests
23 changes: 23 additions & 0 deletions app_python/timeweb/timeweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@
"""This web app shows Moscow time"""
from datetime import datetime
import pytz
import sqlite3
import json
from flask import Flask

UTC = pytz.utc
timez_msk = pytz.timezone('Europe/Moscow')

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():
"""
Expand All @@ -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__':
Expand Down
5 changes: 5 additions & 0 deletions k8s/12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# K8s ConfigMaps

* Add config and upgrade the helm package

![](https://i.ibb.co/jWryK3L/1.png)
1 change: 1 addition & 0 deletions k8s/timeweb/files/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"mydata":"hereismydata"}
9 changes: 9 additions & 0 deletions k8s/timeweb/templates/config.yaml
Original file line number Diff line number Diff line change
@@ -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" }}
8 changes: 8 additions & 0 deletions k8s/timeweb/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down