Azure Kubernetes Service

Azure Kubernetes: Part 1- Deploy an image

Deploy a simple docker image in AKS

Ayush P Gupta

--

The more I learn about Kubernetes, the more I fell in love with it.

After a year of using Kubernetes(k8s) on Azure, I decided to pen down some important use cases and FAQs that one generally comes across when starting deployment on Kubernetes.

This series assumes the reader has basic info about Kubernetes, Docker, Azure Portal, Yaml files.

Introduction to AKS

AKS or Azure Kubernetes Service is a k8s offering by Microsoft on Azure Cloud. It's similar to EKS on AWS. While the two use the same open-source k8s, they both differ in the services provided. One thing to note here, the basic deployment commands are specific to k8s and can be used on any cloud provider running k8s.

Basic Terminologies

Let's cover a basic of terminologies we shall be using

k8s Cluster

A cluster is basically an enclosed world where all your services including k8s specifically lie. Think of a closed bubble. Whatever you add, pod, services, ingress, etc all lie within a cluster.

Pod

Almost the smallest independent unit, where your image runs. Think of this like a small machine. You push your docker image and run that inside a pod.

Service

Multiple instances or pods are grouped together via service. A service is like a parent that sits above all pods and acts as a load balancer. A service generally routes incoming requests evenly to all pods by default.

Namespace

A namespace is simply a grouping technique inside the cluster. You can group multiple services inside a cluster and give them a name. Think of this like an interface that groups certain types of methods.

That’s all you need for the start

Creating an AKS Cluster

Head over to the Azure portal. Sign up for a free account.

Once you land on the Azure dashboard, search Kubernetes in the global search bar.

Choose to create a new cluster

Provide a meaningful name and complete the different sections to create a new Kubernetes cluster.

Login to Azure via VS code

Note- we shall be using VS code to access k8s from our local machine. There are other ways too, but this is the simplest beginner-friendly approach that I found

Once you create a cluster, open up VS code, and install few extensions based on Azure.

Make sure you install Azure Account, and Azure Kubernetes Service. You can install others like Azure Developer CLI, and CLI tools as per need.

Once done installing, you must now sign in using your Azure credentials via the Azure tab in VS Code.

Upon signing in, you shall see your newly created cluster in the Kubernetes tab.

Saving Cluster Info

Once your newly created cluster is visible, right-click on it and click Merge into KubeConfig. This would save the cluster info in your local machine for future access and usage.

You might need different tools to connect to your cluster. One official plugin is kubectl which is the official CLI for k8s. Install it. Your VS code might prompt for this.

After you saved it, you can now see your cluster in the clusters section like below:

I have 2 clusters in my azure account

The “>” prefix on the cluster name indicates which is your active selected cluster. Right-click on your new cluster and click Set as current cluster.

Prepare a Sample Image

Before we proceed, check if kubectl is working fine using following cmd:

kubectl version

If the output displays the version, then congratulations for the work so far. We are now ready to deploy our first image.

Let’s create a new file named apple.yml You can use VS code to open and paste the following content. Technically we call this as Manifest file.

#########################################################
# Apple Pod
#########################################################

kind: Pod
apiVersion: v1
metadata:
name: apple-app
labels:
app: apple
spec:
containers:
- name: apple-app
image: hashicorp/http-echo
args:
- "-text=apple"

---
#########################################################
# Apple Service
#########################################################

kind: Service
apiVersion: v1
metadata:
name: apple-service
spec:
selector:
app: apple
ports:
- port: 5678 # Default port for image

Let's understand the syntax written.
The whole file is divided into 2 sections:

  1. Pod
  2. Service

Pod

A pod is a simple unit that runs our actual code.

kind: Pod            # the type of k8s resource
apiVersion: v1 # the k8s api version
metadata:
name: apple-app # the name of pod
labels: # the labels to be attached to identify this pod
app: apple
spec:
containers:
- name: apple-app # name of container inside pod
image: hashicorp/http-echo # the docker image reference
args: # the extra cmd arguments to run on start
- "-text=apple"

Note: Containers are the smallest units in k8s. There can be multiple containers inside Pod. But that makes this quite advance. Hence for simplicity, we assume 1 container per pod.

Service

A service simply sits on top of all pods. Here we have only 1 pod hence this service looks for only 1 pod.

kind: Service
apiVersion: v1
metadata:
name: apple-service
spec:
selector: # Selectors are used to identify related pods
app: apple # this works by identifying labels attached to a pod
ports:
- port: 5678 # The port on which to expose this service

Here 5678 is the port on which our docker image is running.

We have other ports like nodeport, targetport but we shall learn them later.

For this blog, we have used a simple docker image hashicorp/http-echo. This implements a simple HTTP server echo-ing the text message we supplied via args ie apple.
Learn more about this image: https://hub.docker.com/r/hashicorp/http-echo/

Deploy the manifest

Save the above apple.yml file in a directory and open up the terminal. Make sure to open a terminal in the same directory where your yml file is.

Run the following command:

kubectl apply -f ./apple.yml

Lets understand this cmd:

  1. kubectl the official k8s plugin
  2. apply commanding kubectl for apply operation
  3. -f the supplied input is of file type
  4. ./apple.yml the file location

Run the command, and if everything is correct you get the following output

Check the status

There are multiple ways to check but the easiest is the following:

Head over to your cluster and select default namespace. A new k8s cluster will have a default namespace. “ * ” indicates the currently selected namespace.

the list of namespace I have in one of my cluster

Next, head over to Workloads>Pods and you shall see your newly created pod

Both pod and a service are created

Double-click on the pod (technically descibe pod) and see the deployed manifest.

Congratulations we got our first ever service deployed

Access the Pod

Currently, we didn't expose our new service to the outside world from the cluster. By default, a service remains accessible only to the internal cluster.
Hence you might not see the HTTP server that we just deployed in action.

In our next blog, we shall expose this service to the outside world by assigning a public IP.

Conclusion

In this blog, we learned about the basics of Kubernetes terminologies and deploying a very simple HTTP server image. In subsequent blogs, we shall learn more about exposing service to the outside world, implementing SSL, Adding Gateways/ reverse proxies, etc.

Part 2: Azure Kubernetes: Part 2- Expose a Service to Outside World

Whola! Both you and I learned something new today. Congrats
👏 👏 👏

--

--

Ayush P Gupta

NodeJs | VueJs | Kubernetes | Flutter | Linux | DIY person