Skip to content

Instantly share code, notes, and snippets.

@vinkrish
Last active March 7, 2022 01:45
Show Gist options
  • Save vinkrish/3d5fd8ec5c4a742efebfeea6b15edc77 to your computer and use it in GitHub Desktop.
Save vinkrish/3d5fd8ec5c4a742efebfeea6b15edc77 to your computer and use it in GitHub Desktop.
LEARN KUBERNETES BASICS:
Create a Cluster:
minikube version
minikube start
kubectl version
kubectl cluster-info
kubectl get nodes
Deploy an app:
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
kubectl get deployments
curl http://localhost:8001/version
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/
Explore your app:
kubectl get - list resources
kubectl describe - show detailed information about a resource
kubectl logs - print the logs from a container in a pod
kubectl exec - execute a command on a container in a pod
kubectl get pods
kubectl describe pods
Pods are running in an isolated, private network - so we need to proxy access to them so we can debug and interact with them (in different terminal):
kubectl proxy
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/
kubectl logs $POD_NAME
kubectl exec $POD_NAME -- env
kubectl exec -ti $POD_NAME -- bash
Inside Container:
cat server.js
curl localhost:8080
exit
Expose your app:
kubectl get services
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
kubectl get services
kubectl describe services/kubernetes-bootcamp
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
Now we can test that the app is exposed outside of the cluster using curl:
curl $(minikube ip):$NODE_PORT
kubectl describe deployment
kubectl get pods -l app=kubernetes-bootcamp
kubectl get services -l app=kubernetes-bootcamp
Get the name of the Pod and store it in the POD_NAME environment variable:
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
To apply a new label we use the label command followed by the object type:
kubectl label pods $POD_NAME version=v1
kubectl describe pods $POD_NAME
kubectl get pods -l version=v1
kubectl delete service -l app=kubernetes-bootcamp
kubectl get services
curl $(minikube ip):$NODE_PORT
kubectl exec -ti $POD_NAME -- curl localhost:8080
Scale your app:
kubectl get rs
kubectl scale deployments/kubernetes-bootcamp --replicas=4
kubectl get deployments
kubectl get pods -o wide
kubectl describe deployments/kubernetes-bootcamp
kubectl describe services/kubernetes-bootcamp
Create an environment variable called NODE_PORT that has a value as the Node port:
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
Calling mulitple times should hit different pod:
curl $(minikube ip):$NODE_PORT
curl $(minikube ip):$NODE_PORT
curl $(minikube ip):$NODE_PORT
kubectl scale deployments/kubernetes-bootcamp --replicas=2
kubectl get deployments
kubectl get pods -o wide
Update your app:
To update the image of the application to version 2:
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
kubectl get pods
kubectl describe services/kubernetes-bootcamp
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
curl $(minikube ip):$NODE_PORT
kubectl rollout status deployments/kubernetes-bootcamp
kubectl describe pods
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
To roll back the deployment to your last working version:
kubectl rollout undo deployments/kubernetes-bootcamp
Run in different terminal:
minikube dashboard
minikube stop
minikube delete
---------------------------------------------------------------------------------
Kubernetes Procedures
Installing Kubernetes
Install etcd
docker run --volume=/var/etcd:/var/etcd --net=host -d gcr.io/google_containers/etcd:2.0.12 /usr/local/bin/etcd --addr=127.0.0.1:4001 --bind-addr=0.0.0.0:4001 --data-dir=/var/etcd/data
Install the master
sudo docker run \
--volume=/:/rootfs:ro \
--volume=/sys:/sys:ro \
--volume=/dev:/dev \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/var/lib/kubelet/:/var/lib/kubelet:rw \
--volume=/var/run:/var/run:rw \
--net=host \
--pid=host \
--privileged=true \
-d gcr.io/google_containers/hyperkube:v1.0.1 \
/hyperkube kubelet --containerized --hostname-override="127.0.0.1" --address="0.0.0.0" --api-servers=http://localhost:8080 --config=/etc/kubernetes/manifests
Run the service proxy
docker run -d --net=host --privileged gcr.io/google_containers/hyperkube:v1.0.1 /hyperkube proxy --master=http://127.0.0.1:8080 --v=2
Download kubectl
wget https://storage.googleapis.com/kubernetes-release/...
chmod +x kubectl
mkdir bin
mv kubectl bin
. .profile
Pod and Service Management
Create pod
kubectl create -f pod-file.yml
Create service
kubectl create -f service-file.yml
get pod info
kubectl list pod
Delete pod
kubectl delete pod podname
get service info
kubectl list serice
Delete pod
kubectl delete service servicename
Test an app
export SERVICE_IP=$(kubectl get service nginx-service -o=template -t={‌{.spec.clusterIP})
export SERVICE_PORT=$(kubectl get service nginx-service -o=template '-t={‌{(index .spec.ports 0).port}}')
curl http://${SERVICE_IP}:${SERVICE_PORT}
Cluster Commands:
kops create cluster --name=kubernetes.newtech.academy --state=s3://kops-state-b429b --zones=eu-west-1a --node-count=2 --node-size=t2.micro --master-size=t2.micro --dns-zone=kubernetes.newtech.academy
kops update cluster kubernetes.newtech.academy --yes --state=s3://kops-state-b429b
kops delete cluster --name kubernetes.newtech.academy --state=s3://kops-state-b429b
kops delete cluster --name kubernetes.newtech.academy --state=s3://kops-state-b429b --yes
Cheatsheet: Docker commands
Build image: docker build .
Build & Tag: docker build -t wardviaene/k8s-demo:latest .
Tag image: docker tag imageid wardviaene/k8s-demo
Push image: docker push wardviaene/k8s-demo
List images: docker images
List all containers: docker ps -a
Cheatsheet: Kubernetes commands
kubectl get pod: Get information about all running pods
kubectl describe pod <pod>: Describe one pod
kubectl expose pod <pod> --port=444 --name=frontend: Expose the port of a pod (creates a new service)
kubectl port-forward <pod> 8080: Port forward the exposed pod port to your local machine
kubectl attach <podname> -i: Attach to the pod
kubectl exec <pod> -- command: Execute a command on the pod
kubectl label pods <pod> mylabel=awesome: Add a new label to a pod
kubectl run -i --tty busybox --image=busybox --restart=Never -- sh: Run a shell in a pod - very useful for debugging
kubectl get deployments: Get information on current deployments
kubectl get rs: Get information about the replica sets
kubectl get pods --show-labels: get pods, and also show labels attached to those pods
kubectl rollout status deployment/helloworld-deployment: Get deployment status
kubectl set image deployment/helloworld-deployment k8s-demo=k8s-demo:2: Run k8s-demo with the image label version 2
kubectl edit deployment/helloworld-deployment: Edit the deployment object
kubectl rollout status deployment/helloworld-deployment: Get the status of the rollout
kubectl rollout history deployment/helloworld-deployment: Get the rollout history
kubectl rollout undo deployment/helloworld-deployment: Rollback to previous version
kubectl rollout undo deployment/helloworld-deployment --to-revision=n: Rollback to any version version
Abbreviations use:
Resource type: Abbreviated alias
configmaps: cm
customresourcedefinition: crd
daemonsets: ds
deployments: deploy
horizontalpodautoscalers: hpa
ingresses: ing
limitranges: limits
namespaces: ns
nodes: no
persistentvolumeclaims: pvc
persistentvolumes: pv
pods: po
replicasets: rs
replicationcontrollers: rc
resourcequotas: quota
serviceaccounts: sa
services: svc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment