安装 docker

使用官方安装脚本自动安装docker

1
2
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
systemctl enable docker

配置镜像加速

1
2
3
4
5
6
7
8
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://0vlzmqf0.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

安装 Minikube

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

创建docker_user账户, 并加入docker组(minikube不允许在root下执行)

1
2
3
4
5
groupadd docker
useradd docker_user
passwd docker_user(输入密码)
usermod -a -G docker docker_user
newgrp docker

安装 kubectl

1
2
3
4
# 可替换$(curl -L -s https://dl.k8s.io/release/stable.txt)为指定版本号,如:v1.24.16
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
install -o docker_user -g docker -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client

启动Minikube

切换账户

1
su docker_user

启动

1
2
3
4
5
6
minikube start  #启动minikube
#指定版本、执行镜像源
minikube start --registry-mirror=https://registry.docker-cn.com --image-mirror-country cn --kubernetes-version=v1.24.16

# 注:如果本机配置了http_proxy,可能会遇到`You appear to be using a proxy, but your NO_PROXY environment does not include the minikube IP (ip)`,这种情况可以通过设置NO_PROXY=$(minikube ip)来解决
NO_PROXY=$(minikube ip) minikube start --registry-mirror=https://registry.docker-cn.com --image-mirror-country cn --kubernetes-version=v1.24.16

默认为单节点,添加节点可参考:https://minikube.sigs.k8s.io/docs/commands/node

1
2
minikube node list
minikube node add

可视化

1
2
3
minikube dashboard --url
# 让其它 IP 可以访问(再开个新窗口执行)
kubectl proxy --port=8888 --address='0.0.0.0' --accept-hosts='^.*'

访问:http://10.74.2.71:8888/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/#/overview?namespace=all

使用

部署应用与访问应用

1
2
3
4
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
# 获取访问地址
minikube service --url nginx

也可以通过 kubectl proxy 拼接 url 访问,https://kubernetes.io/zh/docs/tasks/access-application-cluster/access-cluster/#manually-constructing-apiserver-proxy-urls

http://10.74.2.71:8888/api/v1/namespaces/default/services/nginx:80/proxy/

使用LoadBalancer访问,Minikube 网络

1
2
3
4
5
6
7
8
9
10
# 新开窗口运行
minikube tunnel --cleanup=true

# 重新部署
kubectl delete deployment nginx
kubectl delete service nginx
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=LoadBalancer
# 查看外部地址
kubectl get svc

通过转发访问

1
kubectl port-forward pods/nginx-6799fc88d8-p8llb 8080:80 --address='0.0.0.0'

卸载

1
2
3
4
5
6
minikube stop
minikube delete --all
docker rmi kicbase/stable:v0.0.25
rm -rf ~/.kube ~/.minikube
sudo rm -rf /usr/local/bin/kubectl /usr/local/bin/minikube
docker system prune -a