Kubernetes / Kubernetes Objects and Resources
Service Setup
This tutorial will guide you through the process of setting up a Service in Kubernetes. Services in Kubernetes are an abstract way to expose an application running on a set of Pod…
Section overview
4 resourcesCovers the core objects and resources in Kubernetes.
Introduction
This tutorial aims to guide you on how to set up a Service in Kubernetes. A Service in Kubernetes is an abstract way to expose an application running on a set of Pods as a network service. This abstraction decouples the dependency between consumers and producers of the services.
By the end of this tutorial, you will:
- Understand the concept of Services in Kubernetes
- Learn how to set up a Service in Kubernetes
- Gain practical experience through code examples and exercises
Prerequisites
Before you proceed, ensure that you have the following:
- Basic understanding of Kubernetes and its core concepts
- A working Kubernetes cluster
- Kubectl command-line tool installed
- Basic knowledge of YAML
Step-by-Step Guide
A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them. Services enable a loose coupling between dependent Pods.
Creating a Service
To create a Service, you need to define it in a YAML file. Below is a simple example of a Service definition:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: MyApp
ports:
- protocol: TCP
port: 80
targetPort: 9376
Code Examples
Here is a breakdown of the above example:
apiVersion: v1: This defines the version of Kubernetes API you're usingkind: Service: This means you're creating a Servicemetadata:: This section is for data about the Service like its namename: my-service: The name of your Servicespec:: This is where you'll specify the behavior of your Serviceselector:: This defines how the Service finds its Podsapp: MyApp: This selects any Pods with the label "app=MyApp"ports:: This is where you'll specify the ports the Service will listen onprotocol: TCP: The network protocol this Service will use. Kubernetes supports TCP, UDP, and SCTP.port: 80: The port number the Service will listen ontargetPort: 9376: The port number on the Pod that the Service will forward connections to
To apply the Service, run the following command:
kubectl apply -f service.yaml
This will create a Service that routes traffic to Pods labeled "app=MyApp" on port 9376.
Summary
In this tutorial, you learned about Kubernetes Services and how to set them up. You learned about their basic structure and how to write a Service definition in YAML. You then learned how to apply this definition to create a Service.
To continue learning about Kubernetes, you might want to explore Ingress, which is how Kubernetes manages external access to services in a cluster.
Practice Exercises
- Create a Service that routes traffic to Pods labeled "app=TestApp" on port 8080.
- Modify the Service from Exercise 1 to listen on port 1234 instead of 8080.
- Create a Service that routes traffic to Pods labeled "app=TestApp2" on port 8080 and uses the UDP protocol.
Solutions:
- Your YAML file should look like this:
apiVersion: v1
kind: Service
metadata:
name: test-service
spec:
selector:
app: TestApp
ports:
- protocol: TCP
port: 80
targetPort: 8080
-
To change the port, simply replace
port: 80withport: 1234. -
For the third exercise, your YAML should look like this:
apiVersion: v1
kind: Service
metadata:
name: test-service-2
spec:
selector:
app: TestApp2
ports:
- protocol: UDP
port: 80
targetPort: 8080
Remember to apply the YAML file with kubectl apply -f filename.yaml in each case.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article