Kubernetes
How to use Kubernetes?
-
Run cluster startup script
Change cluster version to:
--zone $ZONE --cluster-version "1.23.8-gke.1900" \ -
Use minikube for a local Kubernetes cluster
- Pro: you don’t have to pay cloud costs.
- Con: only one node
minikubeis running in a docker container (check withdocker ps)minikubecomes its own version ofkubectlwhich is definitely compatible withminikube. So it’s safest to NOT runkubectl, butminikube kubectlfor all commands.- Check that the running cluster is the minikube one via
kubectl config viewwill contain (i.e.contextshould be pointing tocluster: minikube):
- context:
cluster: minikube- You can now run
minikube dashboardfor a nice dashboard of the cluster. - Via Docker Desktop: Enable in Configurations
Output of kubectl config view will contain:
- context:
cluster: docker-desktopWhat?
Orchestrate many micro services.
View what’s happening
via kubectl
Run kubectl get pods (or kubectl get pods -A to view all pods - including internal ones, e.g. of minikube) every second:
watch -n 1 kubectl get podsView endpoints, pods, nodes and services:
kubectl get endpoints,pods,nodes,svc -o wideView everything:
kubectl get allGet .yaml file + extra information of horizontal pod autoscaler:
kubectl get hpa -o yamlvia
Container
Kubernetes Yaml config file
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: nginx
image: nginxdemos/hello:0.3
ports:
- containerPort: 80
resources:
requests:
cpu: "100m"
memory: "100Mi"
limits:
cpu: "250m"
memory: "100Mi"-
CPU measured in millicores
- If, for instance, we want to request 0.5 of a CPU, we should express it as
500m— where m represents millicores. - In the example above, we’ve specified the container’s requested CPU as
100mand the limit as250m. This means the processing power that will be reserved for the container is100m. Furthermore, if a process requires more than100m, it can access the additional CPU resources that the scheduler will ensure are available on the node — up to the250mlimit.
- If, for instance, we want to request 0.5 of a CPU, we should express it as
- Memory resources are measured in bytes and can be expressed as fixed-point numbers, integers or power-of-two equivalents. We will use the most common type of expression — the power-of-two equivalent,
Mi. This represents aMebibyte, or220 bytes.
see this link for more.
Multi container pods
apiVersion: v1
kind: Pod
metadata:
name: my-multi-pod2
spec:
containers:
- name: container-a
image: alpine:3.16.0
command:
- "/bin/sh"
args:
- "-c"
- "while true; do echo $(date) > /tmp/buffer; sleep 1; done;"
volumeMounts:
- name: buffer
mountPath: /tmp
- name: container-b
image: alpine:3.16.0
command:
- "/bin/sh"
args:
- "-c"
- "while true; do cat /tmp/buffer; sleep 1; done;"
volumeMounts:
- name: buffer
mountPath: /tmp
volumes:
- name: buffer
emptyDir: {}Jump into a running container (in a pod):
kubectl exec -it pod_name -- sh
kubectl exec -it pod_name container_name -- sh- Files:
ls -lisa - Look at processes
ps ef
Replica Sets and deployments
Selectors
- Each replica set has to have a selector which defines the set of pods.
- A label selector matches certain labels.
Rollout strategies
Services
Run nodes which contain pods.
Service types
ClusterIP: Expose ports locally onlocalhostNodePort: Expose explicit external ports on nodes (on all node’s external IP addresses - view them viakubectl get nodes -o wide)LoadBalancer: Add external load balancer (not managed by Kubernetes)
Auto-Scaling
Horizontal pod autoscaling
Scale horizontally (aka horizontal pod autoscaling) with pods
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: my-autoscaler
spec:
maxReplicas: 5
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-deployment
targetCPUUtilizationPercentage: 5targetCPUUtilizationPercentageis chosen to be low (5%) so that scaling will be triggered fast.
Vertical node auto-scaling
- Scale vertically with nodes (instance groups)
Health checks
Probes
health-checks run by kubernetes to test whether everything is running well.
Types of probes: liveness probes, startup probes
Self-healing
Compare actual state to desired state -> act on differences
Concepts
| Concept | What? |
|---|---|
| cluster | Physical network of nodes on which services/deployments/pods etc. run |
| pod | smallest entity in kubernetes. Contains containers. |
| containerd | default container runtime used by docker + kubernetes |
kubeadm |
create and manage kubernetes clusters |
| controller | generate certain states |
| cloud controller manager | communicates with cloud |
| context | Manage access to a cluster |
controler management (cm) |
Often runs in an own pod |
| worker nodes | On this node, workloads of own machine (nothing external) are running. worker has pods running. |
| kubelet | communicates with container runtime |
| scheduler | chooses best node(s) to place resources (e.g. where to run a new pod/service etc.). Can be manually circumvented with e.g. nodeName to specify particular node |
| ServiceAccount | virtual/technical user (not real person) |
| DaemonSet | Run a pod (+ copies of that pod) on all nodes. When new nodes are created, a new pod will be automatically created for that node. |
| ConfigMap + Secret | Ways to store data |
Namespaces
kubectl get pods --namespace webshop
Useful
View spec
View spec of Kubernetes object (e.g. pod or deployment etc.):
kubectl get deployments.apps my-deployment -o yamlTools
helm the kubernetes package manager
-
helmchartmy-chart/ templates/ configmap.yaml deployment.yaml service.yaml Chart.yaml values.yaml <-- default values file my-own-values.yaml -
values.yamlcolor: cyan replicas: 1 -
Run all kubernetes objects defined in your
helmchart viahelm install my-release-defaults ./my-chart
Prometheus
Grafana
krew
krew# Plugin manager for kubectl.
*You can install it with brew. * kubectl-ctx
Allows to easily switch the context of kubernetes - switching between environments.
install
kubectl krew install ctx
usage
kubectl ctx kubectl-ns
Allows to easily display/switch kube namespaces.
install
kubectl krew install ns
usage
kubectl ns kubectl-view-secret
This plugin allows for easy secret decoding. Useful if you want to see what’s inside of a secret.
install
kubectl krew install view-secret
usage
kubectl view-secret
Discuss on Twitter ● Improve this article: Edit on GitHub