Skip to content

Instantly share code, notes, and snippets.

@aimtiaz11
Last active November 6, 2020 07:33
Show Gist options
  • Save aimtiaz11/d093ea4989f9378f911fcc576b0a48c9 to your computer and use it in GitHub Desktop.
Save aimtiaz11/d093ea4989f9378f911fcc576b0a48c9 to your computer and use it in GitHub Desktop.
Kubernetes Tutorial Notes

Kubernetes Tutorial Notes

Notes taken from Kubernetes.io/docs/tutorials basics training + few personal notes.

Start Minikube: minikube start

Deployments

Create deployment

kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1

List deployments

kubectl get deployments

Delete Deployment

kubectl delete deployment kubernetes-bootcamp

Allow connection between host & cluster

kubectl proxy

The API server will create endpoint for each pod.

HTTP call from console to pod

After proxy created, run:

curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/

Rolling Deployment

Update deployment to new image

kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2

Verify deployment status (rollout)

kubectl rollout status deployments/kubernetes-bootcamp

Rollback

Rollback to previously working version.

kubectl rollout undo deployments/kubernetes-bootcamp

Pods

List and export name of each pod

kubectl get pods

or

export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME

Show finer details of each pod

kubectl describe pods

Show logs of a pod

kubectl logs $POD_NAME

Execute command in a pod

kubectl exec $POD_NAME env

Start a bash session inside pod

kubectl exec -ti $POD_NAME bash

Services

Service Types

  • ClusterIP (default) - Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.

  • NodePort - Exposes the Service on the same port of each selected Node in the cluster using NAT. Makes a Service accessible from outside the cluster using :. Superset of ClusterIP.

  • LoadBalancer - Creates an external load balancer in the current cloud (if supported) and assigns a fixed, external IP to the Service. Superset of NodePort.

  • ExternalName - Exposes the Service using an arbitrary name (specified by externalName in the spec) by returning a CNAME record with the name. No proxy is used. This type requires v1.7 or higher of kube-dns.

List services

kubectl get services

Create new service

kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080

Describe a service

kubectl describe services/kubernetes-bootcamp

Extract NodePort into a variable

export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')

echo NODE_PORT=$NODE_PORT

Labels

Find out label from deloyment

kubectl describe deployment

List pods by label

kubectl get pods -l run=kubernetes-bootcamp

Add label to a pod

kubectl label pod $POD_NAME app=v1

Scale

Scale pods

Create 4 replicas of a pod

kubectl scale deployments/kubernetes-bootcamp --replicas=4

ConfigMap & Secrets

Use ConfigMap and Secrets to store key-value environment variable data and credentials.

Create ConfigMap (from literal)

This command deploys a ConfigMap named sys-app-name to your cluster. It has a key called name with a value of my-system. The --from-literal flag allows you to specify individual key-value pairs to store in this ConfigMap.

kubectl create configmap sys-app-name --from-literal name=my-system

Create Secret (From literal)

kubectl create secret generic sys-app-credentials --from-literal username=bob --from-literal password=bobpwd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment