Kickstart Kubernetes with K3s

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Search for a command to run...

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
No comments yet. Be the first to comment.
What is gRPC? gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authenti...
Let's assume we have a sample Golang application (in a Go project folder). Contents of main.go package main import "fmt" func main() { fmt.Println("hello world") } In the same folder, run the following commands to initialize go modules # initial...

When you create a new Kubernetes Cluster in EKS - you still need to do several things until you can start using it. This simple set of steps aims to help people create an EKS cluster in simple steps and configure the most essential services like Log ...

To implement a stack in golang we can make use of the slice datatype which golang provides which allows us to modify the size on the fly. If we were doing it in C, we would need to define a max size for the array. Lets start with a simple structure ...

The purpose of this guide is to make a simple telegram bot, which replies with a compliment for each and every message that it receives. Create a bot in telegram Look for a bot named BotFather https://t.me/botfather . This bot is used to create bots...

Kubernetes is a relatively new term which is in talks a lot. In this article I will try to explain what kubernetes is and how we can quickly get started with it.
Containers can be seen as lightweight VMs. Though they are much different. When we want to run our application in a VM - we need to install an OS and application requirements, following which we can run our application. Doing this takes a lot of time, space and a lot of processing power and memory goes to waste in maintaining the OS.
What if we could only package our application with the requirments and run them anywhere? thats what containers
are for. Containers generally use a base image (ubuntu, python, golang etc) and you can install your
application requirements over it. For doing this we make use of a Dockerfile. This is where we specify
the base image, the ports, the requirements and application files etc. So, after writing the Dockerfile -
we build the docker image, to manage the docker images we make use of docker registries (can be local
or somewhere in cloud). Later we can spawn as many containers from the registry images.
Explaining more about docker is not in scope of this article but check this cheatsheet for most commonly used docker commands.
Sample Dockerfile for a simple flask application
# Dockefile
FROM python:3
WORKDIR /usr/src/app
COPY . .
RUN pip install -r requirements.txt
EXPOSE 5000
CMD [ "python", "./main.py" ]
So, now we know that docker provides an easy and faster way to package and run applications. But, we can only run docker on one machine at a time. That's where kubernetes comes in.
Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. Thats the technical definition. In more easy terms - we manage the lifecycle of containers accross multiple machines using kubernetes (orchestration).
To know more about kubernetes check this very nice documentation
Kubernetes manages all of its resources using yaml files. There are many resources in kubernetes which help making and running an application, scaling, managing access, routes etc.
The above resouce types are the most basic ones we need to run and access our application. We also make use of other resources like - nodes, daemon set, secrets, config maps, ingress, volume resources etc.
We can spend a week to discuss and know what and how to use all the resource types, but lets jump in and see how to deploy a simple application.
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-deployment
labels:
app: appname
# below starts the deployment spec
spec:
replicas: 3
selector:
matchLabels:
app: appname
template:
metadata:
labels:
app: appname
# below starts the pod spec
spec:
containers:
- name: appname
image: "nginx"
ports:
- name: http
containerPort: 80
protocol: TCP
To explain the above fields -
apiVersion - version of kubernetes resource in use, depending on the version, the yaml format may change
kind - the type of kubernetes resource (deployment, service etc)
metadata name - name of the deployment labels - labels of the deployment. this is used to map this deployment with other resources like a service.
spec - configuration of the deployment/ pod/ service the format depends on the kind
replicas - number of pods to create (horizontal scaling)
selector - used to map resources together (using the labels), if deployment has a label app: app1
we can then use the following in a service to map the service to the deployment. We can use multiple labels.
selector:
macthLabels:
app: app1
template - this is present in a deployment and this is where we define our pod config we tell the metadata and labels like we did for deployment along with the spec (for the pod)
Inside the pod spec we have list of containers with relevant info(name, image, env, ports, volumes etc)
apiVersion: v1
kind: Service
metadata:
name: app-service
labels:
app: appname
spec:
type: NodePort # ClusterIP/ LoadBalancer
ports:
- port: 80
targetPort: http
protocol: TCP
name: http
selector:
app: appname
In the above example the spec is different, Service only takes care of the port mappings.
The other thing we see is the selector - this is used to map the service to the deployment.
So if the deployment created 3 pods (replicas), our service will automatically create a load balancer,
and you will be able to access the application on app-service:80.
Now that we have yaml files ready to be deployed to a kubernetes cluster, lets install kubernetes on local linux machine.
We will use k3s for our usecase. It's lightweight and easiest to setup.
curl -sfL https://get.k3s.io | sh -
# installation done, check if it works
k3s kubectl get node
# more info at https://k3s.io/
Now we have a kubernetes cluster running locally. Save the above deployment yaml and service yaml to files deployment.yaml and service.yaml. Then run the following command to apply the changes in the cluster -
# apply
k3s kubectl apply -f deployment.yaml
k3s kubectl apply -f service.yaml
# check if resources were created
k3s kubectl get deploy
k3s kubectl get pod
k3s kubectl get svc
kubectl can also be installed as a standalone tool from here. to know about more kubectl commands try -
k3s kubectl help
If the deployment and service got created without errors, we should now be able to access it on http://localhost
Image Credit: learnk8s