在 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 处于非活动状态,而版本 2 处于活动状态。

以下命令对您的部署进行了不太详细的概述:

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/"
    }
}

一段时间后,使用容器映像 v1 的部署 v3 将处于 ACTIVE 状态。部署 v2 和 v1 处于 INACTIVE 状态。

您可以通过打开浏览器并将其指向容器服务 URL 来验证是否部署了第一个版本。

gsg-build-lightsail-3

结论

本指南到此结束。在进行合理的休息之前,请务必清理环境,以免在您的 AWS 账户上产生费用。

下一模块:清理资源

请就我们的表现提供反馈。

感谢您的反馈
很高兴此页面对您有所帮助。您是否乐意分享更多详细信息,以帮助我们继续改进?
关闭
感谢您的反馈
很抱歉,此页面未能帮到您。您是否乐意分享更多详细信息,以帮助我们继续改进?
关闭