Cyberithub

Create a Service to Expose your Apps on Kubernetes(v1.16)

Advertisements

In this article, I will take you through the steps to create a service to expose your apps on Kubernetes. If you are following through my articles, previously I have talked about How to Install and Configure Kubernetes on RedHat/CentOS 7. Let's create a service to expose your apps on Kubernetes cluster. Create a service to expose your apps.

Create a Service to Expose your Apps

What is a Service in Kubernetes ?

A Service enables network access to a set of Pods in Kubernetes. Services select Pods based on their labels. When a network request is made to the service, it selects all Pods in the cluster matching the service's selector, chooses one of them, and forwards the network request to it.

Create a Service to Expose your Apps on Kubernetes(v1.16)

Let's create a Service

You will write a yaml file to create your first service. Here you can provide your replica count, image that you want to use, port no which will be used to run the service.

[root@kuber-master ~]# cat test.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
   name: test
spec:
   selector:
     matchLabels:
       run: test-load-balancer
   replicas: 2
   template:
     metadata:
       labels:
         run: test-load-balancer
spec:
  containers:
   - name: test
     image: gcr.io/google-samples/node-hello:1.0
     ports:
     - containerPort: 8080
       protocol: TCP

 

Create a test application 

Once yaml is done, you can create the test application by running kubectl apply command.

[root@kuber-master ~]# kubectl apply -f test.yaml
deployment.apps/test created

 

Check the test application deployment

Once application is created, you can check the deployment and details of the application.

[root@kuber-master ~]# kubectl get deployment test
NAME READY UP-TO-DATE AVAILABLE AGE
test  2/2     2         2       32s

[root@kuber-master ~]# kubectl describe deployment test
Name: test
Namespace: default
CreationTimestamp: Sat, 16 Nov 2019 04:50:55 -0500
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"test","namespace":"default"},"spec":{"replicas":2,"select...
Selector: run=test-load-balancer
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=test-load-balancer
Containers:
test:
Image: gcr.io/google-samples/node-hello:1.0
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: test-67579c7c87 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 45s deployment-controller Scaled up replica set test-67579c7c87 to 2

 

Check the Replicaset

You can also check the replicaset if it is created based on your yaml file configuration before you proceed to create a service to expose your apps on Kubernetes.

[root@kuber-master ~]# kubectl get replicaset
NAME                   DESIRED CURRENT READY AGE
test-67579c7c87          2       2      2    85s

[root@kuber-master ~]# kubectl describe replicaset
Name: test-67579c7c87
Namespace: default
Selector: pod-template-hash=67579c7c87,run=test-load-balancer
Labels: pod-template-hash=67579c7c87
run=test-load-balancer
Annotations: deployment.kubernetes.io/desired-replicas: 2
deployment.kubernetes.io/max-replicas: 3
deployment.kubernetes.io/revision: 1
Controlled By: Deployment/test
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: pod-template-hash=67579c7c87
run=test-load-balancer
Containers:
test:
Image: gcr.io/google-samples/node-hello:1.0
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 91s replicaset-controller Created pod: test-67579c7c87-b4pz6
Normal SuccessfulCreate 90s replicaset-controller Created pod: test-67579c7c87-4mxhb

 

Create a Service test-service to expose your deployment

Now you need to create a service to expose your apps deployment.

[root@kuber-master ~]# kubectl expose deployment test --type=NodePort --name=test-service
service/test-service exposed

 

Check the service

You can check the service through kubectl describe command.

[root@kuber-master ~]# kubectl describe services test-service
Name: test-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: run=test-load-balancer
Type: NodePort
IP: 192.168.36.8
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30253/TCP
Endpoints: 192.168.0.104:8080,192.168.0.105:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>

 

Get the pods running our test application

Check the pods if it is running or not. You need the pod running to create a service to expose your apps.

[root@kuber-master ~]# kubectl get pods --selector="run=test-load-balancer" -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-67579c7c87-4mxhb 1/1 Running 0 4m52s 192.168.0.104 kuber-worker1 <none> <none>
test-67579c7c87-b4pz6 1/1 Running 0 4m52s 192.168.0.105 kuber-worker1 <none> <none>

 

Check the application deployment

Finally you can the check the application deployment using curl command.

[root@kuber-master ~]# curl 192.168.0.104:8080
Hello Kubernetes!

Also Read: How to Install and Configure Kubernetes

References: https://kubernetes.io/

Leave a Comment