【问题标题】:Kubenetes accsess api webKubernetes 访问 api web
【发布时间】:2021-07-15 22:48:25
【问题描述】:

我是 Kub 的新手,我从 docker-compose 转换了我的环境, 我有一个包含 python 代码的 pod - 如果我在同一主机上使用我的 docker,我可以访问 但是当它在 pod 上时没有流量进入,

kubectl 配置视图

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://10.10.10.130:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

api-service.yaml:

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: /snap/kompose/19/kompose-linux-amd64 convert --volumes hostPath
    kompose.version: 1.21.0 (992df58d8)
  creationTimestamp: null
  labels:
    io.kompose.service: api
  name: api
spec:
  ports:
  - name: "5001"
    port: 5001
    targetPort: 5001
  selector:
    io.kompose.service: api
status:
  loadBalancer: {}

api-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: /snap/kompose/19/kompose-linux-amd64 convert --volumes hostPath
    kompose.version: 1.21.0 (992df58d8)
  creationTimestamp: null
  labels:
    io.kompose.service: api
  name: api
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: api
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: /snap/kompose/19/kompose-linux-amd64 convert --volumes hostPath
        kompose.version: 1.21.0 (992df58d8)
      creationTimestamp: null
      labels:
        io.kompose.service: api
    spec:
      containers:
      - image: 127.0.0.1:5000/api:latest
        imagePullPolicy: "Never"
        name: api
        ports:
        - containerPort: 5001
        resources: {}
        volumeMounts:
        - mountPath: /base
          name: api-hostpath0
      restartPolicy: Always
      serviceAccountName: ""
      volumes:
      - hostPath:
          path: /root/ansible/api/base
        name: api-hostpath0
status: {}

pod 日志:

 * Serving Flask app 'server' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://10.244.0.17:5001/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 553-272-086

我尝试达到配置视图显示的内容,我得到了这个: https://10.10.10.130:6443/

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {
    
  },
  "status": "Failure",
  "message": "forbidden: User \"system:anonymous\" cannot get path \"/\"",
  "reason": "Forbidden",
  "details": {
    
  },
  "code": 403
}

通过容器到达的路径是: https://10.10.10.130:5001/ 它没有到达容器并说站点不存在- 这同样适用于 docker 容器,所以我错过了什么?

谢谢

--编辑--

如果我从我进入的主机 curl http://10.244.0.17:5001/(api pod 的地址),为什么我不能从外面进入?

还尝试添加 nginx + api pod 部署

  template:
    spec:
      hostNetwork: true

还是联系不上请帮忙

【问题讨论】:

  • 如果你使用 kubectl port-forward api 5001:5001 在浏览器中检查 localhost:5001 并给出响应会发生什么
  • @rassakra 没有任何作用kubectl port-forward api-5cb96dd789-ftlwd 5001:5001 Forwarding from 127.0.0.1:5001 -> 5001 Forwarding from [::1]:5001 -> 5001
  • 是的,现在您的应用程序可以从 localhost:5001 访问
  • 但它不起作用 - 我在 hostip 10.10.10.130:5001 或 127.0.0.1:5001 上得到 This site can’t be reached,我需要它在我不明白的主机 ip 上工作,使用 docker 它可以工作我错过了什么? @rassakra
  • 您需要为您的服务指定 NodePort 类型。这样,它将在 Kubernetes 集群的所有主机上暴露在一个端口上,通常在 30k-32k 或类似的范围内

标签: kubernetes kubectl


【解决方案1】:

找到了解决方案! 我需要将 externalIPs 添加到我的 pod service.yaml(api 和 nginx)

spec:
  ports:
  - name: "8443"
    port: 8443
    targetPort: 80
  externalIPs:
      - 10.10.10.130

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-07
    • 2019-03-14
    • 2019-05-17
    • 2021-05-23
    • 2019-04-13
    • 1970-01-01
    • 2018-05-28
    • 2018-12-08
    相关资源
    最近更新 更多