wget https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz tar -xvzf Python-${PYTHON_VERSION}.tgz --no-check-certificate cd Python-${PYTHON_VERSION}
ip route add 192.168.6.0/24 dev br0 table 10 ip route add default via 192.168.6.1 table 10 ip route add 192.168.9.0/24 dev br1 table 12 ip route add default via 192.168.9.1 table 12 ip rule add from 192.168.6.0/24 table 10 priority 1 ip rule add from 192.168.9.0/24 table 12 priority 2 # 添加 docker 网络 ip route add 172.17.0.0/16 dev docker0 table 10 ip route add 172.17.0.0/16 dev docker0 table 12 # 刷新配置 ip route flush cache # 校验 $ ip route show table 12 default via 192.168.9.1 dev br1 172.17.0.0/16 dev docker0 scope link 192.168.9.0/24 dev br1 scope link
示例
1 2 3 4 5 6 7 8 9 10 11 12 13
# out ip route add default via 192.168.6.1 dev ens8 table 10 ip route add default via 192.168.9.1 dev ens9 table 12 # in ip rule add from 192.168.6.0/24 table 10 priority 1 ip rule add from 192.168.9.0/24 table 12 priority 2 # 可以不设置 priority # 如果有设置了默认的路由,可以忽略其中的一个,比如有如下默认路由 ip route add default via 192.168.6.1 dev ens8 # 那么只需要设置 192.168.9.0/24 # ip route add default via 192.168.9.1 dev ens9 table 12 ip route add 192.168.9.0/24 dev ens9 proto kernel scope link src 192.168.9.8 ip rule add from 192.168.9.0/24 table 12
$ lshw -c network WARNING: you should run this program as super-user. *-network ... logical name: enp0s31f6 ... *-network DISABLED ... logical name: enxf8e43b1a1229 ...
# k8s01-1 主节点 $ mk add-node From the node you wish to join to this cluster, run the following: microk8s join 192.168.9.103:25000/5b502d061dd31ec58d1f6ddf96e10c56/be841c6899a7
Use the '--worker' flag to join a node as a worker not running the control plane, eg: microk8s join 10.1.0.78:25000/5b502d061dd31ec58d1f6ddf96e10c56/be841c6899a7 --worker
If the node you are adding is not reachable through the default interface you can use one of the following: microk8s join 10.1.0.78:25000/5b502d061dd31ec58d1f6ddf96e10c56/be841c6899a7 # k8s01-2 worker 节点 microk8s join 10.1.0.78:25000/5b502d061dd31ec58d1f6ddf96e10c56/be841c6899a7 --worker # 对 k8s01-3, 重复上述操作
查看状态
1 2 3 4 5
$ k get nodes NAME STATUS ROLES AGE VERSION k8s01-1 Ready <none> 2d12h v1.24.3-2+63243a96d1c393 k8s01-2 Ready <none> 77s v1.24.3-2+63243a96d1c393 k8s01-3 Ready <none> 64s v1.24.3-2+63243a96d1c393
更详细的状态
1 2 3 4 5
$ k get node -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME k8s01-1 Ready <none> 2d12h v1.24.3-2+63243a96d1c393 10.1.0.78 <none> Ubuntu 20.04.4 LTS 5.4.0-124-generic containerd://1.5.13 k8s01-2 Ready <none> 5m23s v1.24.3-2+63243a96d1c393 10.1.0.62 <none> Ubuntu 20.04.4 LTS 5.4.0-124-generic containerd://1.5.13 k8s01-3 Ready <none> 5m10s v1.24.3-2+63243a96d1c393 10.1.0.242 <none> Ubuntu 20.04.4 LTS 5.4.0-124-generic containerd://1.5.13
安装插件
1 2
# 主节点 mk enable dns storage dashboard helm3
获取 k8s 配置
1 2
mkdir ~/.kube mk config > ~/.kube/config
问题排查
查看事件
1
k get events --sort-by=.metadata.creationTimestamp --namespace=kube-system
异常
Failed create pod sandbox
错误信息
1
Failed create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container ... error getting ClusterInformation: Get ... https://10.152.183.1:443 ...
原因
1 2
NAMESPACE↑ NAME TYPE CLUSTER-IP default kubernetes ClusterIP 10.152.183.1
错误信息提示请求 ClusterIP 异常,检查节点 IP。
1 2 3 4
$ k get nodes -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP ... KERNEL-VERSION CONTAINER-RUNTIME a Ready <none> 16h v1.27.2 172.21.0.3 ... 5.4.0-126-generic containerd://1.6.15 b Ready <none> 16h v1.27.2 192.168.6.201 ... 5.15.0-76-generic containerd://1.6.15
解决
在 b 节点无法通过 a 的 INTERNAL-IP 访问 a(controller) 节点,修改两个节点的 --node-ip 为可以访问的 ip。
1 2 3 4 5 6 7 8 9 10 11
microk8s stop # or for workers: sudo snap stop microk8s
sudo vim.tiny /var/snap/microk8s/current/args/kubelet # Add this to bottom: --node-ip=<this-specific-node-lan-ip>
sudo vim.tiny /var/snap/microk8s/current/args/kube-apiserver # Add this to bottom: --advertise-address=<this-specific-node-lan-ip>
microk8s start # or for workers: sudo snap start microk8s
certificate is valid for kubernetes … not for mydomain.com
# create a directory with the registry name sudo mkdir -p /var/snap/microk8s/current/args/certs.d/k8s.gcr.io # create the hosts.toml file pointing to the mirror echo ' server = "https://k8s.gcr.io"
[host."https://registry.aliyuncs.com/v2/google_containers"] capabilities = ["pull", "resolve"] override_path = true ' | sudo tee -a /var/snap/microk8s/current/args/certs.d/k8s.gcr.io/hosts.toml # 2 sudo mkdir -p /var/snap/microk8s/current/args/certs.d/registry.k8s.io echo ' server = "registry.k8s.io"
[host."https://registry.aliyuncs.com/v2/google_containers"] capabilities = ["pull", "resolve"] override_path = true ' | sudo tee -a /var/snap/microk8s/current/args/certs.d/registry.k8s.io/hosts.toml
需要重启
1
sudo snap restart microk8s
检查状态
1 2
# 如果不翻墙/替换镜像, 会在这里卡住 microk8s status --wait-ready
配置
配置 kubectl 命令
1 2 3 4 5
mkdir -p ~/.local/bin/ vim ~/.local/bin/kubectl # 输入如下内容 #!/bin/bash exec /snap/bin/microk8s.kubectl $(echo "$*" | sed 's/-- sh.*/sh/')
配置别名
1 2 3 4 5
# vim ~/.bash_aliases alias kubectl='microk8s kubectl' alias k='microk8s kubectl' alias mk='microk8s' alias helm='microk8s helm3'
# pause ## 从阿里云镜像拉取 docker pull registry.aliyuncs.com/google_containers/pause:3.7 ## 重命名 docker tag registry.aliyuncs.com/google_containers/pause:3.7 k8s.gcr.io/pause:3.7 docker tag registry.aliyuncs.com/google_containers/pause:3.7 registry.k8s.io/pause:3.7 # metric server docker pull registry.aliyuncs.com/google_containers/metrics-server:v0.5.2 docker tag registry.aliyuncs.com/google_containers/metrics-server:v0.5.2 k8s.gcr.io/metrics-server/metrics-server:v0.5.2 docker tag registry.aliyuncs.com/google_containers/metrics-server:v0.5.2 registry.k8s.io/metrics-server/metrics-server:v0.5.2
Join 集群
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# master 节点运行 $ microk8s add-node From the node you wish to join to this cluster, run the following: microk8s join 192.168.1.230:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05
Use the '--worker' flag to join a node as a worker not running the control plane, eg: microk8s join 192.168.1.230:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05 --worker
If the node you are adding is not reachable through the default interface you can use one of the following: microk8s join 192.168.1.230:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05 microk8s join 10.23.209.1:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05 microk8s join 172.17.0.1:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05 # slave 节点运行 $ microk8s join 172.17.0.1:25000/92b2db237428470dc4fcfc4ebbd9dc81/2c0cb3284b05