Last week we discussed the basic concepts of Kubernetes and got you used to some of the terminology involved. If you have not checked out the article, feel free to jump in there now to refresh yourself.
Getting Started With Kubernetes - Basic Concepts Of Kubernetes
Today we are going to get started with some actual hands on work in Kubernetes, starting with the basics of Pods. We will get into setting up your own clusters later on, but first we will assume you already have access to a Kubernetes cluster that you can deploy code to. If you don't one of the best ways to get started is by enabling Kubernetes on Docker Desktop. If you have not done this already, install Docker Desktop onto your system and within the configuration settings and select "Enable Kubernetes". You will also need to click on the "Restart and Apply" button for the changes to take effect, which will be a couple of minutes to restart Docker Desktop.
When you enable kubernetes through Docker Desktop, it will also install kubectl, which will allow you to interact with your kubernetes cluster via the command line. Depending on the amount of reseources you have available on your system, it could take a little while for this to be enabled, but when it is running, run the following command.
FIrstly, make sure you have kubectl installed and running on your system but checking the version:
Secondly, verify your cluster is running by using the kubectl command with the cluster-info option to see further details of your cluster:
kubectl cluster-info Kubernetes control plane is running at https://kubernetes.docker.internal:6443 CoreDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
If non of the information printed to the screen makes any sense, don't worry, we have a lot of content planned and will spend a lot of time working through your cluster configurations. Just know that your cluster is ready and waiting to accept commands.
As we mentioned in our previous article, we do not deploy containers directly on the workers, they are encapsulated in pods. It is the smallest object you can create in kubernetes. Pods usually have a one to one relationship with the container running your application. To scale up your application, you add additional pods, instead of adding containers to your pod. A single pod can have multiple containers, but when this happens, they are not usually containers of the same kind. In cases you might have a helper container that may live along side your application container.
From the command line, we can create a pod using the following command:
kubectl run nginx --image nginx pod/nginx created
Put simply, we are running a new pod called "nginx" using the image named "nginx" from docker hub. To see if this was successful, you can use the get pods command, as we have run below:
kubectl get pods NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 68s
We can also create a pod definition as a yaml file. Pods can be very complex, so by creating a definition, we can store the file in a code repository, allowing us to use it when ever we need to. When using a yaml file to create a pod, you will always need to define four main sections. They are root level fields and are all required.
- apiVersion - The version you are using, for pods, usually v1
- kind - The type of object we are trying to create…This is Pod
- metadata - This is data about the object, like names and labels. This will be in the form of a dictionary. Name will be a string, label will be a dictionary.
- spec - This is where we provide additional information depending on the object. Spec will be in the form of a dictionary.
Just like we created our nginx pod previously, we can do the same with a definition file, like the one below:
apiVersion: v1 kind: Pod metadata: name: nginx2 labels: app: myapp spec: containers: - name: nginx-container image: nginx
To then create the pod we would then run the following:
kubectl create -f yaml-file.yaml pod/nginx2 created
kubectl get pods NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 4m22s nginx2 1/1 Running 0 4s
We can also get a lot more detail on the pod by running the describe command like the one below:
kubectl describe pods <pod_name>
Even if you have not set up the pod using a yaml file, you can still edit the pod using the edit command:
kubectl edit pods <pod_name>
There is still a lot more information to come, but hopefully this is a good start to get you deploying your pods onto a kubernetes cluster.
About The Author
I am a DevOps Engineer, Endurance Athlete and Author. As a DevOps Engineer I specialize in Linux and Open Source Applications. Particularly interested in Search Marketing and Analytic’s, and is currently developing my skills in devops, continuous integration, security, and development(Python).
Posted with STEMGeeks