---
# System prepended metadata

title: K8s code record

---

# K8s code record
[TOC]

## Prepare flask env in container
```bash=
sudo apt install python3-pip
pip install flask
sudo apt install python3-flask
cd /flask
export FLASK_APP=record_player.py
flask run --host k8s-master.phoebe.ncnu.org --port 80 > log.txt 2>&1 &
```
- build image file with container: [phoebe61g/record_player](https://hub.docker.com/repository/docker/phoebe61g/record_player)

## Webpages

```python= record_player.py
from flask import Flask, render_template
app = Flask(__name__)
import os

@app.route('/')
def index():
    return '<h1>Hello World!</h1>'

@app.route('/record_list')
def record_list():
    file_path = os.path.dirname(__file__) + '/static/'
    record_list = os.listdir(file_path)
    return render_template('record_list.html', record_list=record_list)
```

```htmlembedded= record_list.html
<h1>Record List</h1>
<table border='1' width="1000">
{% for record in record_list %}
    <tr>
        <td> {{ record }} </td>
        <td>
                <audio controls>
                                <source src="{{ url_for('static', filename = record ) }}" type="audio/wav">
                                Your browser does not support the audio element.
                        </audio>
        </td>
    </tr>
{% endfor %}
</table>
```
---
## PV & PVC
### PV
```yaml= pv-record.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-record
  labels:
    storage: nfs 
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 10.22.22.174
    path: /mnt/records
```
```bash=
kubectl apply -f pv-record.yaml
kubectl get pv pv-record
```
### PVC
```yaml= pvc-record.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-record
  namespace: default
spec:
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 8Gi
  selector:
    matchLabels:
      storage: "nfs"
```

```bash=
kubectl apply -f pvc-record.yaml
```
---
## Pod

```yaml= pod-record.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-record
  labels:
    app: records
spec:
  containers:
    - name: record-player
      image: phoebe61g/record_player
      command: ["/bin/sh","-c"]
      args: ["cd /flask; export FLASK_APP=record_player.py; flask run -h 0.0.0.0"]
      volumeMounts:
      - name: record-data
        mountPath: /flask/static
      ports:
      - containerPort: 5000
  volumes:
  - name: record-data
    persistentVolumeClaim:
      claimName: pvc-record
```

```bash=
kubectl apply -f pod-record.yaml
```
- check file in container
    ```bash=
    kubectl exec --stdin --tty pod-record -- /bin/bash
    ```
---
## Service

```yaml= svc-record.yaml
apiVersion: v1
kind: Service
metadata:
  name: svc-record
spec:
  type: LoadBalancer
  externalIPs:
  - 10.22.23.169
  ports:
  - port: 5000
    targetPort: 5000
  selector:
    app: records
```
```bash=
kubectl apply -f svc-record.yaml
```

## Problems
- [My kubernetes pods keep crashing with "CrashLoopBackOff" but I can't find any log](https://stackoverflow.com/questions/41604499/my-kubernetes-pods-keep-crashing-with-crashloopbackoff-but-i-cant-find-any-lo)