卸载K3S
/usr/local/bin/k3s-uninstall.sh
/usr/local/bin/k3s-agent-uninstall.sh
所有步骤具体步骤如下:
准备两台服务器,一台做master,一台做work
#设置Hostname
hostnamectl set-hostname $hostname
#添加hosts记录
echo "$host_ip $hostname" >> /etc/hosts
#关闭Selinux
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
#关闭防火墙
systemctl stop firewalld.service && systemctl disable firewalld.service
#检查与设置时区与时间
timedatectl status
#设置时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
timedatectl set-timezone Asia/Shanghai
安装k3s
master 节点,安装k3s
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn INSTALL_K3S_VERSION=v1.20.8+k3s1 sh -
node 节点安装k3sk
-
从master获取K3S_URL和K3S_TOKEN
cat /var/lib/rancher/k3s/server/node-token -
复制粘贴成如下形式,得到K3S_TOKEN
k3s_token=“K1055e0336c8ef095770165a1b9ab7db35b73f352109916438e1c85aaf9abe73358::server:28f7682fb16e799ea283f7c2b46767f0” -
查看master主机IP,(注意:master主机和work主机可以通信。)命令如下:
k3s_url=“https://ip:6443” -
在work主机执行
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn INSTALL_K3S_VERSION=v1.20.8+k3s1 K3S_URL=${k3s_url} K3S_TOKEN=${k3s_token} sh -
-
执行命令检查是否成功:k3s kubectl get node
“”"
[root@k3s-master server]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k3s-master Ready control-plane,master 3m30s v1.21.1+k3s1
k3s-node01 Ready 3s v1.21.1+k3s1
“”"
将k3s集群导入rancher中
-
直接在rancher-ui添加集群,选择导入,集群名字自定义
-
确定后,因为这边使用的rancher自带证书,所以复制第三个命令到k3s集群master主机的终端运行
curl --insecure -sfL https://rancher.skybility.org/v3/import/htzkpn2576pj56dwl8jhc4wg96547rknc4gf4w5jlpprg8578sxqfm_c-9jxmd.yaml | kubectl apply -f -
3.导入成功,集群状态为active
导入k3s集群出现的问题
1.集群添加不成功,一直处于pending,发现问题如下
[root@k3s-master /]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system metrics-server-86cbb8457f-54vds 1/1 Running 0 66m
kube-system local-path-provisioner-5ff76fc89d-cf7hd 1/1 Running 0 66m
kube-system coredns-7448499f4d-tcjpd 1/1 Running 0 66m
kube-system helm-install-traefik-crd-s9kfc 0/1 Completed 0 66m
kube-system helm-install-traefik-w84qr 0/1 Completed 1 66m
kube-system svclb-traefik-ts9ww 2/2 Running 0 65m
kube-system traefik-97b44b794-fz6wz 1/1 Running 0 65m
kube-system svclb-traefik-8hfv5 2/2 Running 0 63m
cattle-system cattle-cluster-agent-7487885496-zm2xr 0/1 CrashLoopBackOff 3 114s
2.查看日日志
[root@k3s-master /]# kubectl logs cattle-cluster-agent-7487885496-zm2xr -n cattle-system
INFO: Environment: CATTLE_ADDRESS=10.42.0.9 CATTLE_CA_CHECKSUM=646733538a1a16126ff41d170a04d1791fa591fe38ff1b292e02ea54b124e17e CATTLE_CLUSTER=true CATTLE_CLUSTER_REGISTRY=registry.cn-hangzhou.aliyuncs.com CATTLE_FEATURES= CATTLE_INTERNAL_ADDRESS= CATTLE_IS_RKE=false CATTLE_K8S_MANAGED=true CATTLE_NODE_NAME=cattle-cluster-agent-7487885496-zm2xr CATTLE_SERVER=https://rancher.skybility.org
INFO: Using resolv.conf: search cattle-system.svc.cluster.local svc.cluster.local cluster.local nameserver 10.43.0.10 options ndots:5
ERROR: https://rancher.skybility.org/ping is not accessible (Could not resolve host: rancher.skybility.org)
问题很明显,虽然客户端配置了 hosts,但是集群节点并不知道 rancher.skybility.org 是哪个机器。
3.解决方法
在所有节点配置 /etc/hosts,先让所有节点都能认识 rancher.skybility.org
vim /etc/hosts
# 添加如下配置
ip rancher.skybility.org
为Agent Pod添加主机别名(/etc/hosts)
kubectl -n cattle-system patch deployments cattle-cluster-agent --patch '{
"spec": {
"template": {
"spec": {
"hostAliases": [
{
"hostnames":
[
"ns.skyrancher.com"
],
"ip": "192.168.4.96"
}
]
}
}
}
}'
如果需要切换容器引擎,默认为container
# 修该容器引擎,定义变量 INSTALL_K3S_EXEC
export INSTALL_K3S_EXEC="--docker --write-kubeconfig ~/.kube/config --write-kubeconfig-mode 666"
curl -sfL https://docs.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
命令参数解释:
INSTALL_K3S_EXEC="(略)"效果为启动k3s服务时使用的额外参数。
--docker :效果为使用docker而不是默认的containerd。
--write-kubeconfig-mode 666 :效果为将配置文件权限改为非所有者也可读可写,进而使kubectl命令无需root或sudo。
--write-kubeconfig ~/.kube/config :效果为将配置文件写到k8s默认会用的位置,而不是k3s默认的位置/etc/rancher/k3s/k3s.yaml。后者会导致istio、helm需要额外设置或无法运行。
问题二
- 导入完成后,一直处于wait状态,查看日志,出现以下错误,dns问题
time="2021-06-21T01:12:44Z" level=error msg="failed to call leader func: settings.management.cattle.io \"install-uuid\" not found"
time="2021-06-21T01:12:49Z" level=error msg="failed to call leader func: settings.management.cattle.io \"install-uuid\" not found"