CDC

Place to Be & Show Yourself

使用azure cli 部署 AKS

  1. kubernetes介绍
    见前一篇《CentOS7 安装kubernetes

  2. 建立命令行环境
    2.1. 安装Cli
    官方推荐方法:

导入 Microsoft 存储库密钥。
bash

sudo rpm –import https://packages.microsoft.com/keys/microsoft.asc
创建本地 azure-cli 存储库信息。
bash

sudo sh -c ‘echo -e “[azure-cli]\nname=Azure CLI\nbaseurl=https://packages.microsoft.com/yumrepos/azure-cli\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc” > /etc/yum.repos.d/azure-cli.repo’
使用 yum install 命令安装。
bash

sudo yum install azure-cli
** 但是上面安装GPG 检查出错,后面使用 yum install –nogpgcheck -y azure-cli 强制安装。

然后即可使用 az 命令来运行 Azure CLI。

2.2. 安装docker
docker 用于本地管理 AKS容器, 例如推、拉 docker 映像。

sudo yum install -y docker
sudo systemctl enable docker && sudo systemctl start docker

2.3. kubectl
kubectl 用于本地管理 AKS, 先要添加 kubernetes 源:

sudo bash -c ‘cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kube*
EOF’

然后安装 kubelet kubeadm kubectl:

sudo yum install -y kubelet kubeadm kubectl –disableexcludes=kubernetes

  1. 创建AKS
    3.1. 登录
    登录是命令行和浏览器结合的过程,

命令行执行 az login, 会出现访问 https://microsoft.com/devicelogin 提示,

访问 https://microsoft.com/devicelogin 并且输入 GH9TL5B6C , 登录完成。

[[email protected] ~]# az login
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code GH9TL5B6C to authenticate.

3.2. 建立资源组
资源组是在其中部署和管理 Azure 资源(例如 Web 应用、数据库和存储帐户)的逻辑容器。

先使用 az account list-locations 观察可用区域:

[[email protected] ~]# az account list-locations
[
……
{
“displayName”: “West US”,
“id”: “/subscriptions/699e1322-64be-4e6c-bc11-053aa48e1101/locations/westus”,
“latitude”: “37.783”,
“longitude”: “-122.417”,
“name”: “westus”,
“subscriptionId”: null
},
……
然后使用 az group create 创建,有两个参数需要指定,一个是资源组所在区域,使用上面命令得到,另外一个是 资源组名字,自己起一个:

[[email protected] ~]# az group create -l westus -n RainKCS
{
“id”: “/subscriptions/699e1322-64be-4e6c-bc11-053aa48e1101/resourceGroups/RainKCS”,
“location”: “westus”,
“managedBy”: null,
“name”: “RainKCS”,
“properties”: {
“provisioningState”: “Succeeded”
},
“tags”: null,
“type”: null
}
创建成功后可以在azure 控制台上看到:

3.3. 创建AKS
创建,使用 az aks create 命令,参数意义:

[[email protected] ~]# az aks create –resource-group RainKCS –name RainCluster –node-count 3 –node-vm-size Standard_B2s –generate-ssh-keys
{
“aadProfile”: null,
“addonProfiles”: null,
“agentPoolProfiles”: [
{
“availabilityZones”: null,
“count”: 3,
“enableAutoScaling”: null,
“maxCount”: null,
“maxPods”: 110,
“minCount”: null,
“name”: “nodepool1”,
“orchestratorVersion”: “1.12.8”,
“osDiskSizeGb”: 100,
“osType”: “Linux”,
“provisioningState”: “Succeeded”,
“type”: “AvailabilitySet”,
“vmSize”: “Standard_B2s”,
“vnetSubnetId”: null
}
],
“apiServerAuthorizedIpRanges”: null,
“dnsPrefix”: “RainCluste-RainKCS-699e13”,
“enablePodSecurityPolicy”: null,
“enableRbac”: true,
“fqdn”: “raincluste-rainkcs-699e13-9ac265ec.hcp.westus.azmk8s.io”,
“id”: “/subscriptions/699e1322-64be-4e6c-bc11-053aa48e1101/resourcegroups/RainKCS/providers/Microsoft.ContainerService/managedClusters/RainCluster”,
“kubernetesVersion”: “1.12.8”,
“linuxProfile”: {
“adminUsername”: “azureuser”,
“ssh”: {
“publicKeys”: [
{
“keyData”: “ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcyTrmGJZiM/x2o4ca9xZvKuJfO5L7Xn/GGbx/awq5siw9ToONDcbFhL95F8xk2lvFj2l3jcVDVxj+62myWAwP6Emp90GnVWTdL8iTrgQmlq6q9aMg1t2GpJSIEUxkvn1E2Oyo8TqXm31xmz4y2yzGkNVswjBwfElmCMOpicimjmZ8GdsQqAk+gLE6STpAZ2jeizKJ2tlYmnt7buNkEH5pgcnUxzPgpUxQDtFG9hUZyeOQYcEomXD3dyPgUucTAHCVo0hcgE5dqHCn8xeFKdZyXEbEtB4RFoWB/DnmsexqkpH+9U8wH0+a+u/2donGkbkvS6+xBNnm8dih3U+kKTKt [email protected]_deb161\n”
}
]
}
},
“location”: “westus”,
“name”: “RainCluster”,
“networkProfile”: {
“dnsServiceIp”: “10.0.0.10”,
“dockerBridgeCidr”: “172.17.0.1/16”,
“networkPlugin”: “kubenet”,
“networkPolicy”: null,
“podCidr”: “10.244.0.0/16”,
“serviceCidr”: “10.0.0.0/16”
},
“nodeResourceGroup”: “MC_RainKCS_RainCluster_westus”,
“provisioningState”: “Succeeded”,
“resourceGroup”: “RainKCS”,
“servicePrincipalProfile”: {
“clientId”: “73b0a29a-429c-4070-b529-7a626714773e”,
“secret”: null
},
“tags”: null,
“type”: “Microsoft.ContainerService/ManagedClusters”
}

创建后可以在portal 面板观察信息:

如果需要本地管理,可以下载连接到K8S群集凭据上下文到本地:

[[email protected] ~]# az aks get-credentials –resource-group RainKCS –name RainCluster
Merged “RainCluster” as current context in /root/.kube/config

然后在本地使用 kubectl 查看状态:

[[email protected] ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
aks-nodepool1-44923515-0 Ready agent 25m v1.12.8
aks-nodepool1-44923515-1 Ready agent 25m v1.12.8
aks-nodepool1-44923515-2 Ready agent 25m v1.12.8

[[email protected] ~]# kubectl get pod –all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-67fd67489b-6hkdp 1/1 Running 0 29m
kube-system coredns-67fd67489b-rlf4g 1/1 Running 0 36m
kube-system coredns-autoscaler-f654c64fd-9b6lw 1/1 Running 0 36m
kube-system heapster-6d879b9dc8-zzwmm 2/2 Running 0 29m
kube-system kube-proxy-dwqfd 1/1 Running 0 30m
kube-system kube-proxy-qbz44 1/1 Running 0 30m
kube-system kube-proxy-tq5s7 1/1 Running 0 30m
kube-system kubernetes-dashboard-7b55c6f7b9-j795q 1/1 Running 1 36m
kube-system metrics-server-67c75dbf7-4jd94 1/1 Running 1 36m
kube-system tunnelfront-699b7c8c86-ldrsh 1/1 Running 0 36m

如果要调整node数,可以使用下面命令:

[[email protected] ~]# az aks scale –resource-group RainKCS –name RainCluster –node-count 2

[[email protected] ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
aks-nodepool1-44923515-0 Ready agent 42m v1.12.8
aks-nodepool1-44923515-1 Ready agent 42m v1.12.8

至此aks 已经建立完成,后面的操作和本地操作kubernetes 一样。

  1. 发布服务测试
    4.1. 创建yaml文件
    把下面保存成 azure-vote.yaml

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
上面用Deployment定义了两个pod,并且声明了一个服务(处理者是带有标签 app: azure-vote-front 的 pod)。

4.2. 创建pod和服务
使用 kubectl apply -f 根据 yaml配置创建pod和服务。

[[email protected] ~]# vi azure-vote.yaml
[[email protected] ~]# kubectl apply -f azure-vote.yaml
deployment.apps/azure-vote-back created
service/azure-vote-back created
deployment.apps/azure-vote-front created
service/azure-vote-front created
观察服务状态:

[[email protected] ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
azure-vote-back-7fb47b8f6d-8k2t6 1/1 Running 0 25s
azure-vote-front-7dbf9f5dfb-p2rnn 0/1 ContainerCreating 0 24s
[[email protected] ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
azure-vote-back-7fb47b8f6d-8k2t6 1/1 Running 0 2m32s
azure-vote-front-7dbf9f5dfb-p2rnn 1/1 Running 0 2m31s
[[email protected] ~]# kubectl get service azure-vote-front –watch
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
azure-vote-front LoadBalancer 10.0.207.62 104.42.127.238 80:32044/TCP 5m47s

上面介绍中说明服务有两个ip: cluster-ip 相当于 dns 用于内部访问,对外服务ip是 external-ip。

使用浏览器访问 external-ip, 界面如下, 说明部署成功。

点赞

发表评论

电子邮件地址不会被公开。