使用k3s来进行kubernetes集群部署,对于初学者,相较于vagrant、minikube甚至kubeadm在使用上都会更加流畅。在easypack上封装了一个200行左右的脚本,就可以轻松实现集群的安装、部署、卸载、服务启动、停止、重启、状态确认,使用起来非常方便。
hostname | IP | 内存 | 硬盘 | 操作系统 | 用途 |
---|---|---|---|---|---|
host121 | 192.168.163.121 | 512MB | 8G | CentOS 7.6 | Master |
host122 | 192.168.163.122 | 512MB | 5G | CentOS 7.6 | Node |
host123 | 192.168.163.123 | 512MB | 5G | CentOS 7.6 | Node |
host124 | 192.168.163.124 | 512MB | 5G | CentOS 7.6 | Node |
下载命令:git clone http://github.com/liumiaocn/easypack
[root@host121 ~]# git clone http://github.com/liumiaocn/easypack
Cloning into 'easypack'...
remote: Enumerating objects: 220, done.
remote: Counting objects: 100% (220/220), done.
remote: Compressing objects: 100% (167/167), done.
remote: Total 3245 (delta 123), reused 91 (delta 42), pack-reused 3025
Receiving objects: 100% (3245/3245), 2.15 MiB | 803.00 KiB/s, done.
Resolving deltas: 100% (1580/1580), done.
[root@host121 ~]# cd easypack/containers/standard/rancher/k3s/shell/
[root@host121 shell]# ls
install.cfg install.sh k3s-mgnt
[root@host121 shell]#
设定对象文件:install.cfg
本文提到的1主3从的方式设定示例如下所示
export HOST_LIST_K3S_SERVER=${HOST_LIST_K3S_SERVER:-192.168.163.121}
export HOST_LIST_K3S_AGENT=${HOST_LIST_K3S_AGENT:-192.168.163.122 192.168.163.123 192.168.163.124}
单机版可使用如下方式进行设定
export HOST_LIST_K3S_SERVER=${HOST_LIST_K3S_SERVER:-192.168.163.121}
export HOST_LIST_K3S_AGENT=
另外可以不必修改设定文件,直接在终端进行export操作,这样只会影响当前安装, 示例如下
export HOST_LIST_K3S_AGENT=“192.168.163.122 192.168.163.123 192.168.163.124”
使用ssh-keygen和ssh-copy-id设定ssh,执行完毕后进行检查。如果手动已经设置完毕,可跳过此步骤。
生成SSH-KEY:sh k3s-mgnt sshkeygen server
设定SSH:sh k3s-mgnt sshcopyid server
检验SSH设定:sh k3s-mgnt sshcheck server
[root@host121 shell]# sh k3s-mgnt sshkeygen server
## ACTION:[sshkeygen] [server] begins ...
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:FdCq3BhyDDxu6UglKFg1p4pclc3zwR+qbwDDP1Z7fHY root@host121
The key's randomart image is:
+---[RSA 2048]----+
|.o.oo.= oo. |
|+ . *= + o.o |
|. =o= o.= . |
|..o.=++ .= . |
|.o.+ ++=S o |
| . . +*.. o o E |
| . + . o . |
| o |
| . |
+----[SHA256]-----+
## ACTION:[sshkeygen] [server] ends ...
[root@host121 shell]# sh k3s-mgnt sshcopyid server
## ACTION:[sshcopyid] [server] begins ...
## Host: 192.168.163.121
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.163.121 (192.168.163.121)' can't be established.
ECDSA key fingerprint is SHA256:y9ir2Jbc7kNZPhP9h/O9juUZbTmGDo6NZi2IZnLwg0s.
ECDSA key fingerprint is MD5:d5:e7:dc:b3:21:06:5a:e5:c0:f7:a4:60:f7:59:d4:48.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.163.121'"
and check to make sure that only the key(s) you wanted were added.
...省略
## ACTION:[sshcopyid] [server] ends ...
[root@host121 shell]#
[root@host121 shell]# sh k3s-mgnt sshcheck server
## ACTION:[sshcheck] [server] begins ...
## Host: 192.168.163.121 :SSH CHECK :OK
## Host: 192.168.163.122 :SSH CHECK :OK
## Host: 192.168.163.123 :SSH CHECK :OK
## Host: 192.168.163.124 :SSH CHECK :OK
## ACTION:[sshcheck] [server] ends ...
[root@host121 shell]#
执行命令:sh k3s-mgnt download server
[root@host121 shell]# sh k3s-mgnt download server
## ACTION:[download] [server] begins ...
download begins, it will take serveral minutes, pls be patient...
download completed ...
-rwxr-xr-x. 1 root root 51027968 Nov 28 21:57 /usr/local/bin/k3s
k3s version v1.0.0 (18bd921c)
## ACTION:[download] [server] ends ...
[root@host121 shell]#
执行命令:sh k3s-mgnt install server
[root@host121 shell]# sh k3s-mgnt install server
## ACTION:[install] [server] begins ...
## Host: 192.168.163.121
begin to install semanage
/usr/sbin/semanage
copy install scripts to 192.168.163.121
install.sh 100% 20KB 9.6MB/s 00:00
k3s server installation and setting begins ...
[INFO] Skipping k3s download and verify
which: no kubectl in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
[INFO] Creating /usr/local/bin/kubectl symlink to k3s
which: no crictl in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
[INFO] Creating /usr/local/bin/crictl symlink to k3s
which: no ctr in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
[INFO] Creating /usr/local/bin/ctr symlink to k3s
[INFO] Creating killall script /usr/local/bin/k3s-killall.sh
[INFO] Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO] env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO] systemd: Creating service file /etc/systemd/system/k3s.service
[INFO] systemd: Enabling k3s unit
Created symlink from /etc/systemd/system/multi-user.target.wants/k3s.service to /etc/systemd/system/k3s.service.
[INFO] systemd: Starting k3s
## ACTION:[install] [server] ends ...
[root@host121 shell]#
[root@host121 shell]# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
host121 NotReady master 10s v1.16.3-k3s.2 192.168.163.121 CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 containerd://1.3.0-k3s.4
[root@host121 shell]#
注:上述获取节点的状态虽然是NotReady,原因只是获取的时间过早,稍等一会就可以看到Ready的结果了
执行命令:sh k3s-mgnt install agent
[root@host121 shell]# sh k3s-mgnt install agent
## ACTION:[install] [agent] begins ...
## Host: 192.168.163.122
begin to install semanage
warning: /var/cache/yum/x86_64/7/base/packages/audit-libs-2.8.5-4.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) "
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
/usr/sbin/semanage
copy install scripts to 192.168.163.122
install.sh 100% 20KB 6.9MB/s 00:00
copy k3s binary file to 192.168.163.122
k3s 100% 49MB 21.1MB/s 00:02
set k3s binary file mode to 755
check k3s version
k3s version v1.0.0 (18bd921c)
k3s agent installation and setting begins ...
[INFO] Finding latest release
[INFO] Using v1.0.0 as release
[INFO] Downloading hash https://github.com/rancher/k3s/releases/download/v1.0.0/sha256sum-amd64.txt
[INFO] Skipping binary downloaded, installed k3s matches hash
which: no kubectl in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
[INFO] Creating /usr/local/bin/kubectl symlink to k3s
which: no crictl in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
[INFO] Creating /usr/local/bin/crictl symlink to k3s
which: no ctr in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
[INFO] Creating /usr/local/bin/ctr symlink to k3s
[INFO] Creating killall script /usr/local/bin/k3s-killall.sh
[INFO] Creating uninstall script /usr/local/bin/k3s-agent-uninstall.sh
[INFO] env: Creating environment file /etc/systemd/system/k3s-agent.service.env
[INFO] systemd: Creating service file /etc/systemd/system/k3s-agent.service
[INFO] systemd: Enabling k3s-agent unit
Created symlink from /etc/systemd/system/multi-user.target.wants/k3s-agent.service to /etc/systemd/system/k3s-agent.service.
[INFO] systemd: Starting k3s-agent
## Host: 192.168.163.123
begin to install semanage
...省略
## ACTION:[install] [agent] ends ...
[root@host121 shell]#
[root@host121 shell]# kubectl version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3-k3s.2", GitCommit:"e7e6a3c4e9a7d80b87793612730d10a863a25980", GitTreeState:"clean", BuildDate:"2019-11-18T18:31:23Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3-k3s.2", GitCommit:"e7e6a3c4e9a7d80b87793612730d10a863a25980", GitTreeState:"clean", BuildDate:"2019-11-18T18:31:23Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"}
[root@host121 shell]#
[root@host121 shell]# kubectl cluster-info
Kubernetes master is running at https://127.0.0.1:6443
CoreDNS is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[root@host121 shell]#
[root@host121 shell]# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
host123 Ready 7m57s v1.16.3-k3s.2 192.168.163.123 CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 containerd://1.3.0-k3s.4
host121 Ready master 12m v1.16.3-k3s.2 192.168.163.121 CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 containerd://1.3.0-k3s.4
host124 Ready 7m22s v1.16.3-k3s.2 192.168.163.124 CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 containerd://1.3.0-k3s.4
host122 Ready 11m v1.16.3-k3s.2 192.168.163.122 CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 containerd://1.3.0-k3s.4
[root@host121 shell]#