Te Whakamahi Kahurangi-Kakariki i nga utu iti rawa

I tenei tuhinga ka whakamahia e matou Tuhinga, SSH, toka и nginx Ka whakarite maatau i te whakatakotoranga o te tono tukutuku. Te whakatakotoranga puru-matomato he tikanga e taea ai e koe te whakahou tonu i tetahi tono me te kore e paopao ki te tono kotahi. Koia tetahi o nga rautaki tuku wa-kore, he pai rawa atu mo nga tono me te tauira kotahi, engari ko te kaha ki te uta i tetahi tauira tuarua, kua rite ki te whakahaere tata.

Me kii he tono tukutuku kei a koe te maha o nga kaihoko e kaha ana ki te mahi, karekau he huarahi mo te takoto mo etahi hēkona. A ka tino hiahia koe ki te whakaputa i tetahi whakahou whare pukapuka, he whakatika iro, he waahanga hauhautanga hou ranei. I roto i nga ahuatanga noa, me whakamutu te tono, whakakapihia ka timata ano. Mo te take o te docker, ka taea e koe te whakakapi i te tuatahi, katahi ano ka whakaara ano, engari tera ano te waa e kore e tukuna nga tono ki te tono, na te mea ko te tikanga he wa roa te tono ki te uta tuatahi. He aha mehemea ka timata, engari ka puta ke te kore e taea te whakahaere? Ko te raruraru tenei, ma tatou e whakaoti ma te iti o nga tikanga me te huatau ka taea.

WHAKATOKANGA: Ko te nuinga o te tuhinga ka whakaatuhia i roto i te whakatakotoranga whakamatautau - i te ahua o te rekoata o te huihuinga papatohu. Ko te tumanako kare tenei e tino uaua ki te maarama me te tuhi tika te waehere. Mo te hau, whakaaro ake ehara enei i nga kohanga tohu noa, engari he pepa mai i te momo waea "rino".

Te Whakamahi Kahurangi-Kakariki i nga utu iti rawa

Ko nga tikanga whakamere he uaua ki a Google ma te panui noa i te waehere ka whakaahuahia i te timatanga o ia waahanga. Mena kei te marama etahi atu mea, google ka tirohia. whakamarama anga (waimarie, ka mahi ano, na te wetewete o te waea waea). Mena kaore e taea e koe a Google tetahi mea, patai i roto i nga korero. Ka harikoa ahau ki te taapiri atu ki te waahanga e pa ana "Nga tikanga whakahihiri".

Tatou ka ha'i.

$ mkdir blue-green-deployment && cd $_

ratonga

Ka mahia he ratonga whakamatautau ka tuu ki roto i te ipu.

Nga tikanga whakamere

  • cat << EOF > file-name (Here Tuhinga + I/O Redirection) he huarahi ki te hanga i te konae raina-maha me te whakahau kotahi. Ko nga mea katoa ka panuihia e te bash /dev/stdin i muri i tenei rarangi me mua i te rarangi EOF ka tuhia ki roto file-name.
  • wget -qO- URL (whakamarama anga) — whakaputa i te tuhinga i riro ma HTTP ki /dev/stdout (whakariterite curl URL).

Tānga

Ka pakaruhia e au te waahanga kia taea ai te whakaatu mo Python. Hei te mutunga ka puta ano tetahi wahi penei. Whakaarohia i enei waahi ka tapahia te pepa kia tukuna ki te tari miramira (kei reira te tohu tohu-a-ringa ki nga tohu), katahi ka whakapirihia enei waahanga.

$ 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

Takawaenga whakamuri

Kia taea ai e ta maatau tono te whakarereke i te kore e kitea, me noho etahi atu hinonga kei mua i a ia ka huna i tana whakakapi. He tūmau tukutuku pea nginx в aratau takawaenga whakamuri. Kua whakaritea he takawaenga whakamuri i waenga i te kiritaki me te tono. Ka whakaaehia nga tono mai i nga kaihoko ka tukuna atu ki te tono me te tuku i nga whakautu a te tono ki nga kaihoko.

Ko te tono me te takawaenga whakamuri ka taea te hono ki roto docker ma te whakamahi whatunga docker. No reira, ko te ipu me te tono kaore e hiahia ki te tuku tauranga ki runga i te punaha kaihautu; ma tenei ka taea te tono kia tino wehea mai i nga riri o waho.

Mena kei te noho te takawaenga whakamuri i runga i tetahi atu kaihautu, me whakarere e koe te whatunga docker me te hono i te tono ki te takawaenga whakamuri ma te whatunga kaihautu, me te tuku whakamua i te tauranga taupānga tawhā --publish, pera i te timatanga tuatahi me te takawaenga whakamuri.

Ka whakahaerehia e matou te takawaenga whakamuri i runga i te tauranga 80, na te mea koinei tonu te hinonga me whakarongo ki te whatunga o waho. Mena kei te pukumahi te tauranga 80 i runga i to kaihautu whakamatautau, hurihia te tawhā --publish 80:80 i runga i --publish ANY_FREE_PORT:80.

Nga tikanga whakamere

Tānga

$ 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>

Te whakatakotoranga ngawari

Me whakaputa he putanga hou o te tono (me te whakanui i nga mahi whakaoho e rua nga wa) ka ngana ki te tuku marie.

Nga tikanga whakamere

  • echo 'my text' | docker exec -i my-container sh -c 'cat > /my-file.txt' — Tuhia kupu my text ki te konae /my-file.txt i roto i te ipu my-container.
  • cat > /my-file.txt — Tuhia nga ihirangi o te whakauru paerewa ki tetahi konae /dev/stdin.

Tānga

$ 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

I tenei wa, ka hangaia te ahua ki runga i te tūmau, me noho nga puna tono ki reira, ka utaina hoki te tūmau ki nga mahi kore. Ko te mahi e whai ake nei ko te tohatoha i te huihuinga whakaahua ki tetahi miihini motuhake (hei tauira, ki te punaha CI) ka whakawhiti atu ki te tūmau.

Te whakawhiti whakaahua

Engari, kaore he tikanga ki te whakawhiti i nga whakaahua mai i te localhost ki te localhost, na tenei waahanga ka taea te tirotiro mena ka rua nga kaihautu kei a koe me Docker. I te iti rawa te ahua penei:

$ 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

rōpū docker save ka tiaki i nga raraunga atahanga ki roto i te puranga .tar, te tikanga he 1.5 taima te taumaha ake i te taumaha i roto i te ahua kōpeke. Na me ruru tatou i runga i te ingoa o te penapena wa me te hokohoko:

$ docker image save uptimer | gzip | ssh production-server 'zcat | docker image load'
Loaded image: uptimer:latest

Ka taea hoki e koe te aro turuki i te mahi tango (ahakoa me whai taputapu tuatoru):

$ docker image save uptimer | gzip | pv | ssh production-server 'zcat | docker image load'
25,7MiB 0:01:01 [ 425KiB/s] [                   <=>    ]
Loaded image: uptimer:latest

Aki: Mena ka hiahia koe ki te maha o nga tawhā hei hono atu ki tetahi tūmau ma te SSH, kaore pea koe e whakamahi i te konae ~/.ssh/config.

Te whakawhiti i te ahua ma docker image save/load - Koinei te tikanga tino iti, engari ehara i te mea anake. Ko etahi atu:

  1. Rehita Ipu (paerewa ahumahi).
  2. Hono atu ki te tūmau daemon docker mai i tetahi atu kaihautu:
    1. taurangi taiao DOCKER_HOST.
    2. Kōwhiringa raina whakahau -H ranei --host taputapu docker-compose.
    3. docker context

Ko te tikanga tuarua (e toru nga waahanga mo tona whakatinanatanga) he pai te korero i roto i te tuhinga Me pehea te tuku ki nga kaihautu Docker mamao me te docker-compose.

deploy.sh

Inaianei me kohikohi nga mea katoa i mahia e maatau ki te tuhi kotahi. Me timata ki te mahi taumata-runga, ka titiro ki etahi atu e whakamahia ana i roto.

Nga tikanga whakamere

  • ${parameter?err_msg} - tetahi o nga karakia makutu bash (aka whakakapinga tawhā). Mena parameter kaore i tohua, putanga err_msg ka puta me te waehere 1.
  • docker --log-driver journald — ma te taunoa, he konae tuhinga te taraiwa taki docker kaore he hurihanga. Ma tenei huarahi, ka tere te whakakii i te kōpae katoa, na reira mo te taiao whakaputa me whakarereke te taraiwa ki te maamaa.

Te whakatakotoranga tuhinga

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
}

Ngā āhuatanga i whakamahia:

  • ensure-reverse-proxy — Me mohio kei te mahi te takawaenga whakamuri (he whai hua mo te tuku tuatahi)
  • get-active-slot service_name — Ka whakatau ko tehea mokamoka e hohe ana mo tetahi ratonga (BLUE ranei GREEN)
  • get-service-status service_name deployment_slot — Ka whakatau mena kua reri te ratonga ki te whakahaere i nga tono taumai
  • set-active-slot service_name deployment_slot — Hurihia te whirihora nginx i roto i te ipu takawaenga whakamuri

I te raupapa:

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
}

mahi get-active-slot me iti te whakamarama:

He aha te take e whakahoki ai i te tau, kaore e whakaputa i te aho?

Heoi ano, i roto i te mahi karanga ka tirohia e matou te hua o ana mahi, me te tirotiro i te waehere putanga ma te whakamahi i te bash he maamaa ake i te tirotiro i te aho. I tua atu, he tino ngawari te tiki aho mai i a ia:
get-active-slot service && echo BLUE || echo GREEN.

He tino ranea nga tikanga e toru hei wehewehe i nga kawanatanga katoa?

Te Whakamahi Kahurangi-Kakariki i nga utu iti rawa

Ahakoa e rua ka nui, ko te mea whakamutunga kei konei mo te whakaoti, kia kore ai e tuhi else.

Ko te mahi anake e whakahoki mai ana i nga whirihora nginx ka kore e tautuhia: get-nginx-config service_name deployment_slot. Ma te whakataurite ki te tirotiro hauora, i konei ka taea e koe te whakarite whirihora mo tetahi ratonga. O nga mea whakamere - anake cat <<- EOF, ka taea e koe te tango i nga ripa katoa i te timatanga. Pono, ko te utu o te whakahōputu pai ko nga ripa whakauru me nga waahi, e kiia ana i tenei ra he ahua kino rawa. Engari ka kaha te bash ki nga ripa, a he pai ano te whakatakotoranga noa i roto i te whirihora nginx. Hei poto, ko te whakaranu i nga ripa me nga waahi kei konei te ahua pai rawa atu o te kino. Engari, kaore koe e kite i tenei i roto i te waahanga i raro nei, na te mea "he pai" a Habr ma te huri i nga ripa katoa ki te 4 mokowhiti me te whakakore i te EOF. Na konei ka kitea.

Kia kore ai e ara ake, ka korero tonu ahau ki a koe cat << 'EOF', ka kitea i muri mai. Mena ka tuhi noa koe cat << EOF, ka noho ki roto i te heredoc ka uru te aho (ka whakaroahia nga taurangi ($foo), waea whakahau ($(bar)) me etahi atu), a, ki te kapitihia e koe te mutunga o te tuhinga ki nga korukī kotahi, ka monokia te whakaurunga me te tohu $ ka whakaatuhia tonu. He aha e hiahia ana koe ki te whakauru i tetahi tuhinga ki roto i tetahi atu tuhinga.

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
}

Koinei te tuhinga katoa. Na ka pera tino nui me tenei tuhinga mo te tango ma te wget, curl ranei.

Te whakahaere i nga tuhinga tawhā i runga i te tūmau mamao

Kua tae ki te wa ki te patoto i te tūmau i whäia. Tenei wa localhost tino pai:

$ 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.

Kua tuhia e matou he tuhinga whakatakotoranga e tango ana i tetahi ahua kua oti te hanga ki te tūmau i whäia, ka whakakapi marie i te ipu ratonga, engari me pehea e taea ai e tatou te mahi i runga i te miihini mamao? He tohenga to te tuhinga, na te mea he ao katoa ka taea te tuku i nga ratonga maha i te wa kotahi i raro i tetahi takawaenga whakamuri (ka taea e koe te whakamahi i nga whirihora nginx hei whakatau ko tehea te url ko tehea ratonga). Kaore e taea te penapena i te tuhinga ki runga i te kaimau, na te mea i tenei keehi kaore e taea e taatau te whakahou aunoa (mo te whakatikatika i nga pepeha me te taapiri i nga ratonga hou), me te nuinga o te whenua = kino.

Rongoā 1: Pupuri tonu te hōtuhi i runga i te tūmau, engari tāruatia i ia wa scp. Na ka hono ma ssh ka mahia te tuhinga me nga tohenga e tika ana.

Cons:

  • E rua nga mahi hei utu mo te kotahi
  • Kaore pea he waahi ka kape koe, karekau pea e uru atu, ka mahia ranei te tuhinga i te wa o te whakakapinga.
  • He mea tika kia horoi i muri i a koe (whakakorehia te tuhinga).
  • E toru nga mahi.

Otinga 2:

  • Kia mau ki nga whakamaramatanga mahi anake i roto i te tuhinga me te whakahaere i tetahi mea
  • Ma te awhina o sed tāpirihia he waea mahi ki te mutunga
  • Tukuna katoa ki te shh ma te paipa (|)

Kaupapa:

  • Tino kore whenua
  • Kaore he hinonga boilerplate
  • Te hauhautanga

Me mahi noa me te kore Ansible. Ae, kua oti kee nga mea katoa te whakaaro. Ae, he paihikara. Tirohia te ngawari, te huatau me te iti o te pahikara:

$ 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'

Heoi, kare e taea e matou te tino mohio kei te tika te kaihautu mamao, no reira ka taapirihia he haki iti i te timatanga (he utu tenei pupuhi marara):

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

Na inaianei he pono:

$ 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!

Inaianei ka taea e koe te whakatuwhera http://localhost/ i roto i te kaitirotiro, whakahaere ano i te horahanga me te whakarite kia pai te haere ma te whakahou i te whaarangi kia rite ki te CD i te wa o te whakatakotoranga.

Kaua e wareware ki te horoi i muri i te mahi :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: will.com