Skip to content

K8s - Service - NodePort

2022-02-07 00:00:00

Create pod & service

kubectl apply -f pod-definition
kubectl apply -f service-definition.yml

Get IP of node

kubectl get pods -o wide
IP of node is under INTERNAL-IP for example (in my case) 192.168.49.2

Get Port

kubectl get service -o wide
Port of service is under PORT(S) for example (in my case) 80:30008/TCP

URL

Go to 192.168.49.2:30008 you should be able to see Welcome to nginx!

pod-definition.yml

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end
spec:
  containers:
  - name: nginx-container
    image: nginx

service-definition.yml

apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  type: NodePort
  ports:
   - targetPort: 80
     port: 80
     nodePort: 30008
  selector:
    app: myapp
    type: front-end

minikube

minikube service myapp-service --urls