Kamora lilemo tse 'maloa, indasteri ea nts'etsopele ea software e ba le phetoho ea paradigm. E 'ngoe ea liketsahalo tsena e ka bonoa e le thahasello e ntseng e hola mohopolong oa li-microservices. Leha li-microservices e se theknoloji e ncha ka ho fetisisa, ke morao tjena botumo ba eona bo eketsehile haholo.
Litšebeletso tse kholo tsa monolithic joale li nkeloa sebaka ke li-microservices tse ikemetseng, tse ikemetseng. Microservice e ka nkoa e le ts'ebeliso e sebeletsang morero o le mong le o ikhethileng haholo. Mohlala, e ka ba DBMS ea kamano, sesebelisoa sa Express, ts'ebeletso ea Solr.
Matsatsing ana, ho thata ho nahana ho theha sistimi e ncha ea software ntle le ho sebelisa li-microservices. Mme boemo bona, bo re lebisa sethaleng sa Docker.
Docker
Platform
Docker Compose
Theknoloji
Ha o sebetsa le Docker Compose, faele ea YAML e sebelisoa ho hlophisa lits'ebeletso tsa kopo le ho hlophisa tšebelisano ea tsona. Ka hona, Docker Compose ke sesebelisoa sa ho hlalosa le ho tsamaisa lits'ebetso tsa Docker tse nang le lisebelisoa tse ngata.
Li-container tse peli tse sebetsang tsamaisong ea baeti
GNU Etsa
Lenaneo make
, ha e le hantle ke sesebelisoa sa ho iketsetsa kaho ea mananeo le lilaebrari ho tsoa ho khoutu ea mohloli. Ka kakaretso, re ka bua joalo make
e sebetsa ts'ebetsong efe kapa efe e kenyelletsang ho fana ka litaelo tse sa reroang ho fetola lisebelisoa tse ling hore e be mofuta o itseng oa tlhahiso, ho fihlela sepheo se itseng. Tabeng ea rona, litaelo docker-compose
e tla fetoloa ho ba lipheo tse sa bonahaleng (
Ho bolella lenaneo make
mabapi le seo re se batlang ho eona, re hloka faele Makefile
.
Ho rona Makefile
e tla ba le litaelo tsa kamehla docker
и docker-compose
, tse etselitsoeng ho rarolla mathata a mangata. Ka mantsoe a mang, re bua ka ho bokella setshelo, mabapi le ho se qala, ho se emisa, ho se qala hape, mabapi le ho hlophisa ho kena ha basebelisi ka setshelo, mabapi le ho sebetsa ka lits'oants'o tsa setshelo, le ho rarolla mathata a mang a tšoanang.
Maemo a Tloaelehileng a Tšebeliso ea Docker Compose
Ha re nahane ka ts'ebeliso ea kamehla ea webo e nang le likarolo tse latelang:
- TimescaleDB database (Postgres).
- Sesebelisoa sa Express.js.
- Ping (setshelo feela, ha se etse letho le ikhethang).
Sesebelisoa sena se tla hloka lijana tse 3 tsa Docker le faele docker-compose
, e nang le litaelo tsa ho laola lijana tsena. Sebaka se seng le se seng se tla ba le li-touchpoints tse fapaneng. Ka mohlala, ka setshelo timescale
ho tla khonahala ho sebetsa ka tsela e batlang e tšoana le ha ba sebetsa le datha. Ha e le hantle, e u lumella ho etsa liketso tse latelang:
- Ho kena ka har'a khetla ea Postgres.
- Litafole le ho kenya kantle ho naha.
- pōpo
pg_dump
litafole kapa li-database.
Setshelo sa kopo sa Express.js, expressjs
, e ka ba le bokhoni bo latelang:
- Ho fana ka data e ncha ho tsoa ho log ea sistimi.
- Kena ho shell ho phethahatsa litaelo tse itseng.
Ho sebelisana le Linkho
Ha re se re thehile puisano lipakeng tsa lijana tse sebelisang Docker Compose, ke nako ea ho buisana le lijana tseo. Ka har'a sistimi ea Docker Compose ho na le taelo docker-compose
, khetho ea tšehetso -f
, e leng se u lumellang ho fetisetsa faele tsamaisong docker-compose.yml
.
U sebelisa bokhoni ba khetho ena, o ka fokotsa tšebelisano le sistimi feela ho lijana tse boletsoeng faeleng. docker-compose.yml
.
Ha re shebeng hore na litšebelisano le lijana li shebahala joang ha u sebelisa litaelo docker-compose
. Haeba re nahana hore re hloka ho kena ka har'a khetla psql
, joale litaelo tse tsamaellanang li ka shebahala tjena:
docker-compose -f docker-compose.yml exec timescale psql -Upostgres
Taelo e tšoanang e sa sebelisoeng ho phethahatsoa docker-compose
le docker
, e ka shebahala tjena:
docker exec -it edp_timescale_1 psql -Upostgres
Ka kopo hlokomela hore maemong a joalo kamehla ho molemo ho sebelisa taelo docker
, le taelo docker-compose
, kaha sena se felisa tlhoko ea ho hopola mabitso a lijana.
Litaelo tse peli tse ka holimo ha li thata hakaalo. Empa haeba re sebelisitse "wrapper" ka sebopeho Makefile
, e neng e tla re fa sebopeho sa litaelo tse bonolo 'me ka boeona e bitsa litaelo tse telele tse tšoanang, joale liphello tse tšoanang li ka finyelloa ka tsela ena:
make db-shell
Ho hlakile hore tšebeliso Makefile
e etsa hore ho sebetsa ka lijana ho be bonolo haholo!
Mohlala o sebetsang
Ho latela setšoantšo se kaholimo sa projeke, re tla theha faele e latelang docker-compose.yml
:
version: '3.3'
services:
api:
build: .
image: mywebimage:0.0.1
ports:
- 8080:8080
volumes:
- /app/node_modules/
depends_on:
- timescale
command: npm run dev
networks:
- webappnetwork
timescale:
image: timescale/timescaledb-postgis:latest-pg11
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
command: ["postgres", "-c", "log_statement=all", "-c", "log_destination=stderr"]
volumes:
- ./create_schema.sql:/docker-entrypoint-initdb.d/create_schema.sql
networks:
- webappnetwork
ping:
image: willfarrell/ping
environment:
HOSTNAME: "localhost"
TIMEOUT: 300
networks:
webappnetwork:
driver: bridge
Ho laola tlhophiso ea Docker Compose le ho sebelisana le lijana tseo e li hlalosang, re tla theha faele e latelang Makefile
:
THIS_FILE := $(lastword $(MAKEFILE_LIST))
.PHONY: help build up start down destroy stop restart logs logs-api ps login-timescale login-api db-shell
help:
make -pRrq -f $(THIS_FILE) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$'
build:
docker-compose -f docker-compose.yml build $(c)
up:
docker-compose -f docker-compose.yml up -d $(c)
start:
docker-compose -f docker-compose.yml start $(c)
down:
docker-compose -f docker-compose.yml down $(c)
destroy:
docker-compose -f docker-compose.yml down -v $(c)
stop:
docker-compose -f docker-compose.yml stop $(c)
restart:
docker-compose -f docker-compose.yml stop $(c)
docker-compose -f docker-compose.yml up -d $(c)
logs:
docker-compose -f docker-compose.yml logs --tail=100 -f $(c)
logs-api:
docker-compose -f docker-compose.yml logs --tail=100 -f api
ps:
docker-compose -f docker-compose.yml ps
login-timescale:
docker-compose -f docker-compose.yml exec timescale /bin/bash
login-api:
docker-compose -f docker-compose.yml exec api /bin/bash
db-shell:
docker-compose -f docker-compose.yml exec timescale psql -Upostgres
Boholo ba litaelo tse hlalositsoeng mona li sebetsa ho lijana tsohle, empa ho sebelisa khetho c=
e o lumella ho fokotsa boholo ba taelo ho setshelo se le seng.
ka mor'a hore ho Makefile
e loketse, u ka e sebelisa ka tsela ena:
make help
— ho fana ka lethathamo la litaelo tsohle tse fumanehang bakeng samake
.
Thuso mabapi le litaelo tse fumanehang
make build
- ho kopanya setšoantšo ho tlohaDockerfile
. Mohlala oa rona re sebelisitse litšoantšo tse tengtimescale
иping
. Empa setšoantšoapi
re batla ho bokella sebakeng. Sena ke sona hantle se tla etsoa ka mor'a ho phethahatsa taelo ena.
Ho aha setshelo sa Docker
make start
- ho qala lijana tsohle. Ho qala setshelo se le seng feela, o ka sebelisa taelo joalo kamake start c=timescale
.
Ho tsamaisa sejana sa nako
Ho matha setshelo sa ping
make login-timescale
- kena ho bash seshene ea setshelotimescale
.
Ho matha bash ka setshelo sa nako
make db-shell
- ho kena hopsql
ka setshelongtimescale
ho etsa lipotso tsa SQL khahlano le database.
Ho matha psql ka setshelo sa timescaledb
make stop
- ho emisa lijana.
Ho emisa setshelo sa nako
make down
- ho emisa le ho tlosa lijana. Ho tlosa setshelo se itseng, o ka sebelisa taelo ena ho hlakisa setshelo se lakatsehang. Ka mohlala -make down c=timescale
kapamake down c=api
.
Ho emisa le ho hlakola lijana tsohle
Liphello
Leha Docker Compose e re fa litaelo tse ngata tsa ho laola lijana, ka linako tse ling litaelo tsena li ka ba telele mme ho thata ho li hopola.
Mokhoa oa tšebeliso Makefile
e re thusitse ho theha puisano e potlakileng le e bonolo le lijana ho tsoa faeleng docker-compose.yml
. Ka mantsoe a mang, re bua ka tse latelang:
- Moetsi o sebelisana feela le lijana tsa morero tse hlalositsoeng ho
docker-compose.yml
, mosebetsi ha o sitisoe ke lijana tse ling tse tsamaisang. - Haeba taelo e itseng e lebetsoe, o ka phethahatsa taelo eo
make help
'me u fumane thuso ho litaelo tse fumanehang. - Ha ho hlokahale hore u hopole lethathamo le lelelele la likhang ho etsa liketso tse kang ho fumana litlaleho tsa morao-rao kapa ho kena tsamaisong. Ka mohlala, taelo e kang
docker-compose -f docker-compose.yml exec timescale psql -Upostgres
fetohamake db-shell
. - faele
Makefile
U ka ikamahanya le eona habonolo ha morero o ntse o hola. Ka mohlala, ho bonolo ho kenya taelo ea ho etsa bekapo ea polokelo ea boitsebiso kapa ho etsa ketso efe kapa efe. - Haeba sehlopha se seholo sa bahlahisi se sebelisa se tšoanang
Makefile
, sena se nolofatsa tšebelisano 'me se fokotsa liphoso.
PES Ho rona
Babali ba ratehang! U etsa Docker Compose joang?
Source: www.habr.com