In Kubernetes (k8s), Expose pod as NodePort

In Kubernetes (k8s), a Pod is the smallest deployable unit that represents a single instance of a running process in a cluster. A NodePort is a type of Kubernetes service that exposes a port on each node of the cluster and forwards traffic to a service.

When you create a Kubernetes service with type NodePort, Kubernetes automatically assigns a random port between 30000 and 32767 to the service. You can also specify a particular port number within that range to be used.

To create a service with type NodePort, you can use the kubectl expose command with the --type=NodePort flag, like this:

kubectl expose pod my-pod --type=NodePort --name=my-service

This command creates a service named my-service that exposes the pod named my-pod on a randomly assigned port.

Alternatively, you can create a service manifest file with the following content:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 8080
    nodePort: 30080
  selector:
    app: my-app

In this example, the service exposes port 80 on each node in the cluster, and forwards traffic to port 8080 on pods with the label app=my-app. The nodePort field specifies that the service should be exposed on port 30080 on each node.

Once the service is created, you can access it using the IP address of any node in the cluster and the nodePort you specified (or the random port assigned by Kubernetes if you didn't specify one). For example, if the IP address of one of your nodes is 10.0.0.1 and the nodePort is 30080, you could access the service using the URL http://10.0.0.1:30080.

댓글

이 블로그의 인기 게시물

Using the MinIO API via curl

How to split a list into chunks of 100 items in JavaScript, 자바스크립트 리스트 쪼개기

HTML Inline divisions at one row by Tailwind

Boilerplate for typescript server programing

가속도 & 속도

Gradle multi-module project

How to checkout branch of remote git, 깃 리모트 브랜치 체크아웃

CDPEvents in puppeteer

Sparse encoder

Reactjs datetime range picker