Welcome! This doc will help you with your K8s journey. To get started to using K8s let's install the pre-requsites:
- Kubectl `brew install kubectl`
- Install Krew (Kubectl Plugin Manager)
``` bash
(
set -x; cd "$(mktemp -d)" &&
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/download/v0.3.4/krew.{tar.gz,yaml}" &&
tar zxvf krew.tar.gz &&
KREW=./krew-"$(uname | tr '[:upper:]' '[:lower:]')_amd64" &&
"$KREW" install --manifest=krew.yaml --archive=krew.tar.gz &&
"$KREW" update
)
```
Add $HOME/.krew/bin directory to your PATH environment variable. To do this, update your .bashrc or .zshrc file and append the following line:
``` bash
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
```
Restart your shell
- Install required Kubectl Plugins
- `kubectl krew install change-ns`
- `kubectl krew install auth-proxy`
- `kubectl krew install kubectx`
- Optional: Install the awesome kubectl aliases https://github.com/ahmetb/kubectl-aliases
- Update KubeConfig
``` bash
aws eks --region eu-west-1 update-kubeconfig --name beta-eks-cluster --profile dubizzle-testing
aws eks --region eu-west-1 update-kubeconfig --name space-eks-cluster --profile dubizzle-testing
```
- Before running any kubectl commands you need to run:
``` bash
aws-okta switch dubizzle-testing
```
- Opening Kubernetes Dashboard (recommendation: Create alias for it)
``` bash
kubectl auth-proxy -n kube-system https://kubernetes-dashboard.svc
```
- Switcing between beta environments
``` bash
kubectl change-ns beta-pro
```
- Getting Running Deployments (use this to get consumers or web apps that are deployed on k8s)
``` bash
kubectl get deployments
```
This is like going to beanstalk dashboard and seeing the running applications
If you are using aliases you can simply run `kgdep`
- Getting List of pods that are running (Pod is smallest unit in K8s. Everything in k8s mostly a POD)
``` bash
kubectl get pods
```
If you are using aliases you can simply run `kgpo`
This is like going to Ec2 dashboard and seeing the list of instances.
- Seeing Logs for your deployment
```
kubectl logs -f deployment/image-upload-service
```
If you are using aliases you can simply run `klo deployment/image-upload-service`
- Running commands in pod
```
kubectl exec <pod_name> <command>
```
you can do something like
```
kubectl exec -it <pod_name> bash
```
OR
```
kubectl exec -it <pod_name> sh
```
to get into pod for debugging purpose
- Scaling deployment
```
kubectl scale --replicas=5 deployments/<deployment-name>
```
- Changing Min and Max Capacity for deployment (Temporarily)
```
kubectl patch hpa <deployment-name>-hpa --patch '{"spec":{"minReplicas":1, "maxReplicas": 10}}'
```
- Getting resource utilization for pod `kubectl top pod <pod_name>`
- Adding New Cron or Background Job
- Clone https://git.naspersclassifieds.com/olx/dubizzle/infrastructure/k8s/helmfiles-apps
- Add cronjob or background job to the appropriate service_name/background_job|cron_job/<environment_name>.yaml
- Push to beta/<branch_name> and it will auto apply to beta
- To deploy to space open PR against staging branch and on merge deployment will happen. CI will comment on your PR with diff please wait for it.
- On merge it will apply the changes you made
- Repeat same for production
Please note if you want change to be permenent you should update the helmfile-apps repository.
## Troubleshooting
### No pods running for my deployment
- Not enough capacity to check run `kubectl top nodes`
- Pod keeps crashing
- Pod running out of resources. To check run `watch kubectl top pods`
- Command execution fails. To check run `kubectl describe pod <pod_name>` or `kubectl logs <pod_name> --all-containers`
- Invalid Vault Path, if pod fails during init it's usually due to incorrect vault path (if specified). Please check the vault in that case and if it's correct please talk to infra team.