Kubernetes:从 aconfig 获取端口?
Kubernetes: get port from aconfig?
有什么方法可以从 ConfigMap
为服务(和其他地方)注入端口值吗?试过这个:
apiVersion: v1
kind: Service
metadata:
name: service
namespace: namespace
spec:
ports:
- port: 80
targetPort:
valueFrom:
configMapKeyRef:
name: config
key: PORT
protocol: TCP
selector:
app: service
但是出现错误
ValidationError(Service.spec.ports[0].targetPort): invalid type for io.k8s.apimachinery.pkg.util.intstr.IntOrString: got "map", expected "string"
好的,所以我更深入地检查了它,看起来你不能在你的 service.spec 中像这样对 ConfigMap 进行引用定义。 valueFrom
的这种用法只能用于容器环境变量,如 here.
中所述
另一方面,您可以在 deployment.spec(在这种情况下 service.spec.ports.targetPort)中指定 targetPort
名称,例如 mycustomport
并在 deployment.spec 和 service.spec 之间引用此 mycustomport
。
根据 Kubernetes API reference docs 的注释:
targetPort - Number or name of the port to access on the pods targeted by the service. Number must be in the range 1 to 65535. Name must be an IANA_SVC_NAME. If this is a string, it will be looked up as a named port in the target Pod's container ports. If this is not specified, the value of the 'port' field is used (an identity map). This field is ignored for services with clusterIP=None, and should be omitted or set equal to the 'port' field. More info: https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service
有什么方法可以从 ConfigMap
为服务(和其他地方)注入端口值吗?试过这个:
apiVersion: v1
kind: Service
metadata:
name: service
namespace: namespace
spec:
ports:
- port: 80
targetPort:
valueFrom:
configMapKeyRef:
name: config
key: PORT
protocol: TCP
selector:
app: service
但是出现错误
ValidationError(Service.spec.ports[0].targetPort): invalid type for io.k8s.apimachinery.pkg.util.intstr.IntOrString: got "map", expected "string"
好的,所以我更深入地检查了它,看起来你不能在你的 service.spec 中像这样对 ConfigMap 进行引用定义。 valueFrom
的这种用法只能用于容器环境变量,如 here.
另一方面,您可以在 deployment.spec(在这种情况下 service.spec.ports.targetPort)中指定 targetPort
名称,例如 mycustomport
并在 deployment.spec 和 service.spec 之间引用此 mycustomport
。
根据 Kubernetes API reference docs 的注释:
targetPort - Number or name of the port to access on the pods targeted by the service. Number must be in the range 1 to 65535. Name must be an IANA_SVC_NAME. If this is a string, it will be looked up as a named port in the target Pod's container ports. If this is not specified, the value of the 'port' field is used (an identity map). This field is ignored for services with clusterIP=None, and should be omitted or set equal to the 'port' field. More info: https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service