在Azure Stack上部署Kubernetes
Azure Stack是我們實現DevOps的重要利器,而要實現DevOps,離不開容器編排平台Kubernetes的支持。本文將介紹如何在Azure Stack ASDK環境里部署Kubernetes,在AKS for Azure Stack Mooncake正式發布之前,可以讓我們更方便地進行測試。
這次實驗環境位於Azure Global,請參考《Azure上一鍵部署Azure Stack》,該環境擁有以下特點:
單節點ASDK環境,默認情況下,其region為Local,外部域名為azurestack.external
位於Azure Global,也就是說可以無限制地訪問Internet,不受偉大的防火牆影響
Azure Stack用Azure中國的Azure AD賬戶進行部署,這和我們國內的生產環境完全一致。目前不支持ADFS模式部署。
不能用原版的acs-engine
大家知道,如果要在Azure中國里創建Kubernetes,可以藉助acs-engine來實現。但是Azure Stack則沒那麼簡單,原因是因為Azure Stack的ARM API端點和Azure公有雲完全不同,對於生產環境的多節點Azure Stack來說,則每個企業部署,其ARM API端點都有變化,所以必須對acs-engine進行修改,把Azure Stack相關的Cloud Profile參數開放出來。
以下是一個Azure Stack部署Kubernets的JSON文檔實例,可以看到這裡可以指定與Azure Stack相關的Cloud Profile參數:
{
"apiVersion":"vlabs",
"properties": {
"orchestratorProfile": {
"orchestratorType":"Kubernetes",
"orchestratorRelease":"1.8",
"kubernetesConfig": {
"CustomHyperkubeImage":"msazurestackdocker/k8s1.8:latest",
"networkPolicy":"none"
}
},
"cloudProfile": {
"name": "AzureStackCloud",
"managementPortalURL": "",
"publishSettingsURL": "",
"serviceManagementEndpoint": "https://management.azurestackci14.partner.onmschina.cn/9290752b-dc4c-4d0b-baf3-3a053daf101b",
"resourceManagerEndpoint": "https://management.local.azurestack.external",
"activeDirectoryEndpoint": "https://login.chinacloudapi.cn/",
"galleryEndpoint": "https://portal.local.azurestack.external:30015/",
"keyVaultEndpoint": "",
"graphEndpoint": "https://graph.chinacloudapi.cn/",
"sqlDatabaseDNSSuffix": "",
"trafficManagerDNSSuffix": "",
"serviceBusEndpointSuffix": "",
"serviceManagementVMDNSSuffix": "chinacloudapp.cn",
"resourceManagerVMDNSSuffix": "cloudapp.azurestack.external",
"containerRegistryDNSSuffix": "",
"resourceManagerRootCertificate": "*******ED26E1A51F5BB96E9356D6D610B74",
"location": "local"
},
"masterProfile": {
"count":1,
"dnsPrefix":"k8s-11145",
"vmSize":"Standard_D2_v2",
"StorageProfile":"StorageAccount"
},
"agentPoolProfiles": [
{
"name":"agentpool1",
"count":1,
"vmSize":"Standard_D2_v2",
"availabilityProfile":"AvailabilitySet",
"StorageProfile":"StorageAccount"
}
],
"linuxProfile": {
"adminUsername":"azureuser",
"ssh": {
"publicKeys": [
{
"keyData":""
}
]
}
},
"servicePrincipalProfile": {
"clientId":"",
"secret":""
}
}
}
我們可以自己修改該json文檔,使其符合我們的Azure Stack部署情況,然後用修改版的acs-engine生成ARM部署模板和參數模板。
但是這樣做太複雜,而且容易出錯,所以我們可以根據老外編寫的一個PowerShell腳本,我對其進行了修改,以便支持mooncake賬戶。來自動幫我們完成所需的工作。原版項目位於以下github地址:
https://github.com/radhikagupta5/AzureStack-QuickStart-Templates/tree/radhikgu-acs/101-acsengine-kubernetes-1803
準備工作
建議將Azure Stack註冊到Azure公有雲並激活,這樣我們就可以使用應用市場項目,需要下載Ubuntu16.04.201802220,以及Custom Script for Linux, 2.0.3。
自動生成自定義的json文檔
可以到以下github地址下載兩個文檔(AzureStack.AcsEngine.psm1和azurestack-default.json),並且拷貝到ASDK的主機文件夾里(建議保存在AzureStack-Tools-master目錄下)。
https://github.com/ahpeng/K8SOnAzureStack
然後執行以下命令,粗斜體部分的參數請用實際的值代替:
Import-module .ConnectAzureStack.Connect.psm1
Import-Module.AzureStack.AcsEngine.psm1 -Force
$namingSuffix = 10000..99999 | Get-Random
$masterDnsPrefix = "k8s-" + $namingSuffix
$tenantSubscriptionId = "*****-9923-4c0f-ad04-cd0045ff9d7f"
$tenantSubscriptionId的參數值參考以下的截圖。
$CloudAdminPass = ConvertTo-SecureString "Password" -AsPlainText -Force
$cloudAdminCredential = New-Object System.Management.Automation.PSCredential ("azurestackcloudadmin", $CloudAdminPass)
$AdminPass = ConvertTo-SecureString "Password" -AsPlainText -Force
$serviceAdminCredential = New-Object System.Management.Automation.PSCredential ($serviceAdmin, $AdminPass)
$TenantAdminPass = ConvertTo-SecureString "Password" -AsPlainText -Force
$tenantAdminCredential = New-Object System.Management.Automation.PSCredential ($TenantAdmin, $TenantAdminPass)
$acsSshKey = "****"
注意以下的命令里的粗斜體字參數,請用實際的ERCS虛擬機的IP地址來替換。
$apiModelParameters = @{"ErcsComputerName" = "192.168.200.225"; "CloudAdminCredential" = $cloudAdminCredential; "ServiceAdminCredential" = $serviceAdminCredential; "TenantAdminCredential" = $tenantAdminCredential; "TenantSubscriptionId" = $tenantSubscriptionId; "MasterDnsPrefix" = $masterDnsPrefix; "LinuxVmSshKey" = $acsSshKey; "NamingSuffix" = $namingSuffix;}
然後執行以下命令。
$apiModel = Prepare-AcseApiModel @apiModelParameters
命令執行結果如圖所示。
VERBOSE: Retrieving stampinformation from ERCS: 192.168.200.225.
VERBOSE: Retrieving Root CAcertificated from: https://management.local.azurestack.external/metadata/endpoints?api-version=1.0
VERBOSE: Total elementes in thecert chain are 2
VERBOSE: Last element in certchain is issued by: CN=contoso-DC-CA, DC=contoso, DC=com
VERBOSE: Retrieved certificatethumbprint is: ******3E92C84C04200B18806D279A55606E4
VERBOSE: TenantId:c7917735-5d61-4832-8b54-b11d5f1e7810, TenantArmEndpoint: https://management.local.azurestack.external
VERBOSE: Adding TenantAzureStack Environment.
VERBOSE: Performing theoperation "adding environment" on target "AzureStackUser".
VERBOSE: Logging to AzureCloudwith tenantId: ****-***-4832-8b54-b11d5f1e7810
WARNING: Parameter "Password" isobsolete. New-AzureRmADApplication: The parameter "Password" is beingchanged from a string to a SecureString in an upcoming breaking change release.
VERBOSE: Created new SPNClientID: ****-***-482e-b487-d2990c4bba65, Secret:****-****-4f49-aeb3-abc9f7b0d916
VERBOSE: Preparing the API model
VERBOSE: Placing the API modelto local location.
VERBOSE: Upload the locallycreated API model to a Storage Account.
VERBOSE: Creating or retrievingresource group: k8ssa-90322.
VERBOSE: Creating or retrievingstorage account: k8ssa90322.
VERBOSE: Creating or retrievingcontainer account: k8ssaci90322.
VERBOSE: Uploaded the API modelto: https://k8ssa90322.blob.local.azurestack.external/k8ssaci90322/azurestack.json.
VERBOSE: Blob root path: https://k8ssa90322.blob.local.azurestack.external/k8ssaci90322
可以看到,該PowerShell命令會從ERCS虛擬機的特權端點獲取Azure Stack的證書指紋,並且會自動在Azure中國創建服務主體,其名稱和密碼如命令行所示。並且自動將這些參數寫入到新生成的azurestack.json文件里。
確保Azure服務主體擁有Azure Stack的許可權
由於Kubernetes需要能夠在Azure Stack上創建負載均衡器、NSG規則等資源,所以需要對Azure服務主體(Azure SPN)進行賦權,以便其擁有合適的許可權,其做法和Azure公有雲並無二致。可以在上述的命令結果中找到SPN AppID,然後在Azure Stack租戶訂閱里給該SPN賬戶賦予參與者的許可權。如圖所示。
生成並部署ARM模板
找一台Linux虛擬機,執行以下命令:
首先克隆修改版的acs-engine項目
git clonehttps://github.com/msazurestackworkloads/acs-engine-bacs-engine-v0140-ci
cd acs-engine
解壓縮acs-engine的命令行:
sudo tar -zxvfexamples/azurestack/acs-engine.tgz
然後把前面生成的azurestack.json文檔上傳到該Linux虛擬機里。並執行以下命令:
sudo./acs-enginegenerate azurestack.json
即可生成Kubetnetes部署的ARM模板,將所生成的_output文件夾拷貝到Azure Stack環境。
打開該_output文件夾,其下有個子目錄,名稱就是azurestack.json里的masterDnsPrefix參數值。可以看到其中的APIServer證書,其中包含我們的Azure Stack的域名。
確保將以下參數改為:
"dockerEngineDownloadRepo": {
"value":"https://mirror.kaiyuanshe.cn/docker-engine/apt/repo/"
」。
並將以下參數改為:
"kubernetesTillerSpec": {
"value":"crproxy.trafficmanager.net:6000/kubernetes-helm/tiller:v2.8.1"
然後將該ARM模板部署到Azure Stack上。
華來四介紹
TAG:華來四Azure混合雲 |