Kubernetes
Containers played a pivotal role in the rise of microservices architecture, by offering a reliable, efficient, and scalable way to package and deploy individual services.
Kubernetes is an open-source platform for managing containerized applications. It automates many common tasks associated with running containers, such as deployment, scaling, and load balancing.
Key Concepts
A Kubernetes cluster consists of a control plane and multiple nodes. Each node hosts one or more pods.
A Pod is the smallest unit of deployment in Kubernetes. It represents a group of containers that share a network namespace and storage volumes.
A Deployment is a declarative specification of the desired state. It ensures that the desired number of pods are running and manages updates to them.
A Service is a network abstraction that defines how a group of pods can be accessed.
Ingress or Gateway exposes services to the outside world, allowing external traffic to reach the appropriate service within the cluster.
Setup
To try out k8s localy, there is minikube. On cloud platforms there is Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS) and Azure Kubernetes Service (AKS).
brew install kubectl minikube
minikube start
kubectl get pods
Usage
kubectl create deployment my-app --image=kicbase/echo-server:1.0
kubectl expose deployment my-app --type=NodePort --port=8080
Get the service endpoint:
minikube service my-app
We've just deployed our app on minikube. We can also see our pod running via kubectl:
kubectl get pod
NAME READY STATUS RESTARTS AGE
my-app-7d48979fd6-rrjrx 1/1 Running 0 1m
Delete the pod:
kubectl delete deployment my-app
kubectl delete service my-app
K8s Manifest
K8s uses a YAML file called manifest to define your deployment and
service. Add a deployment.yml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: kicbase/echo-server:1.0
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: my-app-svc
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
Applay the manifest file:
kubectl apply -f deployment.yml
We can see that there are 2 replicas, as specified in manifest:
kubectl get pod
NAME READY STATUS RESTARTS AGE
my-app-7c4f69b497-kpv2w 1/1 Running 0 7s
my-app-7c4f69b497-wrkjz 1/1 Running 0 7s
You can change it, and apply again.
Now access the service:
minikube service my-app-svc
Finally undo the deployment:
kubectl delete -f deployment.yml