k8s 容器编排初探

2021/2/6 dockers

k8s 容器编排初探以及解决各种奇奇怪怪的小问题

前置说明:系统为 ubuntu 20.04

# 安装

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

# “后端“安装

好了 现在还不能直接启动,以为我们还需要“后端“(后端这个词并不准确)

如果直接启动,会发现

$ minikube start
😄  minikube v1.17.1 on Ubuntu 20.04
👎  Unable to pick a default driver. Here is what was considered, in preference order:
    ▪ vmware: Not installed: exec: "docker-machine-driver-vmware": executable file not found in $PATH
    ▪ docker: Not healthy: "docker version --format {{.Server.Os}}-{{.Server.Version}}" 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.40/version: dial unix /var/run/docker.sock: connect: permission denied
    ▪ kvm2: Not installed: exec: "virsh": executable file not found in $PATH
    ▪ podman: Not installed: exec: "podman": executable file not found in $PATH
    ▪ virtualbox: Not installed: unable to find VBoxManage in $PATH

❌  Exiting due to DRV_NOT_DETECTED: No possible driver was detected. Try specifying --driver, or see https://minikube.sigs.k8s.io/docs/start/
1
2
3
4
5
6
7
8
9
10

由此我们也能发现其支持“后端“挺全面的

# podman - 弃用

这里我首先选择 podman 似乎更为先进,但是后来发现是实验性质的,遂放弃,改为 docker

参考 Vultr 上的安装流程 (opens new window)

确保某些系统标识数据可用(VERSION_ID)。

source /etc/os-release
1

将 Podman debian 软件包存储库添加到 apt

sudo sh -c "echo 'deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list"
wget -nv https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/xUbuntu_${VERSION_ID}/Release.key -O- | sudo apt-key add -
sudo apt-get update -qq
sudo apt-get -qq --yes install podman
1
2
3
4

# docker

官方安装流程 (opens new window)

随后赋予当前用户权限,即加入 docker 用户组

sudo usermod -aG docker ${USER}
1

# 启动

minikube start --driver=docker --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers'
1
😄  minikube v1.17.1 on Ubuntu 20.04
✨  Using the docker driver based on user configuration
✅  Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🔥  Creating docker container (CPUs=2, Memory=3900MB) ...
🐳  Preparing Kubernetes v1.20.2 on Docker 20.10.2 ...
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔎  Verifying Kubernetes components...
🌟  Enabled addons: storage-provisioner, default-storageclass
💡  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
1
2
3
4
5
6
7
8
9
10
11
12
13
14

这里还必须配合 kubectl 使用,可以直接通过 minikube kubectl -- get pods -A 在 minikube 中安装一个,不过这样的话,每次使用 kubectl 必须采用 minikube kubectl *** 的形式,非常的不简洁,而且传入参数也必须利用 --,即类似于 minikube kubectl run *** *** -- --image=***, 所以我选择系统范围内内安装 kubectl :

安装根据 官方教程 (opens new window) 即可,不再赘述

:::tips 如果遇到 minikube start 启动集群失败:

Unable to find image gcr.io/k8s-minikube/kicbase:v0.0.17
StartHost failed, but will try again: config: please provide an IP address
😿  Failed to start ssh bare metal machine. Running "minikube delete" may fix it: config: please provide an IP address

❌  Exiting due to GUEST_PROVISION: Failed to start host: config: please provide an IP address
1
2
3
4
5

类似的错误,先删除在添加 --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers' 参数即可

minikube delete
minikube start --driver=docker --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers'
1
2

:::

# docker 镜像

这里利用阿里云的镜像加速器,加速一下 docker 官方的镜像拉取速度

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

其中 ******** 需要替换为你自己的地址,可前往阿里云自行注册使用

# hello-world

拉取一个 hello-world 镜像试试

kubectl run hellow-world --image=hello-world
1

随后查看信息

kubectl describe pod
1

能看到镜像拉取成功,不过随后会有错误 Back-off restarting failed container,不碍事,这是因为容器内没有任务需要继续运行

:::tips 血泪史 在拉取 Ubuntu 20.04 的时候,不仔细输成了 --image=ubuntu:2004,排查花了我一下午,就离离离离离谱!!! kubectl run wrf-install --image=ubuntu:20.04 即可 :::

剩下的命令主要是需要理解 k8s 的一些概念和逻辑,毕竟只是一个使用者

# 利用 k8s 完成 WRF 编译测试 已弃坑

本来想利用 k8s 并发几十个 pods 来看看脚本是否能够实时完成,后来细细一想,大可不必,我需要的只是 CI/CD 而已,直接用 GitHub action 即可。

也是折腾 0.0

Last Updated: 2023-10-29T08:26:04.000Z