快速入门Kubernetes

安装篇

我们以Ubuntu为例,介绍Kubernetes基础工具的安装,若你使用其他操作系统,可以参考官方文档

首先安装kubectl:

  1. sudo apt-get update
  2. sudo apt-get install -y apt-transport-https ca-certificates curl
  3. sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
  4. echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
  5. sudo apt-get update
  6. sudo apt-get install -y kubectl

接着,我们安装minikube,这是一个用于本地学习和测试的kubernetes集群:

  1. curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb
  2. sudo dpkg -i minikube_latest_amd64.deb

启动minikube

第一次启动Minikube,需要下载虚拟机、对应镜像,时间回稍长一些。

  1. minikube start

成功后,我们看下状态:

  1. minikube
  2. type: Control Plane
  3. host: Running
  4. kubelet: Running
  5. apiserver: Running
  6. kubeconfig: Configured

如果需要关机,可以暂停 / 恢复minikube集群

  1. minikube pause
  2. minikube resume

如果想重置minikube集群,可以使用删除后重新启动

  1. minikube delete

部署你的第一个服务

我们在minikube上部署一台nginx

  1. kubectl create deployment my-nginx --image=nginx:stable

稍等片刻后,我们看下,已经创建成功:

  1. kubectl get pod
  2. NAME READY STATUS RESTARTS AGE
  3. my-nginx-7bc876dc4b-r5zqr 1/1 Running 0 22s

我们查看下pod的信息,特别是IP

  1. kubectl describe pod my-nginx-7bc876dc4b-r5zqr | grep IP
  2. IP: 172.17.0.3

我们尝试访问一下,发现无法成功:

  1. curl "http://172.17.0.3"

这是因为,minikube的网络环境,与我们本机是相互隔离的,我们需要先登录到minikube内,然后再尝试:

  1. minikube ssh
  2. curl "http://172.17.0.3"
  3. % Total % Received % Xferd Average Speed Time Time Time Current
  4. Dload Upload Total Spent Left Speed
  5. 100 612 100 612 0 0 597k 0 --:--:-- --:--:-- --:--:-- 597k

成功!

下面,我们退出minikube集群环境,尝试对nginx部署扩容:

  1. kubectl scale deployment my-nginx --replicas=5
  2. deployment.apps/my-nginx scaled

稍等片刻后,我们查看,发现扩容成功:

  1. kubectl get pod
  2. NAME READY STATUS RESTARTS AGE
  3. my-nginx-7bc876dc4b-226g9 1/1 Running 0 60s
  4. my-nginx-7bc876dc4b-872v2 1/1 Running 0 60s
  5. my-nginx-7bc876dc4b-fvnwf 1/1 Running 0 60s
  6. my-nginx-7bc876dc4b-fzr8s 1/1 Running 0 60s
  7. my-nginx-7bc876dc4b-r5zqr 1/1 Running 1 5m36s

如何在mini集群外(例如我们本地)访问nginx呢?

可以为上述deployment,暴露外部的LoadBalancer:

  1. kubectl expose deployment my-nginx --type=LoadBalancer --port=80

我们看一下状态,会发现外部的IP是”pending”

  1. kubectl get services
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 67m
  4. my-nginx LoadBalancer 10.104.5.62 <pending> 80:30229/TCP 37s 8m18s

需要启用minikube的隧道,来分配”外部IP”,这里的外部是相对于minikube而言的,实际上是我们本机网络的IP。

  1. minikube tunnel
  2. kubectl get services
  3. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  4. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 67m
  5. my-nginx LoadBalancer 10.104.5.62 127.0.0.1 80:30229/TCP 24s
  6. 9m25s

启动隧道后,发现暴露到了127.0.0.1的80端口上,我们试一下:

  1. curl "http://127.0.0.1:80"
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Welcome to nginx!</title>
  6. <style>
  7. body {
  8. width: 35em;
  9. margin: 0 auto;
  10. font-family: Tahoma, Verdana, Arial, sans-serif;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>Welcome to nginx!</h1>
  16. <p>If you see this page, the nginx web server is successfully installed and
  17. working. Further configuration is required.</p>
  18. <p>For online documentation and support please refer to
  19. <a href="http://nginx.org/">nginx.org</a>.<br/>
  20. Commercial support is available at
  21. <a href="http://nginx.com/">nginx.com</a>.</p>
  22. <p><em>Thank you for using nginx.</em></p>
  23. </body>
  24. </html>

minikube也提供了可视化的Dashboard:

  1. minikube dashboard --url
  2. http://127.0.0.1:59352/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/

在浏览器中打开上述连接,可以进入Web版的Dashboard,如下图所示:

f

至此,你已经通过在minikube上的实战演练,掌握了kubernetes的基本用法。

在实际生产环境中,建议你搭建真实的分布式集群,不要使用minikube,我将在后续章节,介绍高可用k8s集群的部署。