Kubernetes Flashcards
container runtime
a k8s component, the underlying software that is used to run containers, e.g. docker
pod is what of k8s
k8s object
to see each pod’s node
kubectl get po -o wide
create yml of pod quickly
kubectl run redis –image=redis123 –dry-run=client -o yaml
edit pod
- use existing yml
- extract into yml and recreate pod
- k edit only for below properties
spec.containers[].image
spec.initContainers[].image
spec.activeDeadlineSeconds
spec.tolerations
spec.terminationGracePeriodSeconds
spec.replica
Replica Set (prev. replication controller)
difference of above two?
- high availability
- load balancer across nodes
selector: use to allow for managing pod that not created by replicaSet directly
edit replicaset
k replace -f xxx.yml
k scale –replicas=6 -f rs-definition.yml
k scale –replicas=6 replicaset my-rs
get version of a k object
k explain replicaset
quick delete multiple pods
in a line: k delete po po1 po2 po3 po4
deployment vs rs
deployment contains replicaset, rs contains pod
–all-namespaces
–label
short -A
-l=”tier=db”
Cert Tip: Imperative Command
Run an instance of the image webapp-color and publish port 8080 on the container to 8282 on the host.
docker run -p 8282:8080 webapp-color
light version docker image
python:3.6-alpine on alpine not debian
Practice test Docker images
answer is missing
docker ps vs docker ps -a
-a list all containers including the stopped ones
container automatically exit when its task/process is done, which is defined by “CMD”. The process has to be things like web server, db server but not “bash”
docker run ubuntu
will exit but
docker run ubuntu [cmd]
docker run ubuntu sleep 5 will lasts for 5 secs
or:
CMD sleep 5
CMD [“sleep”, “5”]
or:
ENTRYPOINT [“sleep”]
docker run ubuntu-sleeper 10
or:
ENTRYPOINT [“sleep”]
CMD [“5”] -> default value
or: modify during runtime
docker run –entrypoint sleep2.0 ubuntu-sleeper 10
k replace –force -f x.yml
replace pods
docker run –name ubuntu-container –entrypoint sleep2.0 ubuntu-sleeper 10 in pod definition
command:
args: [“10”]
imperative vs declarative
k create configmap
k create -f xxx.yml
convert base64
echo -n ‘paswrd’ | base64
echo -n ‘paswrd’ | base64 –decode
ubuntu install
apt-get install
list processes on docker host / inside container
security context
ps aux
PID for different containers on the host are different -> process isolation
by default process run as root, but root user inside container is not like it on the host
change root’s capability,
docker run –add-cap MAC_ADMIN
or –drop-cap
–privilege
get user inside pod
k exec po po-name – whoami
resource CPU unit
memory unit 1G vs 1Gi
1 vCPU
0.1 == 100 m vCPU mili vCPU
minimal 1m vCPU
1 G = gigabyte 1,000,000,000 bytes
1 Gi = gibibyte about 1073,000,000 bytes (2 to the power of 30)
can cpu or mem exceed the limit
CPU not, it is throttled. Mem yes, in the end it will be terminated with OOM (OOM kill)
what is the resource require and limit by default
best CPU configure:
no limit
with requests but no limit
when no request but limit set? what is request then?
request = limit set automatically by k8s
set default resource limit request globally for all newly created pod
limitRange object
restrict the total amount of resource
resourceQuota object
like hard limit/requests in the current namespace
check reason of failed pod
describe po and check last state and Reason: is there
default sa what is it? There’s also way to disable the mounting of sa token
it is automatically mounted to the pod of the ns, it has very restricted permission to run only basic kubectl cmd query. automountSAToken: false in spec
how to change the sa of a pod from default? of deployment?
change it in the spec and recreate! For deployment, no need to recreate, just edit
latest 1.24 sa token is not automatic created, how to create it?
k create token sa-name, it has by default 1h expiry time
or
(not recomanded, no time boundary)
create a kubernetes.io/s-a-token type secret for the account as before
check token of a sa
check taint of a no
describe it and check tokens:
describe and check taints:
check sa of a pod/deploy
describe the pod and find Service Account:
change sa of deploy from default
go to spec/template/spec/ add ServiceAccountName: sa-name
taint nodes
untain no
k taint no no-name app=blue:taint-effect
add a minus in the end
what is restricted by the taint and tolerants
it only restrict the no. A pod with a matched toleration will not guaranteed to be scheduled on the tainted node
check where is the po
-o wide
node-selector
label a node
nodeSelector in pod.spec
(very simple only one label: value)
k label no no-name key=value
node affinity
ensure a pod is hosted on a particular node. More advanced than nodeSelector
requiredDuringSchedulingIgnoredDuringExectution
during schedule of pod, must find the matched node, if not found, don’t schedule
During the pod execution, if node’s label is changed so that the condition doesn’t match any more, ignore it. if required is defined, pod will be evicted.
multi-container pods
logging agent + web server they need to share same lifecycle (volumn, storage, netware)
multi-container pod
sidecar (logging server +web server),
adapter: before sending logs to a central server, we adapt the log in to a unified format
ambassador: to connect to different stage db, you may choose to outsource such logic to separate container, such as at local host it connects to a local database, and the new container will proxy that request to other right db
check pod conditions
k describe po
check conditions section
readiness probes
check if a pod’s ready status is really true or false. it is application relevant, e.g. http test /api/ready. or if a particular TCP socket is listening or just exec a custom script
liveness probes
check if a container is health.
http test - /api/healthy or if a particular TCP socket is listening or just exec a custom script
docker run -d event-simulator
detach mode without output the log
print log of multi-container pod
k logs -f po-name container-name
metrics server
- one for each k8s cluster
- no historical data, only in-memory
with metrics-server, what can do
k top node
k top po
get things based on label
get all pod’s label
k get po –selector app=App1
k get po –l app=App1
k get po –show-labels
annotation
used to record other details for informationary purpose, phone numbers etc. or may be for other integration purpose
check status of each revision
kubectl rollout history deployment nginx –revision=1
record cause:
kubectl set image deployment nginx nginx=nginx:1.17 –record
edit deploy
kubectl rollout status deployment nginx
kubectl rollout history deployment nginx
set deploy image
k set image deploy frontend simple-webapp=kodecloud/webapp-color:v2