Skip to main content

Kubernetes Basics Cheat sheet

Kubernetes Architecture Components

Master Node: The control plane of the cluster, responsible for managing the state of the cluster, scheduling and managing workloads, and providing a centralized configuration.

Pods

Smallest building blocks for deploying and managing applications in a Kubernetes cluster. A pod can contain one or more containers, and all containers in a pod share the same network namespace

kubectl get pods
kubectl describe pod pod_name
kubectl logs pod_name
kubectl delete pod pod_name
kubectl run nginx --image=nginx

Deployments

Deployments allow you to define the desired state for a group of replicas (replica set) of your application, and the deployment controller makes sure that the actual state of the replicas matches the desired state. Deployments are a way to achieve multiple objectives, such as scaling, rolling updates, and rollbacks.

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80

Services

Services enable communication between components of a microservices-based application, or between multiple applications. There are several types of Kubernetes services including ClusterIP, NodePort, LoadBalancer, ExternalName, and Headless services.

kubectl expose deployment my-deployment --port=80 --target-port=8080 --name=my-service --type=ClusterIP
kubectl expose deployment another-deployment --port=80 --target-port=8080 --name=my-service --type=NodePort
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- name: http
port: 80
targetPort: 8080
type: ClusterIP

StatefulSet

A StatefulSet is a type of Kubernetes resource that is used to manage stateful applications. Unlike a Deployment, which creates stateless replicas of a pod, a StatefulSet provides stable network identities and persistent storage to pods.

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: example-statefulset
spec:
selector:
matchLabels:
app: example-statefulset
serviceName: example-service
replicas: 3
template:
metadata:
labels:
app: example-statefulset
spec:
containers:
- name: example-container
image: example-image
ports:
- containerPort: 80
volumeMounts:
- name: example-volume
mountPath: /data
volumeClaimTemplates:
- metadata:
name: example-volume
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi

DaemonSet

A DaemonSet is a type of Kubernetes resource that ensures that exactly one pod is running on each node in a cluster. This is useful for running background tasks, such as logging agents or cluster-level storage daemons, that need to be running on every node.

apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example-daemonset
spec:
selector:
matchLabels:
app: example-daemonset
template:
metadata:
labels:
app: example-daemonset
spec:
containers:
- name: example-container
image: example-image
ports:
- containerPort: 80

ReplicaSet

A ReplicaSet is a type of Kubernetes resource that ensures that a specified number of replicas of a pod are running in a cluster at any given time. The ReplicaSet automatically creates or deletes pods as necessary to maintain the desired number of replicas.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: example-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: example-replicaset
template:
metadata:
labels:
app: example-replicaset
spec:
containers:
- name: example-container
image: example-image
ports:
- containerPort: 80

Secrets

Kubernetes Secrets are a way to securely store sensitive information, such as passwords, tokens, and certificates, in a Kubernetes cluster. Secrets are stored as base64-encoded strings and are encrypted in etcd.

apiVersion: v1
kind: Secret
metadata:
name: example-secret
type: Opaque
data:
example-username: YWRtaW4=
example-password: cGFzc3dvcmQ=
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: example-image
env:
- name: EXAMPLE_USERNAME
valueFrom:
secretKeyRef:
name: example-secret
key: example-username
- name: EXAMPLE_PASSWORD
valueFrom:
secretKeyRef:
name: example-secret
key: example-password

ConfigMaps

A ConfigMap is a Kubernetes resource that allows you to manage configuration data for your applications. You can store configuration data as key-value pairs in a ConfigMap and then reference it from your pods or other resources in the cluster.

apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap
data:
example-key: example-value
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: example-image
env:
- name: EXAMPLE_ENV_VAR
valueFrom:
configMapKeyRef:
name: example-configmap
key: example-key

Kubectl commands with examples

Retrieve information about one or many resources.

kubectl get pods
kubectl describe pod pod_name
kubectl logs pod_name
kubectl delete pod pod_name
kubectl create -f pod.yaml
kubectl apply -f pod.yaml
kubectl exec pod-name -- command
kubectl scale deployment deployment_name --replicas=5
kubectl port-forward pod_name 8080:80
kubectl edit pods pod_name
kubectl top node
kubectl cordon node_name
kubectl uncordon node_name
kubectl drain node_name
kubectl version

Comments