Skip to content

Containers, Kubernetes and infrastructure as code

Just enough about containers, images, pods, services, deployments and infrastructure as code: what each one is, the problem it solves, and what happens when a container runs out of memory.

  • Lesson 97 of 125
  • 12 min read
  • Depth
  • Free to read, no account
On this page
  1. Why interviewers ask this
  2. The idea in plain words
  3. Pause and predict
  4. Worked example
  5. Trade-offs
  6. Common misconceptions
  7. How to say it in the interview
  8. Check your understanding
  9. Answers
  10. Sources

What you need first

This lesson uses ideas from these earlier lessons. Read them first if any idea is new to you.

01Why interviewers ask this

The interviewer asks how you pack your service, keep the right number running, and build servers the same way every time. A weak answer is a list of tool names. A strong answer says what problem each part solves. At SDE-3 level you should also say when this is too much.

02The idea in plain words

Think about a shipping container. Cranes, ships and trucks all handle the same box, whatever is inside. A software container is the same idea.

A container is your program packed with the files and settings it needs, running alone on a shared machine. It shares the operating system core of its machine, so it starts in seconds. A virtual machine (lesson 13) carries a whole operating system of its own, so it is heavier. An image is the fixed package that one build makes. A container starts from an image, and one image can start many containers. The same image runs in testing and live. Servers set up by hand differ, as server 3 did in lesson 94. Images do not.

Kubernetes runs containers on a group of machines, called a cluster. A node is one machine in it. These are its main parts:

  • A pod is the smallest unit that Kubernetes runs: one or more containers that share one address. A pod is disposable, and a replacement has a new address. Keep no data in a pod (lesson 26).
  • A Deployment is your wish: "run 6 pods from this image". Kubernetes keeps comparing the pods that exist with your wish. If a pod is lost, it starts another. If you name a new image, it replaces pods in batches (a rolling release, lesson 95) and can go back.
  • A Service is one steady address in front of changing pods. It shares requests among them, like the load balancer of lesson 28. A pod gets requests only when its readiness check, the health check of lesson 28, passes.

A Deployment can set a memory limit for each container. If a container uses more, the system stops it (an out-of-memory kill). Kubernetes then starts the container again in the same pod.

Infrastructure as code means you write servers, networks and databases in text files, kept and reviewed like code. A tool reads the files and compares them with what exists. It prints a plan, which is the list of changes, such as Plan: 1 to add, 0 to change, 1 to destroy. Then it applies the plan. Terraform is one such tool. A change made by hand makes the real system differ from the files. This is drift, and the next plan shows it.

Stuck on something? The AI tutor has read this lesson. Ask it with a free account

03Pause and predict

Pause. Guess first.

A pod has a 1,000 MB memory limit. It starts at 300 MB, and its memory grows by 100 MB every hour. After how many hours does it stop, and what happens next?

Write your guess before you read on. The answer is in the worked example.

04Worked example

The food-ordering app has a checkout service.

Assumptions (made up to teach with): peak load is 420 requests per second. One pod handles 100 per second at full load. We plan for 70% busy.

Step 1: how many pods. One pod at 70% handles 100 x 0.7 = 70. Pods = 420 / 70 = 6.

Step 2: the memory growth. The pod stops when memory reaches 1,000 MB. The growth is 1,000 - 300 = 700 MB, and 700 / 100 = 7 hours. This answers the predict box. Kubernetes starts the container again from the same image, with memory back at 300 MB. Requests on it fail. If we raise the limit to 2,000 MB, the pod lasts (2,000 - 300) / 100 = 17 hours. The leak is still there.

05Trade-offs

  • Containers and Kubernetes solve one package everywhere and the right number of pods kept running. They cost a cluster to run and more steps when something breaks. I would use plain servers for one small service on two or three of them.
  • Infrastructure as code solves servers that are the same and reviewed. It costs files to write and keep. I would start with the parts that you must rebuild fast.

06Common misconceptions

"A container is a small virtual machine." A container shares the operating system core of its machine. A virtual machine has its own.

"Kubernetes restarts the container, so a memory leak is handled." The restart hides the leak. Users still see errors, and the cause stays.

07How to say it in the interview

Say it like this

I pack the service as an image, so it runs the same everywhere. Kubernetes keeps the pods I ask for running, and I keep the servers in code files that people review.

08Check your understanding

  1. Say in one line what each one is: image, pod, Deployment, Service.
  2. A service peaks at 900 requests per second. One pod handles 150 per second at full load. We plan for 70% busy. (a) How many pods? (b) A pod starts at 200 MB, grows 50 MB an hour, and has an 800 MB limit. After how many hours does it stop?
  3. The plan says Plan: 0 to add, 0 to change, 1 to destroy. The resource is the database. What do you do?

09Answers

Show the answers
  1. Image: the fixed package a container starts from. Pod: the smallest unit Kubernetes runs. Deployment: the wish for a number of pods from one image. Service: one steady address in front of pods.
  2. (a) 150 x 0.7 = 105 per pod. 900 / 105 = 8.6, so 9 pods. (b) (800 - 200) / 50 = 12 hours.
  3. Do not apply. A plan that only destroys the database means the files lost it, or point to the wrong place. A destroyed database loses its data. Fix the files and plan again.

10Sources

Try it now, no sign-up

More practice, in the app (free account)

Ask the AI tutor about this lesson

The tutor has read this lesson. Ask in your own words, any time. Free account, 20 questions a day.

Reading never needs an account. The account is for the tutor, the quiz, your notes and your progress.

All lessons in Module 15: Services and running it