问题
Problem Statement:
I have a created a Docker image successfully from docker.io/joethecoder2/spring-boot-web
. It has been tested with command line arguments, and those work correctly locally with Docker.
I am trying to pass java arguments that are passed to Docker to a Kubernetes POD that is defined with a single image docker.io/joethecoder2/spring-boot-web
The purpose of passing the arguments is to let the POD know what the IP address and port number are for the database service.
Problem Definition:
I have defined a Kubernetes POD here, however I believe the arguments are not passed correctly from singlePod.yaml when the POD is running the service.
Expected Result:
I expect that the Kubernetes POD be compatible with the Docker image defined here.
I expect that the Kubernetes POD accept arguments just like Docker does here:
docker run -it -p 8080:8080 joethecoder2/spring-boot-web -Dcassandra_ip=127.0.0.1 -Dcassandra_port=9042
curl -X POST --header 'Content-Type: application/json' --header 'Accept: text/plain' 'http://localhost:8080/restaurant/arguments'
Correct result is returned-> 127.0.0.1:9042
Wrong Result:
I know that the actual arguments are not passed to the POD, because when I run the following service, I receive no arguments returned.
curl -X POST --header 'Content-Type: application/json' --header 'Accept: text/plain' 'http://192.168.64.3:32308/restaurant/arguments'
Wrong result is returned-> :
Expected result -> 127.0.0.1:9042
, as defined in the singlePod.yaml
file
Why are the arguments missing, even though the POD definition file knows that the arguments have been defined staticly?
回答1:
If you want to inject environment variable into the container.It's better to use ConfigMap so it provides flexibility as well as separation of concern.
apiVersion: v1
kind: Pod
metadata:
name: spring-boot-web-demo
labels:
purpose: demonstrate-spring-boot-web
spec:
containers:
- name: spring-boot-web
image: docker.io/joethecoder2/spring-boot-web
command: ["java","-jar", "spring-boot-web-0.0.1-SNAPSHOT.jar"]
env:
- name: DCASSANDRA_IP
valueFrom:
configMapKeyRef:
key: Dcassandra_ip
name: env-values
- name: DCASSANDRA_PORT
valueFrom:
configMapKeyRef:
key: Dcassandra_port
name: env-values
restartPolicy: OnFailure
---
apiVersion: v1
kind: ConfigMap
metadata:
name: env-values
data:
Dcassandra_port=9042
Dcassandra_ip=127.0.0.1
Now You need to write a Service Manifest file to expose this container in order to access it. I have attached links for further research.
ConfigMap
Service
来源:https://stackoverflow.com/questions/47684382/kubernetes-pod-arguments-are-not-passing-to-service-however-docker-arguments-ar