Kubernetes 简明教程

Kubernetes - Setup

在设置 Kubernetes 之前,设置虚拟数据中心 (vDC) 非常重要。这可以看作一组机器,它们可以通过网络相互通信。对于实践方法,如果您没有设置物理或云基础设施,您可以在 PROFITBRICKS 上设置 vDC。

It is important to set up the Virtual Datacenter (vDC) before setting up Kubernetes. This can be considered as a set of machines where they can communicate with each other via the network. For hands-on approach, you can set up vDC on PROFITBRICKS if you do not have a physical or cloud infrastructure set up.

在任何云上完成 IaaS 设置后,您需要配置 MasterNode

Once the IaaS setup on any cloud is complete, you need to configure the Master and the Node.

Note − 设置已针对 Ubuntu 计算机展示。也可以在其他 Linux 计算机上设置相同的设置。

Note − The setup is shown for Ubuntu machines. The same can be set up on other Linux machines as well.

Prerequisites

Installing Docker − Kubernetes 的所有实例都需要 Docker。以下是安装 Docker 的步骤。

Installing Docker − Docker is required on all the instances of Kubernetes. Following are the steps to install the Docker.

Step 1 − 使用根用户帐户登录到计算机。

Step 1 − Log on to the machine with the root user account.

Step 2 - 更新软件包信息。确保 apt 软件包正常。

Step 2 − Update the package information. Make sure that the apt package is working.

Step 3 - 运行以下命令。

Step 3 − Run the following commands.

$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates

Step 4 - 添加新的 GPG 密钥。

Step 4 − Add the new GPG key.

$ sudo apt-key adv \
   --keyserver hkp://ha.pool.sks-keyservers.net:80 \
   --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
$ echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main" | sudo tee
/etc/apt/sources.list.d/docker.list

Step 5 - 更新 API 软件包映像。

Step 5 − Update the API package image.

$ sudo apt-get update

一旦上述所有任务完成,就可开始实际安装 Docker 引擎。但是,需要先验证所用内核版本是否正确。

Once all the above tasks are complete, you can start with the actual installation of the Docker engine. However, before this you need to verify that the kernel version you are using is correct.

Install Docker Engine

运行以下命令可安装 Docker 引擎。

Run the following commands to install the Docker engine.

Step 1 - 登录到计算机。

Step 1 − Logon to the machine.

Step 2 - 更新软件包索引。

Step 2 − Update the package index.

$ sudo apt-get update

Step 3 - 使用以下命令安装 Docker Engine。

Step 3 − Install the Docker Engine using the following command.

$ sudo apt-get install docker-engine

Step 4 - 启动 Docker 守护程序。

Step 4 − Start the Docker daemon.

$ sudo apt-get install docker-engine

Step 5 - 要验证是否安装了 Docker,请使用以下命令。

Step 5 − To very if the Docker is installed, use the following command.

$ sudo docker run hello-world

Install etcd 2.0

这需要安装在 Kubernetes Master Machine 上。要安装它,请运行以下命令。

This needs to be installed on Kubernetes Master Machine. In order to install it, run the following commands.

$ curl -L https://github.com/coreos/etcd/releases/download/v2.0.0/etcd
-v2.0.0-linux-amd64.tar.gz -o etcd-v2.0.0-linux-amd64.tar.gz ->1
$ tar xzvf etcd-v2.0.0-linux-amd64.tar.gz ------>2
$ cd etcd-v2.0.0-linux-amd64 ------------>3
$ mkdir /opt/bin ------------->4
$ cp etcd* /opt/bin ----------->5

在上述命令集中 -

In the above set of command −

  1. First, we download the etcd. Save this with specified name.

  2. Then, we have to un-tar the tar package.

  3. We make a dir. inside the /opt named bin.

  4. Copy the extracted file to the target location.

现在我们可以准备构建 Kubernetes 了。我们需要在群集的所有计算机上安装 Kubernetes。

Now we are ready to build Kubernetes. We need to install Kubernetes on all the machines on the cluster.

$ git clone https://github.com/GoogleCloudPlatform/kubernetes.git
$ cd kubernetes
$ make release

上述命令将在 kubernetes 文件夹的根目录中创建一个 _output 目录。接下来,我们可以将该目录解压缩到任意我们选择的目录 /opt/bin 等中。

The above command will create a _output dir in the root of the kubernetes folder. Next, we can extract the directory into any of the directory of our choice /opt/bin, etc.

接下来,进入网络部分,其中我们需要开始实际设置 Kubernetes 主机和节点。为了做到这一点,我们将在可以在节点机器上执行的主机文件中创建一个条目。

Next, comes the networking part wherein we need to actually start with the setup of Kubernetes master and node. In order to do this, we will make an entry in the host file which can be done on the node machine.

$ echo "<IP address of master machine> kube-master
< IP address of Node Machine>" >> /etc/hosts

以下是以上命令的输出。

Following will be the output of the above command.

output

现在,我们将开始在 Kubernetes 主机上进行实际配置。

Now, we will start with the actual configuration on Kubernetes Master.

首先,我们将开始将所有配置文件复制到其正确的位置。

First, we will start copying all the configuration files to their correct location.

$ cp <Current dir. location>/kube-apiserver /opt/bin/
$ cp <Current dir. location>/kube-controller-manager /opt/bin/
$ cp <Current dir. location>/kube-kube-scheduler /opt/bin/
$ cp <Current dir. location>/kubecfg /opt/bin/
$ cp <Current dir. location>/kubectl /opt/bin/
$ cp <Current dir. location>/kubernetes /opt/bin/

以上命令将复制所有配置文件到所需位置。现在我们回到我们构建 Kubernetes 文件夹的相同目录。

The above command will copy all the configuration files to the required location. Now we will come back to the same directory where we have built the Kubernetes folder.

$ cp kubernetes/cluster/ubuntu/init_conf/kube-apiserver.conf /etc/init/
$ cp kubernetes/cluster/ubuntu/init_conf/kube-controller-manager.conf /etc/init/
$ cp kubernetes/cluster/ubuntu/init_conf/kube-kube-scheduler.conf /etc/init/

$ cp kubernetes/cluster/ubuntu/initd_scripts/kube-apiserver /etc/init.d/
$ cp kubernetes/cluster/ubuntu/initd_scripts/kube-controller-manager /etc/init.d/
$ cp kubernetes/cluster/ubuntu/initd_scripts/kube-kube-scheduler /etc/init.d/

$ cp kubernetes/cluster/ubuntu/default_scripts/kubelet /etc/default/
$ cp kubernetes/cluster/ubuntu/default_scripts/kube-proxy /etc/default/
$ cp kubernetes/cluster/ubuntu/default_scripts/kubelet /etc/default/

下一步是更新 /etc 下的已复制配置文件。目录。

The next step is to update the copied configuration file under /etc. dir.

使用以下命令在主机上配置 etcd。

Configure etcd on master using the following command.

$ ETCD_OPTS = "-listen-client-urls = http://kube-master:4001"

Configure kube-apiserver

为此,我们需要编辑我们先前复制的 /etc/default/kube-apiserver 文件。

For this on the master, we need to edit the /etc/default/kube-apiserver file which we copied earlier.

$ KUBE_APISERVER_OPTS = "--address = 0.0.0.0 \
--port = 8080 \
--etcd_servers = <The path that is configured in ETCD_OPTS> \
--portal_net = 11.1.1.0/24 \
--allow_privileged = false \
--kubelet_port = < Port you want to configure> \
--v = 0"

Configure the kube Controller Manager

我们需要在 /etc/default/kube-controller-manager 中添加以下内容。

We need to add the following content in /etc/default/kube-controller-manager.

$ KUBE_CONTROLLER_MANAGER_OPTS = "--address = 0.0.0.0 \
--master = 127.0.0.1:8080 \
--machines = kube-minion \ -----> #this is the kubernatics node
--v = 0

接下来,在相应文件中配置 kube 调度程序。

Next, configure the kube scheduler in the corresponding file.

$ KUBE_SCHEDULER_OPTS = "--address = 0.0.0.0 \
--master = 127.0.0.1:8080 \
--v = 0"

完成上述所有任务后,我们就可以通过启动 Kubernetes 主机继续进行。为了做到这一点,我们将重新启动 Docker。

Once all the above tasks are complete, we are good to go ahead by bring up the Kubernetes Master. In order to do this, we will restart the Docker.

$ service docker restart

Kubernetes Node Configuration

Kubernetes 节点将运行两个服务 kubelet and the kube-proxy 。在继续进行之前,我们需要将下载的二进制文件复制到我们希望配置 Kubernetes 节点所需文件夹。

Kubernetes node will run two services the kubelet and the kube-proxy. Before moving ahead, we need to copy the binaries we downloaded to their required folders where we want to configure the kubernetes node.

使用与我们在 Kubernetes 主机上所做的相同文件复制方法。因为它将只运行 kubelet 和 kube-proxy,所以我们将对其进行配置。

Use the same method of copying the files that we did for kubernetes master. As it will only run the kubelet and the kube-proxy, we will configure them.

$ cp <Path of the extracted file>/kubelet /opt/bin/
$ cp <Path of the extracted file>/kube-proxy /opt/bin/
$ cp <Path of the extracted file>/kubecfg /opt/bin/
$ cp <Path of the extracted file>/kubectl /opt/bin/
$ cp <Path of the extracted file>/kubernetes /opt/bin/

现在,我们将内容复制到适当的目录。

Now, we will copy the content to the appropriate dir.

$ cp kubernetes/cluster/ubuntu/init_conf/kubelet.conf /etc/init/
$ cp kubernetes/cluster/ubuntu/init_conf/kube-proxy.conf /etc/init/
$ cp kubernetes/cluster/ubuntu/initd_scripts/kubelet /etc/init.d/
$ cp kubernetes/cluster/ubuntu/initd_scripts/kube-proxy /etc/init.d/
$ cp kubernetes/cluster/ubuntu/default_scripts/kubelet /etc/default/
$ cp kubernetes/cluster/ubuntu/default_scripts/kube-proxy /etc/default/

我们将会配置 kubeletkube-proxy conf 文件。

We will configure the kubelet and kube-proxy conf files.

我们将配置 /etc/init/kubelet.conf

We will configure the /etc/init/kubelet.conf.

$ KUBELET_OPTS = "--address = 0.0.0.0 \
--port = 10250 \
--hostname_override = kube-minion \
--etcd_servers = http://kube-master:4001 \
--enable_server = true
--v = 0"
/

对于 kube-proxy,我们将使用以下命令进行配置。

For kube-proxy, we will configure using the following command.

$ KUBE_PROXY_OPTS = "--etcd_servers = http://kube-master:4001 \
--v = 0"
/etc/init/kube-proxy.conf

最后,我们将重新启动 Docker 服务。

Finally, we will restart the Docker service.

$ service docker restart

现在我们已经完成了配置。您可以通过运行以下命令进行检查。

Now we are done with the configuration. You can check by running the following commands.

$ /opt/bin/kubectl get minions