什么是K8s
Kubernetes 是一个可移植的、可扩展的开源平台,用于管理容器化的工作负载和服务,可促进声明式配置和自动化。 Kubernetes 拥有一个庞大且快速增长的生态系统。Kubernetes 的服务、支持和工具广泛可用。
Kubernetes 这个名字源于希腊语,意为“舵手”或“飞行员”。k8s 这个缩写是因为 k 和 s 之间有八个字符的关系。 Google 在 2014 年开源了 Kubernetes 项目。Kubernetes 建立在 Google 在大规模运行生产工作负载方面拥有十几年的经验 的基础上,结合了社区中最好的想法和实践。
核心组件
-
Kubernetes属于主从分布式架构,主要由Master Node和Worker Node组成,以及包括客户端命令行工具kubectl和其它附加项。
-
Master Node:作为控制节点,对集群进行调度管理;Master Node由API Server、Scheduler、Cluster State Store和Controller-Manger Server所组成;
-
Worker Node:作为真正的工作节点,运行业务应用的容器;Worker Node包含kubelet、kube proxy和Container Runtime;
-
kubectl:用于通过命令行与API Server进行交互,而对Kubernetes进行操作,实现在集群中进行各种资源的增删改查等操作;
-
Add-on:是对Kubernetes核心功能的扩展,例如增加网络和网络策略等能力。
-
repliceation: 用于伸缩副本数量
-
endpoint:用于管理网络请求
minikube安装步骤
-
第一步虚拟驱动
curl -LO https://storage.googleapis.com/minikube/releases/latest/docker-machine-driver-kvm2 \&& sudo install docker-machine-driver-kvm2 /usr/local/bin/### 查看版本 docker-machine-driver-kvm2 version ### 安装docker yum install docker -y
-
安装minikube 安装最新版本
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 sudo install minikube-linux-amd64 /usr/local/bin/minikube
-
安装kubectl 1.15.0
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl
-
切换用户
useradd k8s 创建用户
usermod -aG libvirt es
-
在 centos 中还需要额外修改 libvirt 的默认配置 (ubuntu 不需要)
-
启动cmmond
minikube start --vm-driver=docker --memory=2048 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --base-image registry.cn-hangzhou.aliyuncs.com/google_containers/kicbase:v0.0.20 --force --kubernetes-version=1.23.0 minikube start --vm-driver=kvm2 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --base-image registry.cn-hangzhou.aliyuncs.com/google_containers/kicbase:v0.0.20
启动信息
[k8s@p2a3r3ubvcy9k1jj root]$ minikube start --vm-driver=docker * Centos 8.3.2011 (amd64) 上的 minikube v1.30.1 * 根据用户配置使用 docker 驱动程序 * Using Docker driver with root privileges * Starting control plane node minikube in cluster minikube * Pulling base image ... * Downloading Kubernetes v1.26.3 preload ...> preloaded-images-k8s-v18-v1...: 397.02 MiB / 397.02 MiB 100.00% 19.54 M> index.docker.io/kicbase/sta...: 373.53 MiB / 373.53 MiB 100.00% 3.73 Mi ! minikube was unable to download gcr.io/k8s-minikube/kicbase:v0.0.39, but successfully downloaded docker.io/kicbase/stable:v0.0.39 as a fallback image * Creating docker container (CPUs=2, Memory=2200MB) ... X Docker is nearly out of disk space, which may cause deployments to fail! (97% of capacity). You can pass '--force' to skip this check. * 建议: Try one or more of the following to free up space on the device:1. Run "docker system prune" to remove unused Docker data (optionally with "-a")2. Increase the storage allocated to Docker for Desktop by clicking on:Docker icon > Preferences > Resources > Disk Image Size3. Run "minikube ssh -- docker system prune" if using the Docker container runtime * Related issue: https://github.com/kubernetes/minikube/issues/9024 * 正在 Docker 23.0.2 中准备 Kubernetes v1.26.3…- Generating certificates and keys ...- Booting up control plane ...- 配置 RBAC 规则 ... * Configuring bridge CNI (Container Networking Interface) ...- Using image gcr.io/k8s-minikube/storage-provisioner:v5 * Verifying Kubernetes components... * Enabled addons: default-storageclass, storage-provisioner * kubectl not found. If you need it, try: 'minikube kubectl -- get pods -A' * Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
-
启动信息
## 查看状态 minikube status ### kubectl create deployment --image=nginx my-nginx #创建一个名为my-nginx的deployment
安装kubelet、kubeadm、kubectl,并且将kubelet设置为自启动,然后再启动kubele yum install -y kubelet kubeadm kubectl \ && systemctl enable kubelet \ && systemctl start kubelet
安装如下信息:
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=0 repo_gpgcheck=0 gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpghttp://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg exclude=kubelet kubeadm kubectl EOF sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes sudo systemctl enable --now kubelet
问题
-
X Exiting due to MK_USAGE: flag --base-image is not available for driver 'kvm2'.
解决方案如下:
-
useradd k8s 创建用户
-
usermod -aG libvirt k8s passwd 命令可以设置用户密码。通过 useradd 命令创建的用户初始是没有密码的
账号:k8s 123andAND
账号: kvm
-
用户命令
[k8s@p2a3r3ubvcy9k1jj ~]$ minikube start --driver=docker * Centos 8.3.2011 (amd64) 上的 minikube v1.30.1 * 根据用户配置使用 docker 驱动程序 X Exiting due to PROVIDER_DOCKER_NEWGRP: "docker version --format -:" exit status 1: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version": dial unix /var/run/docker.sock: connect: permission denied * 建议:Add your user to the 'docker' group: 'sudo usermod -aG docker $USER && newgrp docker' * 文档:https://docs.docker.com/engine/install/linux-postinstall/ [k8s@p2a3r3ubvcy9k1jj ~]$ sudo usermod -aG docker $USER && newgrp docker 1. 首先要给账号设置密码,然后切换用户组 sudo usermod -aG docker k8s && newgrp docker
创建成功
[k8s@p2a3r3ubvcy9k1jj root]$ kubectl create deployment --image=nginx my-nginx deployment.apps/my-nginx created
启动界面
# minikube dashboard
http://182.43.165.162:38113/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
http://127.0.0.1:38113/api/v1/namespaces/kubernetes-dashboard/services
-
需要设置代理
1. 成功 kubectl proxy --port=8009 --address='0.0.0.0' --accept-hosts='^.*' &
kubectl scale --replicas=1 deployment/my-dep
-
访问地址
Kubernetes Dashboard
访问成功