Skip to content

Commit

Permalink
fix k8s manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
atrakic committed Sep 1, 2023
1 parent c721e35 commit 15d5605
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Example how to build and run a Machine Learning (ML) models using [Scikit-Learn]
![predict](https://miro.medium.com/v2/resize:fit:1150/format:webp/1*R6MR34xT4Ve6fI744EVN0A.png)


## Usage
## Deployment

- ML image:
```
Expand All @@ -15,22 +15,31 @@ $ docker run -it --rm -e CSV_FILE=/data/prices.csv -v $(PWD)/data/prices.csv:/da

- Web
```
$ docker run -it --rm -e MODEL_FILE=/app/model.pkl -v $(PWD)/ml-model/model.pkl:/app/model.pkl -p 8080:8080 ghcr.io/atrakic/ml-house-pricing-web:latest
$ docker run -it --rm -e MODEL_FILE=/app/model.pkl -v $(PWD)/ml-model/model.pkl:/app/model.pkl -p 8080:8080 ghcr.io/atrakic/ml-house-pricing-web:latest
```

## Docker-compose
### Docker-compose

```
$ docker-compose up --build --no-deps --remove-orphans -d
# Test
$ curl -d '{"rooms":2, "distance":20}' -H "Content-Type: application/json" \
-X POST http://localhost:5000/api
```

## Kubernetes
### Kubernetes

```
$ kubectl apply -f ./k8s-manifests/web.yml
```
# Test
$ kubectl describe -f k8s-manifests/web.yml
$ kubectl run -it --rm --image=curlimages/curl --restart=Never curl-test -- \
-d '{"rooms":2, "distance":20}' -H "Content-Type: application/json" \
-X POST http://$(k get svc ml-house-pricing-web --output=jsonpath='{.spec.clusterIPs[0]}'):80/api
```

## LICENSE
See [LICENSE](LICENSE) for details.
2 changes: 1 addition & 1 deletion frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ COPY ./src .
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

ARG PORT=5000
ARG PORT=8080
ENV PORT $PORT
EXPOSE $PORT

Expand Down
24 changes: 16 additions & 8 deletions k8s-manifests/web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
apiVersion: v1
kind: Service
metadata:
name: web
name: ml-house-pricing-web
labels:
app: web
spec:
ports:
- port: 8080
targetPort: 80
- port: 80
targetPort: 8080
name: web
selector:
app: web
Expand All @@ -32,24 +32,32 @@ spec:
containers:
- name: web
image: ghcr.io/atrakic/ml-house-pricing-web:latest
imagePullPolicy: IfNotPresent # Always
imagePullPolicy: Always # IfNotPresent
ports:
- containerPort: 80
- containerPort: 8080
name: web
env:
- name: PORT
value: "8080"
- name: MODEL_FILE
value: "/opt/model.pkl"
volumeMounts:
- name: data
mountPath: /opt
initContainers:
- name: init-model
# First we need to create model and serialise data on file
- name: init
image: ghcr.io/atrakic/ml-house-pricing-model:latest
restartPolicy: Always
imagePullPolicy: Always # IfNotPresent
# command: ['sh', '-c', 'tail -F /opt/logs.txt']
volumeMounts:
- name: data
mountPath: /opt
env:
- name: MODEL_FILE
value: /opt/model.pkl
- name: CSV_FILE
value: "https://raw.githubusercontent.com/atrakic/ml-model-house-pricing/data/prices.csv"
value: "https://raw.githubusercontent.com/atrakic/ml-model-house-pricing/main/data/prices.csv"
volumes:
- name: data
emptyDir: {}
1 change: 1 addition & 0 deletions ml-model/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
print(f"Model predict : {lm.predict([[15, 61]])}") # format of input

pickle.dump(lm, open(model_file, "wb")) # serialize and save the model to the file
print("Created model file : ", model_file)
9 changes: 9 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -e

curl -d '{"rooms":2, "distance":20}' -H "Content-Type: application/json" \
-X POST http://localhost:5000/api

kubectl run -it --rm --image=curlimages/curl --restart=Never curl-test -- \
-d '{"rooms":2, "distance":20}' -H "Content-Type: application/json" \
-X POST http://$(k get svc ml-house-pricing-web --output=jsonpath='{.spec.clusterIPs[0]}'):80/api

0 comments on commit 15d5605

Please sign in to comment.