Skip to content

AlexeyAlexey/example_service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chmod +x bin/bundle

chmod +x bin/console

chmod +x bin/docker-entrypoint.sh

REST API

Grape

Puma

bundle exec puma
curl http://localhost:3000/v1/hello_world/

systemd

DB Access

Active Record

SERVICE_APP_ENV=development rake --tasks
SERVICE_APP_ENV=development rake db:create_migration create_user

Sequel

branch: feature/sequel-db-access-toolkit

Jobs

SERVICE_APP_ENV=development SIDEKIQ_REDIS_URL='redis://127.0.0.1:6379/0' bundle exec sidekiq -r ./config/environment.rb

Docker

docker build -f restapi.Dockerfile . -t restapi-service -t restapi-service:1.0

docker build -f jobs.Dockerfile . -t jobs-service -t jobs-service:1.0

If you want to set environment variables when you run a container

docker run -e SERVICE_APP_ENV='development' -e SIDEKIQ_REDIS_URL='redis://redis:6379/0' jobs-service

docker-compose up

docker-compose run --rm -e SERVICE_APP_ENV=test restapi rspec

docker-compose exec -e SERVICE_APP_ENV=test restapi rspec

Creating DB

docker-compose exec restapi rake db:create
docker-compose exec restapi rake db:migrate

List of tasks

docker-compose exec restapi rake -T

Production Environment

docker build -f restapi.Dockerfile.prod -t <username>/<image name>:prod .
docker build -f jobs.Dockerfile.prod -t <username>/<image name>:prod .

Kubernetes

Installing

https://minikube.sigs.k8s.io/docs/start/

Drivers

https://minikube.sigs.k8s.io/docs/drivers/

The docker minikube driver was used

minikube start --driver docker

Configuration

Deployment (Doc)

Service (Doc)

Deployment is for stateless Apps

Statefulset is for statefull Apps or Databases (Doc)

DB are often hosted outside of Kubernetes cluster

DB

Deployment was used (replicas: 1) to simplify an example

postgres-config.yaml

Secrets

A Secret is an object that contains a small amount of sensitive data such as a password, a token, or a key. Such information might otherwise be put in a Pod specification or in a container image. Using a Secret means that you don't need to include confidential data in your application code. (Doc)

Don't use the following simple example in a production env. You should read documentation to know more about it.

postgres-secret.yaml

Deployment-Service

postgres.yaml 

App

Deployment-Service

restapi.yaml

Exposing the Service (Doc)

spec:
  type: NodePort

Exposing the Service on each Node's IP at static port

nodePort: 30000

To use an image without uploading it

To use an image without uploading it, you can follow these steps. It is important that you be in the same shell since you are setting environment variables!

  1. Setting the environment variables with
  example_service$ eval $(minikube docker-env)
  1. Building the image (eg docker build -t my-image .)
example_service$ docker build --no-cache -f restapi.Dockerfile . -t restapi-service -t restapi-service:2.0
  1. Setting the image in the pod spec like the build tag
restapi.yaml
spec:
  containers:
  - name: restapi
    image: restapi-service:2.0
    imagePullPolicy: Never
  1. Setting the imagePullPolicy to Never, otherwise, Kubernetes will try to download the image.
restapi.yaml
spec:
  containers:
  - name: restapi
    image: restapi-service:2.0
    imagePullPolicy: Never

imagePullPolicy Never

Deploying

example_service$ kubectl apply -f postgres-config.yaml
example_service$ kubectl apply -f postgres-secret.yaml
example_service$ kubectl apply -f postgres.yaml
deployment.apps/postgres-deployment created
service/postgres-service created
$ kubectl apply -f restapi.yaml
deployment.apps/restapi-deployment created
service/restapi-service created

You can use the following commands to get more details

$ kubectl get all

$ kubectl get configmap

$ kubectl get secret
$ kubectl get pod
NAME                                   READY   STATUS    RESTARTS   AGE
postgres-deployment-6dd86bd65f-v9wbb   1/1     Running   0          18h
restapi-deployment-68b659d5fd-qjkp6    1/1     Running   0          34m
$ kubectl describe service restapi-service
$ kubectl describe pod restapi-deployment-68b659d5fd-qjkp6

View logs of container

$ kubectl get pod
NAME                                   READY   STATUS    RESTARTS   AGE
postgres-deployment-6dd86bd65f-v9wbb   1/1     Running   0          18h
restapi-deployment-68b659d5fd-qjkp6    1/1     Running   0          34m
$ kubectl logs restapi-deployment-7468df8fcc-h9rvb -f

Getting IP adress to access the restapi app

$ kubectl get svc
NAME               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes         ClusterIP   10.96.0.1        <none>        443/TCP          40h
postgres-service   ClusterIP   10.97.184.253    <none>        5432/TCP         19h
restapi-service    NodePort    10.110.203.236   <none>        3000:30000/TCP   19h
$ kubectl get node -o wide
NAME       STATUS   ROLES           AGE   VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION       CONTAINER-RUNTIME
minikube   Ready    control-plane   21h   v1.30.0   192.168.49.2   <none>        Ubuntu 22.04.4 LTS   5.15.0-102-generic   docker://26.0.1

Testing

curl http://192.168.49.2:30000/v1/hello_world/
{"message":"Hello World"}
curl http://192.168.49.2:30000/v1/hello_world/db_version
{"message":"PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit"}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published