azure 使用

AKS is Microsoft Azure’s managed Kubernetes solution that lets you run and manage containerized applications in the cloud. Since this is a managed Kubernetes service, Microsoft takes care of a lot of things for us such as security, maintenance, scalability, and monitoring. This makes us quickly deploy our applications into the Kubernetes cluster without worrying about the underlying details of building it.

AKS是Microsoft Azure的托管Kubernetes解决方案,可让您运行和管理云中的容器化应用程序。 由于这是托管的Kubernetes服务,因此Microsoft为我们处理了很多事情,例如安全性,维护,可伸缩性和监视。 这使我们可以将应用程序快速部署到Kubernetes集群中,而不必担心构建它的底层细节。

In this post, we will see how we can build the Kubernetes cluster on Azure AKS, Accessing clusters from outside, configuring kubectl to work with AKS cluster, and many more.

在本文中,我们将看到如何在Azure AKS上构建Kubernetes群集,如何从外部访问群集,配置kubectl以与AKS群集一起使用等等。

  • Prerequisites

    先决条件

  • Install Azure CLI and Configure

    安装Azure CLI并配置

  • Creating AKS Cluster

    创建AKS集群

  • Configure Kuebctl With AKS Cluster

    使用AKS群集配置Kuebctl

  • Example Project

    示例项目

  • Create a Deployment and Access it

    创建部署并访问它

  • Kubernetes Dashboard

    Kubernetes仪表板

  • Delete the Cluster

    删除集群

  • Summary

    摘要

  • Conclusion

    结论

先决条件 (Prerequisites)

The prerequisites to this post are Docker essentials and Kubernests essentials. We are not going to discuss the basics such as what is a container or what is Kubernetes, rather, we will see how to build a Kubernetes cluster on Azure AKS. Below are the prerequisites you should know before going through this article

本文的前提条件是Docker基础知识和Kubernests基础知识。 我们将不讨论诸如什么是容器或什么是Kubernetes之类的基础知识,而是将看到如何在Azure AKS上构建Kubernetes群集。 以下是阅读本文之前应了解的先决条件

Docker基础 (Docker Essentials)

You need to understand Docker concepts such as creating images, container management, etc. Below are some of the links that you can understand about Docker if you are new.

您需要了解Docker概念,例如创建映像,容器管理等。如果您是新手,则下面是一些您可以了解的有关Docker的链接。

Kubernetes基础知识 (Kubernetes Essentials)

You need to understand Kubernetes' essentials as well along with Docker essentials. Here are some of the docs to help you understand the concepts of Kubernetes.

您需要了解Kubernetes的基本知识以及Docker基本知识。 以下是一些文档,可帮助您了解Kubernetes的概念。

Microsoft Azure帐户 (Microsoft Azure Account)

You should have a Microsoft Azure Account. You can get a free account for one year. You should see the below screen after you login.

您应该有一个Microsoft Azure帐户。 您可以获得一年的免费帐户。 登录后,您应该会看到以下屏幕。

Image for post
Azure Home Screen Azure主屏幕

You need to create a subscription for your account. The most common is Pay As You Go subscription.

您需要为您的帐户创建一个订阅。 最常见的是即付即用订阅。

Image for post
Subscription Offers 订阅优惠
Image for post
Pay-As-You-Go Subscription 随用随付订阅

You need a subscription to be associated with your tenant so that all the cost is billed to this subscription.

您需要一个与您的租户关联的订阅,以便将所有费用记入此订阅。

安装Azure CLI并配置 (Install Azure CLI and Configure)

Once you have the Azure Account you can install Azure CLI. You can go to the below documentation and install Azure CLI based on your operation system. You can configure Azure CLI with your subscription.

拥有Azure帐户后,就可以安装Azure CLI。 您可以转到下面的文档,然后根据您的操作系统安装Azure CLI。 您可以使用订阅配置Azure CLI。

Image for post
az login az登录

Let’s list the subscription with the following command

让我们用以下命令列出订阅

az account list

创建AKS集群 (Creating AKS Cluster)

First, you need a resource group for all your resources. Let’s create a resource with the following command

首先,您需要一个用于所有资源的资源组。 让我们使用以下命令创建资源

az group create --name myAKSGroup --location eastus
Image for post
Resource Group Created 资源组已创建

Let's create a cluster with the following command. Notice that we are using the same resource group that we created above. You can see the JSON formatted result after a few minutes.

让我们使用以下命令创建集群。 注意,我们正在使用与上面创建的资源组相同的资源。 几分钟后,您将看到JSON格式的结果。

az aks create --resource-group myAKSGroup --name sampleAKSCluster --node-count 3 --enable-addons monitoring --generate-ssh-keys

You can see the following cluster in the console.

您可以在控制台中看到以下集群。

Image for post
sampleAKSCluster sampleAKSCluster

示例项目 (Example Project)

We have created the Kubernetes cluster on AKS. It’s time to look at the example project that we are deploying. Here is the Github link you can clone it and run it on your machine.

我们已经在AKS上创建了Kubernetes集群。 现在该来看一下我们正在部署的示例项目。 这是Github链接,您可以克隆它并在您的计算机上运行它。

// clone the project
git clone https://github.com/bbachi/sample-app-aks.git// Running on docker
docker build -t sample-aks-image .
docker run -d --name sample-aks -p 80:80 sample-aks-image// tag and push the image
docker tag sample-aks-image bbachin1/sample-aks-image
docker push bbachin1/sample-aks-image

This is a simple HTML file serving through the Nginx server.

这是一个通过Nginx服务器提供的简单HTML文件。

sample application 样品申请

Here is the manifest.yml file for the Kubernetes deployment and service objects

这是Kubernetes部署和服务对象的manifest.yml文件

manifest.yml manifest.yml

使用AKS群集配置Kuebctl (Configure Kuebctl With AKS Cluster)

Kubectl is the command-line utility for the Kubernetes. You need to install kubectl before you configure it. Run the first command only if you don’t have kubectl on your local machine.

Kubectl是Kubernetes的命令行实用程序。 您需要先安装kubectl,然后才能对其进行配置。 仅当本地计算机上没有kubectl时,才运行第一个命令。

// install CLI
az aks install-cli// connect to your cluster
az aks get-credentials --resource-group myAKSGroup --name sampleAKSCluster// get all the contexts
kubectl config get-contexts// verify the current context
kubectl config current-context// get the node
kubectl get nodes
Image for post
kubectl get nodes kubectl获取节点

创建部署并访问它 (Create a Deployment and Access it)

Let’s create a deployment with the following command and make sure you are in the root folder of the application. Since you are using type LoadBalancer you can have ingress traffic form the web.

让我们使用以下命令创建部署,并确保您位于应用程序的根文件夹中。 由于您使用的是LoadBalancer类型,因此您可以从Web获得入口流量。

// create a deployment
kubectl create -f manifest.yml// verify the deployment
kubectl get deploy
kubectl get po
kubectl get service
Image for post
kubectl get service kubectl获得服务
Image for post
Accessing it from the browser 从浏览器访问它

Kubernetes仪表板 (Kubernetes Dashboard)

We are done with the deployment and accessing it from the external browser. Let’s see our objects in the Kubernetes dashboard with the following command. Make sure you have the correct resource group name and cluster and the following command opens the Kubernetes dashboard in the browser.

部署完成,并从外部浏览器访问它。 让我们使用以下命令在Kubernetes仪表板中查看我们的对象。 确保您具有正确的资源组名称和集群,并且以下命令在浏览器中打开Kubernetes仪表板。

az aks browse --resource-group myAKSGroup --name sampleAKSCluster

You can access the dashboard with either Kubeconfig or Token. You can get the token with the following command.

您可以使用Kubeconfig或Token访问仪表板。 您可以使用以下命令获取令牌。

kubectl config view
Image for post
Kubernetes dashboard Kubernetes仪表板

Once you log in with token and you can see the following dashboard with all the Kubernetes objects.

使用令牌登录后,您将看到以下带有所有Kubernetes对象的仪表板。

Image for post
Kubernetes Dashboard Kubernetes仪表板

Sometime you might get the following error. If you get this error you have to delete and create cluster role binding.

有时您可能会收到以下错误。 如果出现此错误,则必须删除并创建集群角色绑定。

az aks dashboard is empty “There is nothing to display here”

az aks dashboard is empty “There is nothing to display here”

(solution)

// Run the following commandskubectl delete clusterrolebinding kubernetes-dashboardkubectl delete clusterrolebinding kubernetes-dashboard -n kube-systemkubectl create clusterrolebinding kubernetes-dashboard --clusterrole=cluster-admin --serviceaccount=kube-system:kubernetes-dashboard --user=clusterUser

删除集群 (Delete the Cluster)

You can just delete the cluster or the resource group. I created a resource group just for this so I am deleting the resource group with the following command. Make sure you delete if you don’t want to incur charges.

您可以只删除集群或资源组。 我为此专门创建了一个资源组,因此我要使用以下命令删除该资源组。 如果不想收取费用,请确保删除。

az group delete --name myAKSGroup

摘要 (Summary)

  • AKS is Microsoft Azure’s managed Kubernetes solution that lets you run and manage containerized applications in the cloud.

    AKS是Microsoft Azure的托管Kubernetes解决方案,可让您运行和管理云中的容器化应用程序。
  • Before starting this, you need to have docker and Kubernetes essentials. If you don’t have these essentials please go through these with the links provided.

    在开始之前,您需要具有docker和Kubernetes必备软件。 如果您没有这些必需品,请通过提供的链接进行阅读。
  • You need to create a Microsoft Azure Account here.

    您需要在此处创建一个Microsoft Azure帐户

  • You need a subscription to be associated with your tenant so that all the cost is billed to this subscription.

    您需要一个与您的租户关联的订阅,以便将所有费用记入此订阅。
  • You can create the AKS cluster through a portal, Azure CLI, REST API as well.

    您也可以通过门户,Azure CLI,REST API创建AKS群集。
  • You can install Azure CLI and configure it to use with your AKS Cluster.

    您可以安装Azure CLI并将其配置为与AKS群集一起使用。
  • Configure kubectl to use the AKS cluster.

    配置kubectl以使用AKS集群。
  • Create a deployment and service with Loadbalancer so that you can access it from outside.

    使用Loadbalancer创建部署和服务,以便您可以从外部访问它。
  • You can access the dashboard with either Kubeconfig or Token.

    您可以使用Kubeconfig或Token访问仪表板。
  • Make sure you delete if you don’t want to incur charges.

    如果不想收取费用,请确保删除。

结论 (Conclusion)

This is a very basic and beginner guide for managed Kubernetes service AKS from Azure. In future posts, we will explore more options such as RBAC, monitoring, etc.

这是Azure托管Kubernetes服务AKS的非常基础和入门的指南。 在以后的文章中,我们将探讨更多选项,例如RBAC,监视等。

翻译自: https://medium.com/bb-tutorials-and-thoughts/how-to-get-started-with-azure-aks-275fe5d2db40

azure 使用

Logo

开源、云原生的融合云平台

更多推荐