Kubernetes
These notes are from when I read a bit of Kubernetes Up & Running, so they might be outdated. Recently one of my goals has become to learn Kubernetes more in-depth, as part of it, this will be updated regularly. Date: 2024.08.27
Pods
A Pod is the basic execution unit of a Kubernetes application. Each Pod represents a part of aworkload that is running on your cluster. It represents one or more application containers.
Containers within a Pod share the same IP address and port space; they also run on the same Node.
These containers share resources like:
- Shared storage, as Volumes
- Networking, as unique cluster IP address
- Information about how to run each container, such as the container image version or specific ports to use
Services
A Service is an abstraction which defines a logical set of Pods and a policy by which to access them. They allow loose coupling between dependent Pods.
Services are defined using YAML or JSON, and the targeted Pods are usually determined by a LabelSelector. When selector
is not defined, the corresponding Endpoint object won’t be created, also a Label may not be defined when explicitly using type: ExternalName
.
Services are also required to expose Pods outside of the Cluster. These Pods can be exposed in different ways depending on the type
in the ServiceSpec:
- ClusterIP (default) - exposes the Service on an internal IP in the cluster. Then the service is only reachable within the Cluster.
- NodePort - exposes the Service on the same port of each Node in the cluster using NAT. Nodes become available using
<NodeIP>:<NodePort>
. - LoadBalancer - Creates an external load balancer with a fixed external IP to the Service.
- ExternalName - Maps the Serivce to the contents of the
externalName
(i.e.foo.bar.example.com
).
Control Plane
Coordinates the cluster.
Nodes
A Node is a worker that runs an application and can be a VM or a physical computer. These are managed by the Master and can have multiple Pods.
Each Node runs:
- A kubelet, an agent for managing the node.
- A Container Runtime like
containerd
ordocker
.
Deployments
Module 2 - Deploy an app
Step 2 - Create a Deployment
1kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
2# Get deployments
3kubectl get deployments
4# Start proxy
5kubectl proxy
Create a deployment
- Use
kubectl create
1kubectl create deployment hello-node --images=k8s.gcr.io/echoserver:1.4
2# List deployments
3kubectl get deployments
4# List pods
5kubectl get pods
6# List events
7kubectl get events
8# View configuration
9kubectl config view
Create a Service
By default Pods are only accessible by their internal IPs, to expose the Pods you need to create a Service.
- Expose the Pod using the
kubectl expose
command.
1# LoadBalancer indicates this should be exposed outside of the cluster
2kubectl expose deployment hello-node --type=LoadBalancer --port=8080
3# View Services
4kubectl get services
- Normally an IP would be used to access the Service, on Minikube it can be accessed with
minikube service
.
1minikube service hello-node
Cleanup
1kubectl delete service hello-node
2kubectl delete deployment hello-node
Minikube
1minikube version
2minikube start
3kubectl version
4kubectl cluster-info
5kubectl get nodes
Addons
- List addons
1minikube addons list
2# Enable metrics-server
3minikube addons enable metrics-server
4# View the Pod and Service you created
5kubectl get pod,svc -n kube-system
6# Disable metrics-server
7minikube addons disable metrics-server
Cheatsheet
kubectl get
- list resourceskubectl describe
- show detailed information about a resourcekubectl logs
- print the logs from a container in a podkubectl exec
- execute a command on a container pod. Example:kubectl exec kubernetes-bootcamp-57978f5f5d-zpk6t -- env