centos k3s部署

时间:2024-03-09 20:48:28

一、k3s介绍

1、k3s是一个轻量级的 Kubernetes 发行版,它针对边缘计算、物联网等场景进行了高度优化。我们希望安装的 Kubernetes 在内存占用方面只是一半的大小。Kubernetes 是一个 10 个字母的单词,简写为 k8s。所以,有 Kubernetes 一半大的东西就是一个 5 个字母的单词,简写为 k3s。k3s 没有全称,也没有官方的发音。

2、文档/下载地址

3、环境规划

集群角色 ip地址
server节点 192.168.3.17
agent节点 192.168.3.18
  • 关闭防火墙
# 关闭防火墙
systemctl stop firewalld

# 开机禁用
systemctl disable firewalld

# 检查状态
firewall-cmd --state

4、注意:每台计算机必须具有唯一的主机名。如果您的计算机没有唯一的主机名,请传递K3S_NODE_NAME环境变量,并为每个节点提供一个有效且唯一的主机名。

二、在线安装

1、server节点安装

  • 运行脚本
export INSTALL_K3S_VERSION=v1.21.5+k3s2
export K3S_NODE_NAME=k3s-master
export INSTALL_K3S_EXEC="--docker --write-kubeconfig ~/.kube/config --cluster-cidr 10.72.0.0/16 --service-cidr 10.73.0.0/16 --write-kubeconfig-mode 666"

# 高可用(配置外部sql,第二个server节点需要配置K3S_TOKEN)
# export K3S_TOKEN=server1的token
# export INSTALL_K3S_EXEC="--docker --datastore-endpoint=mysql://root:123456@tcp(192.168.3.13:6008)/k3s --write-kubeconfig ~/.kube/config --cluster-cidr 10.72.0.0/16 --service-cidr 10.73.0.0/16 --write-kubeconfig-mode 666"

curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
  • server参数说明:
    • --docker:k3s server组件以containerd作为容器运行时。可以顺便在k3s server节点上启动一个Agent节点,Agent节点可以使用Docker作为容器运行时,这样k3s server节点也可以当做工作节点用。当然也可以不在server节点上启动Agent节点(添加参数--disable-agent即可)。
    • --bind-address:k3s监听的IP地址,非必选,默认是localhost。
    • --cluster-cidr:与Kubernetes一样,也就是Pod所在网络平面,非必选,默认是10.42.0.0/16。
    • --service-cidr:与Kubernetes一样,服务所在的网络平面,非必选,默认是10.43.0.0/16。
    • --kube-apiserver-arg:额外的API Server配置参数,具体可以参考Kuberntes官方网站了解支持的配置选项,非必选。
    • --write-kubeconfig:安装时顺便写一个Kubeconfig文件,方便使用kubectl工具直接访问。如果不加此参数,则默认的配置文件路径为/etc/rancher/k3s/k3s.yaml,默认只有root用户能读。
    • --write-kubeconfig-mode:与--write-kubeconfig一起使用,指定Kubeconfig文件的权限。
    • --node-label:顺便给节点打上一个asrole=worker的label,非必选。

2、agent节点安装

  • 从server节点获取token
cat /var/lib/rancher/k3s/server/node-token
  • agent节点执行脚本
export INSTALL_K3S_VERSION=v1.21.5+k3s2
export K3S_NODE_NAME=k3s-node1
export INSTALL_K3S_EXEC="--docker --write-kubeconfig ~/.kube/config --cluster-cidr 10.72.0.0/16 --service-cidr 10.73.0.0/16 --write-kubeconfig-mode 666"
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn K3S_URL=https://192.168.3.17:6443 K3S_TOKEN=server1的token sh -

三、离线安装

1、server节点安装

  • 下载文件
cd /usr/local/bin

# 下载k3s-airgap-images-amd64.tar
wget http://rancher-mirror.cnrancher.com/k3s/v1.21.5-k3s2/k3s-airgap-images-amd64.tar

# 下载k3s可执行文件
wget http://rancher-mirror.cnrancher.com/k3s/v1.21.5-k3s2/k3s

# 下载安装脚本
wget http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh

# 设置权限
chmod +x k3s-install.sh k3s
  • 设置环境变量并安装
# 跳过二进制文件下载
export INSTALL_K3S_SKIP_DOWNLOAD=true

# 修改节点名称
export K3S_NODE_NAME=k3s-master

# 配置额外参数
export INSTALL_K3S_EXEC="--docker --write-kubeconfig ~/.kube/config --cluster-cidr 10.72.0.0/16 --service-cidr 10.73.0.0/16 --write-kubeconfig-mode 666"

# 高可用(配置外部sql,第二个server节点需要配置K3S_TOKEN)
# export K3S_TOKEN=server1的token
# export INSTALL_K3S_EXEC="--docker --datastore-endpoint=mysql://root:123456@tcp(192.168.3.13:6008)/k3s --write-kubeconfig ~/.kube/config --cluster-cidr 10.72.0.0/16 --service-cidr 10.73.0.0/16 --write-kubeconfig-mode 666"

# 执行安装脚本
./k3s-install.sh

2、agent节点安装

  • 从server节点获取token
cat /var/lib/rancher/k3s/server/node-token
  • 下载文件
cd /usr/local/bin

# 下载k3s-airgap-images-amd64.tar
wget http://rancher-mirror.cnrancher.com/k3s/v1.21.5-k3s2/k3s-airgap-images-amd64.tar

# 下载k3s可执行文件
wget http://rancher-mirror.cnrancher.com/k3s/v1.21.5-k3s2/k3s

# 下载安装脚本
wget http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh

# 设置权限
chmod +x k3s-install.sh k3s
  • 设置环境变量并安装
# 跳过二进制文件下载
export INSTALL_K3S_SKIP_DOWNLOAD=true

# 修改节点名称
export K3S_NODE_NAME=k3s-node1

# 配置server节点信息,token来自server节点
export K3S_URL=https://192.168.3.17:6443
export K3S_TOKEN=server1的token

# 配置额外参数
export INSTALL_K3S_EXEC="--docker --write-kubeconfig ~/.kube/config --cluster-cidr 10.72.0.0/16 --service-cidr 10.73.0.0/16 --write-kubeconfig-mode 666"

# 执行安装脚本
./k3s-install.sh

四、高可用安装

1、一个 K3s HA 集群由以下几个部分组成:

  • 两个或多个server 节点,将为 Kubernetes API 提供服务并运行其他 control-plane 服务
  • 零个或多个agent 节点,用于运行您的应用和服务
  • 外部数据存储 (与单个 k3s server 设置中使用的嵌入式 SQLite 数据存储相反)
  • 固定的注册地址,位于 server 节点的前面,以允许 agent 节点向集群注册

2、指定mysql数据库

  • 格式:mysql://username:password@tcp(hostname:3306)/database-name
  • 如果指定的数据库名称不存在,k3s server 将尝试创建它

3、server节点修改

  • 增加参数:--datastore-endpoint
# 配置数据库信息
export INSTALL_K3S_EXEC="--docker --datastore-endpoint=mysql://root:123456@tcp(192.168.3.13:6008)/k3s --write-kubeconfig ~/.kube/config --write-kubeconfig-mode 666"
  • 第二个server节点需要指定K3S_TOKEN
export K3S_TOKEN=server1的token

4、agent节点不做任何修改

五、配置k3s镜像仓库

在Docker中可以通过配置registry-mirrors来实现从其他镜像仓库中获取K3s镜像。这样配置之后,会先从registry-mirrors配置的地址拉取镜像,如果获取不到才会从默认的docker.io获取镜像,从而满足了我们的需求

  • 修改daemon.json
vim /etc/docker/daemon.json
  • 添加以下配置
{
    "insecure-registries":["192.168.3.12:6007"]
}
  • 重启docker
systemctl daemon-reload
systemctl restart docker

六、Kubernetes 仪表盘

1、kuboard仪表盘(推荐)

kubectl apply -f https://kuboard.cn/install-script/kuboard.yaml
  • 获取token
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep kuboard-user | awk '{print $1}')
  • 卸载
kubectl delete -f https://kuboard.cn/install-script/kuboard.yaml
  • 访问:ip:32567

2、Kubernetes Dashboard仪表盘

  • 新建 kubernetes-dashboard.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: kubernetes-dashboard

---

apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard

---

apiVersion: v1
kind: ServiceAccount
metadata:
  name: dashboard-admin
  namespace: kubernetes-dashboard

---

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  type: NodePort
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30002
  selector:
    k8s-app: kubernetes-dashboard

---

apiVersion: v1
kind: Secret
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-certs
  namespace: kubernetes-dashboard
type: Opaque

---

apiVersion: v1
kind: Secret
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-csrf
  namespace: kubernetes-dashboard
type: Opaque
data:
  csrf: ""

---

apiVersion: v1
kind: Secret
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-key-holder
  namespace: kubernetes-dashboard
type: Opaque

---

kind: ConfigMap
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-settings
  namespace: kubernetes-dashboard

---

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
rules:
  - apiGroups: [""]
    resources: ["secrets"]
    resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs", "kubernetes-dashboard-csrf"]
    verbs: ["get", "update", "delete"]
  - apiGroups: [""]
    resources: ["configmaps"]
    resourceNames: ["kubernetes-dashboard-settings"]
    verbs: ["get", "update"]
  - apiGroups: [""]
    resources: ["services"]
    resourceNames: ["heapster", "dashboard-metrics-scraper"]
    verbs: ["proxy"]
  - apiGroups: [""]
    resources: ["services/proxy"]
    resourceNames: ["heapster", "http:heapster:", "https:heapster:", "dashboard-metrics-scraper", "http:dashboard-metrics-scraper"]
    verbs: ["get"]

---

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
rules:
  - apiGroups: ["metrics.k8s.io"]
    resources: ["pods", "nodes"]
    verbs: ["get", "list", "watch"]

---

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: kubernetes-dashboard
subjects:
  - kind: ServiceAccount
    name: kubernetes-dashboard
    namespace: kubernetes-dashboard

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kubernetes-dashboard
subjects:
  - kind: ServiceAccount
    name: kubernetes-dashboard
    namespace: kubernetes-dashboard

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: dashboard-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
  - kind: ServiceAccount
    name: dashboard-admin
    namespace: kubernetes-dashboard

---

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: kubernetes-dashboard
  template:
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
    spec:
      containers:
        - name: kubernetes-dashboard
          image: kubernetesui/dashboard:v2.4.0
          imagePullPolicy: Always
          ports:
            - containerPort: 8443
              protocol: TCP
          args:
            - --auto-generate-certificates
            - --namespace=kubernetes-dashboard
          volumeMounts:
            - name: kubernetes-dashboard-certs
              mountPath: /certs
            - mountPath: /tmp
              name: tmp-volume
          livenessProbe:
            httpGet:
              scheme: HTTPS
              path: /
              port: 8443
            initialDelaySeconds: 30
            timeoutSeconds: 30
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            runAsUser: 1001
            runAsGroup: 2001
      volumes:
        - name: kubernetes-dashboard-certs
          secret:
            secretName: kubernetes-dashboard-certs
        - name: tmp-volume
          emptyDir: {}
      serviceAccountName: kubernetes-dashboard
      nodeSelector:
        "kubernetes.io/os": linux
      tolerations:
        - key: node-role.kubernetes.io/master
          effect: NoSchedule

---

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: dashboard-metrics-scraper
  name: dashboard-metrics-scraper
  namespace: kubernetes-dashboard
spec:
  ports:
    - port: 8000
      targetPort: 8000
  selector:
    k8s-app: dashboard-metrics-scraper

---

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: dashboard-metrics-scraper
  name: dashboard-metrics-scraper
  namespace: kubernetes-dashboard
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: dashboard-metrics-scraper
  template:
    metadata:
      labels:
        k8s-app: dashboard-metrics-scraper
    spec:
      securityContext:
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: dashboard-metrics-scraper
          image: kubernetesui/metrics-scraper:v1.0.7
          ports:
            - containerPort: 8000
              protocol: TCP
          livenessProbe:
            httpGet:
              scheme: HTTP
              path: /
              port: 8000
            initialDelaySeconds: 30
            timeoutSeconds: 30
          volumeMounts:
          - mountPath: /tmp
            name: tmp-volume
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            runAsUser: 1001
            runAsGroup: 2001
      serviceAccountName: kubernetes-dashboard
      nodeSelector:
        "kubernetes.io/os": linux
      tolerations:
        - key: node-role.kubernetes.io/master
          effect: NoSchedule
      volumes:
        - name: tmp-volume
          emptyDir: {}
  • 安装
kubectl apply -f kubernetes-dashboard.yaml
  • 获取token
kubectl -n kubernetes-dashboard describe secret dashboard-admin-token | grep '^token'
  • 卸载
kubectl delete -f kubernetes-dashboard.yaml
  • 访问:ip:30002

七、常用命令

1、node节点操作

  • 得到节点信息
kubectl get node
或
kubectl get node -o wide
  • 删除node节点
kubectl delete node node1
  • 修改节点ROLES
kubectl label node ${node} node-role.kubernetes.io/worker=worker

2、命名空间操作

  • 查看所有命名空间
kubectl get namespace
或
kubectl get ns
  • 创建命名空间
kubectl create namespace test
  • 删除命名空间
kubectl delete namespaces test

3、pod/deployment/service共有(get)

  • 不加参数默认default命名空间下的内容
  • 指定命名空间:--namespace default / -n default
  • 所有命名空间下的:--all-namespaces
  • 详细信息:-o wide

4、pod操作

  • 查看pod
kubectl get pod
或
kubectl get pods
  • 详情
kubectl describe pod springboot-demo-dept-6568749656-cztl4 --namespace default
  • 删除pod
kubectl delete pod pod1

5、controller操作

  • 查看Deployment控制器类型
kubectl get deployment
或
kubectl get deployment.apps
或
kubectl get deploy
  • 查看ReplicaSet控制器类型
kubectl get replicaset
或
kubectl get replicaset.apps
  • 详情
kubectl describe deployment springboot-demo-dept --namespace default
  • 删除Deployment控制器类型
kubectl delete deployment nginx-app

6、service操作

  • 查看service
kubectl get service
或
kubectl get svc
  • 详情
kubectl describe service springboot-demo-svc --namespace default
  • 删除service
kubectl delete service nginx-app1

7、显示top资源

  • 查看节点top资源
kubectl top node
  • 查看pod top资源
kubectl top pod --all-namespaces

8、查看pod日志

# 实时日志
kubectl logs -f pod-name -n namespace
kubectl logs --tail 200 -f pod-name -n namespace

# 前200条日志
kubectl logs --tail 200 pod-name -n namespace

9、其它操作

  • 查看k3s信息或版本号
k3s
  • 查看kubectl帮助文档
kubectl --help
  • 部署资源清单
kubectl apply -f xxx.yaml
  • 删除部署清单
kubectl delete -f xxx.yaml
  • 查看k3s服务状态
systemctl status k3s
  • 重启k3s服务
systemctl restart k3s
  • 卸载 K3s
# server节点卸载
/usr/local/bin/k3s-uninstall.sh

# agent节点卸载
/usr/local/bin/k3s-agent-uninstall.sh

八、参考