【容器化】K3S部署安装

本文针对版本v1.21.2+k3s1

官网

https://k3s.io/

前期准备

1
2
# 关闭防火墙
systemctl stop firewalld

完成可联网情况下

1
2
3
export INSTALL_K3S_VERSION=v1.21.2+k3s1
export INSTALL_K3S_EXEC="--write-kubeconfig ~/.kube/config --cluster-cidr 10.72.0.0/16 --service-cidr 10.73.0.0/16"
curl -sfL https://get.k3s.io | sh -

连不上Github情况下

1. 上传相关工具、k3s二进制包、k3s安装脚本、Nginx测试yaml文件到服务器

1
2
3
4
scp k3s-selinux-0.2-1.el7_8.noarch.rpm root@xxx.xxx.xxx.xxx:/usr/local/src/
scp k3s root@xxx.xxx.xxx.xxx:/usr/local/src/
scp k3s.sh root@xxx.xxx.xxx.xxx:/usr/local/src/
scp nginx.yaml root@xxx.xxx.xxx.xxx:/usr/local/src/

2. 配置k3s二进制包的路径和权限

1
2
cp /usr/local/src/k3s /usr/local/bin/
chmod 755 /usr/local/bin/k3s

3. yum安装相关工具

1
2
yum install -y container-selinux selinux-policy-base
yum install -y /usr/local/src/k3s-selinux-0.2-1.el7_8.noarch.rpm

4. 执行k3s安装脚本

1
2
3
4
export INSTALL_K3S_VERSION=v1.21.2+k3s1
export INSTALL_K3S_SKIP_DOWNLOAD=true
export INSTALL_K3S_EXEC="--write-kubeconfig ~/.kube/config --cluster-cidr 172.16.0.0/12 --service-cidr 172.16.130.0/12"
sh /usr/local/src/k3s.sh

5. 检查是否安装成功

1
2
3
4
systemctl status k3s
kubectl get node
# 等待所有服务处于RunningCompleted状态
kubectl get pod -A

结果截图:

6. 测试nginx服务

1
2
3
4
# 创建测试nginx服务
kubectl create -f /usr/local/src/nginx.yaml
# 查看服务是否启动成功,nginx服务处于Running状态的则表明都运行成功
kubectl get pod -A

结果截图:

通过浏览器访问:https://[server-ip]:32000

k3s二进制文件地址

下载地址:https://github.com/k3s-io/k3s/releases/download/v1.21.2+k3s1/k3s

k3s-selinux地址

下载网址:https://rpm.rancher.io/k3s/stable/common/centos/7/noarch/k3s-selinux-0.2-1.el7_8.noarch.rpm

k3s.sh文件内容

下载网址:https://get.k3s.io/

nginx.yaml文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
kind: Namespace
apiVersion: v1
metadata:
name: test-project
---
kind: Service
apiVersion: v1
metadata:
name: service-nginx-server
namespace: test-project
labels:
app: service-nginx-server
spec:
ports:
- name: tcp-80
protocol: TCP
port: 80
targetPort: 80
nodePort: 32000
selector:
app: nginx-server
type: NodePort
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: deployment-nginx-server
namespace: test-project
labels:
app: nginx-server
spec:
replicas: 3
selector:
matchLabels:
app: nginx-server
template:
metadata:
labels:
app: nginx-server
spec:
containers:
- name: container-nginx-server
image: 'nginx:1.20.1-alpine'
ports:
- containerPort: 80
protocol: TCP

(•̀ᴗ•́)و ̑̑

Share