Tag Archives: containerization

Kubernetes 101: Deploying and Testing a Service

This hands-on article will help you create a simple deployment and a service, enabling you to manage and access applications efficiently within a Kubernetes cluster. You will create a deployment for the shanoj-testapp service with four replicas and a service that other pods in the cluster can access.

Creating the Deployment

A deployment ensures that a specified number of pod replicas are running at any given time. In this case, we will create a deployment for the shanoj-testapp service with four replicas.

Create the Deployment YAML File

Create a YAML file for the deployment using the cat command and the following content:

cat << EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: shanoj-testapp
labels:
app: shanoj-testapp
spec:
replicas: 4
selector:
matchLabels:
app: shanoj-testapp
template:
metadata:
labels:
app: shanoj-testapp
spec:
containers:
- name: shanoj-testapp
image: nginx:latest
ports:
- containerPort: 80
EOF

Image Reference: https://hub.docker.com/_/nginx

Creating the Service

A service in Kubernetes defines a logical set of pods and a policy by which to access them. In this step, we will create a service to provide access to the shanoj-testapp pods.

Create the Service YAML File

Create a YAML file for the service using the cat command and the following content:

cat << EOF | kubectl apply -f -
kind: Service
apiVersion: v1
metadata:
name: shanoj-svc
spec:
selector:
app: shanoj-testapp
ports:
- protocol: TCP
port: 80
targetPort: 80
EOF

This YAML file defines a service named shanoj-svc that selects pods with the label app=shanoj-testapp and forwards traffic to port 80 on the pods.

Verifying the Service

After creating the service, verify that it is running and accessible within the cluster.

Check the Service Status

Use the following command to check the status of the shanoj-svc service:

kubectl get svc shanoj-svc

Access the Service from a shanojtesting-pod Pod

Creating the shanojtesting-pod Pod

You need to create a shanojtesting-pod pod to use it for testing. Create a YAML configuration file for the shanojtesting-pod pod. Create a file named shanojtesting-pod.yaml with the following content:

cat << EOF > shanojtesting-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: shanojtesting-pod
labels:
app: shanojtesting-pod
spec:
containers:
- name: shanojtesting-pod
image: alpine
command: ['sh', '-c', 'sleep 3600']
EOF

Image Reference: https://hub.docker.com/_/alpine

Apply the configuration to create the shanojtesting-pod pod:

kubectl apply -f shanojtesting-pod.yaml

Ensure the shanojtesting-pod pod is running:

kubectl get pods

Testing the Service

To ensure the service is accessible, use the kubectl exec command to query the shanoj-svc service from the shanojtesting-pod testing pod:

kubectl exec shanojtesting-pod -- curl -s shanoj-svc

If the service is functioning correctly, this command should return a response from the shanoj-testapp pods.

Stackademic 🎓

Thank you for reading until the end. Before you go:

Preparing for a System Design Interview: Focus on Trade-offs, Not Mechanics

Are you getting ready for a system design interview? It is critical to approach it with the proper mindset and preparation. System design deals with components at a higher level, so staying out of the trenches is vital. Instead, interviewers are looking for a high-level understanding of the system, the ability to identify key components and their interactions, and the ability to weigh trade-offs between various design options.

During the interview, pay attention to the trade-offs rather than the mechanics. You must make decisions about the system’s scalability, dependability, security, and cost-effectiveness. Understanding the trade-offs between these various aspects is critical to make informed decisions.

Here are a few examples to prove my point:

  • If you’re creating a social media platform, you must choose between scalability and cost-effectiveness. Should you, for example, use a scalable but expensive cloud platform or a less expensive but less scalable hosting service?
  • When creating an e-commerce website, you must make trade-offs between security and usability. Should you, for example, require customers to create an account with a complex password or let them checkout as a guest with a simpler password?
  • When designing a transportation management system, you must balance dependability and cost-effectiveness. Should you, for example, use real-time data to optimise routes and minimise delays, or should you rely on historical data to save money?