Tam sim no koj tuaj yeem tsim Docker dluab hauv werf siv Dockerfile li niaj zaus

Zoo dua lig dua tsis tau. Los yog yuav ua li cas peb yuav luag ua yuam kev loj los ntawm tsis muaj kev txhawb nqa rau Dockerfiles li niaj zaus los tsim cov duab thov.

Tam sim no koj tuaj yeem tsim Docker dluab hauv werf siv Dockerfile li niaj zaus

Peb mam li tham txog werf - GitOps cov nqi hluav taws xob uas koom nrog txhua qhov CI / CD thiab muab kev tswj hwm tag nrho daim ntawv thov kev ua neej, tso cai:

  • sau thiab luam tawm cov duab,
  • xa cov ntawv thov hauv Kubernetes,
  • rho tawm cov duab tsis siv uas siv cov cai tshwj xeeb.


Txoj haujlwm lub tswv yim yog los sau cov cuab yeej qis rau hauv ib qho kev sib koom ua ke uas muab rau DevOps engineers tswj cov ntawv thov. Yog tias ua tau, cov khoom siv hluav taws xob uas twb muaj lawm (xws li Helm thiab Docker) yuav tsum tau siv. Yog tias tsis muaj kev daws teeb meem, peb tuaj yeem tsim thiab txhawb txhua yam tsim nyog rau qhov no.

Tom qab: koj tus kheej cov duab sau

Qhov no yog qhov tshwm sim nrog cov duab sau hauv werf: Dockerfile ib txwm tsis txaus rau peb. Yog tias koj saib sai ntawm keeb kwm ntawm qhov project, qhov teeb meem no tau tshwm sim nyob rau hauv thawj versions ntawm werf (ces tseem npe dapp).

Thaum tsim cov cuab yeej tsim cov ntawv thov rau hauv Docker cov duab, peb pom tau sai sai tias Dockerfile tsis haum rau peb rau qee yam haujlwm tshwj xeeb:

  1. Qhov xav tau los tsim cov ntawv thov me me hauv web raws li cov qauv hauv qab no:
    • nruab qhov system-wide daim ntawv thov kev vam meej,
    • nruab ib pob ntawm daim ntawv thov kev vam khom qiv,
    • khaws cov khoom muaj nqis,
    • thiab qhov tseem ceeb tshaj, hloov kho cov cai hauv daim duab sai thiab ua tau zoo.
  2. Thaum hloov pauv rau qhov project cov ntaub ntawv, tus tsim yuav tsum sai sai tsim ib txheej tshiab los ntawm kev thov ib thaj rau cov ntaub ntawv hloov.
  3. Yog tias qee cov ntaub ntawv tau hloov pauv, ces nws yog ib qho tsim nyog yuav tsum rov tsim kho cov txheej txheem sib txuas.

Niaj hnub no peb cov neeg sau khoom muaj ntau yam ntxiv, tab sis cov no yog thawj qhov kev ntshaw thiab kev xav.

Feem ntau, tsis xav txog ob zaug, peb tau siv peb tus kheej nrog cov lus programming peb siv (saib hauv qab no) thiab ntaus txoj kev mus rau kev siv tus kheej DSL! Raws li cov hom phiaj, nws tau npaj los piav qhia txog cov txheej txheem sib dhos hauv cov theem thiab txiav txim siab qhov kev vam meej ntawm cov theem no ntawm cov ntaub ntawv. Thiab ntxiv rau nws tus kheej sau, uas tig DSL rau hauv lub hom phiaj kawg - ib daim duab sib dhos. Thaum xub thawj lub DSL yog nyob rau hauv Ruby, tab sis raws li kev hloov mus rau Golang - qhov teeb tsa ntawm peb tus neeg sau tau pib piav qhia hauv YAML cov ntaub ntawv.

Tam sim no koj tuaj yeem tsim Docker dluab hauv werf siv Dockerfile li niaj zaus
Qub config rau dapp hauv Ruby

Tam sim no koj tuaj yeem tsim Docker dluab hauv werf siv Dockerfile li niaj zaus
Tam sim no config rau werf ntawm YAML

Lub mechanism ntawm lub collector kuj hloov raws sij hawm. Thaum xub thawj, peb tsuas yog tsim ib ntus Dockerfile ntawm ya los ntawm peb qhov kev teeb tsa, thiab tom qab ntawd peb pib khiav cov lus qhia sib dhos hauv cov thawv ib ntus thiab cog lus.

NB: Tam sim no, peb tus neeg sau khoom, uas ua haujlwm nrog nws tus kheej config (hauv YAML) thiab hu ua Stapel collector, twb tau tsim los rau hauv lub cuab yeej muaj zog heev. Nws cov lus piav qhia ntxaws tsim nyog cov khoom sib cais, thiab cov ntsiab lus tseem ceeb tuaj yeem pom hauv cov ntaub ntawv.

Kev paub txog qhov teeb meem

Tab sis peb paub, thiab tsis yog tam sim ntawd, tias peb tau ua yuam kev: peb tsis tau ntxiv lub peev xwm tsim cov duab ntawm tus qauv Dockerfile thiab muab lawv tso rau hauv tib qhov kawg-rau-kawg daim ntawv thov kev tswj hwm infrastructure (piv txwv li sau cov duab, xa thiab ntxuav lawv). Yuav ua li cas thiaj ua tau ib lub cuab yeej rau kev xa tawm hauv Kubernetes thiab tsis siv Dockerfile kev txhawb nqa, piv txwv li. txheej txheem los piav txog cov duab rau feem ntau cov haujlwm? ..

Es tsis txhob teb lo lus nug no, peb muab kev daws teeb meem. Yuav ua li cas yog tias koj twb muaj Dockerfile (los yog ib txheej ntawm Dockerfiles) thiab xav siv werf?

NB: Los ntawm txoj kev, vim li cas koj thiaj xav siv werf? Cov yam ntxwv tseem ceeb los mus rau hauv qab no:

  • daim ntawv thov kev tswj xyuas tag nrho suav nrog kev tu duab;
  • lub peev xwm los tswj kev sib dhos ntawm ntau cov duab ib zaug los ntawm ib qho kev teeb tsa;
  • Txhim kho cov txheej txheem xa mus rau Helm-tshaj kab kos.

Ib daim ntawv teev tag nrho ntawm lawv tuaj yeem pom ntawm nplooj ntawv qhov project.

Yog li, yog tias ua ntej peb yuav tau muab rov sau dua Dockerfile hauv peb qhov teeb tsa, tam sim no peb yuav zoo siab hais tias: "Cia werf tsim koj Dockerfiles!"

Yuav siv li cas?

Qhov kev siv tag nrho ntawm qhov tshwj xeeb no tau tshwm sim hauv kev tso tawm werf v1.0.3-beta.1. Lub hauv paus ntsiab lus tseem ceeb yog qhov yooj yim: tus neeg siv qhia txog txoj hauv kev mus rau Dockerfile uas twb muaj lawm hauv werf config, thiab tom qab ntawd khiav cov lus txib werf build... thiab qhov ntawd yog nws - werf yuav sau cov duab. Cia peb saib ib qho piv txwv abstract.

Cia peb tshaj tawm rau yav tom ntej Dockerfile hauv qhov project root:

FROM ubuntu:18.04
RUN echo Building ...

Thiab peb yuav tshaj tawm werf.yamluas siv qhov no Dockerfile:

configVersion: 1
project: dockerfile-example
---
image: ~
dockerfile: ./Dockerfile

Txhua tus! Sab laug khiav werf build:

Tam sim no koj tuaj yeem tsim Docker dluab hauv werf siv Dockerfile li niaj zaus

Ntxiv rau, koj tuaj yeem tshaj tawm cov hauv qab no werf.yaml los tsim ntau cov duab los ntawm Dockerfiles sib txawv ib zaug:

configVersion: 1
project: dockerfile-example
---
image: backend
dockerfile: ./dockerfiles/Dockerfile-backend
---
image: frontend
dockerfile: ./dockerfiles/Dockerfile-frontend

Thaum kawg, nws tseem txhawb nqa dhau qhov kev tsim kho ntxiv, xws li --build-arg ΠΈ --add-host - ntawm werf config. Cov lus piav qhia tiav ntawm Dockerfile duab teeb tsa muaj nyob ntawm nplooj ntawv cov ntaub ntawv.

Ua li cas nws ua hauj lwm?

Thaum lub sijhawm tsim cov txheej txheem, tus qauv cache ntawm cov txheej hauv zos hauv Docker ua haujlwm. Txawm li cas los xij, qhov tseem ceeb yog qhov werf kuj integrates Dockerfile configuration rau hauv nws cov infrastructure. Qhov no txhais li cas?

  1. Txhua daim duab tsim los ntawm Dockerfile muaj ib theem hu ua dockerfile (koj tuaj yeem nyeem ntxiv txog cov theem twg hauv werf no).
  2. Rau theem dockerfile werf xam qhov kos npe uas nyob ntawm cov ntsiab lus ntawm Dockerfile teeb tsa. Thaum Dockerfile configuration hloov, cov theem kos npe hloov dockerfile thiab werf pib rov tsim kho ntawm theem no nrog Dockerfile config tshiab. Yog tias kos npe tsis hloov, ces werf yuav siv cov duab los ntawm cache (Cov ntsiab lus ntxiv txog kev siv kos npe hauv werf tau piav qhia hauv daim ntawv qhia no).
  3. Tom ntej no, cov duab sau tuaj yeem luam tawm nrog cov lus txib werf publish (los yog werf build-and-publish) thiab siv nws rau kev xa mus rau Kubernetes. Cov duab luam tawm mus rau Docker Registry yuav raug ntxuav siv cov cuab yeej werf ntxuav, piv txwv li. Cov duab qub (hnub laus dua N hnub), cov duab cuam tshuam nrog cov ceg tsis muaj Git, thiab lwm txoj cai yuav raug muab ntxuav.

Cov ntsiab lus ntxiv txog cov ntsiab lus piav qhia ntawm no tuaj yeem pom hauv cov ntaub ntawv:

Lus ceeb toom thiab ceev faj

1. Sab nraud URL tsis txaus siab hauv ADD

Tam sim no nws tsis txaus siab siv qhov URL sab nraud hauv cov lus qhia ADD. Werf yuav tsis pib tsim kho dua thaum cov peev txheej ntawm qhov hloov pauv URL. Peb npaj yuav ntxiv qhov no sai sai.

2. Koj tsis tuaj yeem ntxiv .git rau daim duab

Feem ntau hais lus, ntxiv ib daim ntawv teev npe .git hauv daim duab - kev coj phem phem thiab ntawm no yog vim li cas:

  1. Yog hais tias tus .git tseem nyob hauv daim duab kawg, qhov no ua txhaum txoj cai 12 yam app: Txij li cov duab kawg yuav tsum tau txuas mus rau ib qho kev cog lus, nws yuav tsum tsis txhob ua git checkout arbitrary cog lus.
  2. .git nce qhov loj ntawm daim duab (lub chaw cia khoom tuaj yeem loj vim qhov tseeb tias cov ntaub ntawv loj tau ntxiv rau nws thiab tom qab ntawd tshem tawm). Qhov loj ntawm tsob ntoo ua haujlwm tsuas yog cuam tshuam nrog kev cog lus tshwj xeeb yuav tsis nyob ntawm keeb kwm ntawm kev ua haujlwm hauv Git. Nyob rau hauv cov ntaub ntawv no, ntxiv thiab tom qab tshem tawm .git los ntawm daim duab kawg yuav tsis ua haujlwm: daim duab tseem yuav tau txais ib txheej ntxiv - qhov no yog li cas Docker ua haujlwm.
  3. Docker tuaj yeem pib qhov kev tsim kho tsis tsim nyog, txawm tias tib qhov kev cog lus tau tsim, tab sis los ntawm cov ntoo sib txawv. Piv txwv li, GitLab tsim cais cloned directory hauv /home/gitlab-runner/builds/HASH/[0-N]/yourproject thaum uas tig mus los ua ke yog enabled. Qhov ntxiv reassembly yuav yog vim lub fact tias cov directory .git yog sib txawv nyob rau hauv sib txawv cloned versions ntawm tib lub repository, txawm yog hais tias tib lub hom phiaj yog tsim.

Qhov kawg taw tes kuj muaj qhov tshwm sim thaum siv werf. Werf xav kom lub cache ua tau tam sim no thaum khiav qee cov lus txib (piv txwv li. werf deploy). Thaum cov lus txib no khiav, werf suav cov npe kos npe rau cov duab teev hauv werf.yaml, thiab lawv yuav tsum nyob rau hauv lub rooj sib txoos cache - txwv tsis pub cov lus txib yuav tsis tuaj yeem ua haujlwm ntxiv. Yog hais tias theem kos npe nyob ntawm cov ntsiab lus .git, tom qab ntawd peb tau txais lub cache uas tsis ruaj khov rau kev hloov pauv hauv cov ntaub ntawv tsis cuam tshuam, thiab werf yuav tsis tuaj yeem zam txim rau qhov kev saib xyuas no (kom paub meej ntxiv, saib cov ntaub ntawv).

Feem ntau tsuas yog ntxiv qee cov ntaub ntawv tsim nyog los ntawm cov lus qhia ADD nyob rau hauv txhua rooj plaub, tsub kom cov efficiency thiab kev cia siab ntawm tus sau Dockerfile, thiab kuj txhim kho kev ruaj ntseg ntawm cov cache sau rau qhov no Dockerfile, rau kev hloov pauv tsis cuam tshuam hauv Git.

Qhov no

Peb txoj hauv kev pib los sau peb tus kheej tsim rau cov kev xav tau tshwj xeeb yog qhov nyuaj, ncaj ncees thiab ncaj ncees: es tsis txhob siv cov crutches rau saum tus qauv Dockerfile, peb tau sau peb cov kev daws teeb meem nrog kev cai syntax. Thiab qhov no muaj nws qhov zoo: Stapel collector copes nrog nws txoj hauj lwm zoo kawg nkaus.

Txawm li cas los xij, nyob rau hauv tus txheej txheem ntawm kev sau ntawv peb tus kheej tsim, peb tsis pom qhov kev txhawb nqa rau Dockerfiles uas twb muaj lawm. Qhov tsis txaus ntseeg no tau raug kho tam sim no, thiab yav tom ntej peb npaj yuav tsim kev txhawb nqa Dockerfile nrog rau peb cov kev cai Stapel tsim rau kev sib dhos thiab kev sib dhos siv Kubernetes (piv txwv li kev sib dhos ntawm cov neeg sib tw hauv Kubernetes, zoo li ua tiav hauv kaniko).

Yog li, yog tias koj dheev muaj ob peb Dockerfiles dag nyob ib puag ncig ... sim nws werf!

PS Daim ntawv teev cov ntaub ntawv ntawm lub ncauj lus

Nyeem kuj hauv peb blog: "werf - peb cov cuab yeej rau CI / CD hauv Kubernetes (saib xyuas thiab tshaj tawm video)".

Tau qhov twg los: www.hab.com

Ntxiv ib saib