In the previous post we’ve looked into Kubernetes Deployments which are essentially constructs allowing us to declare and manage Pods on the Kubernetes platform.

However, how do you access your applications running inside a Pod? How about accessing your app running on 1+ instances of the same Pod? How is the traffic routed and balanced across these Pods?

Well, the answer is Kubernetes Service object. The Service is there to provide a consistent access point to Pods. It’s essentially a load balancer for Pods.

More Details: Kubernetes Service


Create Service

Let’s start with creating Service workloads. You can generally do this either by adopting Imperative or Declarative syntax with the kubectl create command.

More Details: kubectl create

Imperative

kubectl create svc [SERVICE NAME]

Declarative

kubectl apply -f [SERVICE TEMPLATE].yaml

[SERVICE TEMPLATE].yaml

apiVersion: v1
kind: Service
metadata:
  name: [SERVICE NAME]
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 5000

List Services

To find out what Services are running and in which Namespaces you’ll probably want to use some of these kubectl get svc commands.

More Details: kubectl get

List All Services in Namespace

kubectl get svc -n [NAMESPACE]

List All Services in All Namespaces

kubectl get svc --all-namespaces

List All Services with Labels

kubectl get svc --all-namespaces --show-labels

Service Details

These commands will allow you to look deeper into your chosen Service where you’ll find out all the Service details, labels, ports, endpoints & IP addresses.

More Details: kubectl describe

Get Service

kubectl get svc [SERVICE NAME] -n [NAMESPACE]

Get Service – Wide Output

kubectl get svc [SERVICE NAME] -n [NAMESPACE] -o wide

Get Service – YAML Output

kubectl get svc [SERVICE NAME] -n [NAMESPACE] -o yaml

Export Service – YAML Output to File

kubectl get svc [SERVICE NAME] -n [NAMESPACE] -o yaml --export > [FILE NAME].yaml

Describe Service

kubectl describe svc [SERVICE NAME] -n [NAMESPACE]

Patch Service

To modify an existing Service directly on the Kubernetes data plane use the kubectl patch command.

More Details: kubectl patch

Patch Service with bash

kubectl patch svc [SERVICE NAME] -n [NAMESPACE] -p "$(cat [SERVICE TEMPLATE].yaml)"

Patch Service with Powershell

kubectl patch svc [SERVICE NAME] -n [NAMESPACE] -p $(Get-Content [SERVICE TEMPLATE].yaml -Raw)

Delete Service

Finally, to dispose of a Service simply use the kubectl delete command allowing you to target single Service or even filter on labels or namespaces.

More Details: kubectl delete

Imperative

Delete Service By Name

kubectl delete svc [SERVICE NAME] -n [NAMESPACE]

Delete Service By Label

kubectl delete svc -l name=[LABEL] -n [NAMESPACE]

Delete All Services in Namespace

kubectl delete svc -n [NAMESPACE] --all

Declarative

kubectl delete -f [SERVICE TEMPLATE].yaml

<< Kubernetes Deployments | Kubernetes Services | Kubernetes Ingress >>

Marcin Narloch

Marcin Narloch

Creative and out-of-the-box thinker with strong interests and knowledge in technology and innovation.
Docker Building Blocks Previous post Kubernetes Ingress
Docker Building Blocks Next post Kubernetes Deployments

Leave a Reply

Your email address will not be published. Required fields are marked *