在 Amazon Lightsail 上部署容器 Web 應用程式

入門指南

單元 4:部署新的容器版本

在本單元中,您將學習更新已經部署的容器

簡介

在應用程式的生命中會歷經多次更新。在本單元中,您將學習遇到新應用程式版本發佈時,如何更新部署,以及如何回復部署。

您將學到的內容

  • 更新應用程式碼,及建置新的本機容器映像
  • 推送更新後的容器映像至 Amazon Lightsail
  • 部署新版本的應用程式
  • 萬一出現錯誤時回復部署

 完成時間

10 分鐘

 單元先決條件

  • 有管理員等級存取權限的 AWS 帳戶**
  • 建議的瀏覽器:最新版的 Chrome 或 Firefox

[**]過去 24 小時內建立的帳戶可能尚未有權存取本教學課程所需的服務。

實作

更新您的應用程式碼,及建置新的容器映像

現在假設應用程式首頁上的城市名稱不正確,需要修改。

開啟檔案 code/templates/index.html。搜尋 "Las Vegas",取代為 "Paris"。

接著,依照單元 2 所解說,建置並測試容器。開啟一個終端機,然後鍵入以下命令:

# build the container
docker build -t demo-flask-signup .

# run the container for local testing
docker run -p 8080:80               \
           --rm                     \
           -v ~/.aws:/root/.aws     \
           demo-flask-signup:latest

現在容器已開始執行,請開啟瀏覽器,指向其位址 http://localhost:8080。即應顯示應用程式首頁,並為新城市名稱。

gsg-build-lightsail-4

推送更新後的容器映像至 Amazon Lightsail

現在容器已建置完成,下一個步驟是將這個新容器映像推送至 Amazon Lightsail。

開啟一個終端機,然後鍵入以下命令:

aws lightsail push-container-image    \
    --region eu-west-3                \
    --service-name signup-application \
    --label latest                    \
    --image demo-flask-signup:latest

# the command outputs the following lines
df0f7d33bb59: Pushed 
951ffe99d911: Pushed 
cee40450fe90: Pushed 
1327150a5178: Pushed 
4bc5d67b6427: Layer already exists 
ce8ffb5c560e: Layer already exists 
4226d5d0360f: Layer already exists 
9d1af766c818: Layer already exists 
d97733c0a3b6: Layer already exists 
c553c6ba5f13: Layer already exists 
48b4a40de359: Layer already exists 
ace9ed9bcfaf: Layer already exists 
764055ebc9a7: Layer already exists 
Digest: sha256:42d2e19ee7340bc7f028593318e9b7406e9e70fb4785618d3f6c78443e097a87
Image "demo-flask-signup:latest" registered.
Refer to this image as ":signup-application.latest.2" in deployments. 

輸出的最後一行顯示出容器的內部名稱。請記下來,因為將容器部署至容器服務時會需要用到這個名稱。如果您在稍後想要存取容器名稱,可鍵入以下命令:

aws lightsail get-container-images    \
    --region eu-west-3                \
    --service-name signup-application

# the command outputs the following lines
{
    "containerImages": [
        {
            "image": ":signup-application.latest.2",
            "digest": "sha256:42d2e19ee7340bc7f028593318e9b7406e9e70fb4785618d3f6c78443e097a87",
            "createdAt": "2021-07-17T18:34:00+02:00"
        },
        {
            "image": ":signup-application.latest.1",
            "digest": "sha256:128f84907d30a1fb47c1888720485fa8050cc99bc1034e0cfd1f46d3b6e57e19",
            "createdAt": "2021-07-17T15:11:49+02:00"
        }
    ]
}

請注意 "state": "DEPLOYING" 的狀態。經過一會兒,狀態會變成 ACTIVE。完成部署需要幾分鐘。您可使用以下命令檢查部署的狀態:

# while deploying
aws lightsail get-container-services      \
    --region eu-west-3                    \
    --query "containerServices[].nextDeployment.state"

# once deployed
aws lightsail get-container-services      \
    --region eu-west-3                    \
    --query "containerServices[].currentDeployment.state" 

測試您的部署

若要測試部署,請先擷取為您建立的 URL Lightsail。開啟一個終端機,然後鍵入以下命令:

aws lightsail get-container-services        \
    --region eu-west-3                      \
    --query "containerServices[].url" 

# the command outputs something like the below 
[
    "https://signup-application.me04fvc6dbk4e.eu-west-3.cs.amazonlightsail.com/"
]

我們現在有容器映像的兩個版本 - :signup-application.latest.1 和 :signup-application.latest.2

部署新版本的應用程式

現在容器映像已存放在 Amazon Lightsail,我們可將該映像部署至容器服務,作法是使用類似上一單元所說明的命令:部署容器。

編輯 lc.json 檔案,將所使用的映像名稱 - :signup-application.latest.1 - 取代為新版本 :signup-application.latest.2

新的 lc.json 檔案看起來應像這樣:

{
    "serviceName": "signup-application",
    "containers": {
        "signup-app-container": {
            "image": ":signup-application.latest.2",
            "ports": {
                "80": "HTTP"
            }
        }
    },
    "publicEndpoint": {
        "containerName": "signup-app-container",
        "containerPort": 80
    }
}

接著,鍵入以下命令以部署容器:

aws lightsail create-container-service-deployment \
    --region eu-west-3                            \
    --cli-input-json file://lc.json

# the command outputs the following 

測試您的部署

若要測試部署,請切換至您的瀏覽器視窗,重新整理前一單元所提供容器服務 URL 的頁面。

若要擷取 URL,可在終端機中鍵入此命令:

aws lightsail get-container-services        \
    --region eu-west-3                      \
    --query "containerServices[].url" 

# the command outputs something like the below 
[
    "https://signup-application.me04fvc6dbk4e.eu-west-3.cs.amazonlightsail.com/"
]

回復部署

最後的部署並未覆寫應用程式的第一版,只是建立新版本。

若要確認可用版本,請開啟一個終端機,然後鍵入以下命令:

aws lightsail get-container-service-deployments \
    --region eu-west-3                          \
    --service-name signup-application

# the command outputs a JSON similar to this one 
{
    "deployments": [
        {
            "version": 2,
            "state": "ACTIVE",
            "containers": {
                "signup-app-container": {
                    "image": ":signup-application.latest.2",
                    "command": [],
                    "environment": {},
                    "ports": {
                        "80": "HTTP"
                    }
                }
            },
            "publicEndpoint": {
                "containerName": "signup-app-container",
                "containerPort": 80,
                "healthCheck": {
                    "healthyThreshold": 2,
                    "unhealthyThreshold": 2,
                    "timeoutSeconds": 2,
                    "intervalSeconds": 5,
                    "path": "/",
                    "successCodes": "200-499"
                }
            },
            "createdAt": "2021-07-17T18:40:58+02:00"
        },
        {
            "version": 1,
            "state": "INACTIVE",
            "containers": {
                "signup-app-container": {
                    "image": ":signup-application.latest.1",
                    "command": [],
                    "environment": {},
                    "ports": {
                        "80": "HTTP"
                    }
                }
            },
            "publicEndpoint": {
                "containerName": "signup-app-container",
                "containerPort": 80,
                "healthCheck": {
                    "healthyThreshold": 2,
                    "unhealthyThreshold": 2,
                    "timeoutSeconds": 2,
                    "intervalSeconds": 5,
                    "path": "/",
                    "successCodes": "200-499"
                }
            },
            "createdAt": "2021-07-17T15:31:01+02:00"
        }
    ]
}

閱讀上個命令的輸出,可觀察出建立有兩個部署。版本 1 為 INACTIVE 狀態,版本 2 則為 ACTIVE。

以下命令可為您的部署提供較為精簡的概觀:

aws lightsail get-container-service-deployments \
    --region eu-west-3                          \
    --service-name signup-application           \
    --query 'deployments[].{version: version, state: state, image: containers."signup-app-container".image}'

# the command outputs something similar to 
[
    {
        "version": 2,
        "state": "ACTIVE",
        "image": ":signup-application.latest.2"
    },
    {
        "version": 1,
        "state": "INACTIVE",
        "image": ":signup-application.latest.1"
    }
]

現在請想像您剛部署的應用程式最新版本行為表現不正確。可能是在測試的時候錯誤並未浮現。

若要存取容器的記錄,請開啟終端機,鍵入以下命令:

aws lightsail get-container-log             \
    --region eu-west-3                      \
    --service-name signup-application       \
    --container-name signup-app-container   \
    --output text

# the command outputs log lines similar to this 
LOGEVENTS       2021-07-17T18:14:51+02:00       [pid: 26|app: 0|req: 5821/5821] 172.26.7.236 () {34 vars in 401 bytes} [Sat Jul 17 16:14:51 2021] GET / => generated 4579 bytes in 0 msecs (HTTP/1.1 200) 2 headers in 81 bytes (1 switches on core 0)
LOGEVENTS       2021-07-17T18:14:51+02:00       [pid: 23|app: 0|req: 5824/5824] 172.26.7.236 () {34 vars in 402 bytes} [Sat Jul 17 16:14:51 2021] GET / => generated 4579 bytes in 0 msecs (HTTP/1.1 200) 2 headers in 81 bytes (1 switches on core 0)
LOGEVENTS       2021-07-17T18:14:51+02:00       172.26.7.236 - - [17/Jul/2021:16:14:51 +0000] "GET / HTTP/1.1" 200 4579 "-" "ELB-HealthChecker/2.0" "-"

為了快速回復部署,並重新部署上一版本的應用程式,請變更 lc.json 檔案,使其使用容器映像的第一版,並且重新部署這個映像

lc.json 檔案修改成為如下所示:

# update the lc.json file
{
    "serviceName": "signup-application",
    "containers": {
        "signup-app-container": {
            "image": ":signup-application.latest.1",
            "ports": {
                "80": "HTTP"
            }
        }
    },
    "publicEndpoint": {
        "containerName": "signup-app-container",
        "containerPort": 80
    }
}

接著在終端機中執行以下命令:

aws lightsail create-container-service-deployment \
    --region eu-west-3                            \
    --cli-input-json file://lc.json

# the command outputs the following 
{
    "containerService": {
        "containerServiceName": "signup-application",
        "arn": "arn:aws:lightsail:eu-west-3:486652066693:ContainerService/528a0bcf-fd14-42d4-a09a-943a241adc51",
        "createdAt": "2021-07-17T13:38:40+02:00",
        "location": {
            "availabilityZone": "all",
            "regionName": "eu-west-3"
        },
        "resourceType": "ContainerService",
        "tags": [],
        "power": "nano",
        "powerId": "nano-1",
        "state": "DEPLOYING",
        "scale": 2,
        "currentDeployment": {
            "version": 2,
            "state": "ACTIVE",
            "containers": {
                "signup-app-container": {
                    "image": ":signup-application.latest.2",
                    "command": [],
                    "environment": {},
                    "ports": {
                        "80": "HTTP"
                    }
                }
            },
            "publicEndpoint": {
                "containerName": "signup-app-container",
                "containerPort": 80,
                "healthCheck": {
                    "healthyThreshold": 2,
                    "unhealthyThreshold": 2,
                    "timeoutSeconds": 2,
                    "intervalSeconds": 5,
                    "path": "/",
                    "successCodes": "200-499"
                }
            },
            "createdAt": "2021-07-17T18:40:58+02:00"
        },
        "nextDeployment": {
            "version": 3,
            "state": "ACTIVATING",
            "containers": {
                "signup-app-container": {
                    "image": ":signup-application.latest.1",
                    "command": [],
                    "environment": {},
                    "ports": {
                        "80": "HTTP"
                    }
                }
            },
            "publicEndpoint": {
                "containerName": "signup-app-container",
                "containerPort": 80,
                "healthCheck": {
                    "healthyThreshold": 2,
                    "unhealthyThreshold": 2,
                    "timeoutSeconds": 2,
                    "intervalSeconds": 5,
                    "path": "/",
                    "successCodes": "200-499"
                }
            },
            "createdAt": "2021-07-17T19:40:42+02:00"
        },
        "isDisabled": false,
        "principalArn": "arn:aws:iam::577312533299:role/amazon/lightsail/eu-west-3/containers/signup-application/1jetjd21p8qshe57seuh71tnrnn29ps77lnno20sbgl2ghbuc0r0",
        "privateDomainName": "signup-application.service.local",
        "url": "https://signup-application.me04fvc6dbk4e.eu-west-3.cs.amazonlightsail.com/"
    }
}

經過片刻,部署 v3 (使用容器映像 v1) 呈現 ACTIVE 狀態。部署 v2 和 v1 為 INACTIVE

您可確認第一個版本已經部署,作法是開啟瀏覽器,指向容器服務 URL。

gsg-build-lightsail-3

結語

本指南到此結束。在課後好好休息之前,請務必清理環境,避免在您的 AWS 帳戶上累計費用。

下一步:清理資源

讓我們知道我們表現如何。

感謝您的意見回饋
我們很高興此頁面對您有所幫助。您願意分享更多詳細資訊以協助我們繼續改進嗎?
關閉
感謝您的意見回饋
很抱歉此頁面沒有幫助到您。您願意分享更多詳細資訊以協助我們繼續改進嗎?
關閉