为什么我的kubernetes服务没有针对Pod

时间:2019-12-02 18:35:03

标签: kubernetes

我有一个kubernetes服务,我想用它来定位Pod。

这是我定义服务的方式:

apiVersion: v1
kind: Service
metadata:
    name: my-service
spec:
    selector:
            app: myapp
    ports:
        - port: 77
          targetPort: 80
          nodePort: 32766
    type: NodePort

这就是我定义部署的方式:

apiVersion: apps/v1
kind: Deployment
metadata:
    name: my-deployment
spec:
    replicas: 5
    selector:
        matchLabels:
            app: myapp
    template:
        metadata:
            name: my-pod
            labels:
                app: myapp
        spec:
            containers:
                - name: httd
                  image: httpd
                  imagePullPolicy: Always
                  ports:
                      - containerPort: 80

基本上,我所做的是将Pod中的端口80链接到服务中的端口77,然后将其链接到节点中的端口32766。

我已经知道我的容器正在端口80上运行,因为如果这样做的话:

docker run -p 8989:80 httpd

并请求localhost:8989我可以看到页面。

如果我做kubetctl获得服务,我将得到:

NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        46h
my-service   NodePort    10.100.157.161   <none>        77:32766/TCP   20m

我尝试致电:

10.100.157.161:32766
10.100.157.161:77

但是两者都会导致连接错误。

我想念什么?

1 个答案:

答案 0 :(得分:1)

使用以下YAML。有效

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: myapp
  name: my-deployment
spec:
  replicas: 5
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - image: httpd
        name: httpd
        imagePullPolicy: Always
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: myapp
  ports:
  - port: 77
    targetPort: 80
    nodePort: 32766
  type: NodePort
master $ kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        15m
my-service   NodePort    10.110.219.232   <none>        77:32766/TCP   12s
master $
master $
master $ curl 10.110.219.232:77
<html><body><h1>It works!</h1></body></html>
master $
master $
master $ curl $(hostname -i):32766
<html><body><h1>It works!</h1></body></html>
master $