简体   繁体   English

如何在AWS ECS上部署MySQL Docker映像?

[英]How to deploy MySQL docker image on AWS ECS?

I have troubles deploying MySQL image on AWS ECS FARGATE . 我在AWS ECS FARGATE上部署MySQL映像时遇到麻烦。

The cloudformation script that i have is this (dont mind the syntax, i am using python lib Troposphere to manage cloudfromation templates): 我拥有的cloudformation脚本是这样的(不要介意语法,我正在使用python lib Troposphere来管理cloudfromation模板):

    TaskDefinition(
            'WordpressDatabaseTaskDefinition',
            RequiresCompatibilities=['FARGATE'],
            Cpu='512',
            Memory='2048',
            NetworkMode='awsvpc',   
            ContainerDefinitions=[
                ContainerDefinition(
                    Name='WordpressDatabaseContainer',
                    Image='mysql:5.7',
                    Environment=[
                        Environment(Name='MYSQL_ROOT_PASSWORD', Value='root'),
                        Environment(Name='MYSQL_DATABASE', Value='wpdb'),
                        Environment(Name='MYSQL_USER', Value='root'),
                        Environment(Name='MYSQL_PASSWORD', Value='root'),
                    ],
                    PortMappings=[
                        PortMapping(
                            ContainerPort=3306
                        )
                    ]
                )
            ]
        )

The deployment succeeds. 部署成功。 I can even see that the task is running for few seconds until its state changes to STOPPED . 我什至可以看到任务运行了几秒钟,直到其状态变为STOPPED为止。

The only thing that i can see is: 我唯一看到的是:

  1. Stopped reason Essential container in task exited 停止原因任务中的基本容器已退出
  2. Exit Code 1 退出代码1

On localhost it works like a charm. 在本地主机上,它就像一个魅力。 What am i doing here wrong? 我在这里做什么错了? At least - are there ways to debug this? 至少-有调试方法吗?

With AWS ECS, if it is stopping, it may be failing a health check which is causing the container to restart. 使用AWS ECS,如果正在停止,则可能无法通过运行状况检查,从而导致容器重新启动。 What port is the container DB mapped to and can you check the container logs to see what is happening when it starts then stops? 容器DB映射到哪个端口,您可以检查容器日志以了解启动后停止时发生了什么吗? Also, check the logs in ECS under the service or task. 另外,请检查服务或任务下ECS中的日志。 Post it here so I can take a look at them. 张贴在这里,让我看看他们。

So, I found out a mistake. 所以,我发现了一个错误。

THE VERY FIRST THING YOU DO - is you test that docker container on localhost and see if you can reproduce the issue. 您要做的第一件事-是您在本地主机上测试该docker容器并查看是否可以重现该问题。 In my case docker mysql container on a local machine with the exact same environment crashed too. 就我而言,环境完全相同的本地计算机上的docker mysql容器也崩溃了。 I was able to inspect logs and found out that it fails to create "root" user. 我能够检查日志,发现它无法创建“ root”用户。 Simply changing user and password made everything work, even on ECS. 只需更改用户名和密码,就可以在ECS上正常工作。

This is the complete stack to have a mysql docker image running on AWS ECS FARGATE : 这是在AWS ECS FARGATE上运行mysql docker映像的完整堆栈:

    self.wordpress_database_task = TaskDefinition(
            'WordpressDatabaseTaskDefinition',
            RequiresCompatibilities=['FARGATE'],
            Cpu='512',
            Memory='2048',
            NetworkMode='awsvpc',

            # If your tasks are using the Fargate launch type, the host and sourcePath parameters are not supported.
            Volumes=[
                Volume(
                    Name='MySqlVolume',
                    DockerVolumeConfiguration=DockerVolumeConfiguration(
                        Scope='shared',
                        Autoprovision=True
                    )
                )
            ],

            ContainerDefinitions=[
                ContainerDefinition(
                    Name='WordpressDatabaseContainer',
                    Image='mysql:5.7',
                    Environment=[
                        Environment(Name='MYSQL_ROOT_PASSWORD', Value='root'),
                        Environment(Name='MYSQL_DATABASE', Value='wpdb'),
                        Environment(Name='MYSQL_USER', Value='wordpressuser'),
                        Environment(Name='MYSQL_PASSWORD', Value='wordpressuserpassword'),
                    ],
                    PortMappings=[
                        PortMapping(
                            ContainerPort=3306
                        )
                    ]
                )
            ]
        )

        self.wordpress_database_service = Service(
            'WordpressDatabaseService',
            Cluster=Ref(self.ecs_cluster),
            DesiredCount=1,
            TaskDefinition=Ref(self.wordpress_database_task),
            LaunchType='FARGATE',
            NetworkConfiguration=NetworkConfiguration(
                AwsvpcConfiguration=AwsvpcConfiguration(
                    Subnets=[Ref(sub) for sub in VpcFormation().public_subnets],
                    AssignPublicIp='ENABLED',
                    SecurityGroups=[Ref(self.security_group)]
                )
            ),
        )

Note the AssignPublicIp='ENABLED' option so you would be able to connect to the database remotely. 请注意AssignPublicIp ='ENABLED'选项,这样您就可以远程连接到数据库。

After the stack completed i was able to successfully connect with a command: 堆栈完成后,我可以使用以下命令成功连接:

mysql -uwordpressuser -pwordpressuserpassword -h18.202.31.123 mysql -uwordpressuser -pwordpressuserpassword -h18.202.31.123

Thats it :) 而已 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM