Skip to main content

2 posts tagged with "DevOps"

View All Tags

Test local with k3d

· 7 min read
Guster
Principal Engineer

image

Introduction​

Every Kubernetes manifest I write eventually ends up on EKS. For a long time I tested those manifests by applying them straight to a shared dev cluster. It worked, but the feedback loop was painful. A wrong selector meant waiting for a rollout, a bad probe meant reading events on a remote node, and every experiment left a small mess behind.

So I started looking for a local cluster that was fast enough to create per task and close enough to EKS to trust. I tried kind, then minikube, and finally k3d. k3d is the one I kept.

This is not a feature-by-feature review of every local Kubernetes tool. It is the setup I use, the parts that matter to me, and why k3d became my default.

The short version​

k3dkindminikube
Runtimek3s in DockerKubernetes in DockerVM or container
Start timeSecondsSecondsA minute or more
Multi-node--agents NConfig file--nodes N
Local registryBuilt inManualAddon or manual
IngressTraefik includedInstall manuallyAddon
Best forApp dev and CIUpstream Kubernetes testingLearning and addons

What I wanted from a local cluster​

  • Fast enough to create and delete per task.
  • More than one node, so scheduling, affinity, and rolling updates mean something.
  • A registry that does not require pushing to a cloud account.
  • Ingress on localhost, so I can test real URLs.
  • A small enough footprint to run in CI.
  • Close enough to EKS that a manifest which works locally has a good chance of working there.

Why k3d​

k3d runs k3s inside Docker. k3s is a CNCF conformant Kubernetes distribution, so the API and the core objects behave the way I expect. The difference is packaging: one lightweight binary, a small control plane, and a load balancer container in front.

Creating a cluster with two agents is one command:

k3d cluster create dev \
--agents 2 \
--port "8080:80@loadbalancer" \
--port "8443:443@loadbalancer"

The cluster is ready in seconds. The port flags map localhost to the load balancer, so an Ingress on port 80 is reachable at http://localhost:8080.

Getting my image into the cluster​

This is the part that usually annoys me with local clusters. k3d has image import:

docker build -t my-api:dev .
k3d image import my-api:dev -c dev

The Deployment can then use my-api:dev with imagePullPolicy: IfNotPresent. No registry, no push, no credentials.

If I want a real registry, k3d can create one:

k3d registry create registry.localhost --port 5000
k3d cluster create dev --registry-use k3d-registry.localhost:5000

I use the registry when I am testing Helm charts or CI pipelines that expect to pull by tag. For day to day, image import is faster.

A config file I can commit​

Typing flags gets old, so I keep a k3d-config.yaml in the repo:

apiVersion: k3d.io/v1alpha5
kind: Simple
metadata:
name: dev
servers: 1
agents: 2
ports:
- port: 8080:80
nodeFilters:
- loadbalancer
- port: 8443:443
nodeFilters:
- loadbalancer
k3d cluster create --config k3d-config.yaml

Now the cluster shape is versioned with the app, and a new teammate gets the same setup with one command.

Testing the manifests​

My loop is boring, which is the point:

kubectl apply -f k8s/
kubectl rollout status deployment/my-api
curl http://localhost:8080/health

If a probe is wrong, I see it in seconds. If a selector does not match, kubectl describe is local and instant. If I break the cluster, I delete it and start again:

k3d cluster delete dev

That last command is the reason I stopped using a shared dev cluster for manifest work. I can be careless locally in a way I never could on a cluster someone else also uses.

What k3d is not​

k3d is not EKS. There are no IAM roles for service accounts, no ALB ingress controller, no EBS CSI driver, no VPC CNI, and no managed node groups. The defaults are different too: Traefik instead of ALB, ServiceLB instead of a cloud load balancer, and local-path storage instead of EBS.

If I am testing IAM, load balancer annotations, or storage classes, I still need a small EKS dev cluster. I use k3d for the parts that are application behaviour: deployments, services, config maps, secrets, probes, jobs, ingress rules, and rolling updates.

k3d also needs a Docker daemon. Podman support exists, but I stick to Docker because that is what my CI runners already have.

k3d vs kind vs minikube​

kind is excellent. It runs upstream Kubernetes in Docker and it is the tool I reach for when I want to test something close to the source, like a new Kubernetes version or an admission controller. The trade-off is setup: no ingress controller, no registry, and multi-node clusters need a config file. I can build all of that, but I would rather not rebuild it for every project.

minikube is the most feature-complete of the three. It has addons for ingress, dashboards, metrics, and more, and it is a great way to learn Kubernetes. It is also the heaviest. A VM or container driver, more memory, and a slower start make it a worse fit when I want a cluster for a twenty-minute task. Multi-node works, but it is not the default experience.

k3d sits in the middle and wins on workflow. Multi-node is a flag. Ingress is already there. A registry is one command. Image import is one command. The cluster starts in seconds and deletes cleanly. For application development and CI, that combination is hard to beat.

How I use it day to day​

  • One cluster per project, named after the project.
  • k3d cluster stop at the end of the day, k3d cluster start in the morning, and k3d cluster delete when the branch is merged.
  • A k8s/ directory with Kustomize overlays: local for k3d, prod for EKS. The local overlay only changes the image and points the Ingress host at localhost.
  • Integration tests in CI: create the cluster, import the image, apply the manifests, run the tests, delete the cluster.
  • Pin the k3s image in the config so a new release does not change behaviour mid-sprint.
image: rancher/k3s:<version>-k3s1

What I don't like​

  • Docker dependency. When Docker Desktop is unhappy, k3d is unhappy.
  • Defaults differ from EKS, so some manifests need a local overlay. Traefik versus ALB is the usual one.
  • Image import is manual. Forget it, and you get ImagePullBackOff for a tag that exists locally. I have done this more than once.
  • Networking through the load balancer container can be confusing when a service is not reachable. k3d node list and docker logs are part of the debugging story.
  • Version skew. k3s is not EKS, and a feature that works on one may not exist on the other.
  • Resource use. Two agents plus a server is still three containers plus the load balancer. On a small laptop, one agent is enough.

Which one should you use​

  • k3d if you want a fast, disposable cluster for app development and CI, with multi-node and ingress without extra setup.
  • kind if you are testing Kubernetes itself, need upstream parity, or already have a kind-based workflow.
  • minikube if you are learning Kubernetes and want addons and a dashboard in one command.
  • A real EKS dev cluster for IAM, load balancers, storage classes, and anything else that depends on AWS.

Closing thoughts​

k3d is my preferred local cluster because it removes the friction I actually care about: creating a cluster, getting my image in, reaching it on localhost, and deleting it when I am done. It is not a replacement for EKS, and I do not pretend it is. It is the place where I catch the boring mistakes before they cost me a rollout on the real cluster.

Speedrun .NET + Angular

· 4 min read
Guster
Principal Engineer

image

Introduction

Recently, I had an initial interview with a new company that uses Angular and .NET as its tech stack. Frankly, these two frameworks are pretty unfamiliar to me. I used to work with Angular.js back when it was still v1, which was about 8 years ago. I remember it was renamed to just Angular starting from v2, and now it's already at v18.

What about .NET? Even worse, I have never used it in my entire life! In fact, I don't know C# at all. Though I've heard a lot about it and that it's similar to Java.

Long story short, regardless of whether I advance to the next interview, I decided to take this as an opportunity to learn some new skills. However, I don't want to spend too much time learning. So, I decided to start a speed run on learning .NET and Angular.

How I learn

How I usually learn a new framework or programming language is by building a sample project. Here are a few pros of this approach:

  • Less boring - You get to see your half-baked product evolve while going through the tutorial.
  • Learn based on the features you need - It takes time to learn everything, especially with a complex framework like .NET. It's better to learn as you go. For example, if you need user authentication or a database migration.
  • Feels accomplished once the sample project is done and deployed for showcasing!

I have shared some of the notable points I learned from these frameworks here:

A Todo Project

And so, I decided to build a simple Todo task list. This Todo will contain the following features and acceptance criteria:

  • A login page for user authentication
  • Users can create, read, update, and delete a collection (a group of tasks)
  • Users can create, read, update, and delete a task within a collection
  • A main page with a two-pane layout: the left pane shows a list of collections, and the right pane shows the selected collection’s tasks
  • Users must be authenticated to view the main page
  • Basic UI styling (tailwind for the rescue!)
  • Logout functionality

Deployment

Now that I have finished building the API and web app, I want to deploy them somewhere to showcase if needed. Since I already have AWS, I decided to deploy there.

I want it fast and simple, so here’s what I did:

  • Hosted the Angular app on an S3 bucket as a static site
  • Dockerized the .NET API and deployed it to an EC2 instance
  • Created a Postgres instance in RDS

I used Terraform to ease the process of provisioning these resources. Here’s a sample script.

# VPC, subnets, internet gateway, route table and security groups provisioning
# ...

# ==================== RDS ====================
resource "aws_db_instance" "my_rds" {
allocated_storage = 20
engine = "postgres"
engine_version = "16.4"
instance_class = "db.t3.micro"
db_name = "mydatabase"
username = "username"
password = "password"
vpc_security_group_ids = [aws_security_group.rds_sg.id]
db_subnet_group_name = aws_db_subnet_group.my_db_subnet_group.name
skip_final_snapshot = true
publicly_accessible = false
}
resource "aws_db_subnet_group" "my_db_subnet_group" {
name = "my-db-subnet-group"
subnet_ids = [
aws_subnet.private_subnet_a.id,
aws_subnet.private_subnet_b.id
]
}

# ==================== EC2 ====================
resource "aws_instance" "dotnet_todo_instance" {
ami = "ami-0a6b545f62129c495"
instance_type = "t2.micro"
subnet_id = aws_subnet.public_subnet_a.id
vpc_security_group_ids = [aws_security_group.ec2_sg.id]
tags = {
Name = "dotnet-todo-ec2"
}
}

# ==================== S3 ====================
resource "aws_s3_bucket" "dotnet_todo_web" {
bucket = "dotnet-todo-web"
}
resource "aws_s3_bucket_policy" "dotnet_todo_web_policy" {
bucket = aws_s3_bucket.dotnet_todo_web.id
policy = jsonencode(
{
Version = "2012-10-17"
Statement = [
{
Sid = "PublicReadGetObject"
Action = "s3:GetObject"
Effect = "Allow"
Principal = "*"
Resource = "${aws_s3_bucket.dotnet_todo_web.arn}/*"
},
]
}
)
}

Result

Screenshot 2024-08-19 at 10.34.42 PM.png

Screenshot 2024-08-19 at 10.34.27 PM.png

Concluding Thoughts

Overall, it took me roughly one week to learn Angular, .NET, and complete the project. From a developer's perspective, the experience was quite smooth and easy. I found Angular's opinionated structure to be particularly beneficial. Its out-of-the-box common libraries saved me a significant amount of time that I would have otherwise spent searching for third-party solutions. This allowed me to focus more on building the actual features of my project rather than dealing with setup complexities.

When it comes to .NET, although I don’t particularly like C#’s Pascal-case naming conventions and its syntax formatting, I must admit that .NET is very easy to set up, develop, and build. I found the CLI and generators to be especially helpful tools that streamlined the development process. These tools provided a robust foundation, making it easier to manage and organize the codebase efficiently.

I built a simple todo for this project, but the learning experience has inspired me to explore more complex projects using Angular and .NET. I am definitely going to start building more with Angular and .NET in the future. The combination of these frameworks provides a powerful and efficient development environment that I am excited to continue exploring.