使用节点端口方法无法从集群外部访问从集群部署的kubernetes服务

时间:2019-12-23 14:45:25

标签: kubernetes

我正在尝试访问Kubernetes集群部署的Spring Boot微服务,并试图测试REST API。我在部署脚本中配置了节点端口方法。但是,当我尝试使用Postman工具进行访问时,只会得到“无法获得任何响应”的响应。

我按照以下结构配置了 service.yaml 脚本,

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  ports:
    - port: 7100
      targetPort: 7100
      protocol: TCP
      name: http
      nodePort: 31007
 selector:
      app: my-deployment

我的 deployment.yaml 如下,

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  labels:
    app: my-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-deployment
  template:
    metadata:
      labels:
        app: my-deployment
      annotations: 
        date: "+%H:%M:%S %d/%m/%y"
    spec:
      imagePullSecrets:
        - name: "regcred"
      containers:
       - name: my-deployment-container
         image: spacestudymilletech010/spacestudysecurityauthcontrol:latest
         imagePullPolicy: Always
         ports:
            - name: http
              containerPort: 8065
              protocol: TCP
      tolerations:
      - key: "dedicated-app"
        operator: "Equal"
        value: "my-dedi-app-a"
        effect: "NoSchedule"

我服用kubectl describe service时,输出如下:

enter image description here

并且我正尝试通过以下方式访问已部署的api,

  http://<my-cluster-Worker-NodeIP-Address:31007/<my-deployed-ReST-API-end-point>

更新

当我为自己的部署运行kubectl describe pod命令时,我得到如下响应,

docker@MILDEVKUB010:~$ kubectl describe pod spacestudycontrolalerts- 
deployment-8644449c58-x4zd6
Name:           spacestudycontrolalerts-deployment-8644449c58-x4zd6
Namespace:      default
Priority:       0
Node:           <none>
Labels:         app=spacestudycontrolalerts-deployment
            pod-template-hash=8644449c58
Annotations:    date: +%H:%M:%S %d/%m/%y
Status:         Pending
IP:
IPs:            <none>
Controlled By:  ReplicaSet/spacestudycontrolalerts-deployment-8644449c58
Containers:
  spacestudycontrolalerts-deployment-container:
    Image:        spacestudymilletech010/spacestudycontrolalerts:latest
    Port:         7102/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:
  /var/run/secrets/kubernetes.io/serviceaccount from default-token-6s55b (ro)
Conditions:
  Type           Status
  PodScheduled   False
Volumes:
  default-token-6s55b:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-6s55b
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
             node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason            Age        From               Message
  ----     ------            ----       ----               -------
  Warning  FailedScheduling  <unknown>  default-scheduler  0/2 nodes are available: 2 node(s) had taints that the pod didn't tolerate.

如上所述,我从0/2 nodes are available: 2 node(s) had taints that the pod didn't tolerate.之类的describe pod命令中获取了事件消息。

当我运行kubectl get nodes命令时,我得到如下信息,

NAME           STATUS   ROLES    AGE   VERSION
mildevkub020   Ready    master   5d    v1.17.0
mildevkub040   Ready    master   5d    v1.17.0

我在哪里出现了错误的服务访问权限?

3 个答案:

答案 0 :(得分:4)

如果有事件消息,即0/2 nodes are available: 2 node(s) had taints that the pod didn't tolerate。这意味着您的节点有一个污染

第1步:-要验证是否有污染 kubectl describe node | grep -i taint

第2步:-去除污渍,确认其已被去除。

  

请注意,密钥使用的末尾带有减号。

kubectl taint nodes --all node-role.kubernetes.io/master-

kubectl taint nodes --all node-role.kubernetes.io/not-ready-

kubectl taint nodes --all node-role.kubernetes.io/unreachable-

步骤3:-然后根据您的 deployment.yaml 文件,我们需要创建污染区。

kubectl taint nodes node1 dedicated-app:my-dedi-app-a:NoSchedule

步骤4:-验证是否有污染 kubectl describe node | grep -i taint

第5步:-部署.yaml文件 kubectl apply -f deployment.yaml

您可以在PodSpec中指定容器的公差。以下两个容差都“匹配”上面的kubectl污点线创建的污点,因此具有任一容差的Pod都可以将其调度到 node1

  

https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/

此外,您的describe pod显示您的部署名称为spacestudycontrolalerts-deployment。这使我们对您的 deployment.yaml文件(即 metadata.Name:我的部署)感到困惑。 Make sure you describe pod with respective deployment name

我希望这将对大家有所帮助,以备将来参考。

答案 1 :(得分:2)

快照未显示Endpoints。这意味着在服务或选择器后面没有Pod运行

selector:
      app: my-deployment

...在任何运行中的Pod中都不匹配这样的标签。

答案 2 :(得分:0)

首先,由于在deployment.yaml上定义的容忍度,pod无法调度。yaml与可用节点上应用的污点不匹配。

Events:
  Type     Reason            Age        From               Message
  ----     ------            ----       ----               -------
  Warning  FailedScheduling  <unknown>  default-scheduler  0/2 nodes are available: 2 node(s) had taints that the pod didn't tolerate.

第二次来自“问题中的登录”语句,在service.yaml上定义的选择器与所描述的pod上的标签不匹配,将成为映射到该服务的端点的问题。

service.yaml上的选择器字段

selector:
      app: my-deployment

describe命令中的Pod标签

docker@MILDEVKUB010:~$ kubectl describe pod spacestudycontrolalerts- 
deployment-8644449c58-x4zd6


Labels:         app=spacestudycontrolalerts-deployment
            pod-template-hash=8644449c58