基于k3s快速搭建k8s集群
rancher/k3s 的官方文档比较复杂,其实核心就几个shell命令即可,主要是需要注意版本问题即可。rancher的官方文档:What is Rancher? |
rancher/k3s 的官方文档比较复杂,其实核心就几个shell命令即可,主要是需要注意版本问题即可。
rancher的官方文档:What is Rancher? | Rancher
k3s的官方文档:K3s - 轻量级 Kubernetes | K3s
k3s的主要特点是轻量化,K3s 易于安装,仅需要 Kubernetes 内存的一半,所有组件都在一个小于 100 MB 的二进制文件中。
支持嵌入式系统,边缘计算节点等,如果做如虚实结合类项目,就非常适合。
系统3个节点,一个主节点,两个子节点。系统均为centos7.9
首先在主节点上执行:
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v1.24.8+k3s1 sh -
如果速度过慢,或者访问不了,可以换成国内数据源
curl -sfL https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.sh | INSTALL_K3S_VERSION=v1.24.8+k3s1 sh -
安装完成后,再在主节点上执行:
cat /var/lib/rancher/k3s/server/node-token
可以获得一个密码字符串,记为token,然后在每个子节点上执行:
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v1.24.8+k3s1 K3S_URL=https://主节点ip:6443 K3S_TOKEN=主节点token sh -
可以通过ifconig,获得主节点ip, 然后是token数据。
至此,k8s集群已经搭建完成了。
然后在主节点验证执行:
kubectl get nods
就可以看到3个节点了。
为了能在每个节点上都执行kubectl指令,在主节点上修改文件k3s.yam,主要修改k3s server api端点:
vim /etc/rancher/k3s/k3s.yaml
apiVersion: v1nclusters:n- cluster:n certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJkekNDQVIyZ0F3SUJBZ0lCQURBS0JnZ3Foa2pPUFFRREFqQWpNU0V3SHdZRFZRUUREQmhyTTNNdGMyVnkKZG1WeUxXTmhRREUyTlRBM056TTFNak13SGhjTk1qSXdOREkwTURReE1qQXpXaGNOTXpJd05ESXhNRFF4TWpBegpXakFqTVNFd0h3WURWUVFEREJock0zTXRjMlZ5ZG1WeUxXTmhRREUyTlRBM056TTFNak13V1RBVEJnY3Foa2pPClBRSUJCZ2dxaGtqT1BRTUJCd05DQUFUVXNNMVRIWDFPSUgya3duSFlmVGxmeUl4b0VQb0RJVDFrVzJHWjRCbGQKdlpwQys2cEVuZmNMTUZvaVI5VWYyTlF5K0F5OVpkZ201YnhtSnZ1bjUvaWRvMEl3UURBT0JnTlZIUThCQWY4RQpCQU1DQXFRd0R3WURWUjBUQVFIL0JBVXdBd0VCL3pBZEJnTlZIUTRFRmdRVUlOaklpTS85ZEhnQ0R5eTBPTGF5CmdvdW9qdjR3Q2dZSUtvWkl6ajBFQXdJRFNBQXdSUUlnVDI0eUt4UGNTVDRueXFxZHBZR3RpVFlxdzZ1SXd0Yy8KeXpwb1VqRkxWSWtDSVFDMi8vS1hyKyttUEIrT1VzcFZpLys3TXdQMjVzZXlsQkx4aFc0T0ZJdFR3dz09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0Kn server: https://192.168.0.47:6443 #修改这个ip,改成本机ion name: defaultncontexts:n- context:n cluster: defaultn user: defaultn name: defaultncurrent-context: defaultnkind: Confignpreferences: {}nusers:n- name: defaultn user:n client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJrakNDQVRlZ0F3SUJBZ0lJRElvMjdiMTRsK0F3Q2dZSUtvWkl6ajBFQXdJd0l6RWhNQjhHQTFVRUF3d1kKYXpOekxXTnNhV1Z1ZEMxallVQXhOalV3Tnpjek5USXpNQjRYRFRJeU1EUXlOREEwTVRJd00xb1hEVEl6TURReQpOREEwTVRJd00xb3dNREVYTUJVR0ExVUVDaE1PYzNsemRHVnRPbTFoYzNSbGNuTXhGVEFUQmdOVkJBTVRESE41CmMzUmxiVHBoWkcxcGJqQlpNQk1HQnlxR1NNNDlBZ0VHQ0NxR1NNNDlBd0VIQTBJQUJCYXRTRmN2RE85NXZkdFgKbG5DUk9nQ3huSTlubW5KL3U3UVI2MnlVK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hqOUZ0c0hLVC9FeHZZK0Z1RE1Nbi85ajN3cFJpWFRCa05vPQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==n client-key-data: LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSUFWY2NOL3l1Q2RUUUFvaDUxc1lURkZwTTN5K1lqRHVXSlIvSytkT3NpVzFvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFRnExSVZ5OE03M205MjFlV2NKRTZBTEdjajJlYWNuKzd0QkhyYkpUNktQOW5kVThEQkpDOApYN1NhMWZXSERFM0dnb3ArdDJFdm4xc0lTU3dHMDlyYVlBPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo=n
然后把这个文件 /etc/rancher/k3s/k3s.yaml,复制到每个子节点的/etc/rancher/k3s/位置下即可。
子节点一般没这个目录,需要到每个节点上先创建mkdir -p /etc/rancher/k3s/,然后再复制。
这样一个完整的k8s集群就搭建完成了,后续添加节点,和上面添加节点的方式一样,就是一行脚本就可以了。
上一篇:KS语音号
下一篇:关于zs事件的一些想法