一、 安装 cri-docker
- 下载安装
# 在 https://github.com/Mirantis/ 下载
https://github.com/Mirantis/
tar -xvf cri-dockerd-0.3.1.amd64.tgz
cp cri-dockerd/cri-dockerd /usr/bin/
chmod +x /usr/bin/cri-dockerd
# 确认已安装版本
cri-dockerd --version
- 配置启动文件
cri-docker.service
# 创建cri-docker.service配置文件
vi /lib/systemd/system/cri-docker.service
[Unit]
Description=CRI Interface for Docker Application Container Engine
Documentation=https://docs.mirantis.com
After=network-online.target firewalld.service docker.service
Wants=network-online.target
Requires=cri-docker.socket
[Service]
Type=notify
ExecStart=/usr/bin/cri-dockerd --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.7
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target
cri-docker.socket
# 创建cri-docker.socket配置文件
vi /lib/systemd/system/cri-docker.socket
[Unit]
Description=CRI Docker Socket for the API
PartOf=cri-docker.service
[Socket]
ListenStream=%t/cri-dockerd.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target
- 启动
systemctl daemon-reload
systemctl start docker.service
systemctl enable cri-docker
systemctl status docker.service
二、 安装 crictl
- 下载地址 https://github.com/kubernetes-sigs/cri-tools/releases
- 解压
sudo tar zxvf crictl-xx-linux-amd64.tar.gz -C /usr/local/bin
三、 搭建 minikube
- 安装 docker 见docker离线搭建
- 安装 minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
- 安装 kubectl
wget -c "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
- 开启dashboard
minikube addons enable metrics-server
kubectl proxy --port=45396 --address='0.0.0.0' --disable-filter=true --accept-hosts='^.*' &
minikube dashboard --url &
-
外部浏览器访问
a. 确认关闭防火墙
b. 将下图的IP:Port 替换成外部IP:45396,然后在浏览器中打开即可
-
基本命令
# 启动集群 --driver=docker (默认)
minikube start --force --image-mirror-country='cn' --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
# 查看节点。kubectl 是一个用来跟 K8S 集群进行交互的命令行工具
kubectl get node
# 停止集群
minikube stop
# 清空集群
minikube delete --all
# 安装集群可视化 Web UI 控制台
minikube dashboard
# other
# 将外部 docker 环境映射到 minikube 的 docker 环境
eval $(minikube docker-env)
- nginx 测试
# 创建 nginx 测试
kubectl create deployment nginx --image=nginx
# 创建 service 暴露端口
# port 为service 暴露端口,target-port 为目标容器端口
kubectl expose deployment nginx --port=6001 --target-port=80 --type=NodePort
# 创建端口转发 web 访问端口 :service 暴露端口
kubectl port-forward --address 0.0.0.0 service/nginx 8001:6001 &
- 加载宿主机 docker(解决 minikube 中 docker 无法pull 问题,且设置镜像加速比较麻烦,这个方法目前最好)
# 示例
# 宿主机执行(宿主机配置过加速)
docker pull mysql
minikube image load mysql
示例,可复制为 yaml 文件,或者复制代码在dashboard中执行
- 示例搭建mysql
apiVersion: apps/v1
kind: Deployment # 副本控制器RC
metadata:
name: mysql #RC 的名称,全局唯一
namespace: default # 默认空间
spec:
replicas: 1 #Pod 副本的期待数量
selector:
matchLabels:
app: mysql # 符合目标的Pod拥有此标签
template: # 根据此模版创建Pod的副本
metadata:
labels:
app: mysql # Pod副本拥有的标签,对应RC的Selector
spec:
containers: # Pod的内容的定义部分
- name: mysql # 容器的名称
image: mysql # 容器对应的Docker Image
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306 # 容器应用监听的端口号
env:
- name: MYSQL_ROOT_PASSWORD # 设置mysql的初始化密码
value: "123456" # 设置mysql的初始化密码
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumes:
- name: mysql-data
hostPath:
path: /var/lib/mysql
---
apiVersion: v1
kind: Service # 表明是Kubernetes Service
metadata:
name: mysql # Service 的全局唯一名称
spec:
type: NodePort
selector:
app: mysql
ports: # Service 提供服务的端口
- port: 3306 # Service 对应的Pod拥有这里定义的标签
targetPort: 3306
- 示例搭建 redis
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
labels:
app: redis
spec:
strategy:
type: Recreate
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis
imagePullPolicy: IfNotPresent
# command: ["redis-server","/etc/redis/redis.conf"]
ports:
- containerPort: 6379
---
kind: Service
apiVersion: v1
metadata:
name: redis
spec:
type: NodePort
selector:
app: redis
ports:
- port: 6379