如何在 kubernetes pod 中使用服务名称

how to use service name inside kubernetes pod

我想替换这两个值 (...*** = @IP) enter image description here

带有服务名称 enter image description here

有什么解决办法吗?

例如,假设您的服务规范如下所示:

apiVersion: v1
kind: Service
metadata:
  name: api
  namespace: einstore  # <-- you need to specify the namespace 
  labels:
    app: api
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 30001
      targetPort: 8080
      protocol: TCP
      name: api
  selector:
    app: api

与您的部署类似:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: api
  name: api
  namespace: einstore  # <-- you need to specify the namespace
spec:
  selector:
...

在集群内(例如 pod 到 pod),您可以将服务称为:

- name: APICORE_SERVER_URL
  value: "http://api.einstore:8080/

这是我的服务他叫api

apiVersion: v1
kind: Service
metadata:
  name: api
  labels:
    app: api
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 30001
      targetPort: 8080
      protocol: TCP
      name: api
  selector:
    app: api

这是我的部署

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: api
  name: api
spec:
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: einstore/einstore-core:0.1.1
          env:
            - name: APICORE_STORAGE_LOCAL_ROOT
              value: "/home/einstore"
            - name: APICORE_SERVER_NAME
              value: "Einstore - Enterprise AppStore"
            - name: APICORE_SERVER_MAX_UPLOAD_FILESIZE
              value: "50"
            - name: APICORE_DATABASE_HOST
              value: "postgres"
            - name: APICORE_DATABASE_USER
              value: "einstore"
            - name: APICORE_DATABASE_PASSWORD
              value: "einstore"
            - name: APICORE_DATABASE_DATABASE
              value: "einstore"
            - name: APICORE_DATABASE_PORT
              value: "5432"
            - name: APICORE_DATABASE_LOGGING
              value: "false"
            - name: APICORE_JWT_SECRET
              value: "secret"
            - name: APICORE_STORAGE_S3_ENABLED
              value: "false"
            - name: APICORE_STORAGE_S3_BUCKET
              value: "~"
            - name: APICORE_STORAGE_S3_ACCESS_KEY
              value: "~"
            - name: APICORE_STORAGE_S3_REGION
              value: "~"
            - name: APICORE_STORAGE_S3_SECRET_KEY
              value: "~"
            - name: APICORE_SERVER_URL
              value: "http://**.***.*.***:30001/"

当我尝试用我的机器替换 *** 时 @ip 一切正常,但我需要更改它以便有服务名称,以便我可以在任何其他机器上部署应用程序 第一个解决方案 LGTM 但我收到此错误

curl http://api.einstore:8080/
curl: (6) Could not resolve host: api.einstore

注意:einstore= 我的命名空间的名称