Docker and Kubernetes
some docker and k8s commands I don't wanna search over internet
Docker ecosystem
docker-basics.sh
logger.sh
1
# create an image with a tag from git (courtesy of @ahmetb: https://twitter.com/ahmetb/status/1154882328813924352)
2
docker build -t website:${git describe --always --tags --dirty} .
3
4
# run container
5
docker run -p 3000:8080 -e NODE_ENV='production' --name website-prod \
6
--restart=always -d gcr.io/ivikramtiwari/website:prod
7
8
# access bash inside a running container
9
docker exec -it container-name bash
10
# remove all stopped containers
11
docker rm -v $(docker ps -a -q -f status=exited)
12
13
# stop and remove all docker containers
14
docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)
15
16
# remove all docker images
17
docker rmi $(docker images -a -q)
18
19
# prune docker system
20
docker system prune
21
22
# one liner for docker cleanup
23
docker stop $(docker ps -a -q) && \
24
docker rm $(docker ps -a -q) && \
25
docker rmi $(docker images -a -q) && \
26
docker system prune
Copied!
1
#!/bin/bash
2
function _exit {
3
kill $(jobs -p)
4
}
5
6
trap _exit EXIT
7
8
for name in $(docker ps --format "{{.Names}}"); do
9
eval "docker logs -f --tail=5 \"$name\" | sed -e \"s/^/[$name] /\" &";
10
done
11
12
wait
13
# to exit logging, bring the process in foreground and exit
14
# $ fg
15
# $ Command + C
16
17
# from https://stackoverflow.com/a/54917272/1724300
Copied!
Kubernetes
deployments-using-kubectl.sh
1
# all commands: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
2
3
# get credentials for your gcp cluster
4
gcloud container clusters get-credentials dev-cluster --zone=us-central1-f
5
6
# create a deployment on your cluster
7
kubectl run website-deployment --image=gcr.io/ivikramtiwari/website
8
9
# check if deployment was successful
10
kubectl get deployments
11
12
# get pods
13
kubectl get pods
14
15
# scale deployments
16
kubectl scale deployment website-deployment --replicas=4
17
18
# rolling updates
19
kubectl set image deployments/website-deployment website-deployment=gcr.io/ivikramtiwari/[email protected]:$SHA
20
21
# remove evicted pods
22
kubectl get pods | grep Evicted | awk '{print $1}' | xargs kubectl delete pod
23
24
# remove all pods
25
kubectl delete --all pods
26
27
# tail a deployment
28
kubectl logs deployment/website-deployment --tail 10 -f
29
30
# ssh into a pod
31
kubectl exec -it my-pod -- /bin/bash
Copied!
Last modified 2yr ago
Copy link