Learn two ways to delete a service in Kubernetes. In this Kubernetes tip, you will learn about deleting a service in Kubernetes.
What is a Kubernetes Service?
Services enable communication between various components within and outside of the application. Kubernetes services helps you connect applications together with other applications or users. It provides a stable virtual IP (VIP) address. By using a service IP, clients can reliably connect to the containers running inside the pods.
For example, your application has groups of pods running for various sections such as a group for serving front end load to users and other group for running back end processes and a third group connecting to an external data source.
It is services that enable connectivity between these groups of pods. You can have as many services as required within the cluster.
Why do we use Service?
Kubernetes Pods are unreliable and non-permanent resources because they get created and destroyed to match the state of your cluster. When you create a Kubernetes Deployment to run your app, it can create and destroy Pods dynamically.
Every pod has its own IP address and that might change at a later point in time because that pod might get destroy and a new might come up.
This leads to a problem: if some set of Pods (call them “backends”) provides functionality to other Pods (call them “frontends”) inside your cluster, how do the frontends find out and keep track of which IP address to connect to, so that the frontend can use the backend part of the workload?
That is the reason Kubernetes architects came up with a solution which is known as Service.
Deleting Kubernetes service
First list available services in your kubernetes cluster.
This command will list out all the services created within all namespaces:
root@kmaster-rj:~/pod-create# kubectl get svc --all-namespaces -o wide
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 24d <none>
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 64d k8s-app=kube-dns
webapps my-dep-svc ClusterIP 10.100.159.167 <none> 8080/TCP 2m45s tier=front-end
You can see a service named my-dep-svc
created within webapps
namespace.
Let’s describe that service to understand its functionality quickly.
root@kmaster-rj:~/pod-create# kubectl describe svc my-dep-svc --namespace=webapps
Name: my-dep-svc
Namespace: webapps
Labels: <none>
Annotations: Selector: tier=front-end
Type: ClusterIP
IP: 10.100.159.167
Port: <unset> 8080/TCP
TargetPort: 80/TCP
Endpoints: 172.16.213.223:80,172.16.213.5:80
Session Affinity: None
Events: <none>
As I mentioned earlier, a service is a grouping of pods. You can that this service has two endpoints associated with this. More on endpoints association in a future article perhaps.
Service my-dep-svc
has a virtual IP assigned to it 10.100.159.167
.
For a quick demo, let’s try to access the application running on those two endpoints (pods) by using the Service IP (10.100.159.167) and its port (8080).
root@kmaster-rj:~/pod-create# curl 10.100.159.167:8080
<html><body><h1>It works!</h1></body></html>
root@kmaster-rj:~/pod-create#
I think you might have got a basic idea by now about Kubernetes services.
Now, there are two ways of deleting a service in Kubernetes. Let’s see them one by one.
Method 1: Use kubectl delete command to delete service
You can delete aservice in Kubernetes by supplying resource name directly to kubectl command:
root@kmaster-rj:~/pod-create# kubectl delete svc --namespace=webapps my-dep-svc
service "my-dep-svc" deleted
Now, if you list out all the services:
root@kmaster-rj:~/pod-create# kubectl get svc --all-namespaces -o wide
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 24d <none>
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 64d k8s-app=kube-dns
Method 2: Delete by referring the same YAML file by which it was created
The YAML configuration file from which the service my-dep-svc
was created.
root@kmaster-rj:~/pod-create# cat my-dep-svc.yml
apiVersion: v1
kind: Service
metadata:
name: my-dep-svc
namespace: webapps
spec:
selector:
tier: front-end
ports:
- protocol: TCP
port: 8080
targetPort: 80
Now delete the service by referring the same YAML file which was used to create it.
root@kmaster-rj:~/pod-create# kubectl delete -f my-dep-svc.yml
service "my-dep-svc" deleted
Again list out all the services.
root@kmaster-rj:~/pod-create# kubectl get svc --all-namespaces -o wide
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 24d <none>
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 64d k8s-app=kube-dns
Related searches
- How kubectl delete ingress
- I want kubectl apply
- How to kubectl delete namespace
- Show me kubectl get nodes
- kubectl delete deployment force
- Download kubectl patch
- All kubectl get events
- kubectl run pod
This is the end. In this article, we taught your how to delete service in Kubernetes. Stay tuned for more updates.