Kubernetes 是一个生产级别的 容器编排平台 和 集群管理系统 ,能够 创建、调度容器,监控、管理服务器。
那么,既可以管理软件,也可以管理硬件,就是一个操作系统(Operating System)。
资源管理
和作业调度
。由于云原生的兴起,开发人员从一开始就必须考虑后续的部署运维工作,而运维人员也需要在早期介入开发,才能做好应用的运维监控工作。
快速搭建 Kubernetes 环境的工具:
kind
minikube
我们选择minikube
来学习Kubernetes。
实验环境:2c4g
https://minikube.sigs.k8s.io/docs/
sudo apt install -y docker.io #安装Docker Engine
sudo service docker start #启动docker服务
sudo usermod -aG docker ${USER} #当前用户加入docker组,需要重新登录生效
docker info
# Intel x86_64
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
# Apple arm64
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-arm64
sudo install minikube /usr/local/bin/
安装完成后,查看minikube版本:
lianhaifeng@lianhaifeng-virtual-machine:~$ minikube version
minikube version: v1.30.1
commit: 08896fd1dc362c097c925146c4a0d0dac715ace0
要操作 Kubernetes,还需要另一个专门的客户端工具“kubectl
”:
minikube kubectl
~/.bashrc
追加命令(如果没有的话),简化kubectl命令:
...
alias kubectl="minikube kubectl --"
source <(kubectl completion bash)
minikube start --kubernetes-version=v1.23.3
如果对自己的网速不自信的话,可以更换镜像地址:
minikube stop
minikube delete --all
minikube start --kubernetes-version=v1.23.9 --image-mirror-country='cn' --registry-mirror=https://twm4fpgj.mirror.aliyuncs.com
查看Kubernetes集群状态:
lianhaifeng@lianhaifeng-virtual-machine:~$ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
查看节点:
lianhaifeng@lianhaifeng-virtual-machine:~$ minikube node list
minikube 192.168.58.2
运行一个Nginx应用:
kubectl run ngx --image=nginx:alpine
lianhaifeng@lianhaifeng-virtual-machine:~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
ngx 1/1 Running 0 47h
至此,我们已经安装了一个小而美的Kubernetes环境,接下来让我们探索下Kubernets工作机制的奥秘。
操作系统的一个重要功能就是抽象,从繁琐的底层事务中抽象出一些简洁的概念,然后基于这些概念去管理系统资源。
Kubernetes 的管理目标是大规模的集群和应用,必须要能够把系统抽象到足够高的层次,分解出一些松耦合的对象,才能简化系统模型,减轻用户的心智负担。
k8s架构图
控制面 / 数据面
”(Control Plane / Data Plane)架构。Master Node
,一般简称为 Master。Worker Node
,一般就简称为 Worker 或者 Node。查看 Kubernetes 的节点状态:
lianhaifeng@lianhaifeng-virtual-machine:~$ kubectl get node
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 7d4h v1.23.3
可以看到当前的 minikube 集群里只有一个 Master,那 Node 呢?
是因为 Master 和 Node 的划分不是绝对的。当集群的规模较小,工作负载较少的时候,Master 也可以承担 Node 的工作,就像我们搭建的 minikube 环境,它就只有一个节点,这个节点既是 Master 又是 Node。
Kubernetes 的节点内部也具有复杂的结构,是由很多的模块构成的,这些模块又可以分成组件(Component)
和插件(Addon)
两类:
3.2 Master 里的组件
apiserver
唯一入口
。etcd
scheduler
controller-manager
这 4 个组件也都被容器化了,运行在集群的 Pod 里,我们可以用 kubectl 来查看它们的状态:
lianhaifeng@lianhaifeng-virtual-machine:~$ kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-64897985d-d5zbd 1/1 Running 1 (45h ago) 7d4h
etcd-minikube 1/1 Running 1 (45h ago) 7d4h
kube-apiserver-minikube 1/1 Running 3 7d4h
kube-controller-manager-minikube 1/1 Running 4 (45h ago) 7d4h
kube-proxy-zlztd 1/1 Running 1 (45h ago) 7d4h
kube-scheduler-minikube 1/1 Running 1 (45h ago) 7d4h
storage-provisioner 0/1 ImagePullBackOff 0 7d4h
-n kube-system
参数,表示检查“kube-system”名字空间里的 Pod。
kubelet
(非容器化)
kube-proxy
container-runtime
Kubernetes 的定位是容器编排平台,所以它没有限定 container-runtime 必须是 Docker,完全可以替换成任何符合标准的其他容器运行时,例如 containerd、CRI-O 等等,只不过在这里我们使用的是Docker。
这 3 个组件中只有 kube-proxy 被容器化了,而 kubelet 因为必须要管理整个节点,容器化会限制它的能力,所以它必须在 container-runtime 之外运行。
使用 minikube ssh 命令登录到节点后,可以用 docker ps
看到 kube-proxy:
lianhaifeng@lianhaifeng-virtual-machine:~$ minikube ssh
Last login: Sun May 21 11:53:08 2023 from 192.168.58.1
docker@minikube:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cafb3f8e014e nginx "/docker-entrypoint.…" 46 hours ago Up 46 hours k8s_ngx_ngx_default_066a844b-d429-4057-aca1-536c04d38f70_0
6f488ea5ca69 k8s.gcr.io/pause:3.6 "/pause" 46 hours ago Up 46 hours k8s_POD_ngx_default_066a844b-d429-4057-aca1-536c04d38f70_0
5b77694e7e89 a4ca41631cc7 "/coredns -conf /etc…" 46 hours ago Up 46 hours k8s_coredns_coredns-64897985d-d5zbd_kube-system_99c6f163-5738-41d8-ab8f-9a42dc3c6331_1
74d32f36a523 k8s.gcr.io/pause:3.6 "/pause" 46 hours ago Up 46 hours k8s_POD_storage-provisioner_kube-system_7fa7c71e-cc6a-4408-8b70-8b15534a10c8_1
0a2443c55984 k8s.gcr.io/pause:3.6 "/pause" 46 hours ago Up 46 hours k8s_POD_coredns-64897985d-d5zbd_kube-system_99c6f163-5738-41d8-ab8f-9a42dc3c6331_1
048758be7002 9b7cc9982109 "/usr/local/bin/kube…" 46 hours ago Up 46 hours k8s_kube-proxy_kube-proxy-zlztd_kube-system_b294b472-367b-44ca-8781-fab810428037_1
1148430b4e0f k8s.gcr.io/pause:3.6 "/pause" 46 hours ago Up 46 hours k8s_POD_kube-proxy-zlztd_kube-system_b294b472-367b-44ca-8781-fab810428037_1
5d164c717143 25f8c7f3da61 "etcd --advertise-cl…" 46 hours ago Up 46 hours k8s_etcd_etcd-minikube_kube-system_482305b50d4b0d3dbab5b9f1be9b0b18_1
78b7581b6659 b07520cd7ab7 "kube-controller-man…" 46 hours ago Up 46 hours k8s_kube-controller-manager_kube-controller-manager-minikube_kube-system_b965983ec05322d0973594a01d5e8245_4
3b81da17d618 f40be0088a83 "kube-apiserver --ad…" 46 hours ago Up 46 hours k8s_kube-apiserver_kube-apiserver-minikube_kube-system_b7b1a80caec341a470c95988d72c0626_3
431f68d5dec6 99a3486be4f2 "kube-scheduler --au…" 46 hours ago Up 46 hours k8s_kube-scheduler_kube-scheduler-minikube_kube-system_be132fe5c6572cb34d93f5e05ce2a540_1
315d15480c7d k8s.gcr.io/pause:3.6 "/pause" 46 hours ago Up 46 hours k8s_POD_kube-controller-manager-minikube_kube-system_b965983ec05322d0973594a01d5e8245_1
799bfc75e1af k8s.gcr.io/pause:3.6 "/pause" 46 hours ago Up 46 hours k8s_POD_kube-apiserver-minikube_kube-system_b7b1a80caec341a470c95988d72c0626_1
3a67d5e7cebb k8s.gcr.io/pause:3.6 "/pause" 46 hours ago Up 46 hours k8s_POD_etcd-minikube_kube-system_482305b50d4b0d3dbab5b9f1be9b0b18_1
729db09d17a6 k8s.gcr.io/pause:3.6 "/pause" 46 hours ago Up 46 hours k8s_POD_kube-scheduler-minikube_kube-system_be132fe5c6572cb34d93f5e05ce2a540_1
docker@minikube:~$ docker ps |grep kube-proxy
048758be7002 9b7cc9982109 "/usr/local/bin/kube…" 46 hours ago Up 46 hours k8s_kube-proxy_kube-proxy-zlztd_kube-system_b294b472-367b-44ca-8781-fab810428037_1
1148430b4e0f k8s.gcr.io/pause:3.6 "/pause" 46 hours ago Up 46 hours k8s_POD_kube-proxy-zlztd_kube-system_b294b472-367b-44ca-8781-fab810428037_1
退出(exit),查看 kubelet:
lianhaifeng@lianhaifeng-virtual-machine:~$ ps -ef|grep kubelet
root 4547 3077 6 04:21 ? 01:03:42 /var/lib/minikube/binaries/v1.23.3/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --config=/var/lib/kubelet/config.yaml --container-runtime=docker --hostname-override=minikube --kubeconfig=/etc/kubernetes/kubele .conf --node-ip=192.168.58.2
root 5147 5060 8 04:21 ? 01:20:09 kube-apiserver --advertise-address=192.168.58.2 --allow-privileged=true --authorization-mode=Node,RBAC --client-ca-file=/var/lib/minikube/certs/ca.crt --enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota --enable-bootstrap-token-auth=true --etcd-cafile=/var/lib/minikube/certs/etcd/ca.crt --etcd-certfile=/var/lib/minikube/certs/apiserver-etcd-client.crt --etcd-keyfile=/var/lib/minikube/certs/apiserver-etcd-client.key --etcd-servers=https://127.0.0.1:2379 --kubelet-client-certificate=/var/lib/minikube/certs/apiserver-kubelet-client.crt --kubelet-client-key=/var/lib/minikube/certs/apiserver-kubelet-client.key --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname --proxy-client-cert-file=/var/lib/minikube/certs/front-proxy-client.crt --proxy-client-key-file=/var/lib/minikube/certs/front-proxy-client.key --requestheader-allowed-names=front-proxy-client --requestheader-client-ca-file=/var/lib/minikube/certs/front-proxy-ca.crt --requestheader-extra-headers-prefix=X-Remote-Extra- --requestheader-group-headers=X-Remote-Group --requestheader-username-headers=X-Remote-User --secure-port=8443 --service-account-issuer=https://kubernetes.default.svc.cluster.local --service-account-key-file=/var/lib/minikube/certs/sa.pub --service-account-signing-key-file=/var/lib/minikube/certs/sa.key --service-cluster-ip-range=10.96.0.0/12 --tls-cert-file=/var/lib/minikube/certs/apiserver.crt --tls-private-key-file=/var/lib/minikube/certs/apiserver.key
Kubernetes的大致工作流程
:
minikube 也支持很多的插件,使用命令minikube addons list
就可以查看插件列表:
lianhaifeng@lianhaifeng-virtual-machine:~$ minikube addons list
|-----------------------------|----------|--------------|--------------------------------|
| ADDON NAME | PROFILE | STATUS | MAINTAINER |
|-----------------------------|----------|--------------|--------------------------------|
| ambassador | minikube | disabled | 3rd party (Ambassador) |
| auto-pause | minikube | disabled | Google |
| cloud-spanner | minikube | disabled | Google |
| csi-hostpath-driver | minikube | disabled | Kubernetes |
| dashboard | minikube | disabled | Kubernetes |
| default-storageclass | minikube | enabled ? | Kubernetes |
| efk | minikube | disabled | 3rd party (Elastic) |
| freshpod | minikube | disabled | Google |
| gcp-auth | minikube | disabled | Google |
| gvisor | minikube | disabled | Google |
| headlamp | minikube | disabled | 3rd party (kinvolk.io) |
| helm-tiller | minikube | disabled | 3rd party (Helm) |
| inaccel | minikube | disabled | 3rd party (InAccel |
| | | | [info@inaccel.com]) |
| ingress | minikube | disabled | Kubernetes |
| ingress-dns | minikube | disabled | Google |
| istio | minikube | disabled | 3rd party (Istio) |
| istio-provisioner | minikube | disabled | 3rd party (Istio) |
| kong | minikube | disabled | 3rd party (Kong HQ) |
| kubevirt | minikube | disabled | 3rd party (KubeVirt) |
| logviewer | minikube | disabled | 3rd party (unknown) |
| metallb | minikube | disabled | 3rd party (MetalLB) |
| metrics-server | minikube | disabled | Kubernetes |
| nvidia-driver-installer | minikube | disabled | Google |
| nvidia-gpu-device-plugin | minikube | disabled | 3rd party (Nvidia) |
| olm | minikube | disabled | 3rd party (Operator Framework) |
| pod-security-policy | minikube | disabled | 3rd party (unknown) |
| portainer | minikube | disabled | 3rd party (Portainer.io) |
| registry | minikube | disabled | Google |
| registry-aliases | minikube | disabled | 3rd party (unknown) |
| registry-creds | minikube | disabled | 3rd party (UPMC Enterprises) |
| storage-provisioner | minikube | enabled ? | Google |
| storage-provisioner-gluster | minikube | disabled | 3rd party (Gluster) |
| volumesnapshots | minikube | disabled | Kubernetes |
|-----------------------------|----------|--------------|--------------------------------|
比较重要的有两个:DNS
和 Dashboard
。
DNS
Dashboard
只要在 minikube 环境里执行一条简单的命令,就可以自动用浏览器打开 Dashboard 页面,而且还支持中文:
lianhaifeng@lianhaifeng-virtual-machine:~/桌面$ minikube dashboard
🤔 正在验证 dashboard 运行情况 ...
🚀 Launching proxy ...
🤔 正在验证 proxy 运行状况 ...
🎉 Opening http://127.0.0.1:36667/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
Gtk-Message: 20:12:44.459: Not loading module "atk-bridge": The functionality is provided by GTK natively. Please try to not load it.
宿主机本地访问:
Kubernetes功能非常完善,实现了大部分常见的运维管理工作,而且是全自动化的,能够节约大量的人力成本。