About Kubernetes architecture (2)
The first Kubernetes article https://aleqxan.medium.com/about-kubernetes-architecture-1-963a8a62a7bf on this series introduced some basic stuff about Kubernetes (Pods, Nodes, Kubernetes YAML configuration e.t.c). In this article, we would be looking at installing and setting up kubectl on our various devices, Kubernetes object management, creating a pod, basic Kubectl commands, debugging pods, Namespaces, and Kubernetes dashboard
Installing and setting up Kubectl
Like we said in the first article Kubernetes runs with the aid of Kubectl commands, we would be setting up our Kubernetes environment so we can run these commands. We would be considering three backgrounds; Linux, Mac, and Windows, this is due to their popularity among developers.
The first thing to do is to download the latest release with this command;
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
Next, we install Kubectl with this command;
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
sudofor the above command when you’re not the root or superuser,
sudoallows other users to have access to what root users can do normally on a file.
To check the version you have installed you use this command;
kubectl version --client
Most mac installations are done with a brew manager, so we would be using the brew package manager procedure for our setup.
You run this command for installation;
brew install kubectl
brew install kubernetes-cli
To check the version use this command;
kubectl version --client
You use this command if you have curl installed on your Pc;
curl -LO "https://dl.k8s.io/release/v1.23.0/bin/windows/amd64/kubectl.exe"
To check the version you installed;
kubectl version --client
Kubernetes Object management
kubectl command-line tool supports several different ways to create and manage Kubernetes objects. We would be looking at the various ways this is done.
When using imperative commands, the user operates directly on live objects in a cluster. The user provides operations to the
kubectl command as arguments or flags.
An example is running an instance of the nginx container by creating a deployment object;
kubectl create deployment nginx --image nginx
Despite this being quite easy in terms of only a single step to make changes to the cluster or commands carried out with a single line of action, they do not provide a source of records except for what is live nor a template for creating new objects.
Imperative object configuration
In imperative object configuration, the kubectl command specifies the operation (create, replace, etc.), optional flags, and at least one file name.
An example would be this configuration file created;
kubectl create -f nginx.yaml
The merit of this configuration is that it can be stored in a control version system like git and it provides a template for creating new objects, it also has its demerit such as an additional step of writing a YAML file and you need to understand object schema.
Declarative object configuration
When using declarative object configuration, you operate on object configuration file you stored locally, but you do not define the operations to be taken on the files. Create, update, and delete operations are automatically detected per-object by
An example of this is processing all object configuration files in your
configs directory, you can give it any name you want but we would be using
configs , then create or patch the live objects.
kubectl apply -f configs/
Creating a Pod
This sample would help you create a Pod
You can create with this format on your editor (probably Vs code) inputting your pod specification or on your terminal, in your preferred folder you do the following;
vim (your preferred pod name)
Then click enter, you would be able to edit the configuration on your command-line, then
:wq to save your and exit vim editor.
Next, you implement your configuration with;
kubectl apply -f (file name)
Your pod should be running, you can check by running this command;
kubectl get pod
Other Kubectl commands and debugging pods
Here are some other common kubectl commands and they can be useful when debugging pods or general debugging.
To list all services in a namespace
kubectl get services
To list all pods in a namespace
kubectl get pods --all-namespaces
To list all pods in the current namespace with more details
kubectl get pods -o wide
To list a particular deployment
kubectl get deployment my-dep
To get a Pods YAML
kubectl get pod my-pod -o yaml
To examine the contents of a Pod, used especially when troubleshooting
kubectl exec -it (pod name)
Namespaces are used for environments with many users spread across multiple teams, or projects. For clusters with a few to tens of users, you should not need to create a namespace. They are a way to divide cluster resources between multiple users.
When you create a cluster, Kubernetes provides some namespace for you, to see them input the following command on your terminal;
kubectl get namespace
You would be provided with the following on your terminal;
NAME STATUS AGE
default Active 1d
kube-node-lease Active 1d
kube-public Active 1d
kube-system Active 1d
We would be explaining them below;
defaultThe default namespace for objects with no other namespace
kube-systemThe namespace for objects created by the Kubernetes system
kube-publicThis namespace is created automatically and is readable by all users (including those not authenticated). This namespace is mostly reserved for cluster usage, in case that some resources should be visible and readable publicly throughout the whole cluster. The public aspect of this namespace is only a convention, not a requirement.
kube-node-leaseThis namespace holds Lease objects associated with each node. Node leases allow the kubelet to send heartbeats so that the control plane can detect node failure.
To set up your own namespace for your project you can use the following format(input your spec) and command;
kubectl run nginx --image=nginx --namespace=<insert-namespace-name-here>
To check your created namespace use;
kubectl get pods --namespace=<insert-namespace-name-here>
The Dashboard is a web-based Kubernetes user interface. You can use the Dashboard to deploy containerized applications to a Kubernetes cluster, troubleshoot your containerized application, and manage the cluster resources. With it, you get an overview of applications running on your cluster, as well as for creating or modifying individual Kubernetes resources (such as Deployments, Jobs, DaemonSets, etc). You can scale a deployment, initiate a rolling update, restart a pod or deploy new applications using a deploy wizard, all viewed on the dashboard.
If you have read and followed till this point you would have understood how to install Kubernetes on your system, learned about Kubernetes object management, creating pods, basic kubectl commands, namespaces, and Kubernetes dashboard.