Kubenetes Cheatsheet

Kubenetes notes from learning course: https://www.youtube.com/watch?v=X48VuDVv0do&t=2886s
Thanks TechWorld with Nana

Pre Requirements

  • minikube installed
  • kuberctl installed

Get nodes

1
2
3
4
~$ kubectl get nodes

NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 47h v1.21.2

Create deployment using nginx dockerhub image

1
~$ kubectl create deployment nginx-deployment --image=nginx

Get deployment

1
2
3
4
~$ kubectl get deployment

NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 1/1 1 1 57s

Get pod

1
2
3
4
~$ kubectl get pod

NAME READY STATUS RESTARTS AGE
nginx-deployment-84cd76b964-lx2zk 1/1 Running 0 101s

Edit deployment config file

1
2
3
kubectl edit deployment nginx-deployment

You will see one auto-generated config file for this deployment, you can update image, replicas and others, it will auto apply by kubernates

Update replicas to 2

  • After few seconds the new pod will be created and STATUS become running
1
2
3
4
5
6
7
8
9
~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-84cd76b964-bk8pc 0/1 ContainerCreating 0 4s
nginx-deployment-84cd76b964-lx2zk 1/1 Running 0 6m57s

~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-84cd76b964-bk8pc 1/1 Running 0 38s
nginx-deployment-84cd76b964-lx2zk 1/1 Running 0 7m31s

Show pod logs

1
kubectl get logs nginx-deployment-84cd76b964-lx2zk

Delete deployment

1
kubectl delete deployment nginx-deployment

kubectl apply yaml file

nginx-depl.yaml file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-depl
labels:
app: nginx-depl
spec:
replicas: 1
selector:
matchLabels:
app: nginx-depl
template:
metadata:
labels:
app: nginx-depl
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80

1
kubectl apply -f nginx-depl.yaml

Kubenetes configuration file

  • metedata
  • specification
  • status - addtional, will be auto generated and controled by kubenetes

install hyperhit and minikube

brew update

brew install hyperkit

brew install minikube

kubectl

minikube

create minikube cluster

minikube start --vm-driver=hyperkit

kubectl get nodes

minikube status

kubectl version

delete cluster and restart in debug mode

minikube delete

minikube start --vm-driver=hyperkit --v=7 --alsologtostderr

minikube status

kubectl commands

kubectl get nodes

kubectl get pod

kubectl get services

kubectl create deployment nginx-depl --image=nginx

kubectl get deployment

kubectl get replicaset

kubectl edit deployment nginx-depl

debugging

kubectl logs {pod-name}

kubectl exec -it {pod-name} -- bin/bash

create mongo deployment

kubectl create deployment mongo-depl --image=mongo

kubectl logs mongo-depl-{pod-name}

kubectl describe pod mongo-depl-{pod-name}

delete deplyoment

kubectl delete deployment mongo-depl

kubectl delete deployment nginx-depl

create or edit config file

vim nginx-deployment.yaml

kubectl apply -f nginx-deployment.yaml

kubectl get pod

kubectl get deployment

delete with config

kubectl delete -f nginx-deployment.yaml

#Metrics

kubectl top The kubectl top command returns current CPU and memory usage for a cluster’s pods or nodes, or for a particular pod or node if specified.