Kubernetes cheatsheet and notes, wrote while learning and working on k8s related projects.
Table of Contents:
Basic Terms
some basic termnologies used by kubernetes
- cluster: physical cluster of physical machine.
- context: a group of access parameters, a cluster, a namespace, a user.
- namespace: virtual cluster, use to separate resources across different environments like prod, dev, test.
- node: a virtual/physical machine
- pod: a set of containers to orchestrate, minimal object to scale
- container: docker container runs in pod
- chart: a collect of files that describes your pod orchestration, that can be used to deploy a set of pods.
- Job: a wrapper to pod, usually runs a pod that carrying tasks that run only once, like db migration.
- Service: a wrapper to pod, one or more pods to perform a service.
- Volumes: file storage system, lives with node. Can define external volumes on cloud providers, lives with cloud providers.
- Label: to classify different kind of pods.
Manifests
manifests usually defined by .yaml or .json file, to manage (describe) resources.
- Deployment: deploy pods based on spec
- ReplicaSet: deploy pods as replica
- DaemonSet: deploy pods based on spec, ensure each node get one replica.
- HorizontalPodsAutoscaler(HPA): Autoscaler for pods, make sure the resources usage keeps at same level.(50% CPU/Memory Usage)
- Volume Claim Chain: To better manage a externel volume, to use a volume, must first claim it.
- Network Policy: Define a network policy to a set of pods(by label) like which set of pods(by label) or users(by namespace) can access.
- Secret: some secret you don't want others to know: ssh key, username/password
- Ingress:
Expose A Service
- Create a
Kind:Service
yaml, and define which pods your service is using (by using label)
There are 3 types of manifests to expose a service
NodePort: Assign the service to a port, access the service by port number. Ex:
<NodeIP>:<PortNumber>
, PortNumber is fixed, NodeIP could be any node that the service is running on. pods traffic is balanced in the same node, but not balanced in the different nodes . This is not good for production, but good for local testing by using kube-proxy.LoadBalancer: Similar to NodePort, but expose the all
<NodeIP>:<PortNumber>=
to a external load balancer. Node traffic is balanced.Ingress: Recommended way for production.
Tools
kubectl, kubeadmin, minikube, helm
Kubectl
Kubectl is a user tool to interact with the cluster
get info about the cluster (pod, service, deployment, replicaset, statefulset etc):
everything:kubectl get all
pods:kubectl get pods
nodes:kubectl get nodes
Note: can be filtered by namespace or label.get logs for a pod:
usage:kubectl logs -h
forward a request from a port:
usage:kubectl port-forward -h
Helm
Helm is like a manager of
charts
, you can use helm to pull, push, deploy a chart and modify (upgrade) a depolyment.pull repo from remote chart repository to local:
helm repo add [repoName] [RepoUrl]
helm update
deploy (install) chart from local repo:
helm install [repoName]/[chartName] [releaseName]
[releaseName] can be auto-generated using--generate-name
upgrade:
– get values.yaml of a chart, stores at values.yaml
helm inspect values [chartName] > newValues.yaml
– edit thenewValues.yaml
to describe the upgrade
– deploy the upgrade
helm upgrade [releaseName] [chartName] -f newValues.yaml
clean/remove the release:
helm uninstall [releaseName]
Kubeadm
Kubeadm is admin tool to setup a cluster
Minikube
Minikube: setup & run a single node cluster locally for testing and dev purpose.
- usage:
minikube -h
- usual workflow:
minikube start [computingResourceConfig]
minikube status
minikube stop
- usage:
Written with StackEdit.