Overview
Kubernetes is a portable and open-source platform for managing containerized workloads and services. When you deploy Kubernetes, you get a cluster.
Nodes and Pods
A Kubernetes cluster consists of many machines, called nodes, that run containerized applications. Every cluster has at least one master node that orchestrate the other ones.
The worker nodes host one or more pods that are the components of the application workload, usually a pod contain one service (server application, db, nginx, etc…)
template
pod-definition.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
type: frontend
spec:
metadata:
labels:
tier: frontend
spec:
containers:
- name: nginx-container
image: nginx
kubectl create -f pod-definition.yml
commands
kubectl run [container-name] --image=[image-name]
: create new pod from an imagekubectl get pods
: list of podskubectl get pod [container-name]
: get details about statuskubectl describe pod [container-name]
: get more detailskubectl delete pod [container-name]
: delete a pod
Replica Set
Replica set ensures that a specified number of pods run on node. It automatically brings up a new pod when the existing one fail.
template
rs-definition.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp-rs
labels:
app: myapp
type: frontend
spec:
replicas: 3
selector:
matchLabels:
type: frontend
template:
[POD-TEMPLATE]
selectors is the way for ReplicaSet to know what pods to monitor.
kubectl create -f rs-definition.yml
kubectl replace -f rs-definition.yml
: if you make any changes in the file
commands
kubectl get rs
kubectl delete rs [rs-name]
kubectl scale --replicas=6 rs myapp-rs
: to scale without edit the file
Deployment
A Deployment provides declarative updates for Pods and ReplicaSets. It creates replicaset under an object Deployment
template
deployment-definition.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-rs
labels:
app: myapp
type: frontend
spec:
replicas: 3
selector:
matchLabels:
type: frontend
template:
[POD-TEMPLATE]
selectors is the way for ReplicaSet to know what pods to monitor.
kubectl create -f deployment-definition.yml
commands
kubectl get all
Namespace
Namespaces provide a scope for names. Names of resources need to be unique within a namespace, but not across namespaces. Namespaces cannot be nested inside one another and each Kubernetes resource can only be in one namespace.
template
namespace-definition.yml
apiVersion: v1
kind: Namespace
metadata:
name: dev
kubectl create -f namespace-definition.yml
commands
kubectl get pods
: get only pods in default namespacekubectl get pods --namespace=dev
kubectl get pods --all-namespaces
Useful Imperative Commands
- Generate YAML file
—dry-run=client will not create the resource, it tells you whether the resource can be created and if your command is right.
kubectl run nginx --image=nginx --dry-run=client -o yaml > dp.yaml
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml
kubectl apply -f service.yml
kubectl edit [service] [servicename]
kubectl scale deployment nginx --replicas=4
kubectl apply -f service.yml
- Create a Service named nginx of type NodePort to expose pod nginx’s port 80
kubectl expose pod nginx --port=80 --name nginx-service --dry-run=client -o yaml
To change node-port, generate the definition file and edit it - Output in the plain-text format with more information
kubectl get pods -o wide