Master Kubernetes: From Container Basics to Deploying a Node.js App
This tutorial walks readers through the evolution of web deployment, explains core Kubernetes concepts, and provides step‑by‑step instructions for using kubectl to create Ingress, Service, and Deployment resources that run a multi‑instance Node.js backend, culminating in a functional K8S deployment workflow.
To make the most of container‑based tools and understand cloud product forms, programmers need to learn container technologies.
K8S Overview
Before answering what K8S is, we review the evolution of web application deployment methods.
Host Machine Mode
When I first started software development, applications were deployed by installing a web server (e.g., Nginx or Apache) on a physical server, then installing runtime environments and other software such as databases.
As web applications grew more complex, this approach showed drawbacks.
Modern servers are powerful, but running only a few programs on a single host leads to low resource utilization. Programs running directly on the host compete for resources, and a failure in one program can affect the whole host.
Docker container technology appeared in 2013, gradually replacing this model.
Containers
Various container implementations exist; Docker is the most popular.
The Docker logo reflects the idea of containers as isolated boxes.
Unlike traditional host deployment, containerization provides an isolated environment where programs do not interfere with each other or the host.
A Docker container can be seen as an OS‑level virtual machine that bundles the runtime environment and code, exposing services via port mapping.
Containers can also communicate through the Docker engine, typically using internal IP addresses.
When web applications scale, a single host can no longer meet performance requirements. Multi‑host, multi‑container deployments are needed, leading to the main subject of this article: Kubernetes (K8S).
Open‑Source Container Management Platform
K8S stands for Kubernetes (the “8” represents the eight letters between “k” and “s”). It is an open‑source container orchestration platform originally developed by Google.
K8S enables operators—and even front‑end developers—to easily deploy and manage containers in a cluster. It supports load balancing, high availability, high concurrency (multiple instances), and cluster management.
Load balancing
High availability
High concurrency (multiple instances)
Cluster management
K8S must be installed and running on a server before using the following commands.
K8S Core Concepts
All resources in K8S are managed declaratively as objects. For example, a Namespace object is defined as:
apiVersion: v1
kind: Namespace
metadata:
name: demo-space
spec:
finalizers:
- kubernetes
status:
phase: ActiveEach object includes basic fields such as apiVersion, kind, metadata, and spec.
For more details, refer to the official K8S documentation.
Common K8S objects include Namespace, Ingress, Service, Deployment, and Pod. A Namespace partitions a cluster into logical groups.
Ingress
An Ingress provides a unified entry point for traffic to a cluster, typically implemented with an Nginx controller.
Ingress‑nginx documentation: https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/
Service
Service abstracts a set of Pods and provides a stable IP/port for access, handling dynamic Pod IPs and load balancing.
Deployment
Deployment manages replica sets and container configurations, allowing declarative updates.
A replica set is a group of Pods that maintains a desired number of replicas.
Pod
A Pod is the smallest deployable unit in K8S, containing one or more containers that share networking and storage.
Example multi‑container setup: a web server container and a sidecar container that pulls files.
kubectl Tool Introduction
kubectlis the command‑line tool for interacting with a K8S cluster.
MacOS installation guide: https://kubernetes.io/docs/tasks/tools/install-kubectl-macos/
After installation, create a configuration file: touch $HOME/.kube/config Then add cluster configuration (example shown).
apiVersion: v1
# cluster configuration...
clusters:
- cluster:
server: https://dami.net
name: dami-c3
contexts:
- context:
cluster: dami-c3
namespace: demo-space
user: manooog
name: c3-demo-space-context
current-context: c3-demo-space-context
kind: Config
users:
- name: manooog
user:
token: <yourToken>Common commands:
View Configuration
kubectl get service/<xxx> -o yamlApply Configuration File
kubectl create -f service.yaml
kubectl apply -f service.yamlUse apply for updates.
Difference between apply and create: https://stackoverflow.com/questions/47369351/kubectl-apply-vs-kubectl-create
View Pod Logs
kubectl logs <pod> <container-name>Enter Container Shell
kubectl exec -it demo-5b7846d65b-nvnnm -- shUse -c to specify the container when a Pod has multiple containers.
Demo: Deploy a 3‑Instance Node.js Backend
The demo aims to launch a Node.js backend with three replicas and expose the path /20211215 returning “Hello World”.
curl demo-20211215.io/20211215 # -> Hello WorldConfigure the local /etc/hosts to map the domain to the Ingress IP:
<Ingress IP> demo-20211215.ioIngress Creation
Save the following as ingress.yaml and apply:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: demo-20211215
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
rules:
- host: demo-20211215.io
http:
paths:
- path: /20211215
backend:
serviceName: demo-service-20211215
servicePort: 80After applying, the rule appears in kubectl get ingress. The Ingress will forward requests to the Service once it exists.
Service Creation
Save service.yaml:
apiVersion: v1
kind: Service
metadata:
name: demo-service-20211215
spec:
selector:
app: demo-app-20211215
ports:
- protocol: TCP
port: 80
targetPort: 3000Apply and verify with kubectl get service/demo-service-20211215.
Deployment Creation
Save deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment-20211215
spec:
replicas: 3
selector:
matchLabels:
app: demo-app-20211215
template:
metadata:
labels:
app: demo-app-20211215
spec:
containers:
- name: demo-20211215
image: rxh1212/demo-20211215
ports:
- containerPort: 3000The container image runs a simple Express server that responds “Hello World” on /20211215:
"use strict"
const express = require("express")
const PORT = 3000
const HOST = "0.0.0.0"
const app = express()
app.get("/20211215", (req, res) => {
res.send("Hello World")
})
app.listen(PORT, HOST)
console.log(`Running on http://${HOST}:${PORT}`)After creating the Deployment, accessing demo-20211215.io/20211215 returns a 200 OK with “Hello World”.
Thus we have completed a simple K8S deployment workflow.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
