admin-task-docs-pr-2017-09-08

This commit is contained in:
zhangqx2010
2017-09-08 18:03:12 +08:00
parent 15b57839b8
commit dce409f734
7 changed files with 1202 additions and 0 deletions
@@ -0,0 +1,185 @@
---
title: 同 Pod 内的容器使用共享卷通信
redirect_from:
- "/docs/user-guide/pods/multi-container/"
- "/docs/user-guide/pods/multi-container.html"
- "docs/tasks/configure-pod-container/communicate-containers-same-pod/"
- "docs/tasks/configure-pod-container/communicate-containers-same-pod.html"
---
{% capture overview %}
本文旨在说明如何使用一个卷(Volume)让一个 Pod 内的两个容器进行通信
{% endcapture %}
{% capture prerequisites %}
{% include task-tutorial-prereqs.md %}
{% endcapture %}
{% capture steps %}
## 创建一个两个容器的 Pod
在这个练习中,你会创建一个包含两个容器的 Pod。两个容器共享一个卷用于他们之间的通信。
Pod 的配置文件如下:
{% include code.html language="yaml" file="two-container-pod.yaml" ghlink="/docs/tasks/access-application-cluster/two-container-pod.yaml" %}
在配置文件中,你可以看到 Pod 有一个共享卷,名为 `shared-data`
配置文件中的第一个容器运行了一个 nginx 服务器。共享卷的挂载路径是 `/usr/share/nginx/html`
第二个容器是基于 debian 镜像的,有一个挂载路径是 `/pod-data`。第二个容器运行了下面的命令然后终止。
echo Hello from the debian container > /pod-data/index.html
注意,第二个容器在 nginx 服务器的根目录下写了 `index.html` 文件。
创建一个包含两个容器的 Pod
kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/two-container-pod.yaml
查看 Pod 和容器的信息:
kubectl get pod two-containers --output=yaml
这是输出的一部分:
apiVersion: v1
kind: Pod
metadata:
...
name: two-containers
namespace: default
...
spec:
...
containerStatuses:
- containerID: docker://c1d8abd1 ...
image: debian
...
lastState:
terminated:
...
name: debian-container
...
- containerID: docker://96c1ff2c5bb ...
image: nginx
...
name: nginx-container
...
state:
running:
...
你可以看到 debian 容器已经被终止了,而 nginx 服务器依然在运行。
进入 nginx 容器的 shell
kubectl exec -it two-containers -c nginx-container -- /bin/bash
在 shell 中,确认 nginx 还在运行。
root@two-containers:/# ps aux
输出类似于这样:
USER PID ... STAT START TIME COMMAND
root 1 ... Ss 21:12 0:00 nginx: master process nginx -g daemon off;
回忆一下,debian 容器在 nginx 的根目录下创建了 `index.html` 文件。
使用 `curl` 向 nginx 服务器发送一个 GET 请求:
root@two-containers:/# apt-get update
root@two-containers:/# apt-get install curl
root@two-containers:/# curl localhost
输出显示了 nginx 提供了 debian 容器写的页面:
Hello from the debian container
{% endcapture %}
{% capture discussion %}
## 讨论
Pod 能有多个容器的主要原因是为了支持协助主应用的辅助应用。辅助应用的典型例子是数据抽取,数据推送和代理。
辅助应用和主应用经常需要相互通信。通常这是通过共享文件系统完成的,就如这个练习所示,或者通过回环网络接口
localhost 完成。网络接口的例子是 web 服务器带有一个协助程序用于拉取 Git 仓库的更新。
在本练习中的卷为 Pod 生命周期中的容器相互通信提供了一种方法。如果 Pod 被删除或者重建了,
任何共享卷中的数据都会丢失。
{% endcapture %}
{% capture whatsnext %}
* 更多学习内容
[混合容器的方式](http://blog.kubernetes.io/2015/06/the-distributed-system-toolkit-patterns.html)。
* 学习[模块化架构的混合容器](http://www.slideshare.net/Docker/slideshare-burns)。
* 参见[配置一个使用存储卷的 Pod](/docs/tasks/configure-pod-container/configure-volume-storage/)。
* 参见 [](/docs/api-reference/v1.6/#volume-v1-core)。
* 参见 [Pod](/docs/api-reference/v1.6/#pod-v1-core).
{% endcapture %}
{% include templates/task.md %}
@@ -0,0 +1,126 @@
---
approvers:
- bprashanth
- davidopp
title: 配置你的云平台防火墙
---
许多云服务商(比如 Google Compute Engine)定义防火墙以防止服务无意间暴露到 internet 上。
当暴露服务给外网时,你可能需要在防火墙上开启一个或者更多的端口来支持服务。
本文描述了这个过程,以及其他云服务商的具体信息。
## 负载均衡(LoadBalancer)服务的访问限制
当以 `spec.type: LoadBalancer` 使用服务时,你可以使用 `spec.loadBalancerSourceRanges` 指定允许访问负载均衡的 IP 段。
这个字段采用 CIDR 的 IP 段,Kubernetes 会使用这个段配置防火墙。支持这个功能的平台目前有 Google Compute EngineGoogle Container Engine 和 AWS。
如果云服务商不支持这个功能,这个字段会被忽略。
假设 10.0.0.0/8 是内部的子网。在下面这个例子中,会创建一个只有集群内部 ip 可以访问的负载均衡器。
集群外部的客户端是无法访问这个负载均衡器的。
```yaml
apiVersion: v1
kind: Service
metadata:
name: myapp
spec:
ports:
- port: 8765
targetPort: 9376
selector:
app: example
type: LoadBalancer
loadBalancerSourceRanges:
- 10.0.0.0/8
```
这个例子中,会创建一个只能被 IP 为 130.211.204.1 和 130.211.204.2 的客户端访问的负载据衡器。
```yaml
apiVersion: v1
kind: Service
metadata:
name: myapp
spec:
ports:
- port: 8765
targetPort: 9376
selector:
app: example
type: LoadBalancer
loadBalancerSourceRanges:
- 130.211.204.1/32
- 130.211.204.2/32
```
### 谷歌计算引擎(Google Compute Engine
当以 `spec.type: LoadBalancer` 使用服务时,防火墙会被自动打开。
然而,当以 `spec.type: NodePort` 使用服务时,防火墙默认 *不会* 被打开。
Google Compute Engine 的防火墙文档在[别处](https://cloud.google.com/compute/docs/networking#firewalls_1)。
你可以使用 `gcloud` 命令行工具添加一个防火墙:
```shell
$ gcloud compute firewall-rules create my-rule --allow=tcp:<port>
```
**注意**
使用 Google Compute Engine 平台的防火墙时有一个重要的关于安全的注意点:
在 Kubernetes v1.0.0 版本,GCE 防火墙是定义按虚拟机(VM)来的,而不是按 ip 来的。
这就意味着当你在防火墙上打开一个服务端口时,任何在那台虚拟机 IP 上的同一端口的服务
都有被外部访问的潜在可能。注意,这对于其他 Kubernetes 服务来说不是问题,因为他们监
听的 IP 地址与主机节点的外部 IP 地址不同。
考虑一下:
* 你创建了一个服务,使用了外部服务均衡 (IP 地址为 1.2.3.4) 和 80 端口。
* 你在防火墙上为集群的所有节点都打开了 80 端口,所以外部的服务可以向你的
服务发送数据包。
* 你又在虚拟机(IP 为2.3.4.5)上使用 80 端口启动了一台 nginx 服务器.
这个 nginx 在虚拟机的外部 IP 地址上也被暴露到了 internet 上。
因此,在 Google Compute Engine 或者 Google Container Engine 上开启防火墙端口时请
小心。你可能无意间把其他服务也暴露给了 internet。
这个问题会在 Kubernetes 后续版本中被修复。
### 其他云服务商
即将更新
@@ -0,0 +1,223 @@
---
title: 使用 Service 把前端连接到后端
---
{% capture overview %}
这项任务会描述如何创建一个前端和一个后端的微服务。后端微服务是一个 hello 欢迎程序。
前端和后端的连接是通过 Kubernetes Service 对象完成的。
{% endcapture %}
{% capture objectives %}
* 使用 Deployment 对象创建并运行一个微服务
* 从后端将流量路由到前端
* 使用 Service 对象把前端应用连接到后端应用
{% endcapture %}
{% capture prerequisites %}
* {% include task-tutorial-prereqs.md %}
* 本项任务使用[外部负载均衡服务](/docs/tasks/access-application-cluster/create-external-load-balancer/)
所以需要对应的可支持此功能的环境。如果你的环境不能支持,你可以使用[NodePort](/docs/user-guide/services/#type-nodeport)
的类型 Service 来代替。
{% endcapture %}
{% capture lessoncontent %}
### 使用 Deployment 创建后端
后端是一个简单的 hello 欢迎微服务应用。这是后端应用的 Deployment 配置文件:
{% include code.html language="yaml" file="hello.yaml" ghlink="/docs/tasks/access-application-cluster/hello.yaml" %}
创建后端 Deployment
```
kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/hello.yaml
```
查看后端的 Deployment 信息:
```
kubectl describe deployment hello
```
输出类似于:
```
Name: hello
Namespace: default
CreationTimestamp: Mon, 24 Oct 2016 14:21:02 -0700
Labels: app=hello
tier=backend
track=stable
Selector: app=hello,tier=backend,track=stable
Replicas: 7 updated | 7 total | 7 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
OldReplicaSets: <none>
NewReplicaSet: hello-3621623197 (7/7 replicas created)
Events:
...
```
### 创建后端 Service 对象
前端连接到后端的关键是 Service。Service 创建一个固定 IP 和 DNS 解析名入口,
使得后端微服务可达。Service 使用 selector 标签来寻找目的地 Pods。
首先,浏览 Service 的配置文件:
{% include code.html language="yaml" file="hello-service.yaml" ghlink="/docs/tasks/access-application-cluster/hello-service.yaml" %}
配置文件中,你可以看到 Service 将流量路由到包含 `app: hello``tier: backend` 标签的 Pods。
创建 `hello` Service
```
kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/hello-service.yaml
```
此时,你已经有了一个在运行的后端 Deployment,你也有了一个 Service 用于路由网络流量。
### 创建前端应用
既然你已经有了后端应用,你可以创建一个前端应用连接到后端。前端应用通过 DNS 名连接到后端的工作 Pods。
DNS 名是 "hello",也就是 Service 配置文件中 `name` 字段的值。
前端 Deployment 中的 Pods 运行一个 nginx 镜像,这个已经配置好镜像去寻找后端的 hello Service。
只是 nginx 的配置文件:
{% include code.html file="frontend/frontend.conf" ghlink="/docs/tasks/access-application-cluster/frontend/frontend.conf" %}
与后端类似,前端用包含一个 Deployment 和一个 Service。Service 的配置文件包含了 `type: LoadBalancer`
也就是说,Service 会使用你的云服务商的默认负载均衡设备。
{% include code.html language="yaml" file="frontend.yaml" ghlink="/docs/tasks/access-application-cluster/frontend.yaml" %}
创建前端 Deployment 和 Service
```
kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/frontend.yaml
```
输出确认两个资源都已经被创建:
```
deployment "frontend" created
service "frontend" created
```
**注意**:这个 nginx 配置文件是被打包在[容器镜像](/docs/tasks/access-application-cluster/frontend/Dockerfile)里的。
更好的方法是使用 [ConfigMap](/docs/tasks/configure-pod-container/configmap/),这样的话你可以更轻易地更改配置。
### 与前端 Service 交互
一旦你创建了 LoadBalancer 类型的 Service,你可以使用这条命令查看外部 IP:
```
kubectl get service frontend
```
外部 IP 的字段的生成可能需要一些时间。如果是这种情况,外部 IP 会显示为 `<pending>`
```
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend 10.51.252.116 <pending> 80/TCP 10s
```
使用相同的命令直到它显示外部 IP 地址:
```
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend 10.51.252.116 XXX.XXX.XXX.XXX 80/TCP 1m
```
### 通过前端发送流量
前端和后端已经完成连接了。你可以使用 curl 命令通过你的前端 Service 的外部 IP 访问服务端点。
```
curl http://<EXTERNAL-IP>
```
后端生成的消息输出如下:
```
{"message":"Hello"}
```
{% endcapture %}
{% capture whatsnext %}
* 了解更多 [Services](/docs/concepts/services-networking/service/)
* 了解更多 [ConfigMaps](/docs/tasks/configure-pod-container/configmap/)
{% endcapture %}
{% include templates/tutorial.md %}