How to use NodePort with kind?
in the kind config yaml
containerPort must be the same with the 'NodePort' in service config. in your case is 30000
tips: hostPort can be any port ,it does not need to be the same with containerPort
Actually doing what Arghya Sadhu suggested worked. Not sure why the answer got deleted.
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30000
hostPort: 30000
listenAddress: "0.0.0.0"
protocol: tcp
- role: worker
Kind cluster configuration needs to be like below
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30000
hostPort: 30000
listenAddress: "0.0.0.0" # Optional, defaults to "0.0.0.0"
protocol: tcp # Optional, defaults to tcp
- role: worker
This file is then passed to your creation command as kind create cluster --config=config.yaml
(according to docs).
I will share my answer as i tried out this today by using multi node kind cluster and another important thing is that needs to take care about kubectl client and server versions. I believe this will help someone.
Step 1
kind-api-cluster.yml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30950
hostPort: 30950
listenAddress: "127.0.0.1"
protocol: TCP
- role: worker
- role: worker
Step 2 Kubernetes service and deployment file. ( If you note here, the kind's control-plane hostPort and kubernetes service's nodePort should be the same )
api.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
labels:
app: api
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: api
imagePullPolicy: Never
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: api-svc
spec:
selector:
app: api
type: NodePort
ports:
- name: http
nodePort: 30950
targetPort: 8080
port: 8080
Additional Notes:-
kubectl version
Client Version: version.Info{Major:"1", Minor:"20+", GitVersion:"v1.20.4-dirty", GitCommit:"e87da0bd6e03ec3fea7933c4b5263d151aafd07c", GitTreeState:"dirty", BuildDate:"2021-03-15T09:55:27Z", GoVersion:"go1.16.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-21T01:11:42Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
kind --version
kind version 0.10.0