Skip to content
On this page

Kubernetes

Kubernetes

Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications.

Installation

Docker for Mac

Recently Kubernetes has been included directly on the Docker app, you can activate Kubernetes on Preferences → Kubernetes → Enable Kubernetes:

Docker Kubernetes

Brew

bash
brew install kubernetes-cli

Useful commands

A Cheat Sheet can be found on the official website for more detailed information.

Cluster info

bash
kubectl cluster-info

Context

bash
kubectl config get-contexts
kubectl config set-context <context>

Namespace

bash
kubectl config set-context --current --namespace=<namespace>

Proxy

We can have a connection between our host and the Kubernetes cluster.

bash
kubectl proxy
curl http://localhost:8001/version

Nodes

bash
kubectl get nodes

Deployments

Deploy containerized applications.

Create update instances of your application.

Once the application instances are created, a Kubernetes Deployment Controller continuously monitors those instances. If the Node hosting an instance goes down or is deleted, the Deployment controller replaces the instance with an instance on another Node in the cluster.

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

Pods

Kubernetes abstraction that represents a group of one or more application containers (ex Docker) and shared resources for those containers. The resources include:

  • Shared storage, as Volumes
  • Networking, as unique cluster IP address
  • Information about how to run each container, such as the container image verison or specific ports ot use

A Pod always run on a Node.

Pods information

bash
kubectl get pods
kubectl describe pods

Pod name

bash
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME
bash
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/

Logs

bash
kubectl logs $POD_NAME

Execute command

bash
kubectl exec $POD_NAME env
kubectl exec -ti $POD_NAME bash

Services

Required to access to a deployment.

Services match a set of Pods using labels and selectors, a grouping primitive that allows logical operation on objects in Kubernetes.

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

# NGINX example with LoadBalancer
kubectl expose deployment nginx --port=80 --type=LoadBalancer
bash
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
bash
curl $(minikube ip):$NODE_PORT
bash
kubectl describe deployment

Labels

bash
kubectl get pods -l run=kuberenetes-bootcamp
bash
kubectl get services -l run=kuberenetes-bootcamp
bash
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME
bash
kubectl get pods -l run=kuberenetes-bootcamp
bash
kubectl get pods -l run=kuberenetes-bootcamp
bash
kubectl get pods -l run=kuberenetes-bootcamp
Apply label
bash
kubectl label pod $POD_NAME app=v1
kubectl describe pods $POD_NAME
kubectl get pods -l app=v1

Delete

bash
kubectl delete service -l run=kubernetes-bootcamp
kubectl get services

Scaling

bash
kubectl get deployments
bash
kubectl scale deployments/kubernetes-bootcamp --replicas=4
kubectl get deployments
bash
kubectl get pods -o wide
kubectl describe deployments/kubernetes-bootcamp

Load balancing

bash
kubectl describe services/kubernetes-bootcamp
bash
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
bash
curl $(minikube ip):$NODE_PORT

Rolling update

Update image

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

# Check status
kubectl rollout status deployments/kubernetes-bootcamp

Rollback update

bash
kubectl rollout undo deployments/kubernetes-bootcamp

kubectl

bash
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

Delete all

bash
kubectl delete all --all