Table of contents
What is Service?
In Kubernetes, Services are objects that provide stable network identities to Pods and abstract away the details of Pod IP addresses. Services allow Pods to receive traffic from other Pods, Services, and external clients.
Service type
For some parts of your application (for example, frontends) you may want to expose a Service onto an external IP address, one that's accessible from outside of your cluster.
Kubernetes Service types allow you to specify what kind of Service you want.
The available type
values and their behaviors are:
Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default that is used if you don't explicitly specify a type
for a Service. You can expose the Service to the public internet using an Ingress or a Gateway.
Exposes the Service on each Node's IP at a static port (the NodePort
). To make the node port available, Kubernetes sets up a cluster IP address, the same as if you had requested a Service of type: ClusterIP
.
Exposes the Service externally using an external load balancer. Kubernetes does not directly offer a load balancing component; you must provide one, or you can integrate your Kubernetes cluster with a cloud provider.
Task-01
Step 1: Create vim service.yml
apiVersion: v1
kind: Service
metadata:
name: react-django-app-service
namespace: my-django-app
spec:
type: NodePort
selector:
app: django-app
ports:
- port: 80
targetPort: 8000
nodePort: 30007
apiVersion: v1
This section defines the API version to use, in this case, v1.
kind: Service
This section specifies the type of resource you are creating, in this case, a Service.
metadata:
name: react-django-app-service
This section gives the Service a name, in this name: react-django-app-service
spec:
selector:
app: djnago-app
This section defines the selector for the pods that the Service should target. In this case, it will target pods with the app label set to todo.
type: NodePort
This section specifies the type of Service you want to create, in this case, NodePort.
ports:
- protocol: TCP
port: 8001
targetPort: 8000
nodePort: 30007
This section defines the port mapping for the Service. The Service will listen on port 8001 and forward traffic to the target port 8000 on the pods. Additionally, a nodePort is defined as 30007, which will make the Service accessible from outside the cluster by using the IP address of any node in the cluster and the nodePort defined in the Service.
Step 2: Use kubectl apply -f service.yml command.
Step 3: use kubectl get syc -n=my-django-app so u will see the node port and details of the pod.
Step 4: Go to the worker node and create a rule for 30007. Again come back to the worker-node security group and select public ip copy that. Open a new tab and paste the ip and:30007. You will able to see the app.
Verify that the Service is working by accessing the todo-app using the Service’s IP and Port in your Namespace.
The minikube service command is used to interact with services in a Minikube cluster. The --URL option will return the URL that you can use to access the Service in your browser.
minikube service <service_name> -n=<namespace> --url
Task-02
Step 1: Create vim service.yml
apiVersion: v1
kind: Service
metadata:
name: react-django-app-service
namespace: my-django-app
spec:
type: NodePort
selector:
app: django-app
ports:
- protocol: TCP
port: 8000
targetPort: 8000
type: ClusterIP
Step 2: Use kubectl apply -f service.yml command.
Step 3: Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.
- Get the IP address of the ClusterIP Service:
kubectl get services -n <namespace>
kubectl get services -n my-django-app
Note the name of another Pod in the same Namespace.
- Exec into the Pod: go inside container
kubectl exec -it <pod-name> -n <namespace> -- bash
Task-3
Create a LoadBalancer Service for accessing the todo-app from outside the cluster
Create a LoadBalancer Service definition for your django-app Deployment in a YAML file.
apiVersion: v1
kind: Service
metadata:
name: react-django-app-service
namespace: my-django-app
spec:
type: NodePort
selector:
app: django-app
ports:
- protocol: TCP
port: 8000
targetPort: 8000
type: Loadbalancer
Step 2: Use kubectl apply -f service.yml command.
Step 3: Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.
- Get the IP address of the ClusterIP Service:
kubectl get services -n <namespace>
kubectl get services -n my-django-app
Note the name of another Pod in the same Namespace.
- Exec into the Pod: go inside container
kubectl exec -it <pod-name> -n <namespace> -- bash