博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
守望先锋代码_使用守望台自动刷新SQL Server容器
阅读量:2510 次
发布时间:2019-05-11

本文共 14603 字,大约阅读时间需要 48 分钟。

守望先锋代码

介绍 (Introduction)

I’ve been working with SQL Server in containers for a while now and one of the challenges that I have is ensuring that the databases within them are kept at the correct version. This is important because you don’t want your development teams testing against databases that have an out of date schema (or reference data).

我使用容器中SQL Server已有一段时间了,我面临的挑战之一是确保其中的数据库保持正确的版本。 这很重要,因为您不希望开发团队针对具有过期架构(或参考数据)的数据库进行测试。

The databases that I store in my container image are updated on a weekly basis and currently, the process to update our containers is manual. Once the updated image has been created, the existing running containers are dropped and new ones created from the updated image.

我存储在容器映像中的数据库每周更新一次,目前,更新容器的过程是手动的。 创建更新的映像后,将删除现有的正在运行的容器,并根据更新的映像创建新的容器

But what if we could automatically refresh our containers with the updated image? If we could do that then the only process that’s manual is updating the image. We would no longer have to worry about any containers running SQL instances with databases that are out of date.

但是,如果我们可以使用更新的图像自动刷新容器,该怎么办? 如果我们能够做到这一点,那么唯一的手动过程就是更新映像。 我们不再需要担心任何运行SQL实例且数据库已过时的容器。

Luckily, there’s a way to do this and it’s accessible via an image on the Docker Hub called Watchtower. What Watchtower does is monitor the Docker Hub and if there’s an update to an image it will automatically refresh all running containers that are on the same host.

幸运的是,有一种方法可以通过Docker Hub上名为Watchtower的映像进行访问。 Watchtower的工作是监视Docker Hub,如果更新了映像,它将自动刷新同一主机上的所有正在运行的容器。

This article will guide you through the setup of this process and how to run a simple test.

本文将指导您完成此过程的设置以及如何运行简单的测试。

先决条件 (Pre-requisites)

What you will need to run through this process is:

完成此过程需要执行的操作是:

  • Two Linux VMs with internet access (referred to as Linux1 & Linux2)

    两个具有Internet访问权限的Linux VM(称为Linux1和Linux2)
  • 的说明进行操作
  • 的说明进行操作

Craft.io概述 (Process Overview)

Getting this working involves a few steps. Here is an overview of the process. The initial setup involves (on Linux1):

使这项工作涉及几个步骤。 这是该过程的概述。 初始设置涉及(在Linux1上):

  • Creating a custom SQL image with a test database

    使用测试数据库创建自定义SQL映像
  • Creating a repository in the Docker Hub

    在Docker Hub中创建存储库
  • Pushing the custom image to the Docker Hub

    将自定义映像推送到Docker Hub

Once the setup is complete, we’ll start up Watchtower and run a test. The steps I’ll run through are:

设置完成后,我们将启动“守望台”并运行测试。 我将执行的步骤是:

    • Pulling the Watchtower image from the Docker Hub

      从Docker集线器中获取城楼镜像
    • Running Watchtower in a container

      在容器中运行Watch望塔
    • Creating two SQL containers from the custom image created in the initial setup.

      从在初始设置中创建的自定义映像创建两个SQL容器。
    • Creating an updated image to replace the one in the Docker Hub.

      创建一个更新的映像以替换Docker Hub中的映像。
    • Pushing the updated image to the Docker Hub

      将更新的映像推送到Docker Hub
    • Check to see if containers have been updated automatically

      检查容器是否已自动更新

设置–在Linux 1上 (Setup – On Linux 1)

Create a SQL container

创建一个SQL容器

To get started with creating a custom image, the first thing I will do is spin up a container using the command:

要开始创建自定义图像,我要做的第一件事是使用命令启动一个容器:

  1. docker run -d -p 15789:1433 –env ACCEPT_EULA=Y –env SA_PASSWORD=Testing1122
  2. docker run -d -p 15789:1433 –env ACCEPT_EULA = Y –env SA_PASSWORD = Testing1122
  3. –name testcontainer microsoft/mssql-server-linux  
  4. –名称测试容器microsoft / mssql-server-linux

N.B. If the microsoft/mssql-server-linux image is not in the local repository it will be pulled down automatically.

注意:如果microsoft / mssql-server-linux映像不在本地存储库中,它将被自动下拉。

Next, I need to get the private IP address of that container so that I can connect to the SQL instance. I can do this by running the command. The IP address is shown at the bottom of the output:

接下来,我需要获取该容器的私有IP地址,以便可以连接到SQL实例。 我可以通过运行命令来做到这一点。 IP地址显示在输出的底部:

  1. docker inspect testcontainer  
  2. 码头工人检查testcontainer

Creating a test database within the container

在容器内创建测试数据库

I’m going to connect using sqlcmd and create a test database using the private IP returned from the inspect command:

我将使用sqlcmd进行连接,并使用从inspect命令返回的私有IP创建测试数据库:

  1. sqlcmd – S 172.17.0.2 – U sa  
  2. sqlcmd – S 172.17.0.2 –美国

And then I can verify the databases that are in the SQL instance:

然后,我可以验证SQL实例中的数据库:

  1. select name from sys.databases  
  2. 从sys.databases中选择名称
  3. go

Now I’ll create a new database:

现在,我将创建一个新数据库:

  1. create  database  testdatabase1;    
  2. 创建数据库testdatabase1;
  3. go  
  4. select  name  from  sys.databases;    
  5. 从sys.databases中选择名称;
  6. go    

Commit the container as a custom image

将容器作为自定义图像提交

I now have a container running a SQL instance with a custom user database. I’ll store this as a custom image by first stopping the container:

我现在有一个运行带有自定义用户数据库SQL实例的容器。 我将首先停止容器将其存储为自定义图像:

  1. docker stop testcontainer  
  2. 码头工人停止测试容器

and then executing the command:

然后执行命令:

  1. docker commit testcontainer testimage  
  2. 码头工人提交testcontainer testimage

Upload image to the Docker Hub

将映像上传到Docker Hub

The Docker Hub – – is an online repository for Docker images. Not only can I pull images down from the hub, I can upload as well. It is free to register an account and there are two types of repositories that you can create, public and private (only 1 private repository for a free account).

Docker Hub – –是Docker映像的在线存储库。 我不仅可以从集线器下拉图像,还可以上传图像。 注册帐户是免费的,您可以创建两种类型的存储库:公共存储库和私有存储库(免费帐户只有1个私有存储库)。

Only I will be able to see any images that I upload to a private repository whereas anyone will be able to see an image that I upload to a public repository. You need to bear this in mind when uploading anything to a public repository.

只有我可以看到上传到私有存储库的任何图像,而任何人都可以看到上传到公共存储库的图像。 将任何内容上传到公共存储库时,都需要牢记这一点。

I’ve create a public repository for this demo called dbafromthecold/testsqlrepository:

我为此演示创建了一个公共存储库,称为dbafromthecold / testsqlrepository:

Now I need to log into the Docker Hub back on the server using the command:

现在,我需要使用命令重新服务器上的Docker Hub:

  1. docker login  

    码头工人登录

In order to push the image to the Docker Hub the custom image needs to be tagged with our repository name first. This is done via the command which creates a new image from the named source image:

为了将映像推送到Docker Hub,需要使用我们的存储库名称首先标记自定义映像。 这是通过命令完成的,该命令从命名的源映像创建一个新映像:

  1. docker tag testimage dbafromthecold/testsqlrepository:linux
  2. docker tag testimage dbafromthecold / testsqlrepository:linux
  3. docker images
  4. 码头工人图像

Then it can be pushed to the Docker Hub:

然后可以将其推送到Docker Hub:

  1. docker push dbafromthecold/testsqlrepository:linux
  2. docker pushdbafromthecold / testsqlrepository:linux

Once that’s complete, I can verify that the image is online in the Docker Hub

完成后,我可以在Docker Hub中验证映像是否在线

Or I can use the command on the server:

或者,我可以在服务器上使用命令:

  1. docker search dbafromthecold/testsqlrepository:linux  
  2. docker从冷/ testsql存储库中搜索dba:linux

Before moving on I’m going to clean-up the existing container and image. No pressing need to do this other than clarity (so I know which containers I’m working with):

在继续之前,我将清理现有的容器和图像。 除了清楚之外,没有其他紧迫的事情要做(所以我知道我在使用哪个容器):

  1. docker rm testcontainer  
  2. docker rm testcontainer
  3. docker rmi testimage  
  4. docker rmi testimage

运行守望台容器–在Linux1上 (Running the Watchtower container – On Linux1)

Pull the Watchtower image

拉the望塔图像

In order to automatically refresh containers, the first thing I need to do is search the Docker Hub for the Watchtower image that I want. To locate the image, use the docker search command:

为了自动刷新容器,我需要做的第一件事是在Docker Hub中搜索所需的Watchtower映像。 要找到图像,请使用docker search命令:

  1. docker search watchtower  
  2. 码头工人搜索search望塔

There are a few images that will allow me to run Watchtower. There doesn’t seem to be any official images so I’m going to play it safe and pull down the image with the most stars down to my local Docker repository using the command:

有一些图像可以让我运行Watch望塔。 似乎没有任何官方映像,因此我将使用它进行安全播放,并使用命令将具有最多星星的映像下拉到本地Docker存储库中:

  1. docker pull centurylink/watchtower  
  2. 码头工人拉世纪链接/守望台

When I check my local docker repository I can see that the image is there using the command:

当我检查本地docker存储库时,可以使用命令查看该映像是否存在:

  1. docker images  
  2. 码头工人图像

Start up the Watchtower container

启动the望塔容器

Using the docker run command I will start up the Watchtower container:

使用docker run命令,我将启动Watchtower容器:

  1. docker run -d –name watchtower – v /var/run/docker.sock:/var/run/docker.sock centurylink/watchtower –interval 30  
  2. docker run -d –守望台– v /var/run/docker.sock:/var/run/docker.sock Centurylink / watchtower –interval 30

The -v/var/run/docker.sock:/var/run/docker.sock switch is required as the Watchtower container needs to interact with the Docker API so that it can monitor running containers.

需要-v / var / run / docker.sock:/var/run/docker.sock开关,因为Watchtower容器需要与Docker API交互,以便可以监视正在运行的容器。

The –interval 30 switch specifies how often Watchtower will poll for new images. I’ve set it to 30 seconds for this demo but in reality, it would be longer as my images would not be updated that often.

–interval 30开关指定守望台多久轮询一次新图像。 在本演示中,我将其设置为30秒,但实际上,由于我的图像不会经常更新,因此它会更长。

Running two test SQL containers

运行两个测试SQL容器

For this demo, I’m going to spin up two containers from my custom image:

对于此演示,我将从自定义映像中启动两个容器:

  1. docker run -d -p 15789:1433 –env ACCEPT_EULA=Y –env SA_PASSWORD=Testing1122
  2. docker run -d -p 15789:1433 –env ACCEPT_EULA = Y –env SA_PASSWORD = Testing1122
  3. –name newcontainer dbafromthecold/testsqlrepository:linux  
  4. –从thecold / testsqlrepository:linux命名新容器dba:linux
  1. docker run -d -p 15799:1433 –env ACCEPT_EULA=Y –env SA_PASSWORD=Testing1122
  2. docker run -d -p 15799:1433 –env ACCEPT_EULA = Y –env SA_PASSWORD = Testing1122
  3. –name newcontainer2 dbafromthecold/testsqlrepository:linux  
  4. –namecold / testsqlrepository:linux中的newcontainer2 dba:linux

Now that I have two containers running from my custom image, I’m going to move over to Linux2 in order to create an updated image and push it to the Docker Hub.

现在,我有两个从自定义映像运行的容器,我将移至Linux2以便创建更新的映像并将其推送到Docker Hub。

更新定制映像– Linux 2 (Updating the custom image – Linux 2)

I am now going to create a completely new image on Linux2, tag it with the same custom repository name as the image on Linux1 and push it to the Docker Hub. Once this is done I will check the running containers on Linux1 to see if they have been automatically refreshed.

我现在要在Linux2上创建一个全新的映像,并使用与Linux1上的映像相同的自定义存储库名称对其进行标记,然后将其推送到Docker Hub。 完成此操作后,我将检查Linux1上正在运行的容器,以查看它们是否已自动刷新。

The first thing I will do on Linux2 is create a new container:

我将在Linux2上做的第一件事是创建一个新容器:

  1. docker run -d -p 15789:1433 –env ACCEPT_EULA=Y –env SA_PASSWORD=Testing1122 –name testcontainer microsoft/mssql-server-linux 
  2. docker run -d -p 15789:1433 –env ACCEPT_EULA = Y –env SA_PASSWORD = Testing1122 –name testcontainer microsoft / mssql-server-linux

Next, I will get the container’s private IP address by running the docker inspect command:

接下来,我将通过运行docker inspect命令获得容器的私有IP地址:

  1. docker inspect testcontainer
  2. 码头工人检查testcontainer

Now I can connect to the SQL instance within the container:

现在,我可以连接到容器内SQL实例:

  1. sqlcmd -S 172.17.0.2 -U sa
  2. sqlcmd -S 172.17.0.2 -U sa

And then I can verify the databases that are in the SQL instance:

然后,我可以验证SQL实例中的数据库:

  1. select name from sys.databases   
  2. 从sys.databases中选择名称
  3. go  

What I’m going to do now is create three new databases within the container: –

我现在要做的是在容器内创建三个新数据库:–

  1. create database testdatabase2;
  2. 创建数据库testdatabase2;
  3. go 
  4. create database testdatabase3;
  5. 创建数据库testdatabase3;
  6. go 
  7. create database testdatabase4;
  8. 创建数据库testdatabase4;
  9. go
  10. select name from sys.databases;
  11. 从sys.databases中选择名称;
  12. go  

The container now has three new databases within it. I’m going to commit that container as an image in the local repository on Linux2:

容器中现在包含三个新数据库。 我要将该容器作为映像提交到Linux2上的本地存储库中:

  1. docker stop testcontainer 
  2. 码头工人停止测试容器
  3. docker commit testcontainer updatedimage  
  4. docker commit testcontainer更新了图像
  5. docker images
  6. 码头工人图像

The next steps are to tag that image with the repository name and push to the Docker Hub but first I need to log in:

下一步是用存储库名称标记该映像并推送到Docker Hub,但首先我需要登录:

  1. docker login  
  2. 码头工人登录

Now I can use the docker tag command to create a new image with the repository name:

现在,我可以使用docker tag命令使用存储库名称创建一个新映像:

  1. docker tag updatedimage dbafromthecold/testsqlrepository:linux
  2. 泊坞窗标签已更新映像dbafromthecold / testsqlrepository:linux
  3. docker images  
  4. 码头工人图像

The final step is to push the image to the Docker Hub:

最后一步是将映像推送到Docker Hub:

  1. docker push dbafromthecold/testsqlrepository:linux  
  2. docker pushdbafromthecold / testsqlrepository:linux

确认容器的自动更新– Linux1 (Confirm automatic update of containers – Linux1)

The Watchtower container running on Linux1 should have detected the updated image pushed to the Docker Hub from Linux 2. I’m going to confirm by first running the command to give me the status of the containers:

在Linux1上运行的Watchtower容器应该已经检测到从Linux 2推送到Docker Hub的更新映像。我将通过首先运行命令为我提供容器的状态进行确认:

  1. docker ps  
  2. 码头工人ps

The uptime of the containers shows that they have only been running for about a minute. What’s happened is that Watchtower has detected that the image on the Docker Hub has changed, stopped the existing containers and restarted them using the updated image.

容器的正常运行时间表明它们只运行了大约一分钟。 发生的情况是,t望塔已检测到Docker Hub上的映像已更改,已停止现有容器并使用更新的映像重新启动了它们。

But has Watchtower really refreshed the containers? I’m going to confirm this by logging into one of the SQL instances within the containers and checking the databases.

但是,守望台是否真的刷新了容器? 我将通过登录容器内的一个SQL实例并检查数据库来确认这一点。

I’ll get the IP address of one of the containers:

我将获得其中一个容器的IP地址:

  1. docker inspect newcontainer  
  2. 码头工人检查newcontainer

Connecting using sqlcmd:

使用sqlcmd连接:

  1. sqlcmd -S 172.17.0.4 -U sa  
  2. sqlcmd -S 172.17.0.4 -U sa

And check the databases that are in the SQL instance:

并检查SQL实例中的数据库:

  1. select name from sys.databases;  

    从sys.databases中选择名称;
  2. go  

The new databases are there! The container has been automatically refreshed with the updated image. However, note that the original database, testdatabase1, is no longer there. This is because any changes to the container that are not in the updated image will be lost. This means that the refresh of the containers needs to be scheduled carefully to avoid work being lost (and thus making unhappy developers).

新的数据库在那里! 容器已使用更新的图像自动刷新。 但是,请注意原始数据库testdatabase1不再存在。 这是因为对容器的任何未包含在更新映像中的更改都将丢失。 这意味着需要仔细计划容器的刷新,以避免工作丢失(从而使开发人员不满意)。

结论 (Conclusion)

By using Watchtower running in a container we can automatically refresh our containers when their image is updated on the Docker Hub. If you use containers for development and want to ensure that you’re always testing against databases that are the correct version then this could be an option for you.

通过使用在容器中运行的守望台,我们可以在Docker Hub上更新容器映像时自动刷新容器。 如果您使用容器进行开发,并且想要确保始终使用正确版本的数据库进行测试,那么这可能是您的选择。

Also, as far as I’m aware, this is only available on Linux. I haven’t seen any Watchtower images that will run on in Windows containers.

另外,据我所知,这仅在Linux上可用。 我还没有看到任何可以在Windows容器中运行的守望台图像。

If you have any questions, please leave a comment below

如有任何疑问,请在下面发表评论

Thank you for reading.

感谢您的阅读。

参考资料 (References)

翻译自:

守望先锋代码

转载地址:http://ufiwd.baihongyu.com/

你可能感兴趣的文章
SNMP从入门到开发:进阶篇
查看>>
@ServletComponentScan ,@ComponentScan,@Configuration 解析
查看>>
unity3d 射弹基础案例代码分析
查看>>
thinksns 分页数据
查看>>
os模块
查看>>
LINQ to SQL vs. NHibernate
查看>>
基于Angular5和WebAPI的增删改查(一)
查看>>
windows 10 & Office 2016 安装
查看>>
最短路径(SP)问题相关算法与模板
查看>>
js算法之最常用的排序
查看>>
Python——交互式图形编程
查看>>
经典排序——希尔排序
查看>>
团队编程项目作业2-团队编程项目代码设计规范
查看>>
英特尔公司将停止910GL、915GL和915PL芯片组的生产
查看>>
团队编程项目作业2-团队编程项目开发环境搭建过程
查看>>
Stax解析XML示例代码
查看>>
cookie
查看>>
二级图片导航菜单
查看>>
<Using parquet with impala>
查看>>
07-Java 中的IO操作
查看>>