Showing posts with label AKS. Show all posts
Showing posts with label AKS. Show all posts

Deploying Azure Vote app on AKS

We have already created the AKS cluster successfully in last post and in this post we will utilize the same AKS-cluster and deploy Azure Vote app. Please follow the below screenshots and commands -

First lets get the Azure Cli running and get the clusters credentials and check nodes just for the confirmation as everything is running as expected as shown below -

az aks get-credentials --resource-group ResourceGroup --name AKSCluster

kubectl get nodes




Kubernetes manifest file defines a desired state for the cluster, such as what container images to run & will be using the manifest file for Azure Vote app and it would deploy all the relevant components.

Lest open the vi or nano editor to create azure-vote.yaml and copy the below definition:

Ref link - https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough-portal



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


Once the file is saved run the below command for the deployment :

kubectl apply -f azure-vote.yaml

When the application runs, a Kubernetes service exposes the application front end to the internet. This process can take a few minutes to complete.
To monitor progress, use the kubectl get service command with the --watch argument.

kubectl get service azure-vote-front --watch

Below you can see how it ran on cloud cli -



Now you can see the internal and external IP address for Azure-Vote application and when you try to browse the public IP address it would show you the app where you can vote for cat and dogs as shown :-





Monitor heath and logs : When you created the cluster, Azure Monitor for containers was enabled. This monitoring feature provides health metrics for both the AKS cluster and pods running on the cluster. Navigate to the cluster and follow the below instruction as shown below as well

  1. Under Monitoring on the left-hand side, choose Insights
  2. Across the top, choose to + Add Filter
  3. Select Namespace as the property, then choose <All but kube-system>
  4. Choose to view the Containers.





To see logs for the azure-vote-front pod, select the View container logs link on the right-hand side of the containers list. These logs include the stdout and stderr streams from the container.







Azure Kubernetes Service (AKS) cluster


Kubernetes is a platform for managing containers across multiple hosts. It provides lots of management features for container-oriented applications, such as auto-scaling, rolling deployments, compute resources, HA and storage management. Like containers, it's designed to run anywhere, including on bare metal, in our data center, in the public cloud, or even in the hybrid cloud.


Deployment in Kubernetes ensures that a certain number of groups of containers are up and running.


Azure Kubernetes Service (AKS) makes it simple to deploy a managed Kubernetes cluster in Azure. AKS reduces the complexity and operational overhead of managing Kubernetes by offloading much of that responsibility to Azure.


As a hosted Kubernetes service, Azure handles critical tasks like health monitoring and maintenance for you. The Kubernetes masters are managed by Azure. You only manage and maintain the agent nodes.


As a managed Kubernetes service, AKS is free - you only pay for the agent nodes within your clusters, not for the masters.


There are various ways you can deploy the AKS cluster, in this post we will be deploying through portal and sharing the azure cmd cli and Terraform code as well.


For improved security and management, AKS lets you integrate with Azure Active Directory and use Kubernetes role-based access controls. You can also monitor the health of your cluster and resources.


Navigate  >> to Kubernetes Service  >>  Click on it and follow screen shots :- 








Above is the first page of AKS deployment where we would fill in the basic settings like -

  • Project details - Select an Azure subscription, then select or create an Azure resource group.
  • Cluster Details - Select a region, Kubernetes version, and DNS name prefix for the AKS cluster.
  • Select a VM size for the AKS nodes. The VM size cannot be changed once an AKS cluster has been deployed.
  • Select the number of nodes to deploy into the cluster, I selected -1 as it is for testing purpose. 

Next click on scale & keep the default options & click on Authentication




Authentication page, configure the following options:





  • Create a new service principal by leaving the Service Principal field with (new) default service principal. Or you can choose Configure service principal to use an existing one. If you use an existing one, you will need to provide the SPN client ID and secret.

    • Enable the option for Kubernetes role-based access controls (RBAC). This will provide more fine-grained control over access to the Kubernetes resources deployed in your AKS cluster.





    Click for Networking , 

    By default, Basic networking is used, and Azure Monitor for containers is enabled. 



    Click Review + create and then Create when validation completes.




    Once you deploy it would take few minutes and it would be deploying all the relevant things after creating Master mode and worker node.

    To manage a Kubernetes cluster, you use kubectl, the Kubernetes command-line client. The kubectl client is pre-installed in the Azure Cloud Shell.

    Open Cloud Shell using the >_ button on the top of the Azure portal. as shown below along with the AKS overview -





    To configure kubectl to connect to your Kubernetes cluster, use the az aks get-credentials command. This command downloads credentials and configures the Kubernetes CLI to use them.


    az aks get-credentials --resource-group ResourceGroup --name AKSCluster


    To verify the connection to your cluster, use the kubectl get command to return a list of the cluster nodes.


    kubectl get nodes


    Below is the commands that i ran after creation of the cluster , plz check -




    Now we have seen the deployment of AKS and few basic things about kubernetes and how to connect and check the nodes. Lets check more things regarding AKS -


    **AKS Supports RBAC & RBAC lets you control access to Kubernetes resources and namespaces, and permissions to those resources


    **You can also configure an AKS cluster to integrate with Azure Active Directory (AD). With Azure AD integration, Kubernetes access can be configured based on existing identity and group membership.


    **Azure Monitor for container health collects memory and processor metrics from containers, nodes, and controllers. Container logs are available, and you can also review the Kubernetes master logs. 



    **Cluster node and Pod scalling -You can use both the horizontal pod autoscaler or the cluster autoscaler. This approach to scaling lets the AKS cluster automatically adjust to demands and only run the resources needed.


    **Cluster node upgrades - Azure Kubernetes Service offers multiple Kubernetes versions. As new versions become available in AKS, your cluster can be upgraded using the Azure portal or Azure CLI. During the upgrade process, nodes are carefully cordoned and drained to minimize disruption to running applications.



    **Storage Volume support -To support application workloads, you can mount storage volumes for persistent data. Both static and dynamic volumes can be used backed by either Azure Disks for single pod access, or Azure Files for multiple concurrent pod access.



    **Network settings - An AKS cluster can be deployed into an existing virtual network. In this configuration, every pod in the cluster is assigned an IP address in the virtual network, and can directly communicate with other pods in the cluster, and other nodes in the virtual network. Pods can connect also to other services in a peered virtual network, and to on-premises networks over ExpressRoute or site-to-site (S2S) VPN connections.



    **The HTTP application routing add-on makes it easy to access applications deployed to your AKS cluster. When enabled, the HTTP application routing solution configures an ingress controller in your AKS cluster. As applications are deployed, publicly accessible DNS names are auto configured. The HTTP application routing configures a DNS zone and integrates it with the AKS cluster. You can then deploy Kubernetes ingress resources as normal.


    **AKS supports the Docker image format. For private storage of your Docker images, you can integrate AKS with Azure Container Registry (ACR).



    Dockerfile - Intro, understanding and Best practices

    What is the Dockerfile?

    Dockerfiles are instructions. They contains all of the commands used to build an image.
    • Docker images consist of read-only layers.
    • Each represents a Dockerfile instruction.
    • Layers are stacked. (layered architecture)
    • Each layer is a result of the changes from the previous layer.
    • Images are built using the docker image build command.

    Dockerfile Layers

    Dockerfile:

    FROM ubuntu:15.04  
    COPY . /app  
    RUN make /app  
    CMD python /app/app.py 
     
    • FROM creates a layer from the ubuntu:15.04 Docker image.
    • COPY adds files from your Docker client’s current directory.
    • RUN builds your application with make.
    • CMD specifies what command to run within the container.



    Best Practices =>

    General guidelines:
    • Keep containers as ephemeral as possible.
    • Follow Principle 6 of the 12 Factor App. (https://12factor.net/)
    • Avoid including unnecessary files.
    • Use .dockerignore.
    • Use multi-stage builds.
    • Don’t install unnecessary packages.
    • Decouple applications.
    • Minimize the number of layers.
    • Sort multi-line arguments.
    • Leverage build cache.


    Lets check the instruction manual for Dockerfile :=>

    FROM: Initializes a new build stage and sets the Base Image

    RUN: Will execute any commands in a new layer

    CMD: Provides a default for an executing container. There can only be one CMD instruction in a Dockerfile

    LABEL: Adds metadata to an image

    EXPOSE: Informs Docker that the container listens on the specified network ports at runtime

    ENV: Sets the environment variable <key> to the value <value>

    ADD: Copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>.

    COPY: Copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

    ENTRYPOINT: Allows for configuring a container that will run as an executable

    VOLUME: Creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers

    USER: Sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN, CMD, and ENTRYPOINT instructions that follow it in the Dockerfile

    WORKDIR: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it in the Dockerfile

    ARG: Defines a variable that users can pass at build-time to the builder with the docker build command, using the --build-arg <varname>=<value> flag

    ONBUILD: Adds a trigger instruction to the image that will be executed at a later time, when the image is used as the base for another build

    HEALTHCHECK: Tells Docker how to test a container to check that it is still working

    SHELL: Allows the default shell used for the shell form of commands to be overridden

    What is RBAC Baseline in Azure Landing Zone?

      What is RBAC Baseline in Azure Landing Zone? In simple terms, an RBAC baseline is the default set of access roles and assignments...