Skip to main content

Command Palette

Search for a command to run...

Day 34 : Working with Services in Kubernetes

Updated
2 min read

Task 1 ) Create a Service for your todo-app Deployment

we need to create service.yml as below

apiVersion: v1
kind: Service
metadata:
  name: my-service
  namespace: my-namespace
spec:
  selector:
    app: node-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000

you can apply the Service definition to your K8s (minikube) cluster using the kubectl apply -f service.yml -n my-namespace command.

you can get cluster ip by running following command

Now to check your application you need to do ssh to minikube and then run following command.

you will get response like above screenshot.

Task 2) Create a LoadBalancer Service for accessing the todo-app from outside the cluster

  1. Create a YAML file called load-balancer-service.yml and add the following contents:
apiVersion: v1
kind: Service
metadata:
  name: node-app-lb
spec:
  type: LoadBalancer
  selector:
    app: node-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  1. Save the file and apply the LoadBalancer service definition to your Kubernetes cluster using the following command:
kubectl apply -f load-balancer-service.yml -n my-namespac
  1. To verify that the LoadBalancer service is working, you can access the todo-app from outside the cluster by using the external IP address assigned to the service. Run the following command to get the external IP:
kubectl get service todo-app-lb -n my-namespace
  1. Open a web browser and enter the external IP address in the following format:

     http://your-inctance-ip:80
    

Congratulation your application is running on external IP

That was it for today. If you have queries /suggestions please comment below.

See you another day with another challenge, till then Keep Learning.

More from this blog

Ajay Patel

116 posts

Day 34 : Working with Services in Kubernetes