Open Questions
- As
ConfigMap
s are scoped in its namespace, how to haveConfigMap
s in multiple namespaces without duplication?
- One option is using high level of abstraction such as
cdk8s
Networking
-
Container
Container
communicates with anothercontainer
in the samePod
overlocalhost
.- As a
Pod
behaves like a virtual host,containers
behave like applications,containers
of the samePod
can't listen on the sameport
. - By default,
containers
can use a port of thePod
without explicitly specifying it, andpo.spec.containers.ports.containerPort
is only documentation. Container
crashes and restarts doesn't changePod
'sIP address
.
-
Pod
-
Each
Pod
has its ownnetwork namespace
(opens in a new tab), behaving as if a virtual host. -
A
Pod
gets assigned anIP address
(ClusterIP
) on creation and loses it on deletion. -
Pod
toPod
communication- Same cluster: use
cluster IP
Pod
orService
- Different cluster: use
Service
- Same cluster: use
-
-
Port
-
po.spec.containers.ports.containerPort
- Informational purpose, specifying which
port
of thePod
thecontainer
is using. - The port being unspecified does not stop it from being published by the container, e.g.
nginx
uses port 80 on thePod
no mattercontainerPort: 80
is specified or not.
- Informational purpose, specifying which
-
po.spec.containers.ports.hostPort
- Only bound on
nodes
that are actually running thePod
- Only bound on
-
svc.spec.ports.nodePort
- Bound on all
nodes
- Bound on all
-
svc.spec.ports.port
- The port of the
Service
- The port of the
-
svc.spec.ports.targetPort
- Should match
po.spec.containers.ports.containerPort
- If unspecified, the value of the
svc.spec.ports.port
will be used.
- Should match
-
-
Resources
Kubernetes Object Manifest
spec
What state you desire for the object
-
- Start with a
!
to exclude a label, such askubectl get pods -l '!app'
- Start with a
-
initContainers
Init containers
are always executed before the main application containers, regardless of the definition order in the manifest.
-
spec.containers[].command
Override the
ENTRYPOINT
instruction ofDockerfile
of the container image -
spec.containers[].args
Override the
CMD
instruction ofDockerfile
of the container imageTwo forms of array can be used for
command
orargs
.-
Array
Array form needs more typing as every argument has to be quoted, but therefore also supports special characters better.
args: ["wget", "-O", "-", "google.com"]
-
List
- Some arguments of special characters or numbers must be quoted. Rule of thumb: if any argument doesn't contain letter, it needs to be quoted.
args: - wget - -O - "-" - google.com
-
Kubernetes Resource
Label
List resources with specific labels regardless of values
-
k get po -l environment,tier
-
Equality-based requirement
kubectl get pods -l
environment=production,tier=frontend
-
Set-based requirement
Must be single-quoted
kubectl get pods -l
'environment in (production),tier in (frontend)'
-
Two forms can be used together
kubectl get pods -l
'environment in (production),tier in (frontend)',tier=frontend
Pod
-
Probe
-
Readiness Probe
- When the probe returns
Yes
, thePod
will start receiving incoming traffic. - A
Pod
is consideredready
when all of its containers are ready. - When a
Pod
is notready
, it is removed fromService
load balancers.
- When the probe returns
-
Liveness Probe
- When the probe returns
No
, thePod
will be restarted. - Configure
initialDelaySeconds
when using aliveness probe
, as aliveness probe
failure causes thePod
to restart. You need to make sure the probe doesn’t start until the app is ready.
- When the probe returns
-
Startup Probe
- When the probe returns
Yes
,Readiness
andLiveness
probes will be enabled. - Only executed
at startup
- The
kubelet
usesstartup probes
to know when a container application has started. If such a probe is configured, it disablesliveness
andreadiness
checks until it succeeds, making sure those probes don't interfere with the application startup.
- When the probe returns
-
-
Node affinity
Taints
apply tonodes
, whileaffinity
rules apply toPods
.
ConfigMap
-
Usage
-
Environment variables
for acontainer
-
Add a file in read-only
volume
, for the application to read -
Write code to run inside the
Pod
that uses theKubernetes API
to read aConfigMap
- You have to write code to read the
ConfigMap
and its data. However, because you're using theKubernetes API
directly, your application can subscribe to get updates whenever theConfigMap
changes, and react when that happens. By accessing theKubernetes API
directly, this technique also lets you access aConfigMap
in a differentnamespace
.
- You have to write code to read the
-
-
Sources
-
Literal values, which are
key-value pairs
as plain text--from-literal=<KEY>=<VALUE>
-
A file that contains
key-value pairs
and expects them to beenvironment variables
--from-env-file=config.env
Note: A running
Pod
must be destroyed and recreated for the updatedenvironment variables
to take effects. -
A file with arbitrary contents
--from-file=config.txt
-
A directory with one or many files
--from-file=<CONFIG_DIR>
-
Job
-
job.spec.activeDeadlineSeconds
The
activeDeadlineSeconds
applies to the duration of thejob
, no matter how manyPods
are created.Once a
Job
reachesactiveDeadlineSeconds
, all of its runningPods
are terminated and theJob
status will becometype: Failed
withreason: DeadlineExceeded
. -
job.spec.backoffLimit
Specify the number of retries before considering a
Job
as failed.
CronJob
-
A
CronJob
creates aJob
object about once per execution time of its schedule. -
cj.spec.startingDeadlineSeconds
The grace period for how long can elapse after the scheduled time without being count as a missed job
Deployment
-
A
Deployment
ensures a desired state is maintained at any given time. -
By default, a
Deployment
is created with a labelapp=<DEPLOYMENT_NAME>
. -
spec.selector.matchLabels
must be the same asspec.template.metadata.labels
. -
Strategies
-
Recreate
All existing
Pods
are killed before new ones are created. Successful removal is awaited before anyPod
of the new revision is created.+
More economical-
Downtime
-
RollingUpdate
+
No downtime-
More instances, more costs
-
PersistentVolume
-
spec.capacity
-
spec.accessModes
ReadWriteOnce
ReadOnlyMany
ReadWriteMany
-
StorageClassName
Must match that of the intended
PersistentVolumeClaim
PersistentVolumeClaim
Pods
need to claim aPV
by usingPVC
, which states the storage requirements but not the volume implementation.- The binding to an appropriate
PersistentVolume
happens automatically based on the criteria defined inPersistentVolumeClaim
manifest. PersistentVolumeClaim
serves as the middle layer to decouplePod
s andPersistentVolume
s
ReplicaSet
-
A
ReplicaSet
ensures that a specified number ofpod
replicas are running at any given time. -
Deployment
is preferred overReplicaSet
. -
Use
kubectl scale
command to manually scale up and down replicas
ResourceQuota
-
Resource Unit
One
CPU is equal to1000 millicores
.- Memory is measured in
bytes
. - Kubernetes accepts both
SI
notation andBinary
notation for memory definition.SI
notation (K,M,G,T,P,E
)Binary
notation (Ki,Mi,Gi,Ti,Pi,Ei
)
-
If a
Pod
requests moreCPU
orMemory
than any node has, it will never be scheduled. -
A container using more CPU than allowed will not be killed but throttled.
-
A container using more memory than allowed may be restarted or evicted.
-
Type
Secret
-
Create a Secret
Secret value must be in plain text, kubernetes will encode it with base64.
-
Create a Secret from literal(s)
-
value: unencoded plain text
-
k create secret generic --from-literal=
key=
value
-
-
Create a Secret from file(s)
-
key: file name (including extension), can be overridden by explict key
-
value: file content (unencoded plain text)
-
k create secret generic --from-file=
file-name
-
-
Create a Secret from file(s)
-
Similar to creation from files, and all files under the specified directory will be used for creation.
-
k create secret generic --from-file=
dir-name
-
-
-
Get the contents of a
Secret
k get secret <secret_name> -o jsonpath='{.data}' | jq
-
Get the
base64
encoded token:kubectl get secret <secret_name> -o yaml
-
Get the
base64
decoded token:-
kubectl get secret <secret_name> -o yaml
-
Copy & paste the encoded secret string
-
Use
base64 -d
to decode the secret
-
Ingress
-
Resources
-
IngressClass
- Usually installed as part of
Ingress controller
installation, used to register the associatedIngress controller
into the cluster. - Global resource
- Usually installed as part of
-
Ingress
Ingress
resources define routing rules and config. Will only take effect ifIngress controller
is already deployed)Namespaced
, and must reside in the namespace where the app that they configure reside.- The IP of an
Ingress
(ing.status.loadBalancer.ingress.ip
) is the external IP of the load balancer, therefore the entrypoint. - When multiple
IngressClass
exist or no defaultIngressClass
specified,Ingress
must explicitly choose anIngressClass
withing.spec.ingressClassName
. ing.spec.rules.host[]
specify theFQDN
expression which requests are matched against.
-
-
Ingress controller
is essentially a separatePod/Deployment
along with aService
that implementsIngress
functionality behind the scene such as routing and proxying. -
Ingress
is able to expose low-numbered ports.Services
have been hard-coded not to expose ports lower than 1024. -
Conflicting
Ingress rules
will causeIngress
resource not getting IP address. -
If
Ingress
resource has noIngressClass
, specify one by usinging.spec.ingressClassName
. AvailableIngressClass
can be retrieved withk get ingressclass
.
Service
-
A
Service
is an abstraction layer which defines a logical set ofPods
and enables external traffic exposure, load balancing and service discovery for thosePods
. -
Type
-
ClusterIP
Exposes the
Service
on a cluster-internalIP
. Choosing this value makes theService
only reachable from within the cluster. This is the default value.Service
optionclusterIP: None
lets theService
DNS
name resolve directly to thePod
'sIP
address. This is optimal when you have only onePod
behind aService
and you don't intend to increase the number ofPod
s. -
NodePort
A
Service
of the typeNodePort
includes the functionality of the typeClusterIP
. Traffic is routed from theNodePort
to theClusterIP
. Only high ports can be used.-
Obtain Node IP
minikube
:minikube ip
-
-
LoadBalancer
A
Service
of the typeLoadBalancer
includes the functionality of the typeNodePort
.Exposes the
Service
externally using a cloud provider's load balancer.NodePort
andClusterIP
Services
, to which the external load balancer routes, are automatically created. -
ExternalName
A special type of service without selectors, is used to point to an external
DNS
server.Use of the service returns a
CNAME
record.
-
-
Selector
- Select any
Pod
s having the specifiedLabels
(intersection)
- Select any
-
-
Services
inKubernetes
expose their endpoint using a commonDNS
pattern:<Service Name>.<Namespace Name>.svc.cluster.local
-
Normally, the
Service
name is adequate. If you need to access aService
in anotherNamespace
just use theService
name plus theNamespace
name. -
Service
name must beDNS
compliant, which is lower case alphanumeric characters or-
, start with an alphabetic character, and end with an alphanumeric character.
-
-
Expose a
Service
for aResoruce
-
Use
kubectl expose
command, with following flags:--name
: aDNS
compliant name--type
: the type ofService
, if not explicitly set,ClusterIP
would be used.--port
: theService
port--target-port
: target port on containers
Note: the
Resource
being exposed must already exist -
Use
kubectl create service <service_name> <service_type>
command, with following flags:--tcp=<port>:<targetPort>
: theService
port and target port on containers
and edit the manifest or
Resource
:spec.selector
must be the same asDeployment
'sspec.selector.matchLabels
-
-
Resources
EndPoint
- An
EndPoint
is created whenselector
of aService
finds matchingPod
s, and noEndPoint
will be created if no matchingPod
exists. - An
EndPoint
IP address is aPod
IP address, meaning traffic will be directed byService
to thisPod
. Service
controlsEndpoint
. If anEndPoint
is deleted, a new one will be created byService
. If aService
is deleted, the correspondingEndPoint
will be deleted too.- Use
EndPoint
to check if theService
has found any matchingPod
to serve traffic.
NetworkPolicy
-
Must install a Network Policy provider (opens in a new tab) first for
NetworkPolicy
to work -
You can only write rules that allow traffic.
spec: podSelector: {} # empty selector selects all Pods ingress: [] # empty array indicates nothing is whitelisted
-
NetworkPolicy
are scoped to thenamespace
they are deployed to, sonetworkpolicy.spec.podSelector
does not selectPods
from othernamespaces
. -
By default,
Pods
are non-isolated; they accept traffic from any source.Pods
become isolated by having aNetworkPolicy
that selects them. Once there is anyNetworkPolicy
in aNamespace
selecting a particularPod
, thatPod
will reject any connections that are not allowed by anyNetworkPolicy
. OtherPods
in thenamespace
that are not selected by anyNetworkPolicy
will continue to accept all traffic. -
Network policies
do not conflict; they are additive. If any policy or policies select aPod
, thePod
is restricted to what is allowed by the union of those policies' ingress/egress rules. Thus, order of evaluation does not affect the policy result.
ClusterRole & ClusterRoleBinding
- Both
ClusterRole
andClusterRoleBinding
are global, not scoped to anamespace
ClusterRole
via aRoleBinding
is also confined to itsnamespace
.ClusterRoleBinding
can not referenceRole
s.ClusterRole
can define common permissions expressed in a singlenamespace
when referenced by aRoleBinding
. This removes the need to have duplicatedRole
s in manynamespace
s.
Role & RoleBinding
-
The permissions can only be given to the resources that are in the same
namespace
as theRole
and theRoleBinding
. -
A
RoleBinding
can only reference aRole
in the samenamespace
. -
But
subjects
of aRole
can be from a differentnamespace
. -
User Impersonation
kubectl <verb> <resource> --as=<user/serviceaccount>
-
Verify API Access
kubectl auth can-i <verb> <resource> --as=<user/serviceaccount>
ServiceAccount
-
If not assigned explicitly, a
Pod
uses the defaultService Account
. The defaultService Account
has the same permissions as anunauthenticated user
. This means that thePod
cannot view or modify the cluster state nor list or modify any of its resources. -
Specify a custom
Service Account
when running aPod
kubectl run nginx --image=nginx --serviceaccount=custom
-
The authentication token of
Service Account
with which aPod
is run can be found at the path/var/run/secrets/kubernetes.io/serviceaccount
in the containers. -
--as
global option--as=system:serviceaccount:{namespace}:{service-account-name}
StatefulSet
Node
Taint
kubectl
-
kubeconfig
(opens in a new tab)- A file that is used to configure access to
clusters
is called akubeconfig
file. This is a generic way of referring to configuration files. It does not mean that there is a file namedkubeconfig
. kubectl
command-line tool useskubeconfig
files to find the information it needs to choose a cluster and communicate with theAPI
server of acluster
.- Each
context
has three parameters:cluster
,namespace
, anduser
. - By default, the
kubectl
command-line tool uses parameters from thecurrent context
to communicate with thecluster
.
- A file that is used to configure access to
-
kubectl [command] [TYPE] [NAME] [flags]
-
command
-
create
- Not every
RESOURCE
type is supported to be created from command line. Use-h
to check supportedRESOURCE
.
- Not every
-
-
TYPE
TYPE
is case-insensitive and can be abbreviated, e.g. Pod:po
. Usekubectl api-resources
for full list of abbreviations.
-
-o wide
Display extra columns in output, such as
IP
,NODE
,SELECTOR
,IMAGES
, etc.
-
-
Use cases
-
Pod
-
Create and
run
(opens in a new tab) aPod
Using
run
command to create aPod
will assign thePod
a labelrun=<POD_NAME>
.-
Options
-
--labels
A comma-separated list of labels to apply to the
Pod
, eg:--labels="app=hazelcast,env=prod"
-
--port
The port to expose on the container, equivalent to
$.spec.containers[0].ports[0].containerPort
in object -
--env
-
Specify environment variables of the container
e.g.
--env="DNS_DOMAIN=cluster"
. -
To specify multiple environment variables use multiple
--env
optionse.g.
--env="DNS_DOMAIN=cluster"
--env="POD_NAMESPACE=default"
-
-
--command -- <COMMAND> <arg1> ... <argN>
Start the
Pod
using a different commandIf
--command
is present, the first argument follow--
will becomecommand
in manifest, and the remaining arguments will becomeargs
.If
--command
is absent, the following arguments will become the array ofargs
. -
--rm
If true, delete resources created in this command for attached containers.
Use this option to run a temporary one-off
Pod
-
--restart=Never
By default,
--restart=Always
, setting it toNever
will prevent aPod
from restarting.
-
-
-
-
-
JSONPath Support
- Use
double quotes
to quote text insideJSONPath
expressions. - Use the
range
,end
operators to iterate lists. - Use
negative slice indices
to step backwards through a list. Negative indices do not "wrap around" a list and are valid as long as-index + listLength >= 0
. - The
$
operator is optional since the expression always starts from the root object by default. - The result object is printed as its
String()
function. - JSONPath regular expressions are not supported.
e.g.
-
-ojsonpath='
{range .items[*]}
{.metadata.name}{","}{.spec.containers[0].image}{"\n"}{end}
'range
andend
defines a loop to iterate all eligible items. -
-ojsonpath='{range .items[*]}
{.metadata.name}
{","}{.spec.containers[0].image}
{"\n"}{end}'Use a JSONPath expression to specify an object field
-
-ojsonpath='{range .items[*]}{.metadata.name}
{","}
{.spec.containers[0].image}{"\n"}
{end}'Use any string as delimiter. Also, use line separator at the end of line. String must be double quoted.
- Use
-
Resources
kubectl - Cheatsheet
Display the current context
k config current-context
or
kccc
Switch context
Set the current-context
to CONTEXT_NAME
in a kubeconfig
file
k config use[-context] <CONTEXT_NAME>
or
kcuc <CONTEXT_NAME>
Describe one or many contexts
k config get-contexts
or
kcgc
Show merged kubeconfig
settings
kubectl config view
Get Kubernetes
cluster information
kubectl cluster-info
List all available Resource
types
kubectl api-resources
List every instance of every Resource
type in a specified Namespace
kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found -n <NAMESPACE>
List all namespaces
kubectl get ns
# or
kgns
List resources filtered by the specified namespace
kubectl get <...RESOURCE> -n <NAMESPACE>
List resources across all namespaces
kubectl get $RESOURCE_TYPE1, $RESOURCE_TYPE2, ... -A
Create a namespace
kubectl create ns $NAMESPACE
Change current namespace
k config set-context --current --namespace=$NAMESPACE
Change current namespace
of a specified context
kubectl config set-context <CONTEXT_NAME> --namespace=<NAMESPACE>
Delete a namespace
kubectl delete $NAMESPACE
Note: deleting a namespace
also deletes all resources
in it.
List resources filtered by the specified Label
-
kubectl get all -l <KEY1>=<VALUE1>[,<KEY2>=<VALUE2>]
kubectl get all -l '<KEY> in (<VALUE1>, <VALUE2>...)'
List Resources
with their Labels
kubectl get all --show-labels
List Resources
with specified Label
s as additional columns
kubectl get all -L <LABEL_KEY_1>[,<LABEL_KEY_2>...]
Pretty print all Label
s of a specified Resource
kubectl get <TYPE> <RESOURCE> -o jsonpath='{.items[*].metadata.labels}' | jq
Update existing Label
kubectl label <TYPE> <RESOURCE> <KEY1=VALUE1...> --overwrite
Remove existing Label
kubectl label <TYPE> <RESOURCE> <KEY_1>-
List resources
-
kubectl get <...RESOURCES>
e.g.
kubectl get <..po,svc|all>
Get specified resource
details
kubectl describe <RESOURCE_TYPE> <RESOURCE_NAME_PREFIX>
Get documentation for a field in manifest
-
kubectl explain <RESOURCE>[.<FIELD>]...
-
e.g.
kubectl explain po.spec.containers.volumeMounts.mountPath
-
e.g.
kubectl explain po.spec.containers.envFrom.configMapRef | grep -P '\s+<.+>'
Print all fields of the specified field.
-
e.g.
kubectl explain po.spec --recursive | vim -R -
Print all fields of the specified field recursively, and open with
vim
for easier navigation, which should be used as the main manifest reference method.
-
Print Events
of all Resources
in the current Namespace
kubectl get ev
Update Kubernetes
object
-
Use
kubectl edit
directly update resourcesSet up
KUBE_EDITOR
before using the command.In the event of an error, the changes would be lost.
-
Edit and save local
manifest
, then usekubectl apply -f <manifest>
Less directly, but the changes are saved first.
-
Use
kubectl patch
-
kubectl replace --force -f <filename>
Delete and recreate the
Resource
specified in the manifest
Update annotations
-
kubectl annotate <TYPE> <RESOURCE...> <KEY1>=<VALUE1>...
e.g.
kubectl annotate po pod-1 pod-2 env=dev
Update labels
-
kubectl label <TYPE> <RESOURCE...> <KEY1>=<VALUE1>...
e.g.
kubectl label po pod-1 pod-2 env=dev
Apply all manifests in a directory recursively
kubectl apply -R -f <directory>
List all Pods
in the current namespace
kubectl get po
Get a running Pod
's manifest
-
kubectl get po <POD> -o yaml
Note:
-o yaml
can also be-oyaml
.
Use JSONPath
expression to extract information
kubectl get po <POD> -o jsonpath='{<JSONPATH_EXPRESSION>}'
Compares the current state of the cluster against the state that the cluster would be in if the manifest
was applied
kubectl diff -f <manifest>
Print the API
objects into manifest
file with dry run
kubectl run <POD> --image=<IMAGE:TAG> -o yaml --dry-run=client > pod.yaml
Execute a command in the container
-
Options
-
-c
Specify the
container
name to run the command in the case ofmulti-containers
-
Get logs from a Pod
-
Options
-
--all-containers=true
Get logs from all
containers
in thePod
-
-c
Specify a
container
name that you want to retrievelogs
fromThis can be omitted by directly appending container name:
kubectl logs <POD_NAME> <CONTAINER_NAME>
-
-f
Specify if the
logs
should be streamed
-
Delete a Pod
-
Options
-
--force
, false by defaultForce delete a
Pod
-
Run a command one-off
kubectl exec <POD> -- <COMMAND> <...args>
Open a shell interactively to a running Pod
kubectl exec <POD> -it -- <SHELL>
Attach to a running Pod
's main process
-
kubectl attach <POD> -it [-c <CONTAINER>]
When exiting, the
Pod
could be completed and restarted.
Copy a file out of a Pod
Path in Pod
cannot have leading slash.
-
kubectl cp <POD>:<SRC_PATH> <TARGET_PATH>
e.g.
kubectl cp b3:etc/passwd ./passwd
Forward a port on a Pod
to a port on localhost
kubectl port-forward <RESOURCE_NAME> <LOCALHOST_PORT>:<SERVICE_PORT/POD_PORT>
List revisions
kubectl rollout history deployment <DEPLOYMENT_NAME>
Roll back to a previous revision
kubectl rollout undo deployment <DEPLOYMENT_NAME> --to-revision=<REVISION_NUMBER>
Create a Service
when running a Pod
-
kubectl run nginx --image=nginx --restart=Never --port=80 --expose
Note the
--expose
option
Expose a Service
for a specified Resource
kubectl expose <RESOURCE_TYPE> <RESOURCE_NAME> --type=LoadBalancer --port 80 --target-port 8080
Configure application resources
-
kubectl set <SUBCOMMAND>
Append the
--record
flag to save thekubectl
command that is making changes to the resource, because imperative commands do not keep changes.
Update environment variables
of a Resource
-
kubectl set env <RESOURCE_TYPE> <RESOURCE_NAME> KEY1=VALUE1 [KEY2=VALUE2] ...
You cannot change environment variables of a
Pod
, but you can change that of aDeployment
.
List all environment variables
across all instances of a Resource
type
-
kubectl set env <RESOURCE_TYPE> --all --list
If you want to inspect a specific instance, replace
--all
withResource
name.
Update image of a Resource
kubectl set image <POD/RC/DEPLOY/RS/DS> <Resource_name> <container_name>=<image>:<tag>
Observability
-
Test connectivity from inside of a container
kubectl run t --image=busybox -it --rm --restart=Never -- wget -O - <IP:PORT>
Production
-
Challenges
- Integration with cloud or on-premises infrastructure
- Management across teams, learning curve
Administration
-
Metrics
-
Metrics Server (opens in a new tab)
-
Enable
Metrics Server
forDocker Desktop
Insert
–kubelet-insecure-tls
into the existingargs
sectionargs: - --kubelet-insecure-tls
-
Query the
Metrics API
kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes/<NODE_NAME> | jq
kubectl get --raw /apis/metrics.k8s.io/v1beta1/namespaces/<NAMESPACE>/pods/<POD_NAME> | jq
-
View metrics snapshot
kubectl top node
-
-
Web UI (Dashboard) (opens in a new tab)
- Run:
kubectl proxy
(The command does not return) - URL: http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/ (opens in a new tab)
- Run:
-
Resources
-
Kompose
-
Translate Docker Compose to Kubernetes resources
Kustomize
-
Generate
kuztomized
manifestYAML
filekustomize build <directory containing kustomization.yaml>
-
Directly apply resource config
kustomize build <directory containing kustomization.yaml> | kubectl apply -f -
Using
kustomize
CLI is preferred tokubectl -k
, askustomize
is more up-to-date. -
Resources
Helm
-
Install
-
Setup
helm repo add stable https://charts.helm.sh/stable
-
Repo
-
Cheatsheet
-
Get the list of chart repositories
helm repo list
-
Search for charts in a repository
helm search <keyword>
-
List releases
helm list
-
Install a chart
helm install <release-name> <chart-name>
-
Download a chart archive without installing it
helm pull <repo/chart> -d <dir>
-
-
Pros
- The grouping of related Kubernetes manifests in a single entity (the chart)
- Basic templating and value support for Kubernetes manifests
- Dependency declaration between applications (chart of charts)
- A registry of available applications to be deployed (Helm repository)
- A view of a Kubernetes cluster in the application/chart level
- Management of installation/upgrades of charts as a whole
- Built-in rollback of a chart to a previous version without running a CI/CD pipeline again
-
Resources
Skaffold
-
Deploy
-
Kubectl
- By default,
k8s/*.yaml
are loaded for deployment.
- By default,
-
-
Build
-
Image pruning
skaffold dev --no-prune=false --cache-artifacts=false
- Remove image at the end every time
- Force the build of image
-
-
Resources
minikube
-
Limitations
- The
ingress
, andingress-dns
addons are currently only supported onLinux
.
- The
-
Troubleshooting
-
Sometimes newer versions of Kubernetes might not work, so in those use an earlier version might just work.
minikube start --kubernetes-version=1.24.8
Explicitly specify the Kubernetes version
-
-
Resources
Access a LoadBalancer Service in minikube cluster from host
-
Run
minikube tunnel
, and the terminal must stay open.This will open connection to
LoadBalancer
services. -
Update the
Service
type toLoadBalancer
if not. -
Run
kubectl get svc <service_name>
, and use theEXTERNAL-IP
to access the service from host.
Getting the IP
of minikube
node
minikube ip
Access Kubernetes Dashboard
minikube dashboard
Mount host path to the minikube
node
minikube mount <source directory>:<target directory>
Access host from within minikube
- Use domain name
host.minikube.internal
Network Policy Provider
- Calico:
minikube start --network-plugin=cni --cni=calico
Shell completion
minikube completion <SHELL>
Switch to minikube's Docker daemon
eval $(minikube -p minikube docker-env)
Switch back to host Docker daemon
eval $(minikube -p minikube docker-env -u)
Push an image to minikube's Docker daemon
-
minikube image load <image>:<tag> [--daemon]
This avoid the need to download images again.
minikube's Docker daemon
-
minikube
runs its own instance ofDocker
daemon process.Use
ps -fww -p $(pidof dockerd)
to check. Whileminikube
running, there should be 2dockerd
process, one ofminikube
, the other ofDocker engine
. -
Containers started by minikube Docker Daemon can be access from minikube host IP ( Can be retrieved by
minikube ip
)e.g.
d run --name=jenkins -p 8080:8080 -d jenkins/jenkins:lts-jdk17` curl -I http://$(minikube ip):8080
krew
Certifications
CKAD
-
Outline (2021 (opens in a new tab))
-
Application Design and Build – 20%
- Define, build and modify container images
- Understand Jobs and CronJobs
- Understand multi-container Pod design patterns (e.g. sidecar, init and others)
- Utilize persistent and ephemeral volumes
-
Application Environment, Configuration and Security – 25%
- Discover and use resources that extend Kubernetes (CRD)
- Understand authentication, authorization and admission control
- Understanding and defining resource requirements, limits and quotas
- Understand ConfigMaps
- Create & consume Secrets
- Understand ServiceAccounts
- Understand SecurityContexts
-
Application Deployment – 20%
- Use Kubernetes primitives to implement common deployment strategies (e.g. blue/green or canary)
- Understand Deployments and how to perform rolling updates
- Use the Helm package manager to deploy existing packages
-
Services and Networking – 20%
- Demonstrate basic understanding of NetworkPolicies
- Provide and troubleshoot access to applications via services
- Use Ingress rules to expose applications
-
Application Observability and Maintenance – 15%
- Understand API deprecations
- Implement probes and health checks
- Use provided tools to monitor Kubernetes applications
- Utilize container logs
- Debugging in Kubernetes
-
-
Shell setup
-
.bashrc
alias k=kubectl
alias kg='k get'
alias kga='kg all'
alias kgp='kg po'
alias kaf='k apply -f'
alias kex='k explain'
alias ke='k exec'
alias kc='k create'
alias kr='k run'
alias kd='k describe'
alias kl='k logs'
alias kdel='k delete'
alias kpf='k port-forward'
source <(kubectl completion bash)
complete -F __start_kubectl k
export EDITOR=vim
export KUBE_EDITOR=vim
export do="--dry-run=client -oyaml"
alias v=vim
-
.vimrc
se et
se ts=2
se shiftwidth=2
se nopaste
se is
se hls
-
Patterns
Multi-container Pods
-
Init container
po.spec.initContainers
defines init containers -
Adapter
-
Sidecar
-
Ambassador (opens in a new tab)
- As a proxy, responsible for splitting reads & writes, and sending them on to the appropriate servers
- Application just needs to open a connection on
localhost
and find the proxy without any service discovery. - Commonly used with databases
Resources
-
Official
-
Kubernetes.io - Overview of kubectl (opens in a new tab)
- Abbreviated aliases
-
Kubernetes.io - Kubernetes API Reference (opens in a new tab)
Kubernetes
Object
manifest reference- Alternative is using
kubectl explain
command
-
Guides and API References for Kubectl and Kustomize (opens in a new tab)
-
Spring
-
IBM Developer
-
Google Cloud - Your guide to Kubernetes best practices (opens in a new tab)
-
9 Best Practices and Examples for Working with Kubernetes Labels (opens in a new tab)
-
GitHub - ramitsurana/awesome-kubernetes (opens in a new tab)
A curated list for awesome kubernetes sources