入门指南

如何开始使用 Kubernetes,并创建任务

编辑这个页面

在 Azure (基于Flannel) 上运行 Kubernetes

前提条件

  1. 您需要一个Azure账号。如果没有,你可以在azure.microsoft.com创建一个。
  2. 具有所有者权限的账号。
  3. dockerjq都需要安装到 $PATH 目录中.

群集操作

打造群集

export KUBERNETES_PROVIDER=azure; curl -sS https://get.k8s.io | bash

注意: 如果出现一个 “the input device is not a TTY” 错误, 你需要手动部署。

cd ~/kubernetes
./cluster/kube-up.sh

注意: 这个脚本调用cluster/kube-up.sh, 而cluster/kube-up.sh反过来使用cluster/azure/config-default.sh调用cluster/azure/util.sh

如果你不设置AZURE_SUBSCRIPTION_ID 你将会得到一个错误。 在 Kubernetes 1.3.0 以前,不需要设置 AZURE_TENANT_ID. 这些可以设置到cluster/azure/config-default.sh里或者环境变量中:

export AZURE_SUBSCRIPTION_ID="<subscription-guid>"
export AZURE_TENANT_ID="<tenant-guid>" # only needed for Kubernetes < v1.3.0.

这些设置可以编辑cluster/azure/config-default.sh或者写入系统环境变量中。这些是他们的默认值:

export AZURE_DEPLOY_ID="" # autogenerated if blank
export AZURE_LOCATION="westus"
export AZURE_RESOURCE_GROUP="" # generated from AZURE_DEPLOY_ID if unset
export AZURE_MASTER_SIZE="Standard_A1"
export AZURE_NODE_SIZE="Standard_A1"
export AZURE_USERNAME="kube"
export NUM_NODES=3
export AZURE_AUTH_METHOD="device"

默认情况下,会部署一个具有4个 Standard_A1-大小的虚拟群集: 一个 master node, 3个工作 node。这个过程大概需要5到10分钟。一旦启动群集,就会显示群集的链接信息。此外,kubectl的配置文件也会被更新为这个新群集的信息。

Azure 部署过程中会创建 cluster/azure/_deployments/${AZURE_DEPLOY_ID}文件夹。在这个文件夹中你会找到群集的 PKI 和 SSH的配置文件,还有一个叫util.sh的脚本。下面是一些如何使用它的例子:

$ cd cluster/azure/_deployments/kube-20160316-001122/

# This uses the client cert with curl to make an http call to the apiserver.
$ ./util.sh curl api/v1/nodes

# This uses the client cert with kubectl to target this deployment's apiserver.
$ ./util.sh kubectl get nodes

# This alters the current kubectl configuration to point at this cluster.
$ ./util.sh configure-kubectl

# This will deploy the kube-system namespace, the SkyDNS addon, and the kube-dashboard addon.
$ ./util.sh deploy-addons

# This uses the ssh private key to copy the private key itself to the master node.
$ ./util.sh copykey

# This uses the ssh private key to open an ssh connection to the master.
$ ./util.sh ssh

群集部署示例

部署命名空间 kube-system

群集的组件会创建在命名空间 kube-system 里。

在 Kubernetes 1.3.0 以前,必须手动去创建这个命名空间。1.3.0开始,创建这个命名空间将会是打造 Azure 的一部分。对于1.3.0以前的版本,你可以执行这个脚本来创建kube-system 命名空间:

kubectl create -f https://raw.githubusercontent.com/colemickens/azkube/v0.0.5/templates/coreos/addons/kube-system.yaml

使用 kubectl proxy

kubectl proxy 目前用于链接部署的service

kubectl proxy --port=8001

部署的 service在这里: http://localhost:8001/api/v1/proxy/namespaces/<namespace>/services/<service_name>

组件: SkyDNS

你可以部署 SkyDNS 组件:

kubectl create -f https://raw.githubusercontent.com/colemickens/azkube/v0.0.5/templates/coreos/addons/skydns.yaml

组件: Kube-Dashboard

这命令会部署 kube-dashboard 组件:

kubectl create -f https://raw.githubusercontent.com/colemickens/azkube/v0.0.5/templates/coreos/addons/kube-dashboard.yaml

然后在这里就可以看到控制面板: http://localhost:8001/api/v1/proxy/namespaces/kube-system/services/dashboard-canary

示例: Guestbook

这回部署 guestbook 示例 (集成版):

kubectl create -f https://raw.githubusercontent.com/kubernetes/kubernetes/release-1.2/examples/guestbook/all-in-one/guestbook-all-in-one.yaml

然后你可以在这里看到 guestbook: http://localhost:8001/api/v1/proxy/namespaces/default/services/frontend.

扩容群集

azkube工具会在内部使用kube-up,也可以用来扩容你的群集。 这是把默认部署的3个 node 扩容到10个 node 的例子:

export AZURE_DEPLOY_ID="kube-20160316-001122"
$ docker run -it -v "$HOME/.azkube:/.azkube" -v "/tmp:/tmp" \
    colemickens/azkube:v0.0.5 /opt/azkube/azkube scale \
    --deployment-name="${AZURE_DEPLOY_ID}" \
    --node-size="Standard_A1" \
    --node-count=10

移除群集

你可以使用 kube-down.sh 来移除群集:

export AZURE_DEPLOY_ID="kube-20160316-001122"
$ ./cluster/kube-down.sh

Kubernetes 1.3之前,必须手动用 Azure CLI 来删除 Azure 上的群集。

注意事项

  1. 这些操作必须是所有者权限。
  2. 你可以在[ Azure 门户网站]](https://portal.microsoft.com)上找到你的用户 ID。 (All Resources → Subscriptions)
  3. The AZURE_AUTH_METHOD environment variable controls what authentication mechanism is used when bringing up the cluster. By default it is set to device. This allows the user to login via the a web browser. This interactive step can be automated by creating a Service Principal, setting AZURE_AUTH_METHOD=client_secret and setting AZURE_CLIENT_ID + AZURE_CLIENT_SECRET as appropriate for your Service Principal.
  4. The --node-size used in the scale command must be the same size deployed initially or it will not have the desired effect.
  5. Cluster tear-down requires manual intervention, due to the fact that it deletes the entire resource group and someone else may have deployed other resources since the initial deployment. For this reason you must confirm the list of resources that are to be deleted. If you wish to skip it, you may set AZURE_DOWN_SKIP_CONFIRM to true. This will delete everything in the resource group that was deployed to.
  6. If you are deploying from a checkout of kubernetes, then you will need to take an additional step to ensure that a hyperkube image is available. You can set AZURE_DOCKER_REGISTRY and AZURE_DOCKER_REPO and the deployment will ensure that a hyperkube container is built and available in the specified Docker registry. That hyperkube image will then be used throughout the cluster for running the Kubernetes services. Alternatively, you may set AZURE_HYPERKUBE_SPEC to point to a custom hyperkube image.

深入阅读

Analytics