文章目錄
- 概述
- Command Line Interface (CLI)
- The CF API
- 文檔
- Aliases
- 國際化
- API Endpoints
- Setting the Endpoint
- Authentication
- Authentication: Interactive Terminal Login
- Authentication: Interactive Browser Login
- Authentication: Logging Out
- cf push
- Checking Your Work
- Wait, What Just Happened?
- And What Didn't Happen?
概述
在本子產品中,我們開始使用雲鑄造。我們引入了CLI或指令行接口,用于與雲計算執行個體互動。我們将在平台上部署我們的第一個應用程式,來體驗一下接下來會發生什麼:
- 我們将向您介紹CLI,這是一個用于與Cloud Foundry互動的工具。
- 我們将解釋如何使用不同的雲計算執行個體。
-
您将開始将您的第一個應用程式部署到Cloud Foundry。本節的目的是強調Cloud
Foundry為複雜的開發人員工作流帶來的簡單性和能力。
Command Line Interface (CLI)
指令行接口(或CLI)用于通過REST API與雲計算(CF)執行個體互動。cf CLI向平台使用者公開了人性化的指令,并且是通用核心,這意味着它可以與所有的Cloud Foundry發行版一起工作。
The CF API
cf CLI正在與Cloud Foundry API通信,使開發人員能夠以使用者友好的方式向API送出請求。
您也可以通過cf curl直接向API送出請求,但這往往更複雜。我們稍後會更多地讨論cf旋度。
在本課程中,我們将使用cf CLI (v7)的最新版本,它由最新版本的cf API (v3)支援。確定你使用的是版本7+是很重要的,因為我們将在整個課程中使用v3的特性。
按照指令行版本7的安裝說明安裝指令行。
通過運作cf version,確定運作的是CLI的version 7。
文檔
CLI具有大量的自文檔,是以熟悉它的導航非常有幫助。
作為一個總結:
-
顯示最常用指令的幫助資訊Cf help
-
-顯示所有可用指令的幫助資訊Cf help
-
(或——help)顯示執行特定指令的詳細幫助。Cf -h
如果執行cf <指令>,而不帶必要參數,則CLI将輸出詳細的幫助文本,就像執行cf <指令>——help一樣。然而,省略——help并假設CLI将始終輸出幫助文本是有風險的,因為不是所有指令都需要額外的參數。例如,如果您要運作cf push,希望看到幫助文本和目前目錄中存在的清單,您可能無意中推一個應用程式。是以,熟悉cf <指令>——help工作流是很有幫助的。
嘗試運作
cf help -a
來檢視所有可用的cf指令,然後在幾個指令上運作——help标志,以便熟悉幫助輸出。
如果你正在準備Cloud Foundry Certified Developer考試,你可以在考試期間通路cf help,因為cf CLI安裝在考試系統中。
Aliases
許多指令都有别名(例如,cf m作為cf市場的快捷方式)。别名在幫助描述中列出。例如,在cf幫助中的Services integration:部分,你應該看到:
marketplace,m
逗号後的m是快捷方式。随着您對更常見的CLI指令的熟悉,快捷方式可以節省您的時間。
國際化
The CLI supports displaying text and descriptions in multiple languages. The name of the commands won’t change, but help text and command descriptions show in the language of your choice.
For the most up-to-date information on supported languages, see the Cloud Foundry docs.
To change the locale in use, you can run:
cf config --locale <LOCALE>
For example, you can change to German using:
cf config --locale de-DE
You can rerun cf help commands to see the effect.
Using a locale of CLEAR will remove any value set previously:
cf config --locale CLEAR
We will discuss more advanced CF CLI concepts towards the end of this course.
API Endpoints
cf指令行用于與任何雲計算執行個體互動。它對正在使用的特定執行個體的CF API端點進行RESTful調用。
Setting the Endpoint
Before you interact with a Cloud Foundry instance using the cf CLI, you need set the API endpoint. When you set an API endpoint, you are selecting an instance of Cloud Foundry to use. You may hear some vendors refer to deployments as “foundations” or “instances”, which mean the same thing. They are a running Cloud Foundry.
You can set the API endpoint either with the cf api command, or as part of the cf login command (you will see this below). Go ahead and set your API endpoint using:
cf api <API_ENDPOINT>
You should be able to see the endpoint you set by running:
cf api
Authentication
After setting the API endpoint, you need to authenticate with the Cloud Foundry instance you are using. There are numerous options for authentication (used in different circumstances), but for now, we will interactively authenticate using cf login.
The cf login command has features and flags (view these by running cf login --help). The options allow you to authenticate interactively or in non-interactive mode, which we discuss on the next pages. The -a flag, is used to specify the API endpoint when logging in (rather than using the cf api command separately).
Be careful when using non-interactive authentication, as credentials can end up in your terminal history.
Authentication: Interactive Terminal Login
To authenticate interactively in the terminal, run cf login without any flags. The CLI will prompt you to provide input. In this mode, your sensitive credentials are not stored in the command shell history. If you scroll through the command shell history in your terminal using the up-arrow key, you will see the
cf login
command, but will not see your username or password.
Authentication: Interactive Browser Login
The cf login command also supports interactive login through your browser via the -sso (single sign-on) flag. Interactive browser login works well with browser-integrated password managers like 1Password, LastPass, or KeePass. Because you are authenticating via a browser, you have mitigated the risk of your password ending up in your shell history.
You can login interactively via:
cf login --sso
The CLI will output a URL. Copy and paste this in a browser, and authenticate in exchange for a passcode. You can copy and paste the passcode provided into the terminal window to complete the authentication flow.
CFCD Exam Tip: The one-time passcode flow requires you to open pages in a browser. For this reason, you should not use cf login --sso in the exam, as it may take you to a non-approved URL.
Authentication: Logging Out
When you authenticate with Cloud Foundry, the CLI caches a token locally. When you make requests (like pushing an app), the CLI includes this token in each request. Therefore, you do not have to re-authenticate on every request and continue to work until your token expires (and cannot be refreshed). At this time, you would need to re-authenticate. However, this also means you need to be mindful to log out when appropriate to ensure a malicious actor does not gain access to your Cloud Foundry.
If you run cf target, you will see that you are authenticated. You can then log out using:
cf logout
If you re-run cf target, you will see you are no longer logged in.
cf push
使用cf push 指令将應用部署到Cloud Foundry。
我們将部署的應用程式沒有什麼特别之處——它是一個簡單的靜态應用程式,命名為
first-push
,用于教育訓練目的。您将在課程資源的
/applications
目錄中找到第一個推送應用程式(您可以從Menu中的resources頁籤下載下傳它們)。
在你的終端,更改目錄,使你在第一個push目錄:
cd
在這個目錄中,與應用程式一起的是一個名為manifest (manifest.yml)的檔案。manifest為Cloud Foundry提供了如何運作應用程式的指令。
你現在可以在這個目錄下通過運作以下指令來部署
first-push
應用:
cf push
預設情況下,push指令查找名為manifest的檔案。并使用它來部署應用程式。
您可以檢視用于部署應用程式的清單。讓我們看看
manifest.yml
中的指令:
-
name: first-push
在Cloud Foundry中應用程式的名稱。名稱應該是供人類使用的描述性名稱,可以是任何您想要的名稱。
-
instances: 1
The number of instances of the application Cloud Foundry should create.
-
memory: 32M
The amount of memory allocated to the container for each application instance.
-
disk_quota: 64M
The amount of disk allocated to the container for each application instance.
-
random-route: true
Specifies that Cloud Foundry should generate a random route for accessing the app.
-
buildpacks:
The buildpack(s) used to containerize your application.
staticfile_buildpack - In this case, we only need the staticfile buildpack.
Checking Your Work
The push should take about a minute. If everything is successful, you should see output for your running application similar to:
name: first-push
requested state: started
routes: first-push-active-quokka-dy.<my-domain>
last uploaded: Mon 24 May 11:52:30 BST 2021
stack: cflinuxfs3
buildpacks:
name version detect output buildpack name
staticfile_buildpack 1.5.17 staticfile staticfile
type: web
sidecars:
instances: 1/1
memory usage: 32M
start command: $HOME/boot.sh
state since cpu memory disk details
#0 running 2021-05-24T10:52:41Z 0.0% 0 of 32M 0 of 64M
The application has a user interface that shows some details about the application. You can copy the URL of your application from your terminal output under routes and open it in a tab in your browser.
Wait, What Just Happened?
In short, you deployed an app using a single command! It would have looked the same regardless of the language of the app. The CLI on your machine uploaded the app bits to Cloud Foundry and provided some information about what should happen.
Cloud Foundry took your app bits and used a buildpack to create a container image with your app, plus any necessary runtime dependencies. The resulting image was then run in a container. Once your app was running, Cloud Foundry began to automatically route traffic to it (hence your ability to access it in a browser). We discuss this process in more detail later in the course.
And What Didn’t Happen?
You didn’t build an image with a tool like Docker (though Cloud Foundry supports Docker, too). You didn’t install a runtime or manipulate any filesystems. You didn’t manually provision any resources like compute and storage. You didn’t create and deploy a load balancer or reserve any ports. You didn’t update routing tables or do any health checks. You just typed cf push. Pretty cool, right?
The rest of the course will build on these experiences.
