2

我有一个简单的 kubernetes 集群,有一个 master 和 3 个 minions。在这种情况下,如果我运行一个简单的 nginx 或 mysql pod,它可以正常工作,但是,如果我将 KIND 类型更改为 yaml 文件并尝试运行复制服务,pod 将启动但我无法访问服务。

这是我的 nginx 的 yaml 文件,有 3 个副本:

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

这是服务 yaml 配置文件:

apiVersion: v1
kind: Service
metadata: 
  labels: 
    name: nginx
  name: nginx
spec: 
  ports:
    - port: 80
  selector: 
    name: nginx

我运行它:

# kubectl create -f nginx-rc.yaml
# kubectl create -f nginx-rc-service.yaml

如果我运行:

# kubectl get pod,svc,rc -o wide

我明白:

NAME          READY     STATUS    RESTARTS   AGE       NODE
nginx-kgq1s   1/1       Running   0          1m        node01
nginx-pomx3   1/1       Running   0          1m        node02
nginx-xi54i   1/1       Running   0          1m        node03
NAME         LABELS                                    SELECTOR     IP(S)                           PORT(S)
kubernetes   component=apiserver,provider=kubernetes   <none>       10.254.0.1      443/TCP
nginx        name=nginx                                name=nginx   10.254.47.150   80/TCP
CONTROLLER   CONTAINER(S)   IMAGE(S)   SELECTOR    REPLICAS
nginx        nginx          nginx      app=nginx   3

我可以看到 pod 的描述:

Name:                           nginx-kgq1s
Namespace:                      default
Image(s):                       nginx
Node:                           node01/node01
Labels:                         app=nginx
Status:                         Running
Reason:
Message:
IP:                             172.17.52.3
Replication Controllers:        nginx (3/3 replicas created)
Containers:
  nginx:
    Image:              nginx
    State:              Running
      Started:          Thu, 11 Feb 2016 16:28:08 +0100
    Ready:              True
    Restart Count:      0
Conditions:
  Type          Status
  Ready         True 
Events:
  FirstSeen                             LastSeen                        Count   From                            SubobjectPath                           Reason          Message
  Thu, 11 Feb 2016 16:27:47 +0100       Thu, 11 Feb 2016 16:27:47 +0100 1       {scheduler }                                                            scheduled       Successfully assigned nginx-kgq1s to node01
  Thu, 11 Feb 2016 16:27:57 +0100       Thu, 11 Feb 2016 16:27:57 +0100 1       {kubelet node01}        implicitly required container POD       pulled          Pod container image "gcr.io/google_containers/pause:0.8.0" already present on machine
  Thu, 11 Feb 2016 16:28:02 +0100       Thu, 11 Feb 2016 16:28:02 +0100 1       {kubelet node01}        implicitly required container POD       created         Created with docker id bed30a90c6eb
  Thu, 11 Feb 2016 16:28:02 +0100       Thu, 11 Feb 2016 16:28:02 +0100 1       {kubelet node01}        implicitly required container POD       started         Started with docker id bed30a90c6eb
  Thu, 11 Feb 2016 16:28:07 +0100       Thu, 11 Feb 2016 16:28:07 +0100 1       {kubelet node01}        spec.containers{nginx}                  created         Created with docker id 0a5c69cd0481
  Thu, 11 Feb 2016 16:28:08 +0100       Thu, 11 Feb 2016 16:28:08 +0100 1       {kubelet node01}        spec.containers{nginx}                  started         Started with docker id 0a5c69cd0481

如果我得到 rc 的描述,这就是我看到的:

Name:           nginx
Namespace:      default
Image(s):       nginx
Selector:       app=nginx
Labels:         app=nginx
Replicas:       3 current / 3 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Events:
  FirstSeen                             LastSeen                        Count   From                            SubobjectPath   Reason                  Message
  Thu, 11 Feb 2016 16:27:47 +0100       Thu, 11 Feb 2016 16:27:47 +0100 1       {replication-controller }                       successfulCreate        Created pod: nginx-kgq1s
  Thu, 11 Feb 2016 16:27:47 +0100       Thu, 11 Feb 2016 16:27:47 +0100 1       {replication-controller }                       successfulCreate        Created pod: nginx-pomx3
  Thu, 11 Feb 2016 16:27:47 +0100       Thu, 11 Feb 2016 16:27:47 +0100 1       {replication-controller }                       successfulCreate        Created pod: nginx-xi54i

如果我得到服务描述,这就是我看到的:

Name:                   nginx
Namespace:              default
Labels:                 name=nginx
Selector:               name=nginx
Type:                   ClusterIP
IP:                     10.254.47.150
Port:                   <unnamed>       80/TCP
Endpoints:              <none>
Session Affinity:       None
No events.

如我所见,问题可能是我没有端点,但我不知道如何解决。

4

2 回答 2

5

在我看来,您服务的选择器是错误的。它正在寻找 的标签name: nginx,但您的 pod 实际上有app: nginx.

尝试将您的服务文件更改为:

apiVersion: v1
kind: Service
metadata: 
  labels: 
    name: nginx
  name: nginx
spec: 
  ports:
    - port: 80
  selector: 
    app: nginx

...或更改您的复制控制器模板以使用name: nginx而不是app: nginx作为标签。基本上,标签必须匹配,以便服务知道如何在您的 pod 上呈现统一的外观。

于 2016-02-11T16:09:42.397 回答
0

为了建立@jonskeet 的答案,标签必须匹配的原因是因为 Pod 可以在您的 k8s 集群中的任何节点上运行,并且服务需要一种定位它们的方法。

因此,你在 Pod 前面拍的服务需要能够过滤集群,特别是它所在命名空间中的一组 Pod,并且它利用两个选择器中的这些匹配 k/v 作为它的这样做的方法。

于 2016-02-11T22:06:30.407 回答