🦙 Update in build and deploy

cheshire 2023-10-21 00:11:34 +02:00
parent d0898c804a
commit c4cdf80044
9 changed files with 411 additions and 364 deletions

View File

@ -18,6 +18,7 @@ Intro to hscloud service creation. This set of documents aims to document the pr
- Deploying the ldapweb service ([en](./en/03-create-service.md) / [pl](./pl/03-create-service.md))
## Building own service
## Building and deploying own service
- Building a simple Flask web application ([en](./en/04-build-own-web-app.md) / [pl](./pl/04-build-own-web-app.md))
- Building a simple Flask web application ([en](./en/04-build-web-app.md) / [pl](./pl/04-build-web-app.md))
- Deploying a docker image on hscloud ([en](./en/05-deploy-web-app.md) / [pl](./pl/05-deploy-web-app.md))

View File

@ -1,180 +0,0 @@
# Building your own Flask web service
In this chapter, we will build our own web service using the Flask framework.
## Requirements
You will need two shells (you can open two terminals or even two ssh sessions), in which one will be as nix-shell and the other as standard bash environment. For now, current hscloud environment doesn't support docker, and you actually need it to build and register image in the registry.
You can install `docker` with:
```bash
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
```
You can actually create, build and register docker image in completely other environment, and just deploy it to kubernetes in hscloud environment.
## Steps
We create a directory for our service:
```bash
hs_username=$USER # if your username is the same as your SSO username
mkdir personal/$hs_username/myflask
cd personal/$hs_username/myflask
```
We create a simple Flask application:
```py
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
```
We can check if the application works by running it:
```bash
pip3 install flask
python3 app.py
```
Now we create `Dockerfile` file for our application:
```Dockerfile
# Use an official Python runtime as a base image
FROM python:3.8-slim-buster
# Set the working directory in docker
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --upgrade pip && \
pip install flask gunicorn
# Set environment variables
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
# Expose port 5000 for the Flask app to listen on
EXPOSE 5000
# Define command to run the application using gunicorn
CMD ["gunicorn", "-w", "4", "-b", ":5000", "app:app"]
```
We can test if the application works in Docker:
```bash
docker build -t myflask . # Build the image
docker run -d -p 5000:5000 myflask # Run the container
```
We can check if the application works by opening the browser at `http://localhost:5000`.
## Pushing the image to the registry
First, we need to authenticate to the registry:
```
https://registry.k0.hswaw.net
```
You should get command similar to:
```
docker login -u $hs_username -p *************** https://registry.k0.hswaw.net
```
Now we need to create local server for our docker image:
```bash
docker tag myflask registry.k0.hswaw.net/$hs_username/myflask:latest
docker push registry.k0.hswaw.net/$hs_username/myflask:latest
```
We can now create a `prod.jsonnet` file for our application:
```jsonnet
// Remember to replace $hs_username with your SSO username!
local kube = import "../../../kube/kube.libsonnet";
{
local top = self,
local cfg = self.cfg,
cfg:: {
name: 'myflask',
namespace: 'personal-$hs_username',
domain: 'myflask.$hs_username.hscloud.ovh',
image: 'myflask',
},
ns: kube.Namespace(cfg.namespace),
deployment: top.ns.Contain(kube.Deployment(cfg.name)) {
spec+: {
replicas: 1,
template+: {
spec+: {
containers_: {
default: kube.Container("default") {
image: cfg.image,
ports_: {
http: { containerPort: 5000 },
},
},
},
},
},
},
},
service: top.ns.Contain(kube.Service(cfg.name)) {
target_pod:: top.deployment.spec.template,
},
ingress: top.ns.Contain(kube.Ingress(cfg.name)) {
spec+: {
rules: [
{
host: cfg.domain,
http: {
paths: [
{ path: "/", backend: top.service.name_port },
],
},
},
],
},
},
}
```
We can now deploy our application:
```bash
kubecfg update prod.jsonnet
```
The application should be available at `http://myflask.$hs_username.hscloud.ovh`.
To remove the application, execute:
```bash
kubecfg delete prod.jsonnet
```

111
en/04-build-web-app.md Normal file
View File

@ -0,0 +1,111 @@
# Building your own Flask web service
In this chapter, we will build our own web service using the Flask framework, dockerize it and register it in the registry.
## Requirements
You will require `docker` in this example. Install it from your standard shell.
```bash
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
```
You can actually create, build and register docker image in completely other environment, and just deploy it to kubernetes in hscloud environment.
## Steps
We create a simple Flask application:
```py
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
```
We can check if the application works by running it:
```bash
pip3 install flask
python3 app.py
```
Now we create `Dockerfile` file for our application:
```Dockerfile
# Use an official Python runtime as a base image
FROM python:3.8-slim-buster
# Set the working directory in docker
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --upgrade pip && \
pip install flask gunicorn
# Set environment variables
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
# Expose port 5000 for the Flask app to listen on
EXPOSE 5000
# Define command to run the application using gunicorn
CMD ["gunicorn", "-w", "4", "-b", ":5000", "app:app"]
```
We can test if the application works in Docker:
```bash
docker build -t myflask . # Build the image
docker run -d -p 5000:5000 myflask # Run the container
```
We can check if the application works by opening the browser at `http://localhost:5000`.
## Pushing the image to the registry
First, we need to authenticate to the registry:
```
https://registry.k0.hswaw.net
```
You should get command similar to:
```
docker login -u $hs_username -p *************** https://registry.k0.hswaw.net
```
Now we need to create local server for our docker image:
```bash
docker tag myflask registry.k0.hswaw.net/$hs_username/myflask:latest
docker push registry.k0.hswaw.net/$hs_username/myflask:latest
```
To check if the image is in the registry, you can use:
```bash
curl -X GET https://registry.k0.hswaw.net/v2/$hs_username/myflask/tags/list
```
To remove the image from the registry, you can use:
```bash
docker rmi registry.k0.hswaw.net/$hs_username/myflask:latest
```

91
en/05-deploy-web-app.md Normal file
View File

@ -0,0 +1,91 @@
# Deploying docker image on hscloud
In this chapter, we will deploy our registered docker image on hscloud as a simple HTTP kube service with HTTPS from Let's Encrypt.
## Requirements
We need a registered docker image in the registry. If you don't have one, you can create it by following the [Building your own Flask web service](04-build-web-app.md) chapter. We also need a running `prodaccess` authorization (details: [Building environment](02-build-environment.md)).
## Steps
We can now create a `prod.jsonnet` file for our application:
```jsonnet
# Remember to replace $hs_username with your SSO username!
local kube = import "../../../kube/kube.libsonnet";
{
local top = self,
local cfg = self.cfg,
cfg:: {
name: 'web-app',
namespace: 'personal-$hs_username',
domain: 'web-app.$hs_username.hscloud.ovh',
image: 'registry.k0.hswaw.net/$hs_username/myflask:latest',
},
ns: kube.Namespace(cfg.namespace),
deployment: top.ns.Contain(kube.Deployment(cfg.name)) {
spec+: {
replicas: 1,
template+: {
spec+: {
containers_: {
default: kube.Container("default") {
image: cfg.image,
ports_: {
http: { containerPort: 5000 },
}
},
},
},
},
},
},
service: top.ns.Contain(kube.Service(cfg.name)) {
target_pod:: top.deployment.spec.template,
},
ingress: top.ns.Contain(kube.Ingress(cfg.name)) {
metadata+: {
annotations+: {
"kubernetes.io/tls-acme": "true",
"cert-manager.io/cluster-issuer": "letsencrypt-prod",
"nginx.ingress.kubernetes.io/proxy-body-size": "0",
},
},
spec+: {
tls: [ { hosts: [ cfg.domain ], secretName: cfg.name + "-tls" } ],
rules: [
{
host: cfg.domain,
http: {
paths: [
{ path: "/", backend: top.service.name_port },
],
},
},
],
},
},
}
```
We can now deploy our application:
```bash
kubecfg update prod.jsonnet
```
The application should be available at `http://web-app.$hs_username.hscloud.ovh`. You will have to wait several seconds for the working HTTPS service.
To remove the application, execute:
```bash
kubecfg delete prod.jsonnet
```

View File

@ -17,4 +17,5 @@ Introduction to creating services in hscloud. This set of documents is intended
## Building own service
- [Building a simple Flask web application](04-build-own-web-app.md)
- [Building a simple Flask web application](04-build-web-app.md)
- [Deploying docker image on hscloud](05-deploy-web-app.md)

View File

@ -1,180 +0,0 @@
# Tworzenie własnej usługi internetowej Flask
W tym rozdziale zbudujemy własną usługę sieciową przy użyciu frameworka Flask.
## Wymagania
Będziesz potrzebować dwóch powłok (możesz nawet otworzyć dwa terminale bądź dwukrotnie zalogować się przez ssh do zdalnego serwera), gdzie w jednej operujesz w powłoce nix (`nix-shell`), druga natomiast jest typowym środowiskiem bashowym Twojego systemu. Bieżące środowisko hscloud nie wspiera dockera, którego jednak potrzebujesz by budować i rejestrować obrazy w rejestrze hscloud.
Możesz zainstalować `docker` poprzez:
```bash
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
```
Dodatkowo - możesz tworzyć, budować i rejestrować obraz dockera w kompletnie osobnym środowisku niż to w którym publikujesz go na kubernetesach w środowisku hscloud.
## Kroki
Tworzymy katalog dla naszej usługi:
```bash
hs_username=$USER # jeśli twoja nazwa użytkownika jest taka sama jak twoja nazwa użytkownika SSO
mkdir personal/$hs_username/myflask
cd personal/$hs_username/myflask
```
Tworzymy prostą aplikację Flask:
```py
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
```
Możemy sprawdzić, czy aplikacja działa, uruchamiając ją:
```bash
pip3 install flask
python3 app.py
```
Teraz tworzymy plik `Dockerfile` dla naszej aplikacji:
```Dockerfile
# Use an official Python runtime as a base image
FROM python:3.8-slim-buster
# Set the working directory in docker
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --upgrade pip && \
pip install flask gunicorn
# Set environment variables
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
# Expose port 5000 for the Flask app to listen on
EXPOSE 5000
# Define command to run the application using gunicorn
CMD ["gunicorn", "-w", "4", "-b", ":5000", "app:app"]
```
Możemy sprawdzić, czy aplikacja działa w Dockerze (wymaga zainstalowanego Docker Engine):
```bash
docker build -t myflask . # Build the image
docker run -d -p 5000:5000 myflask # Run the container
```
Sprawdzamy, czy aplikacja działa, otwierając przeglądarkę pod adresem `http://localhost:5000`.
## Przesyłanie obrazu do rejestru
Najpierw musimy uwierzytelnić się w rejestrze:
```
https://registry.k0.hswaw.net
```
Powinniśmy otrzymać polecenie podobne do:
```
docker login -u $hs_username -p *************** https://registry.k0.hswaw.net
```
Teraz musimy utworzyć lokalny serwer dla naszego obrazu docker:
```bash
docker tag myflask registry.k0.hswaw.net/$hs_username/myflask:latest
docker push registry.k0.hswaw.net/$hs_username/myflask:latest
```
Możemy teraz utworzyć plik `prod.jsonnet` dla naszej aplikacji:
```jsonnet
// Pamiętaj, aby zastąpić $hs_username swoją nazwą użytkownika!
local kube = import "../../../kube/kube.libsonnet";
{
local top = self,
local cfg = self.cfg,
cfg:: {
name: 'myflask',
namespace: 'personal-$hs_username',
domain: 'myflask.$hs_username.hscloud.ovh',
image: 'myflask',
},
ns: kube.Namespace(cfg.namespace),
deployment: top.ns.Contain(kube.Deployment(cfg.name)) {
spec+: {
replicas: 1,
template+: {
spec+: {
containers_: {
default: kube.Container("default") {
image: cfg.image,
ports_: {
http: { containerPort: 5000 },
},
},
},
},
},
},
},
service: top.ns.Contain(kube.Service(cfg.name)) {
target_pod:: top.deployment.spec.template,
},
ingress: top.ns.Contain(kube.Ingress(cfg.name)) {
spec+: {
rules: [
{
host: cfg.domain,
http: {
paths: [
{ path: "/", backend: top.service.name_port },
],
},
},
],
},
},
}
```
Możemy teraz wdrożyć naszą aplikację:
```bash
kubecfg update prod.jsonnet
```
Aplikacja powinna być dostępna pod adresem `http://myflask.$hs_username.hscloud.ovh`.
Aby usunąć aplikację, wykonujemy:
```bash
kubecfg delete prod.jsonnet
```

111
pl/04-build-web-app.md Normal file
View File

@ -0,0 +1,111 @@
# Tworzenie własnej usługi internetowej Flask
W tym rozdziale zbudujemy własną usługę sieciową przy użyciu frameworka Flask, dockeryzujemy ją i zarejestrujemy w rejestrze.
## Wymagania
W tym przykładzie wymagany będzie `docker`. Zainstaluj go ze swojej standardowej powłoki.
```bash
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
```
W rzeczywistości można utworzyć, zbudować i zarejestrować obraz docker w zupełnie innym środowisku i po prostu wdrożyć go na kubernetes w środowisku hscloud.
## Kroki
Stworzymy prostą aplikację Flask:
```py
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
```
Możemy sprawdzić, czy aplikacja działa, uruchamiając ją:
```bash
pip3 install flask
python3 app.py
```
Teraz tworzymy plik `Dockerfile` dla naszej aplikacji:
```Dockerfile
# Use an official Python runtime as a base image
FROM python:3.8-slim-buster
# Set the working directory in docker
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --upgrade pip && \
pip install flask gunicorn
# Set environment variables
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
# Expose port 5000 for the Flask app to listen on
EXPOSE 5000
# Define command to run the application using gunicorn
CMD ["gunicorn", "-w", "4", "-b", ":5000", "app:app"]
```
Możemy sprawdzić, czy aplikacja działa w Dockerze:
```bash
docker build -t myflask . # Build the image
docker run -d -p 5000:5000 myflask # Run the container
```
Otwieramy przeglądarkę pod adresem `http://localhost:5000`.
## Przesyłanie obrazu do rejestru
Najpierw musimy uwierzytelnić się w rejestrze:
```
https://registry.k0.hswaw.net
```
Otrzymasz polecenie podobne do:
```
docker login -u $hs_username -p *************** https://registry.k0.hswaw.net
```
Teraz musimy utworzyć lokalny serwer dla naszego obrazu docker:
```bash
docker tag myflask registry.k0.hswaw.net/$hs_username/myflask:latest
docker push registry.k0.hswaw.net/$hs_username/myflask:latest
```
Aby sprawdzić, czy obraz znajduje się w rejestrze:
```bash
curl -X GET https://registry.k0.hswaw.net/v2/$hs_username/myflask/tags/list
```
Aby usunąć obraz z rejestru:
```bash
docker rmi registry.k0.hswaw.net/$hs_username/myflask:latest
```

91
pl/05-deploy-web-app.md Normal file
View File

@ -0,0 +1,91 @@
# Wdrażanie obrazu docker na hscloud
W tym rozdziale wdrożymy nasz zarejestrowany obraz docker na hscloud jako prostą usługę webową z HTTPS z Let's Encrypt.
## Wymagania
Potrzebujemy zarejestrowanego obrazu docker w rejestrze. Jeśli go nie masz, możesz go utworzyć, postępując zgodnie z rozdziałem [Building your own Flask web service](04-build-web-app.md). Potrzebujemy również działającej autoryzacji `prodaccess` (szczegóły: [Budowa środowiska](./02-build-environment.md)).
## Kroki
Możemy teraz utworzyć plik `prod.jsonnet` dla naszej aplikacji:
```jsonnet
# Pamiętaj, aby zastąpić $hs_username nazwą użytkownika SSO!
local kube = import "../../../kube/kube.libsonnet";
{
local top = self,
local cfg = self.cfg,
cfg:: {
name: 'web-app',
namespace: 'personal-$hs_username',
domain: 'web-app.$hs_username.hscloud.ovh',
image: 'registry.k0.hswaw.net/$hs_username/myflask:latest',
},
ns: kube.Namespace(cfg.namespace),
deployment: top.ns.Contain(kube.Deployment(cfg.name)) {
spec+: {
replicas: 1,
template+: {
spec+: {
containers_: {
default: kube.Container("default") {
image: cfg.image,
ports_: {
http: { containerPort: 5000 },
}
},
},
},
},
},
},
service: top.ns.Contain(kube.Service(cfg.name)) {
target_pod:: top.deployment.spec.template,
},
ingress: top.ns.Contain(kube.Ingress(cfg.name)) {
metadata+: {
annotations+: {
"kubernetes.io/tls-acme": "true",
"cert-manager.io/cluster-issuer": "letsencrypt-prod",
"nginx.ingress.kubernetes.io/proxy-body-size": "0",
},
},
spec+: {
tls: [ { hosts: [ cfg.domain ], secretName: cfg.name + "-tls" } ],
rules: [
{
host: cfg.domain,
http: {
paths: [
{ path: "/", backend: top.service.name_port },
],
},
},
],
},
},
}
```
Możemy teraz wdrożyć naszą aplikację:
```bash
kubecfg update prod.jsonnet
```
Aplikacja powinna być dostępna pod adresem `http://web-app.$hs_username.hscloud.ovh`. Należy poczekać kilka sekund na działającą usługę HTTPS.
Aby usunąć aplikację:
```bash
kubecfg delete prod.jsonnet
```

View File

@ -17,4 +17,5 @@ Wprowadzenie do tworzenia usług w hscloud. Niniejszy zestaw dokumentów ma na c
## Budowanie własnej usługi
- [Tworzenie prostej aplikacji internetowej Flask](04-build-own-web-app.md)
- [Tworzenie prostej aplikacji internetowej Flask](04-build-web-app.md)
- [Wdrażanie aplikacji internetowej](05-deploy-web-app.md)