Kubernetes basics

Installing the Kubernetes Command Line

The Kubernetes command line (CLI) tool is called kubectl (pronounced: cube-cuttle) to interact with any Kubernetes cluster, not just the local one. If you don’t want to have a local Kubernetes installation, but you want to operate a Kubernetes cluster, you’ll only use kubectl. Run the following command to verify the installation:

1
kubectl version

Installing locally

Start by installing Docker. Kubernetes does support other container systems, but I think using Docker is the easiest way to learn Kubernetes.

Depending on your OS, I’d recommend a specific install method:

  • Linux: install minikube. You’ll need a hypervisor like VirtualBox or KVM.
  • Windows or Mac: latest version of Docker for desktop. You might need to enable Kubernetes if you are already using the newest version.

Verify that Kubernetes is running with the following command:

1
kubectl get svc

Core concepts

Use the API via CLI to tell Kubernetes how your application should run.

  • Objects: way to set a desired state, defined in a YAML format.
  • Pods: smallest compute unit object you create in Kubernetes, which groups containers (1 container = 1 responsability) for performance and co-scheduling purposes. Containers in a pod share the same networking, the same storage, and are scheduled in the same host.

Define the pod by creating a YAML file definition named pod.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: v1
kind: Pod
metadata:
name: helloworld
labels:
app: helloworld
spec:
containers:
- name: helloworld
image: christianhxc/helloworld:1.0
ports:
- containerPort: 80
resources:
requests:
cpu: 50m
limits:
cpu: 100m

Then create it by running the following command:

1
kubectl apply -f pod.yaml

Finally verify that the pod is running:

1
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld 1/1 Running 0 41s

If for some reason the pod dies, you don’t have to tell Kubernetes to create the pod again. Kubernetes will recreate one automatically because the pod’s state is not the same as the one you defined.

Deployments

  • Pods are mortal, and that means that your application is not resilient.
  • A deployment object is how you can give immortality to pods. You define how many pods you want to have running all the time, the scaling policies, and the policy for zero-downtime deployments.
  • If a pod dies, Kubernetes will spin up a new pod. Kubernetes continually verifies that the desired state matches with the object’s definition.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
apiVersion: apps/v1
kind: Deployment
metadata:
name: helloworld
labels:
app: helloworld
spec:
replicas: 3
selector:
matchLabels:
app: helloworld
template:
metadata:
labels:
app: helloworld
spec:
containers:
- name: helloworld
image: christianhxc/helloworld:1.0
ports:
- containerPort: 80
resources:
requests:
cpu: 50m
limits:
cpu: 100m

Create the deployment object:

1
2
$ kubectl apply -f deployment.yaml
deployment.apps/helloworld created

Verify that the deployment exists:

1
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
helloworld 3 3 3 3 40s

You’ll see that now you have three pods running plus the one we created previously.

1
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld 1/1 Running 0 8h
helloworld-75d8567b94-7lmvc 1/1 Running 0 24s
helloworld-75d8567b94-n7spd 1/1 Running 0 24s
helloworld-75d8567b94-z9tjq 1/1 Running 0 24s

How to kill a pod

Kill a pod actually restarts it, as it will be recreated to match the deployment definition. Ti update the container image, you need to update the deployment.yaml via the apply command.

1
2
apply
kubectl delete podhelloworld-75d8567b94-7lmvc

Services

You need to expose the pods via the service object to communicate with it. Other pods in the cluster can communicate between each other by using the internal IP address. But you can’t rely on an IP to communicate with pods for its dynamism, which is why the solution is still a service.

A service then is a representation of a load balancer for pods. You can configure the load balancing algorithm, and if Kubernetes is integrated with a cloud provider, you’ll use the native load balancers from the cloud provider.

Example service.yaml using labels and selectors:

1
2
3
4
5
6
7
8
9
10
apiVersion: v1
kind: Service
metadata:
name: helloworld
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: helloworld

Create the service:

1
2
$ kubectl apply -f service.yaml
service/helloworld created

To verify that the service is running, and to get the IP address to test it:

1
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
helloworld LoadBalancer 10.105.139.72 localhost 80:30299/TCP 21s
kubernetes ClusterIP 10.96.0.1 443/TCP 12h

You can test the container image with http://localhost/api/values.