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…

Tutorial 2 of 4 4 resources in this section

Section overview

4 resources

Covers 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 using
  • kind: Service : This means you're creating a Service
  • metadata: : This section is for data about the Service like its name
  • name: my-service : The name of your Service
  • spec: : This is where you'll specify the behavior of your Service
  • selector: : This defines how the Service finds its Pods
  • app: MyApp : This selects any Pods with the label "app=MyApp"
  • ports: : This is where you'll specify the ports the Service will listen on
  • protocol: TCP : The network protocol this Service will use. Kubernetes supports TCP, UDP, and SCTP.
  • port: 80 : The port number the Service will listen on
  • targetPort: 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

  1. Create a Service that routes traffic to Pods labeled "app=TestApp" on port 8080.
  2. Modify the Service from Exercise 1 to listen on port 1234 instead of 8080.
  3. Create a Service that routes traffic to Pods labeled "app=TestApp2" on port 8080 and uses the UDP protocol.

Solutions:

  1. 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
  1. To change the port, simply replace port: 80 with port: 1234.

  2. 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

Age Calculator

Calculate age from date of birth.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help