一、k3s介绍

k3s是rancher推出的一个轻量级k8s产品,相比于k8s,它具有安装简单、资源消耗较少等优势。

二、部署环境

cpu:x86_64
内存: 16G
操作系统: kylin v10 server sp1 

三、K3S部署

3.1 运行k3s安装脚本

K3s 提供了一个安装脚本,这个脚本可以在 https://get.k3s.io 获得。要使用这种方法安装 K3s,只需运行以下命令:

systemctl stop firewalld  #关闭防火墙

systemctl disable firewalld

swapoff -a # 临时禁用swap

sed -i 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config  #禁用selinux

curl -sfL https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -

可以配置INSTALL_K3S_SELINUX_WARN=false跳过k3s selinux配置

3.2  脚本运行完成后,查看集群状态

kubectl cluster-info

kubectl get node

[root@localhost ~]# kubectl cluster-info

Kubernetes control plane 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:https/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

[root@localhost ~]# kubectl get node

NAME    STATUS   ROLES                  AGE    VERSION

node1   Ready    control-plane,master   3h7m   v1.25.4+k3s1

3.3 配置镜像加速

cat >/etc/rancher/k3s/registries.yaml <<EOF

mirrors:

"docker.io":

endpoint:

- "https://wkovahih.mirror.aliyuncs.com"

- "https://registry-1.docker.io"

EOF

3.4 重启k3s

systemctl restart k3s

3.5 安装kubernetes-dashboard

部署kubernetes-dashboard

GITHUB_URL=https://github.com/kubernetes/dashboard/releases

VERSION_KUBE_DASHBOARD=$(curl -w '%{url_effective}' -I -L -s -S ${GITHUB_URL}/latest -o /dev/null | sed -e 's#.*/##')

kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/${VERSION_KUBE_DASHBOARD}/aio/deploy/recommended.yaml

3.6 配置RBAC

创建admin-user

cat <<EOF |kubectl apply -f -

apiVersion: v1

kind: ServiceAccount

metadata:

  name: admin-user

  namespace: kubernetes-dashboard

EOF

3.7 绑定cluster-admin角色

cat <<EOF |kubectl apply -f -

apiVersion: rbac.authorization.k8s.io/v1

kind: ClusterRoleBinding

metadata:

  name: admin-user

roleRef:

  apiGroup: rbac.authorization.k8s.io

  kind: ClusterRole

  name: cluster-admin

subjects:

  - kind: ServiceAccount

    name: admin-user

    namespace: kubernetes-dashboard

EOF

3.8 配置kubernetes-dashboard

配置nodePort方式访问

kubectl patch svc kubernetes-dashboard -p '{"spec":{"type":"NodePort"}}' -n kubernetes-dashboard

kubectl get svc kubernetes-dashboard -n kubernetes-dashboard  #查看分配的nodeport端口

NAME                   TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)         AGE

kubernetes-dashboard   NodePort   10.43.13.41   <none>        443:30449/TCP   3h14m

3.9 获取访问TOKEN

kubectl -n kubernetes-dashboard create token admin-user

3.10 界面效果图

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐