Reputation: 8944
I am studying services in k8s from here
I have created service without selector and with one endpoint. What I am trying to do is I have installed apache and it's running on port 80. I have created a node port service on port 31000. Now this service should redirect ip:31000 to ip:80 port.
It is doing for internal ip of service but not on external ip.
my-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- protocol: TCP
port: 9376
targetPort: 80
nodePort: 31000
type: NodePort
my-endpoint.yaml
apiVersion: v1
kind: Endpoints
metadata:
name: my-service
subsets:
- addresses:
- ip: <IP>
ports:
- port: 80
Output for kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 53m <none>
my-service NodePort 10.111.205.207 <none> 9376:31000/TCP 30m <none>
Upvotes: 3
Views: 1305
Reputation: 11445
Uses of services is to set up communication between all of different pods or to get access to a pod from the outside of cluster.
So every pod or every deployment you create is always going to have some kind of matching service along with it. There are several different kinds of services.
1. Cluster IP: Sets up an easy to remember URL to access a pod. Only exposes pods in the cluster. So cluster IP service used anytime that you want to set up communication between different pods inside of your cluster.
for example: event-bus-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: event-bus-depl
spec:
replicas: 1
selector:
matchLabels:
app: event-bus
template:
metadata:
labels:
app: event-bus
spec:
containers:
- name: event-bus
image: lordrafiq/event-bus:latest
---
apiVersion: v1
kind: Service
metadata:
name: event-bus-srv
spec:
selector:
app: event-bus
type: ClusterIP
ports:
- name: event-bus
protocol: TCP
port: 4005
targetPort: 4005
So from inside of any pod you want to communicate with event-bus, you can using event-bus-srv on port 4005.
await axios.post('http://event-bus-srv:4005/events', {});
2. Node Port: Makes a pod accessible from outside of the cluster (Usually only used for dev purposes). So node ports are used for anytime you want to access a pod from outside of your cluster, but it only used for development purposes, load balancer are the right way to access a pod from the cluster.
apiVersion: v1
kind: Service
metadata:
name: posts-srv
spec:
type: NodePort
selector:
app: posts
ports:
- name: posts
protocol: TCP
port: 4000
targetPort: 4000
http://192.168.39.234:31154/posts
31154 is the nodePort we got after creating the node port service.
3. Load Balancer: Makes a pod accessible from outside of the cluster. This is the right way to expose a pod to the outside world.
4. External Name: Redirects an in-cluster request to a CNAME url.
Upvotes: 2
Reputation: 984
First thing is, you need to run a pod inside your cluster then assign the ip of that pod inside the Endpoints yaml with port, because services exposes the pods to within or outside the cluster, we must use either selector or the address of the pod so that service can attach it self to particular pod.
apiVersion: v1
kind: Endpoints
metadata:
name: my-service
subsets:
- addresses:
- ip: <ip address of the pod>
ports:
- port: <port of the pod>
One more thing use Statefulset in place of Deployment to run pods.
Upvotes: 3
Reputation: 904
There are multiple types of services which can give you different levels of access:
ClusterIP - can access the service from another pod ( only inside the Kubernetes Cluster)
NodePort - can access the service from another pod, and from the machine that it is running the Kubernetes cluster
LoadBalancer - can access the service from outside the Kubernetes cluster ( using an external IP )
LoadBalancers are great when you have a TCP level connection. If you have a higher level connection ( http ) you can also use Ingress + NodePort.
Upvotes: 2