• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

End-to-End DevOps: Running a .NET App in Kubernetes with Docker, ACR, and AKS

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Introduction


We will be building a sample web application using dotnet core with any IDE but in my case, I will be using VS Code. The application will be tested in a local Kubernetes cluster before deploying this to Azure Kubernetes Service (AKS). You can also use any language of your choice as long as you can wrap it in a container image; we will be using C# for this project.

Requirements

  • VS Code
  • Docker
  • Azure CLI
  • Azure Account
  • .Net SDK
Create Infrastructure

  • Clone a sample of this repository for this project by running the command:

git clone

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Change to the repository directory

cd kodekloud/AKS/KodeKloudApp


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Test the Application


Before you test your app, make sure that .Net sdk is installed by checking first using the command

dotnet --list-sdks


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


If it is not already installed, go to command pallete by clicking view tab and select Command pallete. Alternatively, you can press command+p buttons together.

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


Search or select install New .NET SDK

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


Wait for installation to complete and check by running

dotnet --version


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Now that you have .Net sdk installed, build your application by running dotnet build


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


I am getting the warning message because my dotnet version is version 8 while the version used in the repository is 7.0. To rectify this, I will change the version in my target framework from 7.0 to 8.0

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Run dotnet run command to run the application
    Image description
  • Copy the localhost ip address (

    Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

    ) and paste it in a new browser to confirm the application.
    Image description
  • While keeping the application running, you can change the value of the message in appsettings.json file. Refresh the browser and you will see the message changed
    Image description
  • You can stop the application by pressing ctrl+c
Build your Docker image

  • Ensure that docker is running.
  • Build the docker image by running the command docker build -t mywebapp:v1 .


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


NOTE:If you get this error, that means the dotnet version in your Dockerfile is different from the version installed on your local machine. Edit the version in your Dockerfile

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • List docker image by running docker images


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Containerize your application

  • We will leverage on this command: docker run -d -p 8080:80 [name of your image]

This command will run the image as a daemon using -d switch and exposing it at port 8080 and matching it to target port 80 using -p switch

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Check the status of the container by running docker container ls


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Navigate to your browser and browse localhost:8080
    Image description
Run your Application on local Kubernetes Cluster


We will use kubectl utility to deploy our app to local Kubernetes. The easiest way to configure one node cluster Kubernetes from Docker.

  • Open your Docker desktop
  • Select settings sign
  • choose Kubernetes tab
  • Click the toggle to enable Kubernetes
  • Click Apply & restart
    Image description
  • Click install.
    Image description
    Image description
  • After the installation is completed, check the current context by running the command kubectl config current-context


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • If the current context is not set to docker-desktop, change it by running the command: kubectl config use-context docker-desktop


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Confirm the version of the kubectl by running kubectl --version


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Create a new deployment by running this imperative command: kubectl create deployment [deployment-name] --image:[your-image-name] --replicas=[number-of-replicas]


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Let's confirm that the deployment is created using kubectl get deployment


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


You can see that the 2/2 replicas are up and in running state.

  • To connect to your deployment, you will need to expose it to a service using the command: kubectl expose deployment [deployment-name] --type:LoadBalancer --port=8080 --target-port=80


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Verify the service by running kubectl get svc


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • If you go to localhost:8080, you will see that that your app is running.
    Image description
  • Remember you have two pods running in your cluster, to verify. which of them is serving you, open another new windows in incognito mode and visit localhost:8080 and compare system name. The two pods are viewed by running kubectl get pod


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • The first page is served by the second pod if you compare the pod name and system name.
  • The second page (opened in incognito mode) is served by the first pod.
    Image description
    Image description
Deploy Azure Kubernetes Service (AKS) Cluster

  • Head over to your Azure portal and search for AKS and select Kubernetes service
    Image description
  • Click create and select Kubernetes cluster
    Image description
  • Fill in the Project details: create a new resource group, give a name to your cluster, choose a region and uncheck all availability zone. Leave other settings as default.
    Image description
  • Change the tab to Node pool. Delete the userpool by checking it and press the delete sign (This is used for testing).
    Image description
  • Edit the the agentpool by clicking on it. For testing purpose, change the node size to DS2_v2, scale method is manual and node count set to 1.
    Image description
  • Change max pod per node to 30, delete Taints and select Update
    Image description
  • Click Integration tab. Click create to create new container registry.
    Image description
  • In the create container registry page, give a name to your container registry, use the previous resource group and region, change the SKU to Basics and click Ok
    Image description
  • Change the tab to Review+create in order to validate. Click Create after validation.
    Image description
    NOTE: The deployment takes several minutes.
    Image description
    Image description
Connect to AKS cluster via Azure CLI

  • Visit

    Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

    to install AZ CLI
  • Run az --version to check the version.
    Image description
  • run az login to connect your CLI to your Azure portal if you have not done so.
  • You can also run az account show to view the account you are connected to
  • After logging into your account, set the default to your AKS resource group by running az configure --defaults group=[resource-group-name]


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Run az aks get-Credentials --name [AKS name] to download the credentials and merge it to your current user context.
    Image description
  • Validate if kubectl can access the cluster by running kubectl config current-context


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Run kubectl get node to view the node (agentpool) you selected while creating AKS cluster.
    Image description
  • You can also confirm this by navigating to your Azure portal, open the created cluster, select Node pools under settings and select the Nodes tab.
    Image description
Push your image to Docker hub


If you create a deployment right now, the status of your pod will be ErrImagePull because the image is on your local Docker and cannot be accessed AKS. Pushing it to Docker hub will make it accessible to create deployment. To push your image, follow these steps:

  • run docker login


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Tag the image by running docker build -t <your-dockerhub-username>/<image-name>:<tag> .


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Push the image to Docker Hub by running docker push <your-dockerhub-username>/<image-name>:<tag>


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


You can confirm the image in your Docker Hub by logging into your Docker through browser and select Repositories

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Now that you have your image in your Docker Hub, you can use it to create deployment.
    Image description
  • You can get the pod also using kubectl get po


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


You can also check the deployment and pod in your Azure portal

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • To access your deployment from outside, expose it by creating service using the command: kubectl expose deployment [deployment-name] --type:LoadBalancer --port=80 --target-port=80


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Run kubectl get svc to get the External IP address to access your deployment
    Image description
  • Paste the External IP to a new browser to check.
    Image description
Scale your application

  • To scale your deployment, run kubectl scale deployment [deployment-name] --replicas=5


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Check the number of pods
    Image description
    You can see that there are 5 pods in the cluster according to the number of scale
Pushing Image to Azure Container Registry


You have created an ACR while creating AKS and you are going to push your Docker image to it.

  • Head over to Azure portal and search & select container registries
    Image description
  • Select your container registry.
    Image description
    There are two things that are needed for your AKS node to pull an Image from ACR:
  • Network connectivity - Because SKU selected was Basics during creation of your ACR, the default network access is Public and cannot be changed.
    Image description
  • IAM policy - AcrPull permission has been assigned by default since you created your ACR together with AKS
    Image description
  • To push your local image to ACR, tag the image by running the command docker tag local-image-name:tag ACR-login-server/image-name:tag

NOTE: You can get your ACR-login-server from ACR overview page

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • To login to ACR from my VS Code, run the command: az acr login --name ACR-login-server


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Once the login is succeeded, run docker push ACR-login-server/image-name:tag


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


Confirm that the image is inside your ACR by selecting Repositories

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Next thing is to use this image to create deployment.
    Image description
  • Expose the deployment and get the External IP
    Image description
  • Paste the External IP in a new browser to check
    Image description
    You have successfully pushed your Image to ACR and tested that it is working.

Thanks for staying till the end


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу