Kulesi sihloko sisebenzisa
Ake sithi unohlelo lwewebhu amaklayenti amaningi asebenza ngalo ngokuzimisela, futhi ayikho nhlobo indlela yokuthi lulale phansi imizuzwana embalwa. Futhi udinga ngempela ukukhipha isibuyekezo selabhulali, ukulungiswa kweziphazamisi, noma isici esisha esihle. Esimeni esijwayelekile, uzodinga ukumisa uhlelo, ulubuyisele esikhundleni bese uluqala futhi. Endabeni ye-docker, ungaqala uyishintshe, bese uyiqala kabusha, kodwa kusazoba nesikhathi lapho izicelo zokufaka isicelo zingeke zicutshungulwe, ngoba ngokuvamile isicelo sithatha isikhathi ukulayisha ekuqaleni. Kuthiwani uma iqala, kodwa ivele ingasebenzi? Lena inkinga, masiyixazulule ngezindlela ezincane futhi ngobuhle ngangokunokwenzeka.
UMUZI Wokuzihlangula: Iningi lendatshana yethulwa ngefomethi yokuhlola - ngendlela yokurekhodwa kweseshini yekhonsoli. Sethemba ukuthi lokhu ngeke kube nzima kakhulu ukukuqonda futhi ikhodi izozibhala ngokwayo ngokwanele. Ngomkhathi, cabanga ukuthi lezi akuzona nje amazwibela ekhodi, kodwa iphepha elivela kuhlobo lwe-teletype "yensimbi".
Amasu athakazelisayo okunzima ku-Google ngokufunda ikhodi achazwe ekuqaleni kwesigaba ngasinye. Uma kukhona okunye okungacacile, ku-google futhi ukuhlole.
Ake siqale.
$ mkdir blue-green-deployment && cd $_
service
Masenze isevisi yokuhlola futhi siyibeke esitsheni.
Amasu athakazelisayo
cat << EOF > file-name
(Lapha Idokhumenti +I/O Ukuqondisa kabusha ) kuyindlela yokwakha ifayela elinemigqa eminingi ngomyalo owodwa. Yonke into i-bash ifundeka kuyo/dev/stdin
ngemva kwalo mugqa nangaphambi komugqaEOF
izorekhodwa kufile-name
.wget -qO- URL
(explainshell ) — khipha umbhalo owamukelwe nge-HTTP kuye/dev/stdout
(i-analoguecurl URL
).
Phrinta
Ngiphula ngokuqondile amazwibela ukuze nginike amandla ukugqamisa kwePython. Ekugcineni kuzoba nesinye isiqeshana esifana nalesi. Cabanga ukuthi kulezi zindawo iphepha lasikwa ukuze lithunyelwe emnyangweni wokugqamisa (lapho ikhodi yayinombala wezandla nama-highlighters), bese lezi zingcezu zinamathiselwe emuva.
$ cat << EOF > uptimer.py
from http.server import BaseHTTPRequestHandler, HTTPServer
from time import monotonic
app_version = 1
app_name = f'Uptimer v{app_version}.0'
loading_seconds = 15 - app_version * 5
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
try:
t = monotonic() - server_start
if t < loading_seconds:
self.send_error(503)
else:
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
response = f'<h2>{app_name} is running for {t:3.1f} seconds.</h2>n'
self.wfile.write(response.encode('utf-8'))
except Exception:
self.send_error(500)
else:
self.send_error(404)
httpd = HTTPServer(('', 8080), Handler)
server_start = monotonic()
print(f'{app_name} (loads in {loading_seconds} sec.) started.')
httpd.serve_forever()
EOF
$ cat << EOF > Dockerfile
FROM python:alpine
EXPOSE 8080
COPY uptimer.py app.py
CMD [ "python", "-u", "./app.py" ]
EOF
$ docker build --tag uptimer .
Sending build context to Docker daemon 39.42kB
Step 1/4 : FROM python:alpine
---> 8ecf5a48c789
Step 2/4 : EXPOSE 8080
---> Using cache
---> cf92d174c9d3
Step 3/4 : COPY uptimer.py app.py
---> a7fbb33d6b7e
Step 4/4 : CMD [ "python", "-u", "./app.py" ]
---> Running in 1906b4bd9fdf
Removing intermediate container 1906b4bd9fdf
---> c1655b996fe8
Successfully built c1655b996fe8
Successfully tagged uptimer:latest
$ docker run --rm --detach --name uptimer --publish 8080:8080 uptimer
8f88c944b8bf78974a5727070a94c76aa0b9bb2b3ecf6324b784e782614b2fbf
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f88c944b8bf uptimer "python -u ./app.py" 3 seconds ago Up 5 seconds 0.0.0.0:8080->8080/tcp uptimer
$ docker logs uptimer
Uptimer v1.0 (loads in 10 sec.) started.
$ wget -qSO- http://localhost:8080
HTTP/1.0 503 Service Unavailable
Server: BaseHTTP/0.6 Python/3.8.3
Date: Sat, 22 Aug 2020 19:52:40 GMT
Connection: close
Content-Type: text/html;charset=utf-8
Content-Length: 484
$ wget -qSO- http://localhost:8080
HTTP/1.0 200 OK
Server: BaseHTTP/0.6 Python/3.8.3
Date: Sat, 22 Aug 2020 19:52:45 GMT
Content-Type: text/html
<h2>Uptimer v1.0 is running for 15.4 seconds.</h2>
$ docker rm --force uptimer
uptimer
Hlehlisa ummeleli
Ukuze isicelo sethu sikwazi ukushintsha singanakiwe, kuyadingeka ukuthi kube nenye inhlangano phambi kwaso ezofihla ukushintshwa kwaso. Kungaba iseva yewebhu
Uhlelo lokusebenza kanye ne-proxy ehlehlayo ingaxhunywa ngaphakathi kwe-docker kusetshenziswa
Uma ummeleli ohlanekezelwe ehlala komunye umsingathi, kuzodingeka ulahle inethiwekhi yedokha futhi uxhume uhlelo lokusebenza kummeleli ongemuva ngenethiwekhi yomsingathi, udlulisele imbobo. izicelo ipharamitha --publish
, njengasekuqaleni nanjengommeleli ongemuva.
Sizosebenzisa ummeleli ongemuva ku-port 80, ngoba leli yilona kanye ibhizinisi okufanele lilalele inethiwekhi yangaphandle. Uma i-port 80 imatasa kumsingathi wakho wokuhlola, shintsha ipharamitha --publish 80:80
on --publish ANY_FREE_PORT:80
.
Amasu athakazelisayo
- “Kumanethiwekhi e-Docker adalwe ngabasebenzisi, awukwazi ukuxhumana neziqukathi hhayi ngekheli le-IP kuphela. Igama lesiqukathi liphinde lixazululwe ekhelini laso le-IP" (
isihloko esithi “Inethiwekhi ngeziqukathi ezizimele”, ingxenye ethi “Sebenzisa amanethiwekhi amabhuloho achazwe ngabasebenzisi” , iphuzu 5 lekhodi yedokha).
Phrinta
$ docker network create web-gateway
5dba128fb3b255b02ac012ded1906b7b4970b728fb7db3dbbeccc9a77a5dd7bd
$ docker run --detach --rm --name uptimer --network web-gateway uptimer
a1105f1b583dead9415e99864718cc807cc1db1c763870f40ea38bc026e2d67f
$ docker run --rm --network web-gateway alpine wget -qO- http://uptimer:8080
<h2>Uptimer v1.0 is running for 11.5 seconds.</h2>
$ docker run --detach --publish 80:80 --network web-gateway --name reverse-proxy nginx:alpine
80695a822c19051260c66bf60605dcb4ea66802c754037704968bc42527bf120
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80695a822c19 nginx:alpine "/docker-entrypoint.…" 27 seconds ago Up 25 seconds 0.0.0.0:80->80/tcp reverse-proxy
a1105f1b583d uptimer "python -u ./app.py" About a minute ago Up About a minute 8080/tcp uptimer
$ cat << EOF > uptimer.conf
server {
listen 80;
location / {
proxy_pass http://uptimer:8080;
}
}
EOF
$ docker cp ./uptimer.conf reverse-proxy:/etc/nginx/conf.d/default.conf
$ docker exec reverse-proxy nginx -s reload
2020/06/23 20:51:03 [notice] 31#31: signal process started
$ wget -qSO- http://localhost
HTTP/1.1 200 OK
Server: nginx/1.19.0
Date: Sat, 22 Aug 2020 19:56:24 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
<h2>Uptimer v1.0 is running for 104.1 seconds.</h2>
Ukuthunyelwa okungenazihibe
Ake sikhiphe inguqulo entsha yohlelo lokusebenza (enomfutho ophindwe kabili wokuqalisa ukusebenza) bese sizame ukulisebenzisa ngaphandle komthungo.
Amasu athakazelisayo
echo 'my text' | docker exec -i my-container sh -c 'cat > /my-file.txt'
— Bhala umbhalomy text
ukufayela/my-file.txt
ngaphakathi kwesitshamy-container
.cat > /my-file.txt
— Bhala okuqukethwe okokufaka okujwayelekile efayeleni/dev/stdin
.
Phrinta
$ sed -i "s/app_version = 1/app_version = 2/" uptimer.py
$ docker build --tag uptimer .
Sending build context to Docker daemon 39.94kB
Step 1/4 : FROM python:alpine
---> 8ecf5a48c789
Step 2/4 : EXPOSE 8080
---> Using cache
---> cf92d174c9d3
Step 3/4 : COPY uptimer.py app.py
---> 3eca6a51cb2d
Step 4/4 : CMD [ "python", "-u", "./app.py" ]
---> Running in 8f13c6d3d9e7
Removing intermediate container 8f13c6d3d9e7
---> 1d56897841ec
Successfully built 1d56897841ec
Successfully tagged uptimer:latest
$ docker run --detach --rm --name uptimer_BLUE --network web-gateway uptimer
96932d4ca97a25b1b42d1b5f0ede993b43f95fac3c064262c5c527e16c119e02
$ docker logs uptimer_BLUE
Uptimer v2.0 (loads in 5 sec.) started.
$ docker run --rm --network web-gateway alpine wget -qO- http://uptimer_BLUE:8080
<h2>Uptimer v2.0 is running for 23.9 seconds.</h2>
$ sed s/uptimer/uptimer_BLUE/ uptimer.conf | docker exec --interactive reverse-proxy sh -c 'cat > /etc/nginx/conf.d/default.conf'
$ docker exec reverse-proxy cat /etc/nginx/conf.d/default.conf
server {
listen 80;
location / {
proxy_pass http://uptimer_BLUE:8080;
}
}
$ docker exec reverse-proxy nginx -s reload
2020/06/25 21:22:23 [notice] 68#68: signal process started
$ wget -qO- http://localhost
<h2>Uptimer v2.0 is running for 63.4 seconds.</h2>
$ docker rm -f uptimer
uptimer
$ wget -qO- http://localhost
<h2>Uptimer v2.0 is running for 84.8 seconds.</h2>
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96932d4ca97a uptimer "python -u ./app.py" About a minute ago Up About a minute 8080/tcp uptimer_BLUE
80695a822c19 nginx:alpine "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp reverse-proxy
Kulesi sigaba, isithombe sakhiwe ngqo kuseva, esidinga ukuthi imithombo yohlelo lokusebenza ibe khona, futhi iphinde ilayishe iseva ngomsebenzi ongadingekile. Isinyathelo esilandelayo ukwaba umhlangano wesithombe emshinini ohlukile (isibonelo, ohlelweni lwe-CI) bese uwudlulisela kuseva.
Kudluliswa izithombe
Ngeshwa, akuwenzi umqondo ukudlulisa izithombe zisuka ku-localhost ziye ku-localhost, ngakho-ke lesi sigaba singahlolwa kuphela uma unababungazi ababili abane-Docker eduze. Okungenani kubukeka kanjena:
$ ssh production-server docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
$ docker image save uptimer | ssh production-server 'docker image load'
Loaded image: uptimer:latest
$ ssh production-server docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
uptimer latest 1d56897841ec 5 minutes ago 78.9MB
Ithimba docker save
ilondoloza idatha yesithombe kungobo yomlando ye-.tar, okusho ukuthi inesisindo esiphindwe izikhathi ezingu-1.5 ngaphezu kwesisindo esiminyanisiwe. Ngakho-ke masiyinyakazise egameni lokonga isikhathi nethrafikhi:
$ docker image save uptimer | gzip | ssh production-server 'zcat | docker image load'
Loaded image: uptimer:latest
Ungaphinda ugade inqubo yokulanda (yize lokhu kudinga insiza yenkampani yangaphandle):
$ docker image save uptimer | gzip | pv | ssh production-server 'zcat | docker image load'
25,7MiB 0:01:01 [ 425KiB/s] [ <=> ]
Loaded image: uptimer:latest
Ithiphu: Uma udinga inqwaba yamapharamitha ukuze uxhume kuseva nge-SSH, kungenzeka ukuthi awusebenzisi ifayela.
~/.ssh/config
.
Kudluliswa isithombe nge docker image save/load
- Lena indlela minimalistic kunazo zonke, kodwa hhayi eyodwa kuphela. Kukhona abanye:
- Irejista Yesitsha (izinga lemboni).
- Xhuma kuseva ye-docker daemon kusuka komunye umsingathi:
- imvelo variable
DOCKER_HOST
. - Inketho yomugqa womyalo
-H
noma--host
insimbidocker-compose
. docker context
- imvelo variable
Indlela yesibili (enezinketho ezintathu zokuqaliswa kwayo) ichazwe kahle esihlokweni
deploy.sh
Manje ake siqoqe konke esikwenze mathupha kusikripthi esisodwa. Ake siqale ngomsebenzi wezinga eliphezulu, bese sibheka ezinye ezisetshenziswe kuwo.
Amasu athakazelisayo
${parameter?err_msg}
- enye yeziphonso zomlingo we-bash (akaukushintshwa kwepharamitha ). Umaparameter
okungacacisiwe, okukhiphayoerr_msg
bese uphuma ngekhodi 1.docker --log-driver journald
— ngokuzenzakalelayo, umshayeli wokugawulwa kwe-docker uyifayela lombhalo ngaphandle kokuzungezisa. Ngale ndlela, izingodo zigcwalisa ngokushesha yonke idiski, ngakho-ke endaweni yokukhiqiza kuyadingeka ukushintsha umshayeli abe ngohlakaniphile.
Iskripthi sokuphakelwa
deploy() {
local usage_msg="Usage: ${FUNCNAME[0]} image_name"
local image_name=${1?$usage_msg}
ensure-reverse-proxy || return 2
if get-active-slot $image_name
then
local OLD=${image_name}_BLUE
local new_slot=GREEN
else
local OLD=${image_name}_GREEN
local new_slot=BLUE
fi
local NEW=${image_name}_${new_slot}
echo "Deploying '$NEW' in place of '$OLD'..."
docker run
--detach
--restart always
--log-driver journald
--name $NEW
--network web-gateway
$image_name || return 3
echo "Container started. Checking health..."
for i in {1..20}
do
sleep 1
if get-service-status $image_name $new_slot
then
echo "New '$NEW' service seems OK. Switching heads..."
sleep 2 # Ensure service is ready
set-active-slot $image_name $new_slot || return 4
echo "'$NEW' service is live!"
sleep 2 # Ensure all requests were processed
echo "Killing '$OLD'..."
docker rm -f $OLD
docker image prune -f
echo "Deployment successful!"
return 0
fi
echo "New '$NEW' service is not ready yet. Waiting ($i)..."
done
echo "New '$NEW' service did not raise, killing it. Failed to deploy T_T"
docker rm -f $NEW
return 5
}
Izici ezisetshenzisiwe:
ensure-reverse-proxy
- Iqinisekisa ukuthi ummeleli ongemuva uyasebenza (uwusizo ekusetshenzisweni kokuqala)get-active-slot service_name
- Inquma ukuthi yisiphi isikhala esisebenzayo njengamanje kusevisi enikeziwe (BLUE
nomaGREEN
)get-service-status service_name deployment_slot
- Inquma ukuthi isevisi isikulungele yini ukucubungula izicelo ezingenayoset-active-slot service_name deployment_slot
- Ishintsha ukucushwa kwe-nginx kusiqukathi sommeleli esibuyela emuva
Ukuze:
ensure-reverse-proxy() {
is-container-up reverse-proxy && return 0
echo "Deploying reverse-proxy..."
docker network create web-gateway
docker run
--detach
--restart always
--log-driver journald
--name reverse-proxy
--network web-gateway
--publish 80:80
nginx:alpine || return 1
docker exec --interactive reverse-proxy sh -c "> /etc/nginx/conf.d/default.conf"
docker exec reverse-proxy nginx -s reload
}
is-container-up() {
local container=${1?"Usage: ${FUNCNAME[0]} container_name"}
[ -n "$(docker ps -f name=${container} -q)" ]
return $?
}
get-active-slot() {
local service=${1?"Usage: ${FUNCNAME[0]} service_name"}
if is-container-up ${service}_BLUE && is-container-up ${service}_GREEN; then
echo "Collision detected! Stopping ${service}_GREEN..."
docker rm -f ${service}_GREEN
return 0 # BLUE
fi
if is-container-up ${service}_BLUE && ! is-container-up ${service}_GREEN; then
return 0 # BLUE
fi
if ! is-container-up ${service}_BLUE; then
return 1 # GREEN
fi
}
get-service-status() {
local usage_msg="Usage: ${FUNCNAME[0]} service_name deployment_slot"
local service=${1?usage_msg}
local slot=${2?$usage_msg}
case $service in
# Add specific healthcheck paths for your services here
*) local health_check_port_path=":8080/" ;;
esac
local health_check_address="http://${service}_${slot}${health_check_port_path}"
echo "Requesting '$health_check_address' within the 'web-gateway' docker network:"
docker run --rm --network web-gateway alpine
wget --timeout=1 --quiet --server-response $health_check_address
return $?
}
set-active-slot() {
local usage_msg="Usage: ${FUNCNAME[0]} service_name deployment_slot"
local service=${1?$usage_msg}
local slot=${2?$usage_msg}
[ "$slot" == BLUE ] || [ "$slot" == GREEN ] || return 1
get-nginx-config $service $slot | docker exec --interactive reverse-proxy sh -c "cat > /etc/nginx/conf.d/$service.conf"
docker exec reverse-proxy nginx -t || return 2
docker exec reverse-proxy nginx -s reload
}
Umsebenzi get-active-slot
idinga incazelo encane:
Kungani ibuyisela inombolo futhi ingakhiphi iyunithi yezinhlamvu?
Noma kunjalo, emsebenzini wokushaya sibheka umphumela womsebenzi wawo, futhi ukuhlola ikhodi yokuphuma usebenzisa i-bash kulula kakhulu kunokuhlola uchungechunge. Ngaphezu kwalokho, ukuthola intambo kuyo kulula kakhulu:
get-active-slot service && echo BLUE || echo GREEN
.
Ingabe izimo ezintathu zanele ngempela ukuhlukanisa zonke izifundazwe?
Ngisho nezimbili zizokwanela, ezokugcina zilapha ukuze ziphelele nje, ukuze zingabhali else
.
Umsebenzi obuyisela i-nginx configs kuphela osala ungachazwanga: get-nginx-config service_name deployment_slot
. Ngokufanisa nokuhlolwa kwezempilo, lapha ungasetha noma yikuphi ukulungiselelwa kwanoma iyiphi isevisi. Ezintweni ezithakazelisayo - kuphela cat <<- EOF
, okukuvumela ukuthi ususe wonke amathebhu ekuqaleni. Yiqiniso, intengo yokufometha okuhle ixubene namathebhu anezikhala, namuhla abhekwa njengefomu elibi kakhulu. Kodwa i-bash iphoqa amathebhu, futhi kungaba kuhle nokuba nokufometha okujwayelekile ku-nginx config. Ngamafuphi, ukuxuba amathebhu nezikhala lapha ngempela kubonakala njengesixazululo esingcono kakhulu kokubi kakhulu. Kodwa-ke, ngeke ukubone lokhu kumazwibela angezansi, njengoba uHabr “ekwenza kahle” ngokushintsha wonke amathebhu abe yizikhala ezi-4 nokwenza i-EOF ingavumelekile.
Ukuze ungavuki kabili, ngizokutshela ngaso leso sikhathi
cat << 'EOF'
, esizohlangana nayo kamuva. Uma ubhala kalulacat << EOF
, khona-ke ngaphakathi kwe-heredoc intambo iyahlanganiswa (okuguquguqukayo kuyanwetshwa ($foo
), izingcingo zokuyala ($(bar)
) njll.), futhi uma ufaka isiphetho sedokhumenti ngezingcaphuno ezilodwa, ukuhumusha kuyacishwa kanye nophawu.$
iboniswa njengoba injalo. Okudingayo ukuze ufake iskripthi phakathi kwesinye iskripthi.
get-nginx-config() {
local usage_msg="Usage: ${FUNCNAME[0]} service_name deployment_slot"
local service=${1?$usage_msg}
local slot=${2?$usage_msg}
[ "$slot" == BLUE ] || [ "$slot" == GREEN ] || return 1
local container_name=${service}_${slot}
case $service in
# Add specific nginx configs for your services here
*) nginx-config-simple-service $container_name:8080 ;;
esac
}
nginx-config-simple-service() {
local usage_msg="Usage: ${FUNCNAME[0]} proxy_pass"
local proxy_pass=${1?$usage_msg}
cat << EOF
server {
listen 80;
location / {
proxy_pass http://$proxy_pass;
}
}
EOF
}
Lesi iskripthi sonke. Manje
Isebenzisa imibhalo enepharamitha kuseva ekude
Isikhathi sokungqongqoza kuseva eqondiwe. Ngalesi sikhathi localhost
kufanelekile:
$ ssh-copy-id localhost
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
himura@localhost's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'localhost'"
and check to make sure that only the key(s) you wanted were added.
Sibhale isikripthi sokuphakelwa esilanda isithombe esakhelwe ngaphambilini kusiphakeli esiqondiwe futhi simiselele kalula isiqukathi sesevisi, kodwa singasisebenzisa kanjani emshinini wesilawuli kude? Iskripthi sinezimpikiswano, njengoba sisebenza emhlabeni wonke futhi singasebenzisa izinsiza ezimbalwa ngesikhathi esisodwa ngaphansi kommeleli oyedwa obuyela emuva (ungasebenzisa ukulungiselelwa kwe-nginx ukuze unqume ukuthi iyiphi i-url ezoba iyiphi isevisi). Iskripthi asikwazi ukugcinwa kuseva, njengoba kulokhu ngeke sikwazi ukusibuyekeza ngokuzenzakalelayo (ngokwenjongo yokulungiswa kweziphazamisi nokwengeza izinsizakalo ezintsha), futhi ngokuvamile, isimo = ububi.
Isixazululo 1: Namanje gcina umbhalo kuseva, kodwa usikopishe ngaso sonke isikhathi scp
. Bese uxhuma nge ssh
futhi ukhiphe iskripthi ngezimpikiswano ezidingekayo.
Umthengi:
- Izenzo ezimbili esikhundleni esisodwa
- Kungase kungabi khona indawo lapho ukopisha khona, noma kungase kungabi khona ukufinyelela kukho, noma umbhalo ungase usetshenziswe ngesikhathi sokushintsha.
- Kunconywa ukuthi uhlanze ngemuva kwakho (susa umbhalo).
- Izenzo ezintathu kakade.
Isixazululo 2:
- Gcina izincazelo zomsebenzi kuphela kusikripthi futhi ungasebenzisi lutho nhlobo
- Ngosizo luka
sed
engeza ikholi yomsebenzi ekupheleni - Thumela konke ngqo ku-shh ngepayipi (
|
)
Izinzuzo:
- Ayinasimo ngempela
- Azikho izinhlangano ze-boilerplate
- Ukuzizwa epholile
Ake sikwenze nje ngaphandle koAnsible. Yebo, yonke into isivele yasungulwa. Yebo, ibhayisikili. Bheka ukuthi ibhayisikili lilula, linenhle kangakanani futhi lincane kangakanani:
$ cat << 'EOF' > deploy.sh
#!/bin/bash
usage_msg="Usage: $0 ssh_address local_image_tag"
ssh_address=${1?$usage_msg}
image_name=${2?$usage_msg}
echo "Connecting to '$ssh_address' via ssh to seamlessly deploy '$image_name'..."
( sed "$a deploy $image_name" | ssh -T $ssh_address ) << 'END_OF_SCRIPT'
deploy() {
echo "Yay! The '${FUNCNAME[0]}' function is executing on '$(hostname)' with argument '$1'"
}
END_OF_SCRIPT
EOF
$ chmod +x deploy.sh
$ ./deploy.sh localhost magic-porridge-pot
Connecting to localhost...
Yay! The 'deploy' function is executing on 'hut' with argument 'magic-porridge-pot'
Kodwa-ke, asikwazi ukuqiniseka ukuthi umsingathi wesilawuli kude une-bash eyanele, ngakho-ke sizongeza isheke elincane ekuqaleni (lokhu esikhundleni sokuthi
if [ "$SHELL" != "/bin/bash" ]
then
echo "The '$SHELL' shell is not supported by 'deploy.sh'. Set a '/bin/bash' shell for '$USER@$HOSTNAME'."
exit 1
fi
Futhi manje kuyiqiniso:
$ docker exec reverse-proxy rm /etc/nginx/conf.d/default.conf
$ wget -qO deploy.sh https://git.io/JUURc
$ chmod +x deploy.sh
$ ./deploy.sh localhost uptimer
Sending gzipped image 'uptimer' to 'localhost' via ssh...
Loaded image: uptimer:latest
Connecting to 'localhost' via ssh to seamlessly deploy 'uptimer'...
Deploying 'uptimer_GREEN' in place of 'uptimer_BLUE'...
06f5bc70e9c4f930e7b1f826ae2ca2f536023cc01e82c2b97b2c84d68048b18a
Container started. Checking health...
Requesting 'http://uptimer_GREEN:8080/' within the 'web-gateway' docker network:
HTTP/1.0 503 Service Unavailable
wget: server returned error: HTTP/1.0 503 Service Unavailable
New 'uptimer_GREEN' service is not ready yet. Waiting (1)...
Requesting 'http://uptimer_GREEN:8080/' within the 'web-gateway' docker network:
HTTP/1.0 503 Service Unavailable
wget: server returned error: HTTP/1.0 503 Service Unavailable
New 'uptimer_GREEN' service is not ready yet. Waiting (2)...
Requesting 'http://uptimer_GREEN:8080/' within the 'web-gateway' docker network:
HTTP/1.0 200 OK
Server: BaseHTTP/0.6 Python/3.8.3
Date: Sat, 22 Aug 2020 20:15:50 GMT
Content-Type: text/html
New 'uptimer_GREEN' service seems OK. Switching heads...
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
2020/08/22 20:15:54 [notice] 97#97: signal process started
The 'uptimer_GREEN' service is live!
Killing 'uptimer_BLUE'...
uptimer_BLUE
Total reclaimed space: 0B
Deployment successful!
Manje ungavula
Ungakhohlwa ukuhlanza ngemva komsebenzi :3
$ docker rm -f uptimer_GREEN reverse-proxy
uptimer_GREEN
reverse-proxy
$ docker network rm web-gateway
web-gateway
$ cd ..
$ rm -r blue-green-deployment
Source: www.habr.com