Still in the
kube_demo directory, there are the subdirectories
mini_postgres,
rs_backend and
mini_nginx that were used to
build the images for the web application in
this tutorial.
Build these images and publish them to the registry, taking care to
replace
gX-YY with the name of the selected user:
GXYY=g9-99 # substitute with the selected user
podman build -t mini_postgres ./mini_postgres
podman build -t rs_backend ./rs_backend
podman build -t mini_nginx ./mini_nginx
podman image ls
podman tag mini_postgres registry.kube.enib.fr/${GXYY}-mini_postgres:1.0
podman tag rs_backend registry.kube.enib.fr/${GXYY}-rs_backend:1.0
podman tag mini_nginx registry.kube.enib.fr/${GXYY}-mini_nginx:1.0
podman image ls
podman push registry.kube.enib.fr/${GXYY}-mini_postgres:1.0
podman push registry.kube.enib.fr/${GXYY}-rs_backend:1.0
podman push registry.kube.enib.fr/${GXYY}-mini_nginx:1.0
./kube_enib_registry.py
Using a database requires the same credentials as in the previous
experiment; specify them in the
secret demo-sec
Normally, the demo-sec secret still exists after the previous
experiment.
The following command will create it if it did not exist or update it
if necessary.
:
kubectl create secret generic demo-sec \
--from-literal=APP_DB_USER=demo_user \
--from-literal=APP_DB_PASS=demo_secret \
--dry-run=client -o yaml | kubectl apply -f -
Check that it is present with
kubectl get secrets, and its description
with
kubectl describe secrets demo-sec.
Edit the contents of the supplied
mini_app_demo.yaml file, replacing
gX-YY in the name of your images and in the
host parameter of
demo-ingress.
Study the constituent elements of this file:
- The config-map demo-cfg contains the name of the database,
an initialisation file for the database and a configuration file
for the frontend.
- The deployment demo-db-dpl will create a pod running your
mini_postgres image
In theory, a database should not be described in a deployment
but in a stateful-set.
This type of resource makes it possible to consider replicating
databases, but this mechanism is rather complicated in practice.
To keep the approach consistent throughout this tutorial, you will use
a deployment; you simply need to make sure never to replicate it.
.
This pod will use the database name specified by demo-cfg and the
credentials specified by demo-sec.
It will mount the init.sql file of demo-cfg for its
initialisation.
It will also mount the storage volume to make the data persistent.
- The service demo-db-svc will allow connections to port 5432
(standard for PostgreSQL) of the pod belonging to the
demo-db-dpl deployment.
- The deployment demo-backend-dpl will create the pod running your
rs_backend image.
This pod will connect to the demo-db-svc service on port 5432.
It will use the database name specified by demo-cfg and the
credentials specified by demo-sec.
- The service demo-backend-svc will allow connections to port 9988
(hardcoded in your backend) of the pod belonging to the
demo-backend-dpl deployment.
- The deployment demo-frontend-dpl will create the pod running your
mini_nginx image.
This pod will listen to HTTP connections (port 80).
It will connect to the demo-backend-svc service on port 9988.
It will mount the nginx.conf file of demo-cfg for its
configuration.
It will also mount the demo-site config-map as the contents
of the web site to expose; this config-map will be introduced later.
- The service demo-frontend-svc will allow connections to port 80
(HTTP) of the pod belonging to the demo-frontend-dpl deployment.
- The ingress demo-ingress acts as the HTTP frontend for the
Kubernetes cluster as a whole.
HTTP requests to your virtual host gX-YY.kube.enib.fr
will be forwarded to your demo-frontend-svc service.
Note that the
demo-frontend-dpl deployment relies on the
demo-site
config-map, which does not yet exist.
Indeed, it is a directory containing multiple files whose contents cannot be
described by a few lines in the previous file.
Create
demo-site from the command line so that it contains the contents
of the subdirectory:
kubectl create configmap demo-site --from-file=./site/. \
--dry-run=client -o yaml | kubectl apply -f -
Check that it is present with
kubectl get configmaps, and its description
with
kubectl describe configmaps demo-site.
Make the cluster process the previous file with the command
kubectl apply -f mini_app_demo.yaml.
Check the status of the resources with:
- kubectl get configmaps
- kubectl describe configmaps demo-cfg
- kubectl get deployments
- kubectl get pods
- kubectl get services
They are all of type ClusterIP since they are only used for
communication within the cluster.
- kubectl get ingresses
It is demo-ingress that makes your HTTP application accessible from
outside the cluster, as the virtual host
gX-YY.kube.enib.fr.
It is now possible to interact with your web application using
firefox http://gX-YY.kube.enib.fr/,
replacing with the name of the selected user.
Check that the application works as expected and that many clients
can use your web application simultaneously.
In the
mini_app_demo.yaml file, modify the
replicas property of
the
demo-backend-dpl and
demo-frontend-dpl deployments to
request the execution of
2 pods for each of these deployment.
Be careful
not to replicate the database; indeed, since data storage
on disk is unique, simultaneous writes by several pods could compromise
these files
Database replication mechanisms do exist, but this must be taken into
account by the database software itself, which is beyond the
scope of this tutorial.
.
Apply this change with
kubectl apply -f mini_app_demo.yaml and check
that it has been taken into account with
kubectl get deployments,
which should indicate
2/2 for the backend and the frontend.
The
kubectl get pods command should show you the names of the four
pods belonging to these deployments.
In two terminals, monitor the logs produced by each of the two backend pods
with
kubectl logs -f demo-backend-dpl-DDDDDDDDDD-PPPPP,
replacing precisely the name of the pods.
In two other terminals, do the same for the frontend pods with
kubectl logs -f demo-frontend-dpl-DDDDDDDDDD-PPPPP,
replacing precisely the name of the pods.
Make numerous requests to your web application from your browser (using
“Reload” repeatedly, for example) and observe in the logs the
distribution of processing across the multiple frontends and
backends
You can skip lines in the log terminals, with the Enter key,
in order to make it easier to observe the scrolling of the various
lines.
.
Les mécanismes de réplication dynamique et d'équilibrage de charge
fournis par le cluster Kubernetes offrent la possibilité à votre
application (ou au autre bien plus exigente) de passer à l'échelle
pour réagir à une éventuelle très forte charge induite par les
sollicitations de nombreux clients.
The web application deployed on the Kubernetes cluster currently only uses
HTTP, whereas your frontend image is potentially capable of handling
HTTPS.
Indeed, the usual practice is to entrust the handling of HTTP to the
cluster's ingresses rather than to the pods.
You can already see that, by default, your ingress works over HTTPS with
firefox https://gX-YY.kube.enib.fr/,
replacing the name of the selected user.
However, you have to accept the certificate obtained because it is signed by
the cluster itself (your browser does not know this certification
authority).
Even though your application is accessible over HTTPS, the situation is not
satisfactory since there is nothing preventing it from also being used
over HTTP.
The goal is then to choose the certificate your web application will
explicitly provide.
For the purposes of this tutorial, this will be a self-signed certificate,
but in practice it could just as well be signed by a recognised
certification authority.
Attempts to use the application over HTTP will also have to be systematically
redirected to HTTPS.
Generate your own self-signed certificate with this command,
replacing the name of the selected user:
openssl req -x509 -newkey rsa:2048 -nodes -days 3650 \
-keyout https_server.key -out https_server.crt \
-subj '/CN=gX-YY.kube.enib.fr'
Import this certificate and its associated private key into an
ingress-tls-sec secret:
kubectl create secret tls ingress-tls-sec \
--cert=https_server.crt --key=https_server.key
Check that it is present with
kubectl get secrets, and its description
with
kubectl describe secrets ingress-tls-sec.
Complete the
mini_app_demo.yaml file with the following contents:
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: redirect-https
spec:
redirectScheme:
scheme: https
permanent: true
The
--- marker is important, as it is used to separate the various
resources in the same file.
This
redirect-https middleware redirects HTTP requests to their
HTTPS equivalent.
Still in the
mini_app_demo.yaml file, modify the
demo-ingress
ingress to use the
redirect-https middleware and the
ingress-tls-sec secret, replacing the name of the selected user:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-ingress
annotations:
traefik.ingress.kubernetes.io/router.middlewares: gX-YY-ns-redirect-https@kubernetescrd
spec:
tls:
- hosts:
- gX-YY.kube.enib.fr
secretName: ingress-tls-sec
rules:
- host: gX-YY.kube.enib.fr
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: demo-frontend-svc
port:
number: 80
Apply this change with
kubectl apply -f mini_app_demo.yaml and check
these ressources with:
- kubectl get middlewares
- kubectl describe ingresses demo-ingress
Try accessing your web application over HTTP using
firefox http://gX-YY.kube.enib.fr/,
replacing the name of the selected user.
Verify that you are redirected to the HTTPS equivalent.
You will need to accept the self-signed certificate you installed;
however, with a properly issued certificate, this connection would
be secure.
To release the resources from this first experiment, use:
kubectl delete deployments demo-backend-dpl demo-db-dpl demo-frontend-dpl
kubectl delete services demo-backend-svc demo-db-svc demo-frontend-svc
kubectl delete ingresses demo-ingress
kubectl delete configmaps demo-cfg demo-site
Check, with
kubectl get pods,
kubectl get deployments,
kubectl get services,
kubectl get configmaps and
kubectl get ingresses , that nothing remains
As part of this tutorial, deletion of secrets has been
disabled to avoid risking loss of access to the cluster
in the event of accidental deletion of account-token.
.