Skip to content

Instantly share code, notes, and snippets.

@rstacruz
Last active March 16, 2023 05:25
Show Gist options
  • Save rstacruz/14db9e0d65f3540d73456c01a59c4eca to your computer and use it in GitHub Desktop.
Save rstacruz/14db9e0d65f3540d73456c01a59c4eca to your computer and use it in GitHub Desktop.
Learn Kubernetes in X minutes

Learn Kubernetes in X minutes

Nodes

A Kubernetes cluster has many nodes, or physical machines.

$ kubectl get nodes

  NAME             STATUS   ROLES    AGE   VERSION
  docker-desktop   Ready    master   48m   v1.14.7

Deployments

Deploying Docker images: Deploy images by creating a deployment. A deployment can contain Docker images and volumes.

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

  deployment.apps/bootcamp created

A Kubernetes cluster can have many deployments.

$ kubectl get deployments

  NAME       READY   UP-TO-DATE   AVAILABLE   AGE
  bootcamp   0/1     1            0           15s

Each deployment has a template, which defines what resources are going to be running in the deployment. The one we just created has a Docker image.

$ kubectl describe deployments/bootcamp

  Name:                   kubernetes-bootcamp
  Replicas:               1 desired | 1 total | 1 available
  ...
  Pod Template:
    Containers:
     bootcamp:
       Image:        gcr.io/google-samples/kubernetes-bootcamp:v1
       ...

Pods

Deployments will create pods. Each pod has an IP (172.18.0.2 in this example).

$ kubectl get pods -o wide

  NAME                        READY   STATUS    AGE     IP           NODE
  bootcamp-5b48cfdcbd-crkm8   1/1     Running   4m29s   172.18.0.2   minikube

A pod runs a deployment's resources.

$ kubectl describe pod bootcamp-5b48cfdcbd-crkm8

  Image:          gcr.io/google-samples/bootcamp:v1
  Port:           8080/TCP
  State:          Running
  ...

Getting inside pods

A pod is kind of a virtual machine, or a Docker container. You can run kubectl exec to run things inside it.

$ kubectl exec -it bootcamp-5b48cfdcbd-crkm8 -- bash

  root@bootcamp-5b48cfdcbd-crkm8:/# ps aux

    USER       PID  TIME  COMMAND
    root         1  0:00  /bin/sh -c node server.js
    root         7  0:00  node server.js
    ...

  root@bootcamp-5b48cfdcbd-crkm8:/# curl 127.0.0.1:8080

    Hello Kubernetes bootcamp! | Running on: ...

See: Documentation


Exposing apps using Services

Services will expose apps to the world. This will create a NodePort service, which exposes a port to the outside world.

$ kubectl expose deployment/bootcamp --type=NodePort --port 8080

The service we created exposes the guest 8080 to the world on port 31252 (this was randomly selected).

$ kubectl get services

  NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
  bootcamp   NodePort    10.96.71.9   <none>        8080:31252/TCP   2s

We can access this now from the node's public IP.

$ curl $(minikube ip):31252

  Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5b48cfdcbd-4vdjk | v=1

Scaling

Our deployment runs in one pod by default.

$ kubectl get pods

  NAME                        READY   STATUS    RESTARTS   AGE
  bootcamp-75bccb7d87-5xhkz   1/1     Running   0          18s

Use kubectl scale to increase number of pods.

$ kubectl scale deployments/bootcamp --replicas=4

  deployments.extensions/bootcamp staled

This should increase the number of running pods.

$ kubectl get pods

  NAME                        READY   STATUS    RESTARTS   AGE
  bootcamp-75bccb7d87-5xhkz   1/1     Running   0          18s
  bootcamp-75bccb7d87-6pbcj   1/1     Running   0          18s
  bootcamp-75bccb7d87-b4z65   1/1     Running   0          18s
  bootcamp-75bccb7d87-p7fhz   1/1     Running   0          3m15s

Updating your app

Using kubectl set image will initiate a rolling update on a deployment.

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

  deployment.extensions/bootcamp image updated
$ kubectl rollout status deployments/bootcamp

  deployment "bootcamp" successfully rolled out

Part 2

Configuration files

Kubernetes resources can be defined as YAML files. For example, instead of doing kubectl create deployment to create deployments, you can create them as a YAML file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  minReadySeconds: 5
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.7.9
          ports:
            - containerPort: 80

You can then apply this:

kubectl apply -f nginx-deployment.yml

You can even apply multiple files:

kubectl apply -f <directory>/

Managing config files

Updating: If you update config files, use diff to check for changes before applying them:

kubectl diff -f nginx-deployment.yml

Deleting: You can delete resources:

kubectl delete -f nginx-deployment.yml

Referring to resources

In this document, we often refer to resources in the syntax of deployment/nginx-deployment. They can be written in other notations too. All of these are the same:

$ kubectl delete deployment/nginx-deployment
$ kubectl delete deployment nginx-deployment
$ kubectl delete deploy nginx-deployment

Most commands that take resources can be substituted with files, so these two are the same:

$ kubectl expose deployment/nginx-deployment ...
$ kubectl expose -f nginx-deployment.yml ...
@kukula
Copy link

kukula commented Jan 10, 2020

💚 💚 💚

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment