Is there way to specify a custom NodePort port in a kubernetes service YAML definition? I need to be able to define the port explicitly in my configuration file.
Is there way to specify a custom NodePort port in a kubernetes service YAML definition? I need to be able to define the port explicitly in my configuration file.
You can define static NodePort using nodeport in service.yaml file
spec:
type: NodePort
ports:
- port: 3000
nodePort: 31001
name: http
Yeah you can define all those three port by your own
apiVersion: v1
kind: Service
metadata:
name: posts-srv
spec:
type: NodePort
selector:
app: posts
ports:
- name: posts
protocol: TCP
port: 4000
targetPort: 4000
nodePort: 31515
you can actually run this command to see how you can achieve that in yaml.
kubectl create service hello-svc --tcp=80:80 --type NodePort --node-port 30080 -o yaml --dry-run > hello-svc.yaml
https://pachehra.blogspot.com/2019/11/kubernetes-imperative-commands-with.html
For those who need to use kubectl commands without creating a yaml file, you can create a NodePort service with a specified port:
kubectl create nodeport NAME [--tcp=port:targetPort] [--dry-run=server|client|none]
For example:
kubectl create service nodeport myservice --node-port=31000 --tcp=3000:80
You can check Kubectl reference for more: