如何在Ubuntu上使用kubeadm设置Kubernetes 1.4
如果我们阅读了有关在本地运行Kubernetes的文章,则可能是我们熟悉Kubernetes,并渴望在真正的集群上尝试使用Kubernetes,而不是在Minikube的一个节点上运行它。
好吧,从那时起,Kubernetes 1.4出现了,它使得使用新的kubeadm命令设置任意大小的集群变得非常容易。
现在,我们将在两个Ubuntu 16.04节点上安装kubernetes 1.4.
准备机器
机器可以是VM实例或者物理服务器。
要准备机器,请在所有机器上运行:
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add
这是新存储库的关键,我们在此处添加的存储库
cat <<EOF > /etc/apt/sources.list.d/kubernetes.list deb http://apt.kubernetes.io/kubernetes-xenial main EOF
然后我们加载新的存储库列表
apt-get update
要实际安装,请运行以下命令:
apt-get install -y docker.io kubelet kubeadm kubectl kubernetes-cni
在所有节点上都运行完之后,我们可以继续建立集群。
初始化集群
安装完所需的一切之后,我们需要选择一个节点作为主节点。
在主运行中:
kubeadm init
该命令将运行几分钟,最后会给我们命令,我们需要在节点上运行以将其连接到该主服务器。
像这样
root@ubuntu-theitroad01:~# kubeadm init <master/tokens> generated token: "7fa96f.ddb39492a1894689" <master/pki> created keys and certificates in "/etc/kubernetes/pki" <util/kubeconfig> created "/etc/kubernetes/admin.conf" <util/kubeconfig> created "/etc/kubernetes/kubelet.conf" <master/apiclient> created API client configuration <master/apiclient> created API client, waiting for the control plane to become ready <master/apiclient> all control plane components are healthy after 23.051433 seconds <master/apiclient> waiting for at least one node to register and become ready <master/apiclient> first node is ready after 5.012029 seconds <master/discovery> created essential addon: kube-discovery, waiting for it to become ready <master/discovery> kube-discovery is ready after 13.005947 seconds <master/addons> created essential addon: kube-proxy <master/addons> created essential addon: kube-dns Kubernetes master initialised successfully! You can now join any number of machines by running the following on each node: kubeadm join --token 7fa96f.ddb39352a1894689 45.55.89.83 root@ubuntu-theitroad01
我重点介绍了在master上运行的kubeadm init命令以及它产生的kubeadm join命令。
将此命令保密,任何拥有它的人都可以使用它来将自己的节点连接到集群。
运行命令kubeadm join将节点加入到主服务器后,可以在主服务器上运行以下命令以验证所有操作是否顺利:
kubectl get nodes
但这并不意味着我们已经可以将应用程序放入群集中。
我们还需要做几件事。
首先是在主机上安装Pod网络,以便Pod可以相互通信:
kubectl apply -f https://git.io/weave-kube
其次,要使Pod在主节点和节点上运行,请使用以下命令:
kubectl taint nodes --all dedicated
如果运行kubernetes的机器数量很少,这很有用,在本教程中,我们有两个,一个主节点和一个节点。
在较大的群集上,我们可以省略该命令,因为最好选择不运行任何Pod的主机来确保安全性。
部署测试应用程序
我们要尝试的第一个应用程序是kubectl手册中的微服务示例。
让我们从使用git克隆应用程序开始
git clone https://github.com/microservices-demo/microservices-demo
接下来,我们使用kubectl命令部署应用程序:
kubectl apply -f microservices-demo/deploy/kubernetes/manifests
这将需要一些时间,因为要创建很多容器。
我们输入以下内容即可查看进度
kubectl get pods
如果有显示“ ContainterCreating”或者“ Running”以外的内容的Pods,则意味着我们需要再等一些,也许要吃三明治或者咖啡。
当所有容器都显示“正在运行”时,我们可以移至下一个命令。
这是:
kubectl describe svc front-end
这将为我们提供有关前端服务(svc或者服务)的数据,我们需要该数据来从外部访问该站点。
root@ubuntu-theitroad01:~# kubectl describe svc front-end Name: front-end Namespace: default Labels: name=front-end Selector: name=front-end Type: NodePort IP: 100.77.43.252 Port: <unset> 80/TCP NodePort: <unset> 31939/TCP Endpoints: 10.40.0.5:8079 Session Affinity: None
或者更好的图像:
这里最重要的是Type:NodePort行和NodePort:<unset> 31939/TCP。
kubeadm尚不支持LoadBalancer,因此我们使用NodePort,它将服务公开给主机IP上某个端口上的ideide world。
因此,我们获取了主IP,并从上述命令的NodPort:行添加了端口号,在本例中为31939.
因此,当我们在浏览器中键入该IP时,便获得了带有袜子的示例站点
这是我们的示例微服务应用程序。
如果要删除袜子应用程序,请运行以下命令
kubectl delete -f microservices-demo/deploy/kubernetes/manifests
从这里我们可以尝试更多有关kubernetes的示例
在Kubernetes 1.4中尝试新命令
kubeadm不仅在Kubernetes 1.4中进行了更改。
离得很远。
kubectl也进行了更新。
例如,kubectl hel命令变得更加清晰,而且……更有用。
让我们看看怎么说:
root@ubuntu-theitroad02:~# kubectl help kubectl controls the Kubernetes cluster manager. Find more information at https://github.com/kubernetes/kubernetes. Basic Commands (Beginner): create Create a resource by filename or stdin expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service run Run a particular image on the cluster set Set specific features on objects Basic Commands (Intermediate): get Display one or many resources explain Documentation of resources edit Edit a resource on the server delete Delete resources by filenames, stdin, resources and names, or by resources and label selector Deploy Commands: rollout Manage a deployment rollout rolling-update Perform a rolling update of the given ReplicationController scale Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job autoscale Auto-scale a Deployment, ReplicaSet, or ReplicationController Cluster Management Commands: cluster-info Display cluster info top Display Resource (CPU/Memory/Storage) usage cordon Mark node as unschedulable uncordon Mark node as schedulable drain Drain node in preparation for maintenance taint Update the taints on one or more nodes Troubleshooting and Debugging Commands: describe Show details of a specific resource or group of resources logs Print the logs for a container in a pod attach Attach to a running container exec Execute a command in a container port-forward Forward one or more local ports to a pod proxy Run a proxy to the Kubernetes API server Advanced Commands: apply Apply a configuration to a resource by filename or stdin patch Update field(s) of a resource using strategic merge patch replace Replace a resource by filename or stdin convert Convert config files between different API versions Settings Commands: label Update the labels on a resource annotate Update the annotations on a resource completion Output shell completion code for the given shell (bash or zsh) Other Commands: api-versions Print the supported API versions on the server, in the form of "group/version" config Modify kubeconfig files help Help about any command version Print the client and server version information Use "kubectl <command> --help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands).
Thre是新的-n标志,它使我们可以仅向一个虚拟集群或者所谓的名称空间发出命令。
所以这个命令:
kubectl get nodes -n kube-system
将获得属于kube-system名称空间的pod。
kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE etcd-ubuntu-theitroad02 1/1 Running 0 4h kube-apiserver-ubuntu-theitroad02 1/1 Running 0 4h kube-controller-manager-ubuntu-theitroad02 1/1 Running 0 4h kube-discovery-982812725-dxj6r 1/1 Running 0 4h kube-dns-2247936740-n44pu 3/3 Running 0 4h kube-proxy-amd64-oe87p 1/1 Running 0 4h kube-proxy-amd64-tuzwv 1/1 Running 0 4h kube-scheduler-ubuntu-theitroad02 1/1 Running 0 4h weave-net-as39l 2/2 Running 0 4h weave-net-fc0v5 2/2 Running 0 4h
还有一个新的kubectl top命令,它使我们能够获得Pod中的CPU和内存使用情况,但是对于该命令,我们需要安装heapster。