Vmware 虚拟机中安装 Ubuntu 22.04 server,完成系统的基本配置。
安装脚本借鉴于:https://blog.csdn.net/LeoForBest/article/details/126524892,在此基础上做了修改。
#!/usr/bin/bash
# ~~~~~~~~~
# Ubuntu 22.04 Minikube install
# Update Author: yuluo
# Usage: bash install.sh (不要sudo,使用普通用户)
echo "正在准备环境..."
sudo apt-get update -y
sudo apt-get install ca-certificates curl gnupg lsb-release apt-transport-https -y
function install_docker() {
echo "正在卸载旧版本docker..."
sudo apt-get remove docker docker-engine docker.io containerd runc -y
echo "正在添加docker gpg..."
sudo mkdir -p /etc/apt/keyrings
if [ -f "/etc/apt/keyrings/docker.gpg" ]; then
sudo rm /etc/apt/keyrings/docker.gpg
fi
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
echo "正在安装docker..."
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
echo "正在添加当前用户${USER}到docker组..."
sudo usermod -aG docker "$USER"
echo "正在设置docker registry国内镜像..."
if [ -f "/etc/docker/daemon.json" ]; then
sudo mv /etc/docker/daemon.json{,.bak}
fi
cat <<EOF | sudo tee /etc/docker/daemon.json >/dev/null
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn", "https://registry.docker-cn.com"]
}
EOF
# 将 docker.sock 文件所有者修改为当前用户,确保 minikube 启动成功
sudo chown $USER /var/run/docker.sock
sudo systemctl restart docker.service
echo "Docker安装完成."
}
function install_kubectl() {
echo "正在下载安装 kubectl"
# 和 minukube 同理
# sudo curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
sudo chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/
echo "kubectl 安装完成..."
}
install_kubectl
function install_minikube() {
echo "正在下载安装minikube-linux-amd64..."
# 提前下在 minikube 到当前路径下,因为网络原因下载太慢,因此注释此步骤
# sudo curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
echo "正在启动minikube..."
# minikube 清除了所有内容,谨慎使用
# minikube delete
# --kubernetes-version=v1.23.8 https://github.com/kubernetes/minikube/issues/14477
minikube start
minikube status
echo "minikube 启动成功,安装minikube完毕..."
}
install_docker
# echo "正在安装virtualbox..."
# 这里在物理linux机器上运行时使用,如果已经在 vm 虚拟机上,裸机运行即可
# sudo apt install virtualbox virtualbox-ext-pack -y
install_minikube
echo -e "\n\n"
cat <<EOF
**************************************
docker version
**************************************
EOF
sudo docker version
cat <<EOF
*******************************************
设置 minikube kubectl 别名为 kubectl
*******************************************
EOF
# 可选
echo 'alias kubectl="minikube kubectl --"' >> ~/.profile
source ~/.profile
cat <<EOF
**************************************
kubectl -- get po -A
**************************************
EOF
kubectl get pods -A
echo -e "\n 为 root 用户添加 kubectl 的执行权限,原因如下:因为 minikube 在普通用户下启动,root 用户下无 minikube 应用。所以使用时会 8080 refused"
sudo mkdir -p /root/.kube
sudo cp $HOME/.kube/config /root/.kube
sudo su
echo -e "\n 更多信息可参考: https://minikube.sigs.k8s.io/docs/start/"
安装最终效果如下:
yuluo@yuluo-ubuntu:~/minikube$ kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-5d78c9869d-s4hrm 1/1 Running 0 2m57s
kube-system etcd-minikube 1/1 Running 0 3m10s
kube-system kube-apiserver-minikube 1/1 Running 0 3m10s
kube-system kube-controller-manager-minikube 1/1 Running 0 3m10s
kube-system kube-proxy-sbpzx 1/1 Running 0 2m57s
kube-system kube-scheduler-minikube 1/1 Running 0 3m10s
kube-system storage-provisioner 1/1 Running 1 (2m36s ago) 3m9s
yuluo@yuluo-ubuntu:~/minikube$
minikube dashboard
kubectl get pods -A | grep dashboard
kubectl proxy --address='0.0.0.0' --disable-filter=true
http://ip:8001/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/