Getting started with Azure Kubernetes Services - Step by Step - 1

Kubernetes.io defines Kubernetes as an open-source system for automating deployment, scaling, and management of containerized applications. Rather than defining and explaining the basics of K8s, this post intends to quickly get you up to speed with AKS. By the end of this post, you will have a high level understanding of some of the most important Kubernetes components. Let's get started.

Install Azure CLI

Although you can do a lot of the following tasks using the UI, it is recommended that you use the command line so you can easily repeat the steps. Start by installing Azure CLI. As on 18th Aug, the version is 2.0.71.

$ az --version                                                                                                               
azure-cli                         2.0.71

command-modules-nspkg               2.0.3  
core                              2.0.71  
nspkg                              3.0.4  
telemetry                          1.0.3

...
...
Your CLI is up-to-date.  

Login using command line

To login using command line, you must use:

$ az login

On hitting enter, a browser will open and ask you to login with Microsoft ID. Login and switch back to the terminal. It should be able to pull all your subscription details (if you have multiple).

Get your subscription list

To get your subscription information, run $ az account list. Notice that one of them (in this case) Attosol Two will have isDefault property as true.

[
  {
    "cloudName": "AzureCloud",
    "id": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx",
    "isDefault": false,
    "name": "Attosol One",
    "state": "Enabled",
    "tenantId": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx",
    "user": {
      "name": "<email>",
      "type": "user"
    }
  },
  {
    "cloudName": "AzureCloud",
    "id": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx",
    "isDefault": true,
    "name": "Attosol Two",
    "state": "Enabled",
    "tenantId": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx",
    "user": {
      "name": "<email>",
      "type": "user"
    }
  },
]

Switch to another subscription

Copy the subscription id and run the following to activate that subscription:

$ az account set --subscription "<ID>"

Create a new AKS cluster

List the locations using the following command so that you could finalize where your resource group will be created:

$ az account list-locations

Once you have finalized the location, create a resource group using:

$ az group create -l <LOCATION> -n <RESOURCE GROUP NAME>

To create a new cluster using an existing SSH Public Key run the following:

$ az aks create --resource-group <RESOURCE GROUP NAME> --name <AKS Cluster Name> --node-count 1 --ssh-key-value <SSH PUBLIC KEY>

Connect to the Cluster

Now that you have a cluster created, you must connect to it using kubectl. You can install it locally using:

$ az aks install-cli

Let's get the credentials now in order to connect...

$ az aks get-credentials --resource-group <RESOURCE GROUP NAME> --name <AKS Cluster Name>

You can list all your clusters using:

$ az aks list

Check the nodes

Your cluster comprises of multiple nodes. A node is nothing but a VM. To view your node details, you can use:

$ kubectl get nodes
aks-nodepool1-19287650-0   Ready    agent   2m   v1.13.9  

Create multiple namespaces

You may want to create multiple namespaces in the same cluster so that deployments and other related assets can be clearly viewed and managed. To create a couple of namespaces like dev and prod use:

$ kubectl create namespace dev
$ kubectl create namespace prod

To view all namespaces, use:

$ kubectl get namespaces
NAME          STATUS   AGE     LABELS  
default       Active   43m     <none>  
dev           Active   9m18s   <none>  
kube-public   Active   43m     <none>  
kube-system   Active   43m     <none>  
prod          Active   9m13s   <none>  

Benefits of namespace

Let's use a simple web application that contains a front-end and a back-end in the dev namespace. Create a file called azure-vote.yaml and paste the code below:

apiVersion: apps/v1  
kind: Deployment  
metadata:  
  name: azure-vote-back
spec:  
  replicas: 1
  selector:
    matchLabels:
      app: azure-vote-back
  template:
    metadata:
      labels:
        app: azure-vote-back
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: azure-vote-back
        image: redis
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        ports:
        - containerPort: 6379
          name: redis
---
apiVersion: v1  
kind: Service  
metadata:  
  name: azure-vote-back
spec:  
  ports:
  - port: 6379
  selector:
    app: azure-vote-back
---
apiVersion: apps/v1  
kind: Deployment  
metadata:  
  name: azure-vote-front
spec:  
  replicas: 1
  selector:
    matchLabels:
      app: azure-vote-front
  template:
    metadata:
      labels:
        app: azure-vote-front
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: azure-vote-front
        image: microsoft/azure-vote-front:v1
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        ports:
        - containerPort: 80
        env:
        - name: REDIS
          value: "azure-vote-back"
---
apiVersion: v1  
kind: Service  
metadata:  
  name: azure-vote-front
spec:  
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: azure-vote-front

Now, switch to the already created namespace dev:

$ kubectl config set-context $(kubectl config current-context) --namespace=dev

Let's deploy the application using:

kubectl apply -f azure-vote.yaml  

This will start the deployment based on the azure-vote.yaml file. To check the status of the deployment, use:

$ kubectl get service azure-vote-front
azure-vote-front   LoadBalancer   10.0.208.231   52.163.224.194   80:31214/TCP   13m  

If you can see an external IP, it means you can access the service using a browser. How cool is that!?!

You can switch to prod similarly, using:

$ kubectl config set-context $(kubectl config current-context) --namespace=prod

List all the pods:

$ kubectl get pods
No resources found.  

Why? Because, you are looking at prod namespace. Let's switch back to dev and repeat the exercise.

$ kubectl config set-context $(kubectl config current-context) --namespace=dev
$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE  
azure-vote-back-7ff55dd855-lm6vz    1/1     Running   0          5m  
azure-vote-front-7465699999-zcptx   1/1     Running   0          5m  

If you have been following up successfully so far... Good Job! You have seen how easy it is to setup and get going with Azure Kubernetes.

Delete and clean up the resources

To delete the deployments, you must use:

$ kubectl delete deployments azure-vote-front
$ kubectl delete deployments azure-vote-back

To delete the services, you must use:

$ kubectl delete services azure-vote-front
$ kubectl delete services azure-vote-back

What next?

Well, stay tuned for upcoming articles. Say hi, share this article, leave a comment or Subscribe now to stay updated through our newsletter. Also, check out our services or contact us at contact@attosol.com for your software and consultancy requirements.

Ads:

Disclosure: Some links you will find on this web page are considered affiliate links. This means that if you press on the link and ultimately make a purchase, we might receive a commission from the advertiser. This does not affect you or your purchase price, only helps us dedicate resources to make this site more useful to you.

Rahul Soni

⌘⌘ Entrepreneur. Author. Geek. ⌘⌘

Kolkata, India

Subscribe to Attosol Technologies

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!