1. k8s_install_by_minikube


  • 国内安装 minikube


1.1. 安装准备


1.1.1. 安装配置 docker 环境


  • 使用阿里云源安装 docker

mkdir -p /opt/tools/k8s
cd /opt/tools/k8s
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun


1.1.2. 安装 kubectl


  • 下载 kubectl

wget "https://storage.googleapis.com/kubernetes-release/release/v1.18.1/bin/linux/amd64/kubectl" -O "/usr/local/bin/kubectl"
chmod 755 /usr/local/bin/kubectl
kubectl version

  • 更新 kubectl 版本

wget "https://storage.googleapis.com/kubernetes-release/release/v1.20.2/bin/linux/amd64/kubectl" -O "/usr/local/bin/kubectl"
chmod 755 /usr/local/bin/kubectl
kubectl version


1.1.3. 安装基础工具

apt install conntrack -y


1.2. 下载安装 minikube


1.2.1. 国内主机推荐-安装阿里云版本的 minikube-v1.18.1


  • 国内主机可以使用阿里云镜像仓库下载安装

curl -Lo minikube https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.18.1/minikube-linux-amd64
chmod 755 minikube
mv minikube /usr/local/bin/
ll /usr/local/bin/minikube


文档:https://developer.aliyun.com/article/221687 其他:https://developer.aliyun.com/article/672675 仓库:https://github.com/AliyunContainerService/minikube



1.2.2. 国外主机推荐-安装最新版 minikube


  • 国外主机可以尝试下载安装最新版 minikube,体验新功能
  • 最新版需要从谷歌仓库下载,国内访问可能会有限制

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


官方仓库:https://github.com/kubernetes/minikube/releases/tag/v1.19.0 官方文档:https://minikube.sigs.k8s.io/docs/start/



1.3. 使用 minikube 安装 k8s 集群


1.3.1. 删除旧的 minikube 虚拟机及缓存

minikube delete
rm -rf /root/.minikube


1.3.2. 使用 minikube 启动 k8s 虚拟机


  • 默认联网安装最新版 k8s 集群,使用官方仓库,国内主机可能安装失败

minikube start

  • 国内主机建议使用阿里云仓库安装指定版本的 k8s 集群

# 指定安装版本
minikube start --vm-driver=none --registry-mirror=https://registry.docker-cn.com --image-mirror-country cn --kubernetes-version=v1.18.3

  • 常用安装选项

# 为了访问海外的资源,阿里云提供了一系列基础设施,请按照如下参数进行配置。其中常见参数
--driver=*** 从1.5.0版本开始,Minikube缺省使用本地最好的驱动来创建Kubernetes本地环境,测试过的版本 docker, kvm
--image-mirror-country cn 将缺省利用 registry.cn-hangzhou.aliyuncs.com/google_containers 作为安装Kubernetes的容器镜像仓库 (阿里云版本可选)
--iso-url=*** 利用阿里云的镜像地址下载相应的 .iso 文件 (阿里云版本可选)
--registry-mirror=*** 为了拉取Docker Hub镜像,需要为 Docker daemon 配置镜像加速,参考阿里云镜像服务
--cpus=2 为minikube虚拟机分配CPU核数
--memory=2048mb 为minikube虚拟机分配内存数
--kubernetes-version=*** minikube 虚拟机将使用的 kubernetes 版本

  • 其他不常用的安装选项

# 使用旧版本运行第二个 minikube 虚拟机,驱动为 docker 时可用
minikube start -p aged --kubernetes-version=v1.16.1

# 安装 minikube,指定虚拟机配置
minikube start --cpus=4 --memory=4096mb

# 安装 minikube,指定虚拟机使用 Docker 驱动,适用于物理机安装 minikube
minikube start --driver=docker

# 安装 minikube,指定虚拟机使用 KVM2 驱动,适用于 kvm 虚拟机安装 minikube
minikube start --driver=kvm2


1.4. minikube 集群管理命令


  • minikube 常用命令

# 检查安装结果
minikube help
minikube status
kubectl version
kubectl get nodes
kubectl get pods -A

# 查询运行的 pod
minikube kubectl -- get po -A

# 挂起虚拟机
minikube pause

# 停止虚拟机
minikube stop

# 修改虚拟机内存配置
minikube config set memory 16384

# 查看 minikube 的安装目录列表
minikube addons list

# 启动 dashboard 控制台
minikube dashboard
curl 127.0.0.1:23341

# 删除所有 minikube 虚拟机
minikube delete --all

# 部署目录
/var/lib/kubelet
/var/lib/minikube

  • k8s 常用命令

# 创建带有终端的 Pod,并进入(后续测试用)
kubectl run busybox --image=busybox -it

# 部署 app 进行测试
kubectl run nginx02 --image=nginx
kubectl run hello-minikube --image=k8s.gcr.io/echoserver:1.4 --port=8081

# deployment
kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4
kubectl expose deployment hello-minikube --type=NodePort --port=8080
kubectl get service hello-minikube

# 使用 minikube 访问服务
minikube service hello-minikube

# 使用端口映射访问服务
kubectl port-forward service/hello-minikube 7080:8080
curl http://localhost:7080/

# 使用 LB 类型的 deployment 测试
kubectl create deployment balanced --image=k8s.gcr.io/echoserver:1.4
kubectl expose deployment balanced --type=LoadBalancer --port=8080
minikube tunnel
kubectl get services balanced


1.5. 实例演示


  • 实例演示:安装默认版本 minikube

root@zuiyoujie:/opt/scrtips/k8s# ll /usr/local/bin/
total 95516
drwxr-xr-x 2 root root 4096 Apr 19 17:50 ./
drwxr-xr-x 13 root root 4096 Apr 6 12:31 ../
-rwxr-xr-x 1 root root 44032000 May 20 2020 kubectl*
-rwxr-xr-x 1 root root 53765167 Apr 19 17:49 minikube*

-------------------------
root@zuiyoujie:/opt/tools/k8s# minikube start
* minikube v1.18.1 on Ubuntu 18.04
* Automatically selected the docker driver
* The "docker" driver should not be used with root privileges.
* If you are running minikube within a VM, consider using --driver=none:
* https://minikube.sigs.k8s.io/docs/reference/drivers/none/

root@zuiyoujie:/opt/tools/k8s# minikube start --vm-driver=none --registry-mirror=https://registry.docker-cn.com --image-mirror-country cn
* minikube v1.18.1 on Ubuntu 18.04 (amd64)
* Using the none driver based on user configuration
* Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
* Starting control plane node minikube in cluster minikube
* Running on localhost (CPUs=4, Memory=7774MB, Disk=100664MB) ...
* OS release is Ubuntu 18.04.3 LTS
* Preparing Kubernetes v1.20.2 on Docker 20.10.6 ...
- kubelet.resolv-conf=/run/systemd/resolve/resolv.conf
> kubeadm.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
> kubectl.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
> kubelet.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
> kubeadm: 37.40 MiB / 37.40 MiB [---------------] 100.00% 17.10 MiB p/s 2s
> kubectl: 38.37 MiB / 38.37 MiB [---------------] 100.00% 15.26 MiB p/s 3s
> kubelet: 108.73 MiB / 108.73 MiB [-------------] 100.00% 12.13 MiB p/s 9s
- Generating certificates and keys ...
- Booting up control plane ...
- Configuring RBAC rules ...
* Configuring local host environment ...
*
! The 'none' driver is designed for experts who need to integrate with an existing VM
* Most users should use the newer 'docker' driver instead, which does not require root!
* For more information, see: https://minikube.sigs.k8s.io/docs/reference/drivers/none/
*
! kubectl and minikube configuration will be stored in /root
! To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run:
*
- sudo mv /root/.kube /root/.minikube $HOME
- sudo chown -R $USER $HOME/.kube $HOME/.minikube
*
* This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
* Verifying Kubernetes components...
- Using image registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v4 (global image repository)
* Enabled addons: default-storageclass, storage-provisioner
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
root@zuiyoujie:/opt/tools/k8s#

---------------------------------
root@zuiyoujie:/opt/scrtips/k8s# kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:52:00Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:43:34Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
root@zuiyoujie:/opt/scrtips/k8s#

root@zuiyoujie:/opt/scrtips/k8s# kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
default busybox 1/1 Running 1 9m11s
default hello-minikube 0/1 ErrImagePull 0 7m42s
kube-system coredns-546565776c-ggfpx 1/1 Running 1 21m
kube-system etcd-zuiyoujie 1/1 Running 1 21m
kube-system kube-apiserver-zuiyoujie 1/1 Running 1 21m
kube-system kube-controller-manager-zuiyoujie 1/1 Running 1 21m
kube-system kube-proxy-x5pft 1/1 Running 1 21m
kube-system kube-scheduler-zuiyoujie 1/1 Running 1 21m
kube-system storage-provisioner

root@zuiyoujie:/opt/scrtips/k8s# kubectl run busybox --image=busybox -it
Error from server (AlreadyExists): pods "busybox" already exists
root@zuiyoujie:/opt/scrtips/k8s# kubectl attach busybox -c busybox -it
If you don't see a command prompt, try pressing enter.
/ #
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
/ #
Session ended, resume using 'kubectl attach busybox -c busybox -i -t' command when the pod is running

  • 实例演示:安装指定版本 minikube

  • 实例演示:安装指定版本 minikube

root@zuiyoujie:/opt/tools/k8s# minikube delete
* Uninstalling Kubernetes v1.20.2 using kubeadm ...
* Deleting "minikube" in none ...
* Removed all traces of the "minikube" cluster.

root@zuiyoujie:/opt/tools/k8s# minikube start --vm-driver=none --registry-mirror=https://registry.docker-cn.com --image-mirror-country cn --kubernetes-version=v1.18.3
* minikube v1.18.1 on Ubuntu 18.04 (amd64)
* Using the none driver based on user configuration
* Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
* Starting control plane node minikube in cluster minikube
* Running on localhost (CPUs=4, Memory=7774MB, Disk=100664MB) ...
* OS release is Ubuntu 18.04.3 LTS
* Preparing Kubernetes v1.18.3 on Docker 20.10.6 ...
- kubelet.resolv-conf=/run/systemd/resolve/resolv.conf
> kubelet.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
> kubectl.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
> kubeadm.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
> kubeadm: 37.97 MiB / 37.97 MiB [---------------] 100.00% 11.72 MiB p/s 3s
> kubectl: 41.99 MiB / 41.99 MiB [---------------] 100.00% 10.95 MiB p/s 4s
> kubelet: 108.04 MiB / 108.04 MiB [------------] 100.00% 10.87 MiB p/s 11s
- Generating certificates and keys ...
- Booting up control plane ...
- Configuring RBAC rules ...
* Configuring local host environment ...
*
! The 'none' driver is designed for experts who need to integrate with an existing VM
* Most users should use the newer 'docker' driver instead, which does not require root!
* For more information, see: https://minikube.sigs.k8s.io/docs/reference/drivers/none/
*
! kubectl and minikube configuration will be stored in /root
! To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run:
*
- sudo mv /root/.kube /root/.minikube $HOME
- sudo chown -R $USER $HOME/.kube $HOME/.minikube
*
* This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
* Verifying Kubernetes components...
- Using image registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v4 (global image repository)
* Enabled addons: default-storageclass, storage-provisioner

! /usr/local/bin/kubectl is version 1.20.2, which may have incompatibilites with Kubernetes 1.18.3.
- Want kubectl v1.18.3? Try 'minikube kubectl -- get pods -A'
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

root@zuiyoujie:/opt/tools/k8s# kubectl get node
NAME STATUS ROLES AGE VERSION
zuiyoujie Ready master 4m26s v1.18.3

root@zuiyoujie:/opt/tools/k8s# kubectl version
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-13T13:28:09Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:43:34Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
root@zuiyoujie:/opt/tools/k8s#