使用原子主机（Atomic Host）、Ansible 和 Cockpit 部署容器
原子主机 容器 Docker
![](/data/attachment/album/201610/13/232927uyijz5xg4gx5hlf0.jpg)


我在[红帽](https://www.redhat.com/)工作的期间，每天在 [Fedora Atomic host](https://getfedora.org/en/cloud/download/atomic.html) 上使用 [Docker](https://www.docker.com/) 容器。 来自<ruby> <a href="http://www.projectatomic.io/">  原子项目 </a> <rp>  （ </rp> <rt>  Project Atomic </rt> <rp>  ） </rp></ruby>的<ruby> 原子主机 <rp>  （ </rp> <rt>  Atomic Host </rt> <rp>  ） </rp></ruby>是一个轻量级容器操作系统，可以以 Docker 格式运行 Linux 容器。它专门为提高效率而定制，使其成为用于云环境的 Docker 运行时系统的理想选择。


幸运的是，我发现一个很好的方式来管理在主机上运行的容器：[Cockpit](http://cockpit-project.org/)。 它是一个具有漂亮的 Web 界面的 GNU/Linux 服务器远程管理工具。它可以帮我管理在主机上运行的服务器和容器。你可以在从之前发布在这里的[这篇概述](https://fedoramagazine.org/cockpit-overview/)中了解 Cockpit 的更多信息。不过，我也希望在主机上可以自动运行容器，我可以使用 [Ansible](https://www.ansible.com/) 来完成这个工作。


请注意，我们不能在原子主机上使用 dnf 命令。原子主机并没有设计为通用操作系统，而是更适合容器和其他用途。但在原子主机上设置应用程序和服务仍然非常容易。这篇文章向您展示了如何自动化和简化这个过程。


### 设置组件


开始之前，请确保你的系统上安装了 Ansible。



```
sudo dnf -y install ansible

```

首先，我们需要在原子主机上运行 cockpit 容器。在你的机器上从 <https://github.com/trishnaguha/fedora-cloud-ansible> 下载它的源代码。



```
$ git clone https://github.com/trishnaguha/fedora-cloud-ansible.git

```

现在切换到 `cockpit` 的目录，并如下编辑 `inventory` 文件：



```
$ cd fedora-cloud-ansible
$ cd cockpit
$ vim inventory

```

完成如下修改：


1. 使用你的原子主机的 IP 替换掉 `IP_ADDRESS_OF_HOST`。
2. 用您的 SSH 私钥文件的路径替换 `ansible_ssh_private_key_file ='PRIVATE_KEY_FILE'` 行中的`PRIVATE_KEY_FILE`。


然后保存并退出 `inventory` 文件编辑。


接下来，编辑 ansible 配置文件：



```
$ vim ansible.cfg

```

替换 `remote_user=User` 中 `User` 为你的原子主机上的远程用户名。然后保存并退出文件编辑。


### 结合起来


现在是运行 Ansible 的 PlayBook 的时候了。此命令开始运行原子主机上的 Cockpit 容器：



```
$ ansible-playbook cockpit.yml

```

Cockpit 现在运行在原子主机上了。使用浏览器去访问你的实例的公网 IP 的 9090 端口——这是 Cockpit 的默认端口。举个例子，如果该实例的 IP 地址是 192.168.1.4，就去访问 192.168.1.4:9090，你将会看到如下的 Web 界面：


![](/data/attachment/album/201610/13/232955lo6melo1mp9kmpkk.jpg)


### 管理你的容器


使用原子主机的登录信息或以 root 用户身份登录。然后访问 Cockpit 管理器上的 Containers 部分来查看原子主机上运行的容器。在下面的示例中，您会看到我还设置了其他容器，如 [httpd](https://github.com/trishnaguha/fedora-cloud-ansible/tree/master/httpd) 和 [redis](https://github.com/trishnaguha/fedora-cloud-ansible/tree/master/redis)：


![](/data/attachment/album/201610/13/233023xcrcsj5gso5kkgpc.jpg)


注意，该界面允许您直接在 Cockpit 管理器中使用 Run 和 Stop 按钮启动和停止容器。您还可以使用 Cockpit 管理器管理您的原子主机。转到 Tools -> Terminals，在这里里你可以使用原子主机的终端：


![](/data/attachment/album/201610/13/233036g7hqvrg0386qr3qo.jpg)


如果您打算在原子主机上部署容器化的应用程序，则可以简单地为其编写一个 PlayBook。然后，您可以使用 `ansible-playbook` 命令进行部署，并使用 Cockpit 来管理容器。


![](/data/attachment/album/201610/13/233046racagpaabh7np8ap.jpg)


欢迎你对这个[仓库](https://github.com/trishnaguha/fedora-cloud-ansible)进行分支或添加容器的 PlayBook。




---


via: <https://fedoramagazine.org/deploy-containers-atomic-host-ansible-cockpit/>


作者：[trishnag](https://fedoramagazine.org/author/trishnag/) 译者：[Bestony](https://github.com/Bestony) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In the course of my job at [Red Hat](https://www.redhat.com), I work with [Docker](https://www.docker.com) containers on [Fedora Atomic host](https://getfedora.org/en/cloud/download/atomic.html) every day. The Atomic Host from [Project Atomic](http://www.projectatomic.io/) is a lightweight container OS that can run Linux containers in Docker format. It’s been modified for efficiency, making it optimal to use as a Docker run-time system for cloud environments.

Fortunately I’ve found a great way to manage containers running on the host: [Cockpit](http://cockpit-project.org/). Cockpit is a remote manager for GNU/Linux servers with a nice Web UI. It lets me manage servers and containers running on the host. You can read more about Cockpit in [this overview article](https://fedoramagazine.org/cockpit-overview/) previously published here. However, I also wanted to automate running containers on the host, which I’ve done using [Ansible](https://www.ansible.com/).

Note that we cannot use the *dnf* command on the Atomic Host. The host is designed not as a general purpose OS, but to be more fit for containers and other purposes. But it’s still very easy to set up applications and services on the Atomic Host. This post shows you how to automate and simplify this process.

## Setting up the components

Before getting started, make sure you have Ansible installed on your system.

sudo dnf -y install ansible

First we will need to run the cockpit container on Atomic host. Copy the sources down from [https://github.com/trishnaguha/fedora-cloud-ansible](https://github.com/trishnaguha/fedora-cloud-ansible) on your machine.

$ git clone https://github.com/trishnaguha/fedora-cloud-ansible.git

Now change your directory to cockpit and edit its *inventory* file as shown below:

$ cd fedora-cloud-ansible $ cd cockpit $ vim inventory

Make the following changes:

- Replace
*IP_ADDRESS_OF_HOST*with the IP address of your Atomic host. - Replace
*PRIVATE_KEY_FILE*in the line*ansible_ssh_private_key_file=’PRIVATE_KEY_FILE’*with your SSH private key file.

Now save and exit the inventory file.

Next, edit the ansible configuration file:

$ vim ansible.cfg

Replace *User* in the line *remote_user=User* with your remote user on your Atomic host. Then save and exit the file.

## Putting it all together

Now it’s time to run the playbook. This command starts running the Cockpit container on the Atomic host:

$ ansible-playbook cockpit.yml

Cockpit is now running on the Atomic host. Use your web browser to visit the public IP of your instance on port 9090. This is the default port of Cockpit. For instance, if the IP address of the instance is 192.168.1.4, browse to *192.168.1.4:9090*. You’ll now see the web interface of Cockpit on the web browser:

## Managing your containers

Login with the credentials of your Atomic host or as *root*. Then visit the *Containers* section on the Cockpit manager to see the containers running on your Atomic host. In the example below, you’ll see I also set up others like [httpd](https://github.com/trishnaguha/fedora-cloud-ansible/tree/master/httpd) and [redis](https://github.com/trishnaguha/fedora-cloud-ansible/tree/master/redis):

Notice the interface lets you start and stop containers directly in the Cockpit manager using the *Run* and *Stop* buttons. You can also manage your Atomic host using the Cockpit manager. Go to *Tools -> Terminals*. There you can use the terminal of the Atomic host:

If you plan to deploy your containerized application on Atomic host, you can simply write a playbook for it. Then you can deploy using the *ansible-playbook* command and manage the containers using Cockpit.

Feel free to fork or add playbooks for containers in the repository [https://github.com/trishnaguha/fedora-cloud-ansible](https://github.com/trishnaguha/fedora-cloud-ansible).

## Denis M.

I will try this out, thank you

## trishnag

Denis M., Glad to hear that :).

## Mario Ojeda

It does work whit windows too?

## trishnag

Mario Ojeda, Yes. You just need to have Ansible installed on the windows system.

## Sébastien Wilmet

Not related to this post, but the RSS stream seems to be broken.

I have this error with Liferea:

“””

There were errors while parsing this feed!

Details

Could not detect the type of this feed! Please check if the source really points to a resource provided in one of the supported syndication formats!

XML Parser Output:

The URL you want Liferea to subscribe to points to a webpage and the auto discovery found no feeds on this page. Maybe this webpage just does not support feed auto discovery.Could not determine the feed type.

You may want to contact the author/webmaster of the feed about this!

“””

## Justin W. Flory

Hey Sébastien, thanks for noticing this. We’re working to resolve the problem with the RSS feed not validating in this ticket. We should have it fixed soon!

## Gerard Braad

It might be helpful to mention the requirements from an initial state, like installation.

This means that what first needs to be done is:

$ dnf install -y atomic

$ systemctl enable docker

$ systemctl start docker

Also, the Ansible scripts themselves only wrap the atomic command. Might be helpful to explain what it does.

Besides that, thanks for providing a nice intro to using the atomic command and running cockpit.

## Gerard Braad

Sorry, my bad. Forgot it was also about Atomic Host. The use of Atomic and

as a command is confusing at times ;-). So the initial state is a way to reproduce it on a stock Fedora install, while the comment about the Ansible playbooks still stands. Some explaination about the command might clear confusion that exists with others.

## trishnag

Gerard Braad, So the idea is to run containers on the atomic host itself. We are executing all the commands above on our host (I have used workstation here). We are just using the IP Address of Atomic host in the inventory file where we want to run the containers. You just need to have Ansible installed on your host (Workstation/ Cloud/ Server).

## Gerard Braad

I understand (already corrected myself as I got interrupted and had too little coffee; atomic host and atomic command). What I meant is the following:

Your Ansible script invokes: cmd: atomic run [containername]

what does the command do.

* pulls image on the target host

* runs the container according to the labels set

* etc.

Explaining what happens on a host can be helpful to newcomers. Treat an article to be targeting a general audience; ELI6 (explain like I am 6). This can prevent questions like: how do I run this? Looking forward to future articles. Hopefully, we will see more about atomic and related projects, like atomic-reactor and commissaire 😉

## trishnag

Yeah sure. Thanks for your feedback :).

## Kefah Issa

An easy and nice post. I created an AWS Fedora 24 atomic instance, clone the repo and configured, then ran ansible-playbook. all went fine.

On https://my-ip:9090 its not accepting any passwords though. (password-based ssh is disable per default with AWS F24 instances).

Is there any thing that I can do to make it work? Should enable passwordful ssh access?

## trishnag

Yeah you need to enable passwordful ssh access for only once. After enabling perform a reboot. After reboot try opening your . Disable passwordful ssh access after that. It will work. For more info you can check this: https://trishnag.wordpress.com/2016/08/17/cockpit-container-on-atomic-host.