k3s安装部署说明

前言:
为什么不是k8s,k8s机子要求资源太高了,先来个简单的k3s

1:环境
ubuntu18

2安装docker
ubuntu18.0.4 如下

1:禁用防火墙及SELinux(可能需要禁止)
systemctl stop firewalld && systemctl disable firewalld
2: 开启路由转发
sudo vim /etc/sysctl.conf
打开 net.ipv4.ip_forward=1
3:安装docker
1>sudo apt-get update
2>安装 apt 依赖包,用于通过HTTPS来获取仓库:
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
3>添加Docker官方GPG key并验证
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add
sudo apt-key fingerprint 0EBFCD88
4>设置稳定版仓库,用于下载更新Docker
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”
5>再次更新镜像源索引
sudo apt-get update
6>安装docker相关
sudo apt-get install docker-ce docker-ce-cli containerd.io
7>测试是否安装成功
docker -v

3:测试镜像
1>修改镜像地址 改为国内镜像地址,本文是阿里云镜像
编辑文件:sudo vim /etc/docker/daemon.json
在该daemon.json配置文件中加入(如果没有该文件会自动建一个):
{
“registry-mirrors”: [“https://mirrors.aliyun.com/”]
}

ubuntu18@ubuntu:/etc/docker$ vim /etc/docker/daemon.json
ubuntu18@ubuntu:/etc/docker$ sudo vim /etc/docker/daemon.json
ubuntu18@ubuntu:/etc/docker$ sudo systemctl daemon-reload
ubuntu18@ubuntu:/etc/docker$ sudo systemctl restart docker
ubuntu18@ubuntu:/etc/docker$ sudo docker info|grep Mirrors -A 1

2>使daemon.json生效
sudo systemctl daemon-reload
3>重启docker服务
sudo systemctl restart docker
4>查看docker镜像源
sudo docker info|grep Mirrors -A 1
5>运行hello-world
sudo docker run hello-world

ubuntu18@ubuntu:/etc/docker$ sudo docker run hello-world
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
Status: Downloaded newer image for hello-world:latest
6>查看本地镜像
sudo docker images
ubuntu18@ubuntu:/etc/docker$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 4 months ago 13.3kB

7>卸载
sudo apt-get purge docker-ce docker-ce-cli containerd.io
如果要删除镜像和容器
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

8>搜索镜像
docker search node

9>拉取镜像
实际上,镜像的仓库名称中还应该添加仓库地址,即是注册服务器(registry)的地址作为前缀,当省略前缀时,则默认拉取的官方Docker hub所提供的镜像。
docker pull ubuntu:16.04表示从官方docker hub中拉去进行
docker pull cloudcomputing/ubuntu:tagname表示从自己在Docker hub上创建的个人仓库中拉取镜像
docker pull regis**.aliyuncs.com/cloudubuntu/ubuntu:16.04表示从自己在阿里云上注册的仓库中拉取镜像

sudo docker pull ubuntu:18.04

4:
1>启动容器
运行Docker容器
docker run -it --name mycontainer ubuntu:18.04 /bin/bash

ubuntu18@ubuntu:/etc/docker$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 f9a80a55f492 3 months ago 63.2MB
hello-world latest 9c7a54a9a43c 4 months ago 13.3kB
运行命令
ubuntu18@ubuntu:/etc/docker$ docker run -it --name ubuntu18 ubuntu:18.04 /bin/bash

1>拷贝文件
docker cp /path/on/host :/path/to/file

将主机/www/runoob目录拷贝到容器96f7f14e99ab的/www目录下。
docker cp /www/runoob 96f7f14e99ab:/www/

将容器96f7f14e99ab的/www目录拷贝到主机的/tmp目录中。
docker cp 96f7f14e99ab:/www /tmp/

docker cp /root/H-test 0b51220058b9:/root/docker-test/index/folder/

5:停止/退出/删除 容器
1>停止
docker ps 命令列出所有正在运行的容器
docker stop

2>
退出
exit命令
退出后再进入
docker attach <container_id>
docker exec -it <container_id> /bin/bash

3>
运行以下命令来删除镜像:
docker rmi <镜像ID或名称>
强制删除 sudo docker rmi -f f9a80a55f492

4>查看容器
sudo docker ps -a 查看所有容器
sudo docker container ls -all --all, -a false 显示所有容器(默认只显示运行的)
删除容器 sudo docker container rm festive_mccarthy

6:docker 加速
创建或修改 /etc/docker/daemon.json 文件

vi /etc/docker/daemon.json

{
“registry-mirrors”: [“https://registry.docker-cn.com”]
}
systemctl daemon-reload
systemctl restart docker.service
1.网易
http://hub-mirror.c.163.com

2.Docker中国区官方镜像
https://registry.docker-cn.com

3.中国科技大学
https://docker.mirrors.ustc.edu.cn

4.阿里云容器服务
https://cr.console.aliyun.com/
5。阿里专属
首页点击“创建我的容器镜像” 得到一个专属的镜像加速地址,类似于“https://1234abcd.mirror.aliyuncs.com”

3:安装k3s
参考
k3s:https://github.com/k3s-io/k3s/releases

k3s 57.6 MB last week 2进制文件(第一项)
k3s-airgap-images-amd64.tar 552 MB last week 镜像 (第2项)
Source code (zip) 2 weeks ago 源码 (倒数第2项)

离线部署
https://blog.csdn.net/qq_30614345/article/details/131387040

1:

两台主机分别设置

$ hostnamectl set-hostname master
$ hostnamectl set-hostname slave

查看当前主机名称

$ hostname

2::设置 ip 和 hostname 的对应关系

两台主机分别设置

$ cat >> /etc/hosts << EOF
192.168.43.183 master
192.168.43.184 slave
EOF

3:
手动部署镜像(all node)
从 K3s GitHub Release 页面下载 K3s v1.22.13+k3s1二进制文件,K3s 二进制文件需要与离线镜像的版本匹配。
下载地址:https://github.com/rancher/k3s/releases
这里选择 v1.22.13 版本:
https://github.com/k3s-io/k3s/releases/tag/v1.22.13+k3s1

4:
下载下面的两个文件:
k3s
k3s-airgap-images-amd64.tar.gz
将二进制文件放在 /usr/local/bin 目录下,并确保这个二进制文件是可执行的。
$ cp k3s /usr/local/bin/
$ chmod 755 /usr/local/bin/k3s
$ mkdir -p /var/lib/rancher/k3s/agent/images/
$ cp k3s-airgap-images-amd64.tar.gz /var/lib/rancher/k3s/agent/images/

5:下载安装脚本(all node)
下载安装脚本:
https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.sh

chmod +x k3s-install.sh

6: master节点安装

master节点安装

安装k3s,跳过下载二进制文件

$ INSTALL_K3S_SKIP_DOWNLOAD=true ./k3s-install.sh

生成token

[root@master ~]# cat /var/lib/rancher/k3s/server/node-token
K10ae4e5bb6d803f8c2a0056973f4731740c0c78e40538078ed904c51ca0fd33ed0::server:444495845f5bf047ea343bfa5a1d92f5
[root@master ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 27s v1.22.13+k3s1

7:slave节点安装

slave节点安装

INSTALL_K3S_SKIP_DOWNLOAD=true K3S_URL=https://192.168.43.183:6443 K3S_TOKEN=K10ae4e5bb6d803f8c2a0056973f4731740c0c78e40538078ed904c51ca0fd33ed0::server:444495845f5bf047ea343bfa5a1d92f5 ./k3s-install.sh

INSTALL_K3S_SKIP_DOWNLOAD=true K3S_URL=https://192.168.1.29:6443 K3S_TOKEN=K10e6b8a92130993a2f4d0f15842ecbd081b37a381943e81e0048814a888614eb3c::server:05e2b26f063a01522e94b32180678590 sh ./install.sh

8:
查看节点 sudo kubectl get nodes
删除节点 sudo kubectl delete node k8s-node3

卸载 k3s
如果想重装 k3s 或卸载 k3s,可以执行如下命令。

在 server 节点上

k3s-uninstall.sh

在 worker 节点上

k3s-agent-uninstall.sh

9:
sudo kubectl get nodes

输出类似如下,表示集群安装成功,说明一个 server 节点,二个 worker 节点已创建成功。

NAME STATUS ROLES AGE VERSION
server Ready control-plane,master 129m v1.23.4+k3s1
node1 Ready 3m2s v1.23.4+k3s1
node2 Ready 34s v1.23.4+k3s1

10 纯命令启动(非脚本)
启动master和worker

没有脚本启动方便,需要自己指定启动参数,如server、agent等

sudo k3s server &

Kubeconfig is written to /etc/rancher/k3s/k3s.yaml

sudo k3s kubectl get nodes

On a different node run the below. NODE_TOKEN comes from
/var/lib/rancher/k3s/server/node-token on your server

sudo k3s agent --server https://myserver:6443 --token ${NODE_TOKEN}

https://blog.csdn.net/liumiaocn/article/details/103342443
https://blog.csdn.net/qq_30614345/article/details/131387040
11:部署镜像
kubectl run
创建并运行一个或多个容器镜像。
创建一个deployment 或job 来管理容器。
run NAME --image=image [–env=“key=value”] [–port=port] [–replicas=replicas] [–dry-run=bool] [–overrides=inline-json] [–command] – [COMMAND] [args…]
master 上 部署 nginx
sudo kubectl run --image=nginx nginx-app --port=80

ubuntu18@ubuntu:~/k3s$ sudo kubectl run --image=nginx nginx-app --port=80
[sudo] password for ubuntu18:
pod/nginx-app created

[root@host121 ~]# sudo kubectl run --image=nginx nginx-app --port=80
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-app created

通过kubectl get node命令发现此ningx的pod运行所在的节点信息为host122,执行日志如下所示
ubuntu18@ubuntu:~/k3s$ sudo kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app 1/1 Running 0 32s 10.42.2.4 master

使用expose命令以NodePort的方式生成服务
kubectl expose
将资源暴露为新的Kubernetes Service。
expose (-f FILENAME | TYPE NAME) [–port=port] [–protocol=TCP|UDP] [–target-port=number-or-name] [–name=name] [–external-ip=external-ip-of-service] [–type=type]
ubuntu18@ubuntu:~/k3s$ sudo kubectl expose pod nginx-app --port=80 --type=NodePort --target-port=80 --name=nginx-service
service/nginx-service exposed

https://blog.csdn.net/qq_30614345/article/details/131387040
ubuntu18@ubuntu:~/k3s$ sudo kubectl get pods,svc -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-app 1/1 Running 0 104m 10.42.2.4 master

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.43.0.1 443/TCP 2d2h
service/nginx-service NodePort 10.43.60.107 80:31544/TCP 11m run=nginx-app

查看 k3s 安装之后容器内存在的pod:
[root@master ~]# kubectl get pods -A
pod 的状态为 Running 和 Completed 说明安装成功。

如果觉得有用,麻烦点个赞,加个收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值