[TOC]
Every kubernetes cluster has the following 6 components:
Brief explanation:
kubectl
command, you're interacting with the API Server)Used to interact with kubernetes
examples:
kubectl get-info
kubectl get nodes
A Pod is the smalles object that you can create in kubernetes.
A Pod can have one or more containers.
It's recommended to group in the same pod only containers that need to scale together.
Very minimum Pod yaml:
# example with nginx
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: container-nginx
image: nginx:1.23
Creating a Pod from CLI:
# example with nginx
kubectl run nginx --image=nginx:1.23
Always present 4 top-level keys in a k8s yaml file:
apiVersion:
kind:
metadata:
spec:
https://kodekloud.com/topic/pods-4/
Replication Controller
The Replication Controller is responsible to respawn a new pod when the current one fails.
It can also scale the numbers of pods based on the workload, and spawn new pods on different nodes in order to distribute the workload.
Replication Controller != ReplicaSet
Replication Controller is a technology that is being replaced by ReplicaSet
yaml of a ReplicationController:
apiVersion: v1
kind: ReplicationController
metadta:
name: myapp-rc
labels:
app: myapp
type: front-end
spec:
template:
metadata:
name: myapp-pod
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-containers
image: nginx
replicas: 3
# apparently there's no 'selector'
yaml of a ReplicaSet
apiVersion: apps/v1
kind: ReplicaSet
metadta:
name: myapp-replicaset
labels:
app: myapp
type: front-end
spec:
template:
metadata:
name: myapp-pod
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-containers
image: nginx
replicas: 3
# the difference is that a ReplicaSet definition is able
# to manage replication of pods not defined in this
# file's spec.template
selector:
matchLabels:
type: front-end
Directly from command line:
# already running ReplicaSet
kubectl scale --replicas 6 replicaset myapp-replicaset
# overwriting the value in the definition file
kubectl scale --replicas 6 -f replicaset-definition.yaml
It's recommended to change the YAML file, in order to keep the change for the future.
same as ReplicaSet, just replace kind: ReplicaSet
with kind: Deployment
.
In the pod's definition we can set the namespace in the metadata.namespace
key.
namespace definition file
apiVersion: v1
kind: Namespace
metadata:
name: dev
For getting info for a namespace different from the default one:
kubectl get pods --namespace=dev
kubectl get pods --namespace=prod
To change the default namespace:
kubectl config set-context \
$(kubectl config current-context) \
--namespace=dev
View pods on all namespaces:
kubectl get pods --all-namespaces
I'm not really sure why in the end of the lecture the tutor showed a ResourceQuota definition file:
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-quota
namespace: dev
spec:
hard:
pods: "10"
requests.cpu: "4"
requests.memory: 5Gi
limits.cpu: "10"
limits.memory: 10Gi