Kubernetes(Minikube):环境变量

时间:2018-06-05 19:25:54

标签: docker kubernetes microservices

我正在使用Minikube运行一个简单的弹簧微服务项目。我有两个项目:lucky-word-client(在端口8080上)和lucky-word-server(在端口8888上)。 lucky-word-client必须与lucky-word-server通信。我想在lucky-word-client的Kuberenetes部署脚本中注入lucky-word-server(http://192 *。 .100:32002)的静态Nodeport作为环境变量。我怎么办?

这是lucky-word-server的部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: lucky-server
spec:
  selector:
      matchLabels:
        app: lucky-server
  replicas: 1
  template:
    metadata:
      labels:
        app: lucky-server
    spec:
      containers:
        - name: lucky-server
          image: lucky-server-img
          imagePullPolicy: Never
          ports:
            - containerPort: 8888

这是lucky-word-server的服务:

kind: Service
apiVersion: v1
metadata:
  name: lucky-server
spec:
  selector:
    app: lucky-server
  ports:
  - protocol: TCP
    targetPort: 8888
    port: 80
    nodePort: 32002
  type: NodePort

这是lucky-word-client的部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: lucky-client
spec:
  selector:
      matchLabels:
        app: lucky-client
  replicas: 1
  template:
    metadata:
      labels:
        app: lucky-client
    spec:
      containers:
        - name: lucky-client
          image: lucky-client-img
          imagePullPolicy: Never
          ports:
            - containerPort: 8080

这是lucky-word-client的服务:

kind: Service
apiVersion: v1
metadata:
  name: lucky-client
spec:
  selector:
    app: lucky-client
  ports:
  - protocol: TCP
    targetPort: 8080
    port: 80
  type: NodePort

1 个答案:

答案 0 :(得分:1)

Kubernetes自动将服务注入环境变量。 https://kubernetes.io/docs/concepts/services-networking/service/#environment-variables

但是你不应该使用它。除非在创建pod时所有服务都已到位,否则这将不起作用。它受到“docker”的启发,现在也转向基于DNS的服务发现。因此,“基于环境的服务发现”已成为过去。

请依赖DNS服务发现。 Minikube附带kube-dns,因此您只需使用lucky-server主机名(或lucky-server[.default[.svc[.cluster[.local]]]]个名称之一)即可。阅读文档:https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/