Tag Archives: kubernetes
Docker and Kubernetes basics for developers
Docker Docker containers are much more lightweight compared to classic VMs as they leverage host OS instead of starting their own OS. Containers are using 2 features of Linux-based OS: Namespaces and Cgroups (Control Groups). Namespace Lets you allocate resources in an isolated environment (like a sandbox). On a container start, docker daemon generates a …
Using tcpdump for k8s pods
You need wireshark to be installed on your local machine — download (more on this below). If you don’t have tcpdump installed inside the pod, you can install it with
1 2 3 4 5 |
# get pod name kubectl get po -Lversion,cfg,infra-cfg -lapp=<pod-tag> kubectl exec -it <pod-name> sh # inside the pod, alpine apk add tcpdump |
Then, on your local machine:
1 |
kubectl exec <pod-name> -- tcpdump -i eth0 -w - | wireshark -k -i - |
If you want to see packets in grpc/http2, you can add a rule in Wireshark. For this, click …
Forward multiple kubernetes pods for local development
kubefwd is a great tool that can forward a lot of pods at once to your local machine. The only downside for me is that it touches your local /etc/hosts. That’s how I use it:
1 |
sudo KUBECONFIG=<path to your k8s config> kubefwd svc -l "app in (first-api, second-api, third-service)" |
It does not have huge documentation, but it is written in golang, and you can check how the source …
How to update your k8s pod limits
1 |
kubectl edit deployment ←name-of-your-app→ |
Then search for memory (for instance) and update request/limit for it. When you quit, it will be saved automatically for you.
Port forwarding with kubectl
If you want some kubernetes pod to forward all the traffic to you local machine, you can do this: 1. get pod id with
1 |
kubectl --kubeconfig <config> get po -lapp=<pod_label_app> -Lversion,cfg,infra-cfg |
2. forward exact port from it to you localhost with
1 |
kubectl --kubeconfig <config> port-forward <pod-id> localhost_port:pod_port |
And you can request you localhost to reach pod on selected port.
Kubernetes’ cronjobs
That’s what it is — https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/ Get all cronjobs in env:
1 |
kubectl --kubeconfig ... --namespace platform -o wide get cronjobs |
Get info on exact cronjob:
1 |
kubectl --kubeconfig ... --namespace platform describe cronjob/smartrouting-cron-import-from-bi |
Get pods with this cronjob:
1 |
kubectl --kubeconfig ... --namespace platform -o wide get pods|grep "rou" |
Get logs of cronjob
1 |
kubectl --kubeconfig ... --namespace platform logs smartrouting-cron-import-from-bi-1555060200-dqg5j |
you can add -f for live updating logs and -p for watching logs of previous pod.
Kubectl helpful commands
k8s cheatsheet. Get all pods by label
1 |
$ kubectl --kubeconfig <config file> get po -lapp=<pod name without hash> -Lversion,cfg,infra-cfg |
Exec into the pod
1 |
$ kubectl --kubeconfig <config file> exec -it <pod name> sh |
Port forwarding
1 |
$ kubectl --kubeconfig <config file> port-forward <pod name> <local port>:<remote port> |
Getting logs
1 |
$ kubectl --kubeconfig <config file> logs <pod name> <pod name without hash> |
Get replica limits from configs:
1 2 3 4 5 |
$ kubectl --kubeconfig <config file> get hpa <pod name without hash> # more details $ kubectl --kubeconfig <config file> get deployment <pod name without hash> --output=yaml $ kubectl --kubeconfig <config file> describe deployments <pod name without hash> $ kubectl --kubeconfig <config file> describe pod <pod name without hash> # CPU and memory limits/requests (quota) |
Remove pod from load balancing to debug startup errors:
1 2 3 |
$ kubectl --kubeconfig <config file> label pod <pod_name> <labels to remove> # and remove (kill the pod) afterwards $ kubectl --kubeconfig <config file> delete pod <pod_name> |
Scale (up/down) pods:
1 2 3 4 |
$ kubectl --context=xx-live-default get pods -l app=boblin -o wide $ kubectl --context=xx-live-default scale --current-replicas=12 --replicas=3 deployment/boblin $ kubectl --context=xx-live-default delete pods boblin-3353777365-qjdpd $ kubectl --kubeconfig <config file> scale deployment <pod name without hash> --current-replicas=0 --replicas=2 |
1 2 3 4 |
$ kubectl --kubeconfig <config file> get po -lapp=<label> -Lversion,cfg $ watch -n 1 kubectl --kubeconfig <config file> get po -lapp=<label> -Lversion,cfg $ kubectl --kubeconfig <config file> exec <exact pod hash> sh $ kubectl --kubeconfig <config file> get hpa <label> |
More commands here: https://kubernetes.io/docs/reference/kubectl/overview/ https://kubernetes.io/docs/reference/kubectl/cheatsheet/