Kubernetes

:::info 前提条件:

  1. 访问主机CentOS8的和Docker的驱动不兼容,所以不能使用CentOS8以上系列的服务主机
  2. 2核CPU的服务主机(必要
  3. 大于2G 内存的服务器主机[大于2G是为了防止其他硬件占用内存导致Kubernetes的使用内存不足的问题](必要
  4. 这里的驱动是基于Docker,如果未安装则需要安装启动Docker,执行命令:yum install -y docker

:::

1、先决条件

安装Docker

Docker安装和启动

安装kubelet kubeadm kubectl

安装升级Kubernetes基础组件kubelet kubeadm kubectl

2、安装minikube

参考:https://minikube.sigs.k8s.io/docs/start/

rpm方式安装

minikube-latest.x86_64.zip

  1. rpm -Uvh minikube-latest.x86_64.rpm

命令安装

  1. curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
  2. sudo install minikube-linux-amd64 /usr/local/bin/minikube

3、创建非root用户以及Docker组,并授权

  1. useradd fcant
  2. passwd fcant
  3. groupadd docker
  4. usermod -aG docker fcant
  5. usermod -s /bin/bash fcant

:::danger 注意:如果在创建非root用户时已经启动Docker,则需要在用户授权成功后重启Docker使授权生效。

执行命令:systemctl restart docker

避免的问题:在非root用户启动时,导致无法读取的权限错误。

:::

MiniKube—Kubernetes单机版部署 - 图1

4、为非root用户授予root用户权限

:::tips 防止后面的操作中出现创建的非root用户没有权限的错误

MiniKube—Kubernetes单机版部署 - 图2

:::

  1. sudo visudo
  2. # 或者下面的命令、vim编辑时有高亮提示
  3. vim /etc/sudoers

MiniKube—Kubernetes单机版部署 - 图3

5、切换非root用户,启动minikube

国内存在网络问题,所以启动时指定了镜像仓库地址

  1. $ su fcant
  2. $ minikube start --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
  3. 😄 minikube v1.20.0 on Centos 7.6.1810 (amd64)
  4. Using the docker driver based on existing profile
  5. 👍 Starting control plane node minikube in cluster minikube
  6. 🚜 Pulling base image ...
  7. > index.docker.io/kicbase/sta...: 358.10 MiB / 358.10 MiB 100.00% 2.78 MiB
  8. 🤷 docker "minikube" container is missing, will recreate.
  9. 🔥 Creating docker container (CPUs=2, Memory=2200MB) ...
  10. 🐳 Preparing Kubernetes v1.20.2 on Docker 20.10.6 ...
  11. Generating certificates and keys ...
  12. Booting up control plane ...
  13. Configuring RBAC rules ...
  14. 🔎 Verifying Kubernetes components...
  15. Using image registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetesui/dashboard:v2.1.0@sha256:7f80b5ba141bead69c4fee8661464857af300d7d7ed0274cf7beecedc00322e6 (global image repository)
  16. Using image registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-minikube/storage-provisioner:v5 (global image repository)
  17. Using image registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetesui/metrics-scraper:v1.0.4@sha256:555981a24f184420f3be0c79d4efb6c948a85cfce84034f85a563f4151a81cbf (global image repository)
  18. 🌟 Enabled addons: default-storageclass, storage-provisioner, dashboard
  19. 🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

MiniKube—Kubernetes单机版部署 - 图4

出错1:failed to parse kernel config: unable to load kernel module: “configs”

错误内容

  1. $ minikube start --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --driver=docker --extra-config=kubelet.cgroup-driver=systemd
  2. 😄 minikube v1.26.1 on Ubuntu 22.04
  3. Using the docker driver based on existing profile
  4. 👍 Starting control plane node minikube in cluster minikube
  5. 🚜 Pulling base image ...
  6. 🏃 Updating the running docker "minikube" container ...
  7. 🐳 Preparing Kubernetes v1.24.3 on Docker 20.10.17 ...
  8. kubelet.cgroup-driver=systemd
  9. Generating certificates and keys ...
  10. 💢 initialization failed, will try again: wait: /bin/bash -c "sudo env PATH="/var/lib/minikube/binaries/v1.24.3:$PATH" kubeadm init --config /var/tmp/minikube/kubeadm.yaml --ignore-preflight-errors=DirAvailable--etc-kubernetes-manifests,DirAvailable--var-lib-minikube,DirAvailable--var-lib-minikube-etcd,FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml,FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml,FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml,FileAvailable--etc-kubernetes-manifests-etcd.yaml,Port-10250,Swap,Mem,SystemVerification,FileContent--proc-sys-net-bridge-bridge-nf-call-iptables": Process exited with status 1
  11. stdout:
  12. [init] Using Kubernetes version: v1.24.3
  13. [preflight] Running pre-flight checks
  14. [preflight] The system verification failed. Printing the output from the verification:
  15. KERNEL_VERSION: 5.15.0-46-generic
  16. OS: Linux
  17. CGROUPS_CPU: enabled
  18. CGROUPS_CPUSET: enabled
  19. CGROUPS_DEVICES: enabled
  20. CGROUPS_FREEZER: enabled
  21. CGROUPS_MEMORY: enabled
  22. CGROUPS_PIDS: enabled
  23. CGROUPS_HUGETLB: enabled
  24. CGROUPS_BLKIO: missing
  25. [preflight] Pulling images required for setting up a Kubernetes cluster
  26. [preflight] This might take a minute or two, depending on the speed of your internet connection
  27. [preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
  28. [certs] Using certificateDir folder "/var/lib/minikube/certs"
  29. [certs] Using existing ca certificate authority
  30. [certs] Using existing apiserver certificate and key on disk
  31. stderr:
  32. W0901 15:17:29.597926 4076 initconfiguration.go:120] Usage of CRI endpoints without URL scheme is deprecated and can cause kubelet errors in the future. Automatically prepending scheme "unix" to the "criSocket" with value "/var/run/cri-dockerd.sock". Please update your configuration!
  33. [WARNING SystemVerification]: missing optional cgroups: blkio
  34. [WARNING SystemVerification]: failed to parse kernel config: unable to load kernel module: "configs", output: "modprobe: FATAL: Module configs not found in directory /lib/modules/5.15.0-46-generic\n", err: exit status 1
  35. [WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
  36. error execution phase certs/apiserver-kubelet-client: [certs] certificate apiserver-kubelet-client not signed by CA certificate ca: x509: certificate signed by unknown authority (possibly because of "crypto/rsa: verification error" while trying to verify candidate authority certificate "minikubeCA")
  37. To see the stack trace of this error execute with --v=5 or higher
  38. Generating certificates and keys ...
  39. 💣 Error starting cluster: wait: /bin/bash -c "sudo env PATH="/var/lib/minikube/binaries/v1.24.3:$PATH" kubeadm init --config /var/tmp/minikube/kubeadm.yaml --ignore-preflight-errors=DirAvailable--etc-kubernetes-manifests,DirAvailable--var-lib-minikube,DirAvailable--var-lib-minikube-etcd,FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml,FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml,FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml,FileAvailable--etc-kubernetes-manifests-etcd.yaml,Port-10250,Swap,Mem,SystemVerification,FileContent--proc-sys-net-bridge-bridge-nf-call-iptables": Process exited with status 1

解决方案

参考https://github.com/kubernetes/minikube/issues/14477,通过指定参数--kubernetes-version=v1.23.8降低Kubernetes版本成功启动,注意在重启前需要删除原来的旧版本的Kubernetes集群

  1. $ minikube start --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --driver=docker --kubernetes-version=v1.23.8
  2. 😄 minikube v1.26.1 on Ubuntu 22.04
  3. 🙈 Exiting due to K8S_DOWNGRADE_UNSUPPORTED: Unable to safely downgrade existing Kubernetes v1.24.3 cluster to v1.23.8
  4. 💡 Suggestion:
  5. 1) Recreate the cluster with Kubernetes 1.23.8, by running:
  6. minikube delete
  7. minikube start --kubernetes-version=v1.23.8
  8. 2) Create a second cluster with Kubernetes 1.23.8, by running:
  9. minikube start -p minikube2 --kubernetes-version=v1.23.8
  10. 3) Use the existing cluster at version Kubernetes 1.24.3, by running:
  11. minikube start --kubernetes-version=v1.24.3
  12. $ minikube delete
  13. 🔥 Deleting "minikube" in docker ...
  14. 🔥 Deleting container "minikube" ...
  15. 🔥 Removing /home/fcant/.minikube/machines/minikube ...
  16. 💀 Removed all traces of the "minikube" cluster.

旧集群删除完成后,再启动指定Kubernetes版本的集群

  1. $ minikube start --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --driver=docker --kubernetes-version=v1.23.8
  2. 😄 minikube v1.26.1 on Ubuntu 22.04
  3. Using the docker driver based on user configuration
  4. Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
  5. 📌 Using Docker driver with root privileges
  6. 👍 Starting control plane node minikube in cluster minikube
  7. 🚜 Pulling base image ...
  8. 🔥 Creating docker container (CPUs=2, Memory=2200MB) ...
  9. > kubeadm.sha256: 64 B / 64 B [-------------------------] 100.00% ? p/s 0s
  10. > kubelet.sha256: 64 B / 64 B [-------------------------] 100.00% ? p/s 0s
  11. > kubectl.sha256: 64 B / 64 B [-------------------------] 100.00% ? p/s 0s
  12. > kubeadm: 43.12 MiB / 43.12 MiB [-------------] 100.00% 5.73 MiB p/s 7.7s
  13. > kubectl: 44.44 MiB / 44.44 MiB [--------------] 100.00% 3.71 MiB p/s 12s
  14. > kubelet: 118.78 MiB / 118.78 MiB [------------] 100.00% 6.27 MiB p/s 19s
  15. Generating certificates and keys ...
  16. Booting up control plane ...
  17. Configuring RBAC rules ...
  18. 🔎 Verifying Kubernetes components...
  19. Using image registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v5
  20. 🌟 Enabled addons: storage-provisioner, default-storageclass
  21. /usr/local/bin/kubectl is version 1.25.0, which may have incompatibilites with Kubernetes 1.23.8.
  22. Want kubectl v1.23.8? Try 'minikube kubectl -- get pods -A'
  23. 🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

6、启动可视化管理界面Dashboard

  1. $ minikube dashboard --url
  2. 🤔 Verifying dashboard health ...
  3. 🚀 Launching proxy ...
  4. 🤔 Verifying proxy health ...
  5. http://127.0.0.1:44581/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/

MiniKube—Kubernetes单机版部署 - 图5

启动kube proxy

  1. $ kubectl proxy --port=8888 --address='0.0.0.0' --accept-hosts='^.*'
  2. Starting to serve on [::]:8888

MiniKube—Kubernetes单机版部署 - 图6

然后使用minikube dashboard --url命令给出的URL(将端口修改为上面的代理端口8888,如果使用的是虚拟机则需要将IP修改为虚拟机的IP),然后在浏览器中访问即可看到如下管理界面:

MiniKube—Kubernetes单机版部署 - 图7

7、配置tunnel外网访问服务

如果未配置tunnel,EXTERNAL-IP列显示的是pending状态

MiniKube—Kubernetes单机版部署 - 图8

由于应用部署在集群内部,集群有自己对应的IP,所以外网无法访问,需要用到tunnel。

  1. $ minikube tunnel
  2. [sudo] password for fcant:
  3. Status:
  4. machine: minikube
  5. pid: 76873
  6. route: 10.96.0.0/12 -> 192.168.49.2
  7. minikube: Running
  8. services: [ingress-nginx]
  9. errors:
  10. minikube: no errors
  11. router: no errors
  12. loadbalancer emulator: no errors
  13. Status:
  14. machine: minikube
  15. pid: 76873
  16. route: 10.96.0.0/12 -> 192.168.49.2
  17. minikube: Running
  18. services: [ingress-nginx]
  19. errors:
  20. minikube: no errors
  21. router: no errors
  22. loadbalancer emulator: no errors

注意该命令执行需要输入当前用户的登录密码,命令执行示例如下:

MiniKube—Kubernetes单机版部署 - 图9

然后再检查EXTERNAL-IP,可以发现已经有正确的IP了,并且使用curl去访问能得到正确的响应。

MiniKube—Kubernetes单机版部署 - 图10