﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-xiaomage234-随笔分类-docker</title><link>http://www.blogjava.net/xiaomage234/category/55128.html</link><description>生命本就是一次凄美的漂流，记忆中放不下的，永远是孩提时代的那一份浪漫与纯真！</description><language>zh-cn</language><lastBuildDate>Tue, 25 Oct 2016 06:31:05 GMT</lastBuildDate><pubDate>Tue, 25 Oct 2016 06:31:05 GMT</pubDate><ttl>60</ttl><item><title>部署私有Docker Registry</title><link>http://www.blogjava.net/xiaomage234/archive/2016/10/25/431924.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Tue, 25 Oct 2016 06:24:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2016/10/25/431924.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/431924.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2016/10/25/431924.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/431924.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/431924.html</trackback:ping><description><![CDATA[<h1><div style="display: inline-block;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;">来自：&nbsp;<a href="http://tonybai.com/2016/02/26/deploy-a-private-docker-registry/" rel="nofollow" target="_blank" style="box-sizing: border-box; background-color: transparent; color: #005fa9; text-decoration: none;">http://tonybai.com/2016/02/26/deploy-a-private-docker-registry/</a></p><div style="box-sizing: border-box; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">安装部署一个私有的Docker Registry是引入、学习和使用&nbsp;<a href="https://github.com/docker/docker" rel="nofollow,noindex" target="_blank" style="box-sizing: border-box; background-color: transparent; color: #005fa9; text-decoration: none;">Docker</a>&nbsp;这门技术的必经之路之一。尤其是当Docker被所在组织接受，更多人、项目和产品开始接触和使用Docker时，存储和分发自制的Docker image便成了刚需。Docker Registry一如既往的继承了&#8220;Docker坑多&#8221;的特点，为此这里将自己搭建&#8221;各类&#8221;Registry过程中执行的步骤、遇到的问题记录下来，为己备忘，为他参考。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">Docker在2015年推出了&nbsp;<a href="https://github.com/docker/distribution" rel="nofollow,noindex" target="_blank" style="box-sizing: border-box; background-color: transparent; color: #005fa9; text-decoration: none;">distribution</a>&nbsp;项目，即Docker Registry 2。相比于&nbsp;<a href="https://github.com/docker/docker-registry" rel="nofollow,noindex" target="_blank" style="box-sizing: border-box; background-color: transparent; color: #005fa9; text-decoration: none;">old registry</a>&nbsp;，Registry 2使用Go实现，在安全性、性能方面均有大幅改进。Registry设计了全新的Rest API，并且在image存储格式等方面不再兼容于old Registry。去年8月份，docker官方hub使用Registriy 2.1替代了原先的old Registry。如果你要与Registry2交互，你的Docker版本至少要是Docker 1.6。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">Docker的开发者也一直在致力于改善Registry安装和使用的体验，通过提供&nbsp;<a href="https://hub.docker.com/_/registry/" rel="nofollow,noindex" target="_blank" style="box-sizing: border-box; background-color: transparent; color: #005fa9; text-decoration: none;">官方Registry Image</a>以及&nbsp;<a href="https://github.com/docker/compose" rel="nofollow,noindex" target="_blank" style="box-sizing: border-box; background-color: transparent; color: #005fa9; text-decoration: none;">Docker Compose工具</a>&nbsp;等来简化Registry的配置。不过在本文中，我们只是利用Docker以及Registry的官方Image来部署Registry，这样更便于全面了解Registry的部署配置细节。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">Registry2在镜像存储方面不仅支持本地盘，还支持诸多主流第三方存储方案。通过分布式存储系统你还可以实现一个分布式Docker Registry服务。这里仅以本地盘以及single node registry2为例。</p></div></div></h1><h3 id="articleHeader0" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 43.2px; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 1.5em;">一、环境</h3><h1><div style="display: inline-block;"><div style="box-sizing: border-box; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">这里还是复用以往文章中的Docker环境：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">Docker Registry Server: 10.10.105.71 Ubuntu 14.04 3.16.0-57-generic；docker 1.9.1

其他两个工作Server：
10.10.105.72 Ubuntu 14.04 3.19.0-25-generic; docker 1.9.1
10.10.126.101 Ubuntu 12.04 3.16.7-013607-generic; docker 1.9.1</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">本次Registry使用当前最新stable版本:Registry 2.3.0。由于镜像采用本地磁盘存储，root分区较小，需要映射使用其他volume。</p></div></div></h1><h3 id="articleHeader1" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 43.2px; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 1.5em;">二、初次搭建</h3><h1><div style="display: inline-block;"><div style="box-sizing: border-box; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">本以为Docker Registry的搭建是何其简单的，甚至简单到通过一行命令就可以完成的。比如我们在Registry Server上执行：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">在~/dockerregistry下，执行：

$sudo docker run -d -p 5000:5000 -v `pwd`/data:/var/lib/registry --restart=always --name registry registry:2
Unable to find image 'registry:2' locally
2: Pulling from library/registry
f32095d4ba8a: Pull complete
9b607719a62a: Pull complete
973de4038269: Pull complete
2867140211c1: Pull complete
8da16446f5ca: Pull complete
fd8c38b8b68d: Pull complete
136640b01f02: Pull complete
e039ba1c0008: Pull complete
c457c689c328: Pull complete
Digest: sha256:339d702cf9a4b0aa665269cc36255ee7ce424412d56bee9ad8a247afe8c49ef1
Status: Downloaded newer image for registry:2
e9088ef901cb00546c59f89defa4625230f4b36b0a44b3713f38ab3d2a5a2b44

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
registry            2                   c457c689c328        9 days ago          165.7 MB

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
e9088ef901cb        registry:2          "/bin/registry /etc/d"   About a minute ago   Up About a minute   0.0.0.0:5000-&gt;5000/tcp   registry</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">Registry container已经跑起来了，其启动日志可以通过：docker logs registry查看。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">我们在71本地给busybox:latest打一个tag，并尝试将新tag下的image push到Registry中去：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker tag busybox:latest 10.10.105.71:5000/tonybai/busybox:latest
$ docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
registry                            2                   c457c689c328        9 days ago          165.7 MB
busybox                             latest              65e4158d9625        9 days ago          1.114 MB
10.10.105.71:5000/tonybai/busybox   latest              65e4158d9625        9 days ago          1.114 MB
... ...</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">push到Registry中：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker push 10.10.105.71:5000/tonybai/busybox
The push refers to a repository [10.10.105.71:5000/tonybai/busybox] (len: 1)
unable to ping registry endpoint https://10.10.105.71:5000/v0/
v2 ping attempt failed with error: Get https://10.10.105.71:5000/v2/: Tunnel or SSL Forbidden
 v1 ping attempt failed with error: Get https://10.10.105.71:5000/v1/_ping: Tunnel or SSL Forbidden</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">出错了！简单分析了一下，可能是71上docker daemon配置中加了http代理的缘故，导致无法ping通registry endpoint。于是在/etc/default/docker中注释掉export http_proxy=&#8221;xxx&#8221;的设置，并重启docker daemon。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">再次尝试push：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker push 10.10.105.71:5000/tonybai/busybox
The push refers to a repository [10.10.105.71:5000/tonybai/busybox] (len: 1)
unable to ping registry endpoint https://10.10.105.71:5000/v0/
v2 ping attempt failed with error: Get https://10.10.105.71:5000/v2/: tls: oversized record received with length 20527
 v1 ping attempt failed with error: Get https://10.10.105.71:5000/v1/_ping: tls: oversized record received with length 20527</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">虽然还是失败，但错误信息已有所不同了。这次看来连接是可以建立的，但client端通过https访问server端，似乎想tls通信，但这一过程并未完成。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">在其他机器上尝试push image到registry也遇到了同样的错误输出，如下：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">10.10.105.72:

$ docker push 10.10.105.71:5000/tonybai/ubuntu
The push refers to a repository [10.10.105.71:5000/tonybai/ubuntu] (len: 1)
unable to ping registry endpoint https://10.10.105.71:5000/v0/
v2 ping attempt failed with error: Get https://10.10.105.71:5000/v2/: tls: oversized record received with length 20527
 v1 ping attempt failed with error: Get https://10.10.105.71:5000/v1/_ping: tls: oversized record received with length 20527</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">从错误信息来看，client与Registry交互，默认将采用https访问，但我们在install Registry时并未配置指定任何tls相关的key和crt文件，https访问定然失败。要想弄清这个问题，只能查看&nbsp;<a href="https://github.com/docker/distribution/blob/master/docs/deploying.md" rel="nofollow,noindex" target="_blank" style="box-sizing: border-box; background-color: transparent; color: #005fa9; text-decoration: none;">Registry Manual</a>&nbsp;。</p></div></div></h1><h3 id="articleHeader2" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 43.2px; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 1.5em;">三、Insecure Registry</h3><h1><div style="display: inline-block;"><div style="box-sizing: border-box; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">Registry的文档还是相对详尽的。在文档中，我们找到了&nbsp;<a href="https://github.com/docker/distribution/blob/master/docs/insecure.md" rel="nofollow,noindex" target="_blank" style="box-sizing: border-box; background-color: transparent; color: #005fa9; text-decoration: none;">Insecure Registry</a>&nbsp;，即接收plain http访问的Registry的配置和使用方法，虽然这不是官方推荐的。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">实际上对于我们内部网络而言，Insecure Registry基本能满足需求，部署过程也避免了secure registry的那些繁琐步骤，比如制作和部署证书等。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">为了搭建一个Insecure Registry，我们需要先清理一下上面已经启动的Registry容器。</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker stop registry
registry
$ docker rm registry
registry</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">修改Registry server上的Docker daemon的配置，为DOCKER_OPTS增加&#8211;insecure-registry：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">DOCKER_OPTS="--insecure-registry 10.10.105.71:5000 ....</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">重启Docker Daemon，启动Registry容器：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ sudo service docker restart
docker stop/waiting
docker start/running, process 6712
$ sudo docker run -d -p 5000:5000 -v `pwd`/data:/var/lib/registry --restart=always --name registry registry:2
5966e92fce9c34705050e19368d19574e021a272ede1575385ef35ecf5cea019</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">尝试再次Push image:</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker push 10.10.105.71:5000/tonybai/busybox
The push refers to a repository [10.10.105.71:5000/tonybai/busybox] (len: 1)
65e4158d9625: Pushed
5506dda26018: Pushed
latest: digest: sha256:800f2d4558acd67f52262fbe170c9fc2e67efaa6f230a74b41b555e6fcca2892 size: 2739</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">这回push ok！</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">我们将本地的tag做untag处理，再从Registry pull相关image：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
registry                            2                   c457c689c328        9 days ago          165.7 MB
10.10.105.71:5000/tonybai/busybox   latest              65e4158d9625        9 days ago          1.114 MB
busybox                             latest              65e4158d9625        9 days ago          1.114 MB
ubuntu                              14.04               6cc0fc2a5ee3        5 weeks ago         187.9 MB

$ docker rmi 10.10.105.71:5000/tonybai/busybox
Untagged: 10.10.105.71:5000/tonybai/busybox:latest

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
registry            2                   c457c689c328        9 days ago          165.7 MB
busybox             latest              65e4158d9625        9 days ago          1.114 MB
ubuntu              14.04               6cc0fc2a5ee3        5 weeks ago         187.9 MB

$ docker pull 10.10.105.71:5000/tonybai/busybox
Using default tag: latest
latest: Pulling from tonybai/busybox
Digest: sha256:800f2d4558acd67f52262fbe170c9fc2e67efaa6f230a74b41b555e6fcca2892
Status: Downloaded newer image for 10.10.105.71:5000/tonybai/busybox:latest

$ docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
registry                            2                   c457c689c328        9 days ago          165.7 MB
10.10.105.71:5000/tonybai/busybox   latest              65e4158d9625        9 days ago          1.114 MB
busybox                             latest              65e4158d9625        9 days ago          1.114 MB
ubuntu                              14.04               6cc0fc2a5ee3        5 weeks ago         187.9 MB</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">可以看到：Pull过程也很顺利。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">在Private Registry2中查看或检索Repository或images，&nbsp;<a href="https://github.com/docker/distribution/blob/master/ROADMAP.md#indexing-search-and-discovery" rel="nofollow,noindex" target="_blank" style="box-sizing: border-box; background-color: transparent; color: #005fa9; text-decoration: none;">将不能用docker search</a>&nbsp;：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker search 10.10.105.71:5000/tonybai/busybox/
Error response from daemon: Unexpected status code 404</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">但通过v2版本的API，我们可以实现相同目的：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$curl  http://10.10.105.71:5000/v2/_catalog
{"repositories":["tonybai/busybox"]}

$ curl  http://10.10.105.71:5000/v2/tonybai/busybox/tags/list
{"name":"tonybai/busybox","tags":["latest"]}</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">在其他主机上，我们尝试pull busybox：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">10.10.105.72:

$docker pull 10.10.105.71:5000/tonybai/busybox
Using default tag: latest
Error response from daemon: unable to ping registry endpoint https://10.10.105.71:5000/v0/
v2 ping attempt failed with error: Get https://10.10.105.71:5000/v2/: tls: oversized record received with length 20527
 v1 ping attempt failed with error: Get https://10.10.105.71:5000/v1/_ping: tls: oversized record received with length 20527</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">我们发现依旧不能pull和push！在Registry手册中讲到，如果采用insecure registry的模式，那么所有与Registry交互的主机上的Docker Daemon都要配置：&#8211;insecure-registry选项。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">我们按照上面的配置方法，修改105.72上的/etc/default/docker，重启Docker daemon，再执行pull/push就会得到正确的结果：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ sudo vi /etc/default/docker
$ sudo service docker restart
docker stop/waiting
docker start/running, process 10614
$ docker pull 10.10.105.71:5000/tonybai/busybox
Using default tag: latest
latest: Pulling from tonybai/busybox
5506dda26018: Pull complete
65e4158d9625: Pull complete
Digest: sha256:800f2d4558acd67f52262fbe170c9fc2e67efaa6f230a74b41b555e6fcca2892
Status: Downloaded newer image for 10.10.105.71:5000/tonybai/busybox:latest

$ docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu                              14.04               36248ae4a9ac        8 days ago          187.9 MB
10.10.105.71:5000/tonybai/ubuntu    14.04               36248ae4a9ac        8 days ago          187.9 MB
10.10.105.71:5000/tonybai/busybox   latest              65e4158d9625        9 days ago          1.114 MB

$ docker push 10.10.105.71:5000/tonybai/ubuntu
The push refers to a repository [10.10.105.71:5000/tonybai/ubuntu] (len: 1)
36248ae4a9ac: Pushed
8ea5373bf5a6: Pushed
2e0188208e83: Pushed
e3c70beaa378: Pushed
14.04: digest: sha256:72e56686cb9fb38438f0fd68fecf02ef592ce2ef7069bbf97802d959d568c5cc size: 6781</pre></div></div></h1><h3 id="articleHeader3" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 43.2px; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 1.5em;">四、Secure Registry</h3><h1><div style="display: inline-block;"><div style="box-sizing: border-box; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">Docker官方是推荐你采用Secure Registry的工作模式的，即transport采用tls。这样我们就需要为Registry配置tls所需的key和crt文件了。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">我们首先清理一下环境，将上面的Insecure Registry停掉并rm掉；将各台主机上Docker Daemon的DOCKER_OPTS配置中的&#8211;insecure-registry去掉，并重启Docker Daemon。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">如果你拥有一个域名，域名下主机提供Registry服务，并且你拥有某知名CA签署的证书文件，那么你可以建立起一个Secure Registry。不过我这里没有现成的证书，只能使用自签署的证书。严格来讲，使用自签署的证书在Docker官方眼中依旧属于Insecure，不过这里只是借助自签署的证书来说明一下Secure Registry的部署步骤罢了。</p></div></div></h1><h4 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin: 5px auto; font-size: 1.3em; padding: 0px;">1、制作自签署证书</h4><h1><div style="display: inline-block;"><div style="box-sizing: border-box; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">如果你有知名CA签署的证书，那么这步可直接忽略。</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ openssl req -newkey rsa:2048 -nodes -sha256 -keyout certs/domain.key -x509 -days 365 -out certs/domain.crt
Generating a 2048 bit RSA private key
..............+++
............................................+++
writing new private key to 'certs/domain.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Liaoning
Locality Name (eg, city) []:shenyang
Organization Name (eg, company) [Internet Widgits Pty Ltd]:foo
Organizational Unit Name (eg, section) []:bar
Common Name (e.g. server FQDN or YOUR name) []:mydockerhub.com
Email Address []:bigwhite.cn@gmail.com</pre></div></div></h1><h4 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin: 5px auto; font-size: 1.3em; padding: 0px;">2、启动Secure Registry</h4><h1><div style="display: inline-block;"><div style="box-sizing: border-box; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">启动带证书的Registry：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker run -d -p 5000:5000 --restart=always --name registry \
  -v `pwd`/data:/var/lib/registry \
  -v `pwd`/certs:/certs \
  -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \
  -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \
  registry:2
35e8ce77dd455f2bd50854e4581cd52be8a137f4aaea717239b6d676c5ea5777</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">由于证书的CN是mydockerhub.com，我们需要修改一下/etc/hosts文件:</p><pre style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">10.10.105.71 mydockerhub.com</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">重新为busybox制作一个tag:</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$docker tag busybox:latest mydockerhub.com:5000/tonybai/busybox:latest</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">Push到Registry:</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker push mydockerhub.com:5000/tonybai/busybox
The push refers to a repository [mydockerhub.com:5000/tonybai/busybox] (len: 1)
unable to ping registry endpoint https://mydockerhub.com:5000/v0/
v2 ping attempt failed with error: Get https://mydockerhub.com:5000/v2/: x509: certificate signed by unknown authority
 v1 ping attempt failed with error: Get https://mydockerhub.com:5000/v1/_ping: x509: certificate signed by unknown authority</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">push失败了！从错误日志来看，docker client认为server传输过来的证书的签署方是一个unknown authority（未知的CA），因此验证失败。我们需要让docker client安装我们的CA证书：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ sudo mkdir -p /etc/docker/certs.d/mydockerhub.com:5000
$ sudo cp certs/domain.crt /etc/docker/certs.d/mydockerhub.com:5000/ca.crt
$ sudo service docker restart //安装证书后，重启Docker Daemon</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">再执行Push，我们看到了成功的输出日志。由于data目录下之前已经被push了tonybai/busybox repository，因此提示&#8220;已存在&#8221;：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$docker push mydockerhub.com:5000/tonybai/busybox
The push refers to a repository [mydockerhub.com:5000/tonybai/busybox] (len: 1)
65e4158d9625: Image already exists
5506dda26018: Image already exists
latest: digest: sha256:800f2d4558acd67f52262fbe170c9fc2e67efaa6f230a74b41b555e6fcca2892 size: 2739</pre></div></div></h1><h4 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin: 5px auto; font-size: 1.3em; padding: 0px;">3、外部访问Registry</h4><h1><div style="display: inline-block;"><div style="box-sizing: border-box; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">我们换其他机器试试访问这个secure registry。根据之前的要求，我们照猫画虎的修改一下hosts文件，安装ca.cert，去除&#8211;insecure-registry选项，并重启Docker daemon。之后尝试从registry pull image：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker pull mydockerhub.com:5000/tonybai/busybox
Using default tag: latest
latest: Pulling from tonybai/busybox

Digest: sha256:800f2d4558acd67f52262fbe170c9fc2e67efaa6f230a74b41b555e6fcca2892
Status: Downloaded newer image for mydockerhub.com:5000/tonybai/busybox:latest

$ docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
10.10.105.71:5000/tonybai/ubuntu       14.04               36248ae4a9ac        9 days ago          187.9 MB
ubuntu                                 14.04               36248ae4a9ac        9 days ago          187.9 MB
10.10.105.71:5000/tonybai/busybox      latest              65e4158d9625        9 days ago          1.114 MB
mydockerhub.com:5000/tonybai/busybox   latest              65e4158d9625        9 days ago          1.114 MB</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">这样来看，如果使用自签署的证书，那么所有要与Registry交互的Docker主机都需要安装mydockerhub.com的ca.crt(domain.crt)。但如果你使用知名CA，这一步也就可以忽略。</p></div></div></h1><h3 id="articleHeader4" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 43.2px; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 1.5em;">五、Registry的鉴权管理</h3><h1><div style="display: inline-block;"><div style="box-sizing: border-box; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">Registry提供了一种基础的鉴权方式。我们通过下面步骤即可为Registry加上基础鉴权：</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">在Register server上，为Registry增加foo用户，密码foo123：（之前需要停掉已有的Registry，并删除之）</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">//生成鉴权密码文件
$ mkdir auth
$ docker run --entrypoint htpasswd registry:2 -Bbn foo foo123  &gt; auth/htpasswd
$ ls auth
htpasswd

//启动带鉴权功能的Registry：
$ docker run -d -p 5000:5000 --restart=always --name registry \
   -v `pwd`/auth:/auth \
   -e "REGISTRY_AUTH=htpasswd" \
   -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
   -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
   -v `pwd`/data:/var/lib/registry \
   -v `pwd`/certs:/certs \
   -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \
   -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \
   registry:2
199ad0b3591fb9613b21b1c96f017267f3c39661a7025d30df636c6805e7ab50</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">在105.72上，我们尝试push image到Registry：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker push mydockerhub.com:5000/tonybai/busybox
The push refers to a repository [mydockerhub.com:5000/tonybai/busybox] (len: 1)
65e4158d9625: Image push failed
Head https://mydockerhub.com:5000/v2/tonybai/busybox/blobs/sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4: no basic auth credentials</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">错误信息提示：鉴权失败。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">在72上执行docker login:</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$docker login mydockerhub.com:5000
Username: foo
Password:
Email: bigwhite.cn@gmail.com
WARNING: login credentials saved in /home/baiming/.docker/config.json
Login Succeeded</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">login成功后，再行Push：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ docker push mydockerhub.com:5000/tonybai/busybox
The push refers to a repository [mydockerhub.com:5000/tonybai/busybox] (len: 1)
65e4158d9625: Image already exists
5506dda26018: Image already exists
latest: digest: sha256:800f2d4558acd67f52262fbe170c9fc2e67efaa6f230a74b41b555e6fcca2892 size: 2739</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">Push ok！</p></div></div></h1><h3 id="articleHeader5" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 43.2px; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 1.5em;">六、Registry中images的管理</h3><h1><div style="display: inline-block;"><div style="box-sizing: border-box; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">前面提到过，通过V2版Rest API可以查询Repository和images：</p><pre class="brush:shell; toolbar: true; auto-links: false;" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13.6px; padding: 6px; margin-top: 0px; margin-bottom: 10px; line-height: 1.45; color: #333333; word-break: break-all; word-wrap: break-word; background-color: #f6f6f6; border: none; border-radius: 3px;">$ curl --cacert domain.crt  --basic --user foo:foo123 https://mydockerhub.com:5000/v2/_catalog
{"repositories":["tonybai/busybox","tonybai/ubuntu"]}</pre><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">但如果要删除Registry中的Repository或某个tag的Image，目前v2还不支持，原因见&nbsp;<a href="https://github.com/docker/distribution/blob/master/ROADMAP.md#deletes" rel="nofollow,noindex" target="_blank" style="box-sizing: border-box; background-color: transparent; color: #005fa9; text-decoration: none;">Registry的roadmap中的说明</a>&nbsp;。</p><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">不过如果你的Registry的存储引擎使用的是本地盘，倒是有一些第三方脚本可供使用，比如：<a href="https://github.com/burnettk/delete-docker-registry-image" rel="nofollow,noindex" target="_blank" style="box-sizing: border-box; background-color: transparent; color: #005fa9; text-decoration: none;">delete-docker-registry-image</a>&nbsp;。</p></div></div></h1><h3 id="articleHeader6" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 43.2px; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 1.5em;">七、小结</h3><h1><div style="box-sizing: border-box; line-height: 28.8px; color: #404040; font-family: &quot;Microsoft YaHei&quot;, Verdana, sans-serif, SimSun; font-size: 16px; font-weight: normal; background-color: #ffffff;"><p style="box-sizing: border-box; margin: 0px 0px 25px; line-height: 28.8px;">Registry2发布不到1年，目前还有许多问题待解决，就比如delete image的问题，相信在2.4以及后续版本这些问题会被逐个解决掉或能找到一个相对理想的方案。</p></div></h1><img src ="http://www.blogjava.net/xiaomage234/aggbug/431924.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2016-10-25 14:24 <a href="http://www.blogjava.net/xiaomage234/archive/2016/10/25/431924.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CentOS 7实战Kubernetes部署</title><link>http://www.blogjava.net/xiaomage234/archive/2016/10/25/431922.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Tue, 25 Oct 2016 06:23:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2016/10/25/431922.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/431922.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2016/10/25/431922.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/431922.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/431922.html</trackback:ping><description><![CDATA[from:http://www.infoq.com/cn/articles/centos7-practical-kubernetes-deployment<br /><br /><h2>. 前言</h2><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">上一节我们阐述了Kubernetes的系统架构，让大家对Kubernetes有一定的初步了解，但是就如何使用Kubernetes， 也许大家还不知如何下手。本文作者将带领大家如何在本地部署、配置Kubernetes集群网络环境以及通过实例演示跨机器服务间的通信，主要包括如下内容：</p><ul style="margin: 0px 0px 15px 10px; padding: 0px; list-style-position: initial; list-style-image: initial; border: 0px; clear: left; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;"><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">部署环境介绍</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">Kubernetes集群逻辑架构</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">部署Open vSwitch、Kubernetes、Etcd组件</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">演示Kubernetes管理容器</li></ul><h2>2. 部署环境</h2><ul style="margin: 0px 0px 15px 10px; padding: 0px; list-style-position: initial; list-style-image: initial; border: 0px; clear: left; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;"><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">VMware Workstation：10.0.3</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">VMware Workstation网络模式：NAT</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">操作系统信息：CentOS 7 64位</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">Open vSwitch版本信息：2.3.0</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">Kubernetes版本信息：0.5.2</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">Etcd版本信息：0.4.6</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;">Docker版本信息：1.3.1</li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">服务器信息:</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 573.297px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">        | Role      | Hostname   | IP Address  | 	|:---------:|:----------:|:----------: | 	|APIServer  |kubernetes  |192.168.230.3| 	|Minion     | minion1    |192.168.230.4| 	|Minion     | minion2    |192.168.230.5|</pre></li></ul><h2>3. Kubernetes集群逻辑架构</h2><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">在详细介绍部署Kubernetes集群前，先给大家展示下集群的逻辑架构。从下图可知，整个系统分为两部分，第一部分是Kubernetes APIServer，是整个系统的核心，承担集群中所有容器的管理工作；第二部分是minion，运行Container Daemon，是所有容器栖息之地，同时在minion上运行Open vSwitch程序，通过GRE Tunnel负责minion之间Pod的网络通信工作。</p><div style="margin: 0px; border: 0px; height: 0px; clear: both; font-size: 0px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;"></div><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161011-0321_4/resource/articles/centos7-practical-kubernetes-deployment/zh/resources/1126000.png" width="550" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;"  alt="" /></p><h2>4. 部署Open vSwitch、Kubernetes、Etcd组件</h2><h3>4.1 安装Open vSwitch及配置GRE</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">为了解决跨minion之间Pod的通信问题，我们在每个minion上安装Open vSwtich，并使用GRE或者VxLAN使得跨机器之间Pod能相互通信，本文使用GRE，而VxLAN通常用在需要隔离的大规模网络中。对于Open vSwitch的具体安装步骤，可参考这篇<a href="https://n40lab.wordpress.com/2014/09/04/openvswitch-2-3-0-lts-and-centos-7/" style="text-decoration: none; color: #286ab2; outline: none !important; margin: 0px; border: 0px; padding: 0px;">博客</a>，我们在这里就不再详细介绍安装步骤了。安装完Open vSwitch后，接下来便建立minion1和minion2之间的隧道。首先在minion1和minion2上建立OVS Bridge,</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@minion1 ~]# ovs-vsctl add-br obr0 </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">接下来建立gre，并将新建的gre0添加到obr0，在minion1上执行如下命令，</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@minion1 ~]# ovs-vsctl add-port obr0 gre0 -- set Interface gre0 type=gre options:remote_ip=192.168.230.5 </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">在minion2上执行,</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@minion2 ~]# ovs-vsctl add-port obr0 gre0 -- set Interface gre0 type=gre options:remote_ip=192.168.230.4 </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">至此，minion1和minion2之间的隧道已经建立。然后我们在minion1和minion2上创建Linux网桥kbr0替代Docker默认的docker0（我们假设minion1和minion2都已安装Docker），设置minion1的kbr0的地址为172.17.1.1/24， minion2的kbr0的地址为172.17.2.1/24，并添加obr0为kbr0的接口，以下命令在minion1和minion2上执行。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@minion1 ~]# brctl addbr kbr0               //创建linux bridge [root@minion1 ~]# brctl addif kbr0 obr0          //添加obr0为kbr0的接口 [root@minion1 ~]# ip link set dev docker0 down   //设置docker0为down状态 [root@minion1 ~]# ip link del dev docker0        //删除docker0 </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">为了使新建的kbr0在每次系统重启后任然有效，我们在/etc/sysconfig/network-scripts/目录下新建minion1的ifcfg-kbr0如下：</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">DEVICE=kbr0 ONBOOT=yes BOOTPROTO=static IPADDR=172.17.1.1 NETMASK=255.255.255.0 GATEWAY=172.17.1.0 USERCTL=no TYPE=Bridge IPV6INIT=no </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">同样在minion2上新建ifcfg-kbr0，只需修改ipaddr为172.17.2.1和gateway为172.17.2.0即可，然后执行systemctl restart network重启系统网络服务，你能在minion1和minion2上发现kbr0都设置了相应的IP地址。为了验证我们创建的隧道是否能通信，我们在minion1和minion2上相互ping对方kbr0的IP地址，从下面的结果发现是不通的，经查找这是因为在minion1和minion2上缺少访问172.17.1.1和172.17.2.1的路由，因此我们需要添加路由保证彼此之间能通信。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@minion1 network-scripts]# ping 172.17.2.1 PING 172.17.2.1 (172.17.2.1) 56(84) bytes of data. ^C --- 172.17.2.1 ping statistics --- 2 packets transmitted, 0 received, 100% packet loss, time 1000ms  [root@minion2 ~]#  ping 172.17.1.1 PING 172.17.1.1 (172.17.1.1) 56(84) bytes of data. ^C --- 172.17.1.1 ping statistics --- 2 packets transmitted, 0 received, 100% packet loss, time 1000ms </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">由于通过ip route add添加的路由会在下次系统重启后失效，为此我们在/etc/sysconfig/network-scripts目录下新建一个文件route-eth0存储路由，这里需要注意的是route-<span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">eth0</span>和ifcfg-<span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">eth0</span>的黑体部分必须保持一致，否则不能工作，这样添加的路由在下次重启后不会失效。为了保证两台minion的kbr0能相互通信，我们在minion1的route-eth0里添加路由172.17.2.0/24 via 192.168.230.5 dev eno16777736，eno16777736是minion1的网卡，同样在minion2的route-eth0里添加路由172.17.1.0/24 via 192.168.230.4 dev eno16777736。重启网络服务后再次验证，彼此kbr0的地址可以ping通，如：</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@minion2 network-scripts]# ping 172.17.1.1 PING 172.17.1.1 (172.17.1.1) 56(84) bytes of data. 64 bytes from 172.17.1.1: icmp_seq=1 ttl=64 time=2.49 ms 64 bytes from 172.17.1.1: icmp_seq=2 ttl=64 time=0.512 ms ^C --- 172.17.1.1 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1002ms rtt min/avg/max/mdev = 0.512/1.505/2.498/0.993 ms </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">到现在我们已经建立了两minion之间的隧道，而且能正确的工作。下面我们将介绍如何安装Kubernetes APIServer及kubelet、proxy等服务。</p><h3>4.2 安装Kubernetes APIServer</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">在安装APIServer之前，我们先下载Kubernetes及Etcd，做一些准备工作。在kubernetes上的具体操作如下：</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes ~]# mkdir /tmp/kubernetes [root@kubernetes ~]# cd /tmp/kubernetes/ [root@kubernetes kubernetes]# wget https://github.com/GoogleCloudPlatform/kubernetes/releases/download/v0.5.2/kubernetes.tar.gz [root@kubernetes kubernetes]# wget https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">然后解压下载的kubernetes和etcd包，并在kubernetes、minion1、minion2上创建目录/opt/kubernetes/bin，</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes kubernetes]# mkdir -p /opt/kubernetes/bin [root@kubernetes kubernetes]# tar xf kubernetes.tar.gz [root@kubernetes kubernetes]# tar xf etcd-v0.4.6-linux-amd64.tar.gz [root@kubernetes kubernetes]# cd ~/kubernetes/server [root@kubernetes server]# tar xf kubernetes-server-linux-amd64.tar.gz [root@kubernetes kubernetes]# /tmp/kubernetes/kubernetes/server/kubernetes/server/bin </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">复制kube-apiserver，kube-controller-manager，kube-scheduler，kubecfg到kubernetes的/opt/kubernetes/bin目录下，而kubelet，kube-proxy则复制到minion1和minion2的/opt/kubernetes/bin，并确保都是可执行的。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes amd64]# cp kube-apiserver kube-controller-manager kubecfg kube-scheduler /opt/kubernetes/bin [root@kubernetes amd64]# scp kube-proxy kubelet root@192.168.230.4:/opt/kubernetes/bin [root@kubernetes amd64]# scp kube-proxy kubelet root@192.168.230.5:/opt/kubernetes/bin </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">为了简单我们只部署一台etcd服务器，如果需要部署etcd的集群，请参考<a href="https://github.com/coreos/etcd/tree/master/Documentation" style="text-decoration: none; color: #286ab2; outline: none !important; margin: 0px; border: 0px; padding: 0px;">官方文档</a>，在本文中将其跟Kubernetes APIServer部署同一台机器上，而且将etcd放置在/opt/kubernetes/bin下，etcdctl跟ectd同一目录。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes kubernetes]# cd /tmp/kubernetes/etcd-v0.4.6-linux-amd64 [root@kubernetes etcd-v0.4.6-linux-amd64]# cp etcd etcdctl /opt/kubernetes/bin </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">需注意的是kubernetes和minion上/opt/kubernetes/bin目录下的文件都必须是可执行的。到目前，我们准备工作已经差不多，现在开始给apiserver，controller-manager，scheduler，etcd配置unit文件。首先我们用如下脚本etcd.sh配置etcd的unit文件，</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">#!/bin/sh  ETCD_PEER_ADDR=192.168.230.3:7001 ETCD_ADDR=192.168.230.3:4001 ETCD_DATA_DIR=/var/lib/etcd ETCD_NAME=kubernetes  ! test -d $ETCD_DATA_DIR &amp;&amp; mkdir -p $ETCD_DATA_DIR cat &lt;&lt;EOF &gt;/usr/lib/systemd/system/etcd.service [Unit] Description=Etcd Server  [Service] ExecStart=/opt/kubernetes/bin/etcd \\     -peer-addr=$ETCD_PEER_ADDR \\     -addr=$ETCD_ADDR \\     -data-dir=$ETCD_DATA_DIR \\     -name=$ETCD_NAME \\     -bind-addr=0.0.0.0  [Install] WantedBy=multi-user.target EOF  systemctl daemon-reload systemctl enable etcd systemctl start etcd </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">对剩下的apiserver,controller-manager,scheduler的unit文件配置的脚本，可以在github 上<a href="https://github.com/yangzhares/GetStartingKubernetes" style="text-decoration: none; color: #286ab2; outline: none !important; margin: 0px; border: 0px; padding: 0px;">GetStartingKubernetes</a>找到，在此就不一一列举。运行相应的脚本后，在APIServer上etcd, apiserver, controller-manager, scheduler服务就能正常运行。</p><h3>4.3 安装Kubernetes Kubelet及Proxy</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">根据Kubernetes的设计架构，需要在minion上部署docker, kubelet, kube-proxy，在<span style="font-weight: 600; margin: 0px; border: 0px; padding: 0px;">4.2</span>节部署APIServer时，我们已经将kubelet和kube-proxy已经分发到两minion上，所以只需配置docker,kubelet,proxy的unit文件，然后启动服务就即可，具体配置见<a href="https://github.com/yangzhares/GetStartingKubernetes" style="text-decoration: none; color: #286ab2; outline: none !important; margin: 0px; border: 0px; padding: 0px;">GetStartingKubernetes</a>。</p><h2>5. 演示Kubernetes管理容器</h2><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">为了方便，我们使用Kubernetes提供的例子<a href="https://github.com/GoogleCloudPlatform/kubernetes/tree/master/examples/guestbook" style="text-decoration: none; color: #286ab2; outline: none !important; margin: 0px; border: 0px; padding: 0px;">Guestbook</a>来演示Kubernetes管理跨机器运行的容器，下面我们根据Guestbook的步骤创建容器及服务。在下面的过程中如果是第一次操作，可能会有一定的等待时间，状态处于pending，这是因为第一次下载images需要一段时间。</p><h3>5.1 创建redis-master Pod和redis-master服务</h3><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes ~]# cd /tmp/kubernetes/kubernetes/examples/guestbook [root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 -c redis-master.json create pods [root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 -c redis-master-service.json create services </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">完成上面的操作后，我们可以看到如下redis-master Pod被调度到192.168.230.4。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 list pods Name                                   Image(s)                   Host                Labels                                       Status ----------                             ----------                 ----------          ----------                                   ---------- redis-master                           dockerfile/redis           192.168.230.4/      name=redis-master                            Running </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">但除了发现redis-master的服务之外，还有两个Kubernetes系统默认的服务kubernetes-ro和kubernetes。而且我们可以看到每个服务都有一个服务IP及相应的端口，对于服务IP，是一个虚拟地址，根据apiserver的portal_net选项设置的CIDR表示的IP地址段来选取，在我们的集群中设置为10.10.10.0/24。为此每新创建一个服务，apiserver都会在这个地址段中随机选择一个IP作为该服务的IP地址，而端口是事先确定的。对redis-master服务，其服务地址为10.10.10.206，端口为6379。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 list services Name                Labels              Selector                                  IP                  Port ----------          ----------          ----------                                ----------          ---------- kubernetes-ro                           component=apiserver,provider=kubernetes   10.10.10.207        80 redis-master        name=redis-master   name=redis-master                         10.10.10.206        6379 kubernetes                              component=apiserver,provider=kubernetes   10.10.10.161        443 </pre><h3>5.2 创建redis-slave Pod和redis-slave服务</h3><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 -c redis-slave-controller.json create replicationControllers [root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 -c redis-slave-service.json create services </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">然后通过list命令可知新建的redis-slave Pod根据调度算法调度到两台minion上，服务IP为10.10.10.92，端口为6379</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 list pods Name                                   Image(s)                   Host                Labels                                       Status ----------                             ----------                 ----------          ----------                                   ---------- redis-master                           dockerfile/redis           192.168.230.4/      name=redis-master                            Running 8c0ddbda-728c-11e4-8233-000c297db206   brendanburns/redis-slave   192.168.230.5/      name=redisslave,uses=redis-master            Running 8c0e1430-728c-11e4-8233-000c297db206   brendanburns/redis-slave   192.168.230.4/      name=redisslave,uses=redis-master            Running  [root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 list services Name                Labels              Selector                                  IP                  Port ----------          ----------          ----------                                ----------          ---------- redisslave          name=redisslave     name=redisslave                           10.10.10.92         6379 kubernetes                              component=apiserver,provider=kubernetes   10.10.10.161        443 kubernetes-ro                           component=apiserver,provider=kubernetes   10.10.10.207        80 redis-master        name=redis-master   name=redis-master                         10.10.10.206        6379 </pre><h3>5.3 创建Frontend Pod和Frontend服务</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">在创建之前修改frontend-controller.json的Replicas数量为2，这是因为我们的集群中只有2台minion，如果按照frontend-controller.json的Replicas默认值3，那会导致有2个Pod会调度到同一台minion上，产生端口冲突，有一个Pod会一直处于pending状态，不能被调度。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 -c frontend-controller.json create replicationControllers [root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 -c frontend-service.json create services </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">通过查看可知Frontend Pod也被调度到两台minion，服务IP为10.10.10.220，端口是80。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 list pods Name                                   Image(s)                   Host                Labels                                       Status ----------                             ----------                 ----------          ----------                                   ---------- redis-master                           dockerfile/redis           192.168.230.4/      name=redis-master                            Running 8c0ddbda-728c-11e4-8233-000c297db206   brendanburns/redis-slave   192.168.230.5/      name=redisslave,uses=redis-master            Running 8c0e1430-728c-11e4-8233-000c297db206   brendanburns/redis-slave   192.168.230.4/      name=redisslave,uses=redis-master            Running a880b119-7295-11e4-8233-000c297db206   brendanburns/php-redis     192.168.230.4/      name=frontend,uses=redisslave,redis-master   Running a881674d-7295-11e4-8233-000c297db206   brendanburns/php-redis     192.168.230.5/      name=frontend,uses=redisslave,redis-master   Running  [root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 list services Name                Labels              Selector                                  IP                  Port ----------          ----------          ----------                                ----------          ---------- kubernetes-ro                           component=apiserver,provider=kubernetes   10.10.10.207        80 redis-master        name=redis-master   name=redis-master                         10.10.10.206        6379 redisslave          name=redisslave     name=redisslave                           10.10.10.92         6379 frontend            name=frontend       name=frontend                             10.10.10.220        80 kubernetes                              component=apiserver,provider=kubernetes   10.10.10.161        443 </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">除此之外，你可以删除Pod、Service及更新ReplicationController的Replicas数量等操作，如删除Frontend服务：</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 delete services/frontend Status ---------- Success </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">还可以更新ReplicationController的Replicas的数量，下面是更新Replicas之前ReplicationController的信息。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 list replicationControllers Name                   Image(s)                   Selector            Replicas ----------             ----------                 ----------          ---------- redisSlaveController   brendanburns/redis-slave   name=redisslave     2 frontendController     brendanburns/php-redis     name=frontend       2 </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">现在我们想把frontendController的Replicas更新为1，则这行如下命令，然后再通过上面的命令查看frontendController信息，发现Replicas已变为1。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 resize frontendController 1  [root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 list replicationControllers Name                   Image(s)                   Selector            Replicas ----------             ----------                 ----------          ---------- redisSlaveController   brendanburns/redis-slave   name=redisslave     2 frontendController     brendanburns/php-redis     name=frontend       1 </pre><h3>5.4 演示跨机器服务通信</h3><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">完成上面的操作后，我们来看当前Kubernetes集群中运行着的Pod信息。</p><pre style="margin-top: 10px; margin-bottom: 10px; padding: 10px 10px 10px 5px; border: 1px solid #e8e8e8; font-family: Consolas, Monaco, &quot;Andale Mono&quot;, monospace; background-color: #f5f2f0; color: #314e64; line-height: 21px; width: 597.797px; overflow: auto; clear: none; background-position: -29px 0px; float: none !important;">[root@kubernetes guestbook]# kubecfg -h http://192.168.230.3:8080 list pods Name                                   Image(s)                   Host                Labels                                       Status ----------                             ----------                 ----------          ----------                                   ---------- a881674d-7295-11e4-8233-000c297db206   brendanburns/php-redis     192.168.230.5/      name=frontend,uses=redisslave,redis-master   Running redis-master                           dockerfile/redis           192.168.230.4/      name=redis-master                            Running 8c0ddbda-728c-11e4-8233-000c297db206   brendanburns/redis-slave   192.168.230.5/      name=redisslave,uses=redis-master            Running 8c0e1430-728c-11e4-8233-000c297db206   brendanburns/redis-slave   192.168.230.4/      name=redisslave,uses=redis-master            Running </pre><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">通过上面的结果可知当前提供前端服务的PHP和提供数据存储的后端服务Redis master的Pod分别运行在192.168.230.5和192.168.230.4上，即容器运行在不同主机上，还有Redis slave也运行在两台不同的主机上，它会从Redis master同步前端写入Redis master的数据。下面我们从两方面验证Kubernetes能提供跨机器间容器的通信：</p><ul style="margin: 0px 0px 15px 10px; padding: 0px; list-style-position: initial; list-style-image: initial; border: 0px; clear: left; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;"><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">在浏览器打开http://${IP<span style="margin: 0px; border: 0px; padding: 0px; line-height: 21px;">Address}:8000，IP</span>Address为PHP容器运行的minion的IP地址，其暴漏的端口为8000，这里IP_Address为192.168.230.5。打开浏览器会显示如下信息：</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161011-0321_4/resource/articles/centos7-practical-kubernetes-deployment/zh/resources/1126001.png" width="500" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;"  alt="" /></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">你可以输入信息并提交，如"Hello Kubernetes"、"Container"，然后Submit按钮下方会显示你输入的信息。</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161011-0321_4/resource/articles/centos7-practical-kubernetes-deployment/zh/resources/1126002.png" width="500" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;"  alt="" /></p>由于前端PHP容器和后端Redis master容器分别在两台minion上，因此PHP在访问Redis master服务时一定得跨机器通信，可见Kubernetes的实现方式避免了用link只能在同一主机上实现容器间通信的缺陷，对于Kubernetes跨机器通信的实现方法，以后我会详细介绍。<p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">&nbsp;</p></li><li style="margin: 0px 0px 0px 15px; padding: 0px; border: 0px; float: none; clear: none;"><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">从上面的结果，可得知已经实现了跨机器的通信，现在我们从后端数据层验证不同机器容器间的通信。根据上面的输出结果发现Redis slave和Redis master分别调度到两台不同的minion上，在192.168.230.4主机上执行docker exec -ti c41711cc8971 /bin/sh，c41711cc8971是Redis master的容器ID，进入容器后通过redis-cli命令查看从浏览器输入的信息如下：</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161011-0321_4/resource/articles/centos7-practical-kubernetes-deployment/zh/resources/1126003.png" width="400" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;"  alt="" /></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">如果我们在192.168.230.5上运行的Redis slave容器里查到跟Redis master容器里相同的信息，那说明Redis master和Redis slave之间的数据同步正常工作，下面是从192.168.230.5上运行的Redis slave容器查询到的信息：</p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;"><img _href="img://null" _p="true" src="http://cdn4.infoqstatic.com/statics_s2_20161011-0321_4/resource/articles/centos7-practical-kubernetes-deployment/zh/resources/1126004.png" width="400" style="border: 0px; margin: 0px 10px 10px 0px; padding: 0px;"  alt="" /></p><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 585px;">由此可见Redis master和Redis slave之间数据同步正常，OVS GRE隧道技术使得跨机器间容器正常通信。</p></li></ul><h2>6. 结论</h2><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">本文主要介绍如何在本地环境部署Kubernetes集群和演示如何通过Kubernetes管理集群中运行的容器，并通过OVS管理集群不同minion的Pod之间的网络通信。接下来会对Kubernetes各个组件源码进行详细分析，阐述Kubernetes的工作原理。</p><h2>7. 个人简介</h2><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">杨章显，现就职于Cisco，主要从事WebEx SaaS服务运维，系统性能分析等工作。特别关注云计算，自动化运维，部署等技术，尤其是Go、OpenvSwitch、Docker及其生态圈技术，如Kubernetes、Flocker等Docker相关开源项目。Email: yangzhangxian@gmail.com</p><h2>8. 参考资料</h2><ol style="margin: 10px 0px 10px 10px; padding: 0px 0px 0px 20px; border: 0px; width: 549px; clear: left; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;"><li style="margin: 4px 0px; padding: 0px 0px 0px 10px; border: none; float: none; clear: none;">https://n40lab.wordpress.com/2014/09/04/openvswitch-2-3-0-lts-and-centos-7/</li><li style="margin: 4px 0px; padding: 0px 0px 0px 10px; border: none; float: none; clear: none;">https://github.com/GoogleCloudPlatform/kubernetes/tree/master/examples/guestbook</li></ol><hr style="margin: 0px; border: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;" /><p style="margin: 0px 0px 15px; padding: 0px; border: 0px; float: none; line-height: 1.8; clear: none; width: 610px; font-family: &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, Helvetica, sans-serif; background-color: #ffffff;">感谢<a href="http://www.infoq.com/cn/author/%E9%83%AD%E8%95%BE" style="text-decoration: none; color: #286ab2; outline: none !important; margin: 0px; border: 0px; padding: 0px;">郭蕾</a>对本文的策划和审校。</p><img src ="http://www.blogjava.net/xiaomage234/aggbug/431922.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2016-10-25 14:23 <a href="http://www.blogjava.net/xiaomage234/archive/2016/10/25/431922.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> Kubernetes使用问题总结</title><link>http://www.blogjava.net/xiaomage234/archive/2016/10/25/431923.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Tue, 25 Oct 2016 06:23:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2016/10/25/431923.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/431923.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2016/10/25/431923.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/431923.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/431923.html</trackback:ping><description><![CDATA[from:http://blog.csdn.net/linuxgo/article/details/52121125<br /><br /><h2>加快Kubernetes编译速度</h2><p style="margin: 0px; padding: 0px; color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;"></p><p style="margin: 0px; padding: 0px; color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">除了<a href="http://lib.csdn.net/base/linux" title="Linux知识库" target="_blank" style="text-decoration: none; color: #df3434; font-weight: bold;">Linux</a>/amd64，默认还会为其他平台做交叉编译。为了减少编译时间，可以修改hack/lib/golang.sh，把KUBE_SERVER_PLATFORMS， KUBE_CLIENT_PLATFORMS和KUBE_TEST_PLATFORMS中除linux/amd64以外的其他平台注释掉</p><h2><a name="t1" style="color: rgb(12, 137, 207);"></a>gcr.io无法访问</h2><p style="margin: 0px; padding: 0px; color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">Kubernetes在创建Pod的时候，需要从gcr.io下载一个helper镜像（目前是 gcr.io/google_containers/pause-amd64:3.0 ）。</p><p style="margin: 0px; padding: 0px; color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">但是目前国内无法访问gcr.io，这个问题会导致无法下载该镜像，然后Pod一直处于ContainerCreating状态。</p><h3><a name="t2" style="color: rgb(12, 137, 207);"></a>解决办法</h3><span style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">1) 在可以访问gcr.io的地方</span><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><span style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">docker pull gcr.io/google_containers/pause-amd64:3.0</span><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><span style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">传到私有docker registry</span><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><span style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">docker tag gcr.io/google_containers/pause-amd64:3.0 k8s-docker.mydomain.com/google_containers/pause-amd64:3.0</span><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><span style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">docker push k8s-docker.mydomain.com/google_containers/pause-amd64:3.0</span><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><span style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">2) 在所有的k8s节点</span><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><span style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">docker pull k8s-docker.mydomain.com/google_containers/pause-amd64:3.0</span><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><span style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">docker tag k8s-docker.mydomain.com/google_containers/pause-amd64:3.0 gcr.io/google_containers/pause-amd64:3.0</span><br style="color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;" /><h3><a name="t3" style="color: rgb(12, 137, 207);"></a>Note</h3><p style="margin: 0px; padding: 0px; color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">不通过私有registry中转，而是使用<a href="http://lib.csdn.net/base/docker" title="Docker知识库" target="_blank" style="text-decoration: none; color: #df3434; font-weight: bold;">Docker</a>&nbsp;save/load应该也可以，只是要把save导出的文件复制到所有节点.</p><h2><a name="t4" style="color: rgb(12, 137, 207);"></a>如何从集群外访问Service和Pod</h2><p style="margin: 0px; padding: 0px; color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">这里说的集群外是指K8s集群以外的主机，比如使用nginx/HAProxy搭建的负载均衡主机。这些主机跟K8s集群部署在一起，到K8s网络可达。</p><p style="margin: 0px; padding: 0px; color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">对于不是部署在GCE以及AWS等云平台的K8s，我们一般需要自己搭建负载均衡，然后分发请求到到Service。</p><p style="margin: 0px; padding: 0px; color: #555555; font-family: &quot;microsoft yahei&quot;; font-size: 15px; background-color: #ffffff;">使用NodePort方式发布服务，那么负载均衡主机上不需要额外配置；使用ClusterIP方式，为了能够访问Service的ClusterIP， 需要在这些主机上安装Flanneld和kube-proxy</p><img src ="http://www.blogjava.net/xiaomage234/aggbug/431923.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2016-10-25 14:23 <a href="http://www.blogjava.net/xiaomage234/archive/2016/10/25/431923.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>