杨梅冲
每天在想什么呢?

三、安装k8s

k3s官网:https://k3s.io

文档:

github:https://github.com/k3s-io/k3s

3.1 安装基础环境

# 安装基础环境
1.安装yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

2.修改hostname
hostnamectl set-hostname master && bash

3.关闭防火墙、关闭selinux,关闭iptables
systemctl stop firewalld ; systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

service iptables stop && systemctl disable iptables
iptables -F

4.关闭swap
swapoff -a 临时关闭
注销/etc/fstab中swap

5.修改机器内核参数
modprobe br_netfilter
echo "modprobe br_netfilter" >> /etc/profile

cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

6.配置hosts
cat /etc/hosts
192.168.10.50 master
192.168.10.51 node1

7.时间同步设置定时同步任务
yum install ntpdate -y
ntpdate time1.aliyun.com
* */1 * * * /usr/sbin/ntpdate time1.aliyun.com
systemctl restart crond

8.开启ipvs支持
#!/bin/bash
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in ${ipvs_modules}; do
 /sbin/modinfo -F filename ${kernel_module} > /dev/null 2>&1
 if [ 0 -eq 0 ]; then
 /sbin/modprobe ${kernel_module}
 fi
done

chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep ip_vs

9.安装containerd
yum install -y containerd
systemctl start containerd && systemctl status containerd && systemctl enable containerd
名称 IP 配置 用途
master 192.168.10.50 2核2G K3s Server
node1 192.168.10.51 2核2G K3s agent

 

 

 

本来是2核1G,结果卡死了,只能将内存升级到2G

3.1 安装k3s server节点

国内用户安装执行以下命令:

# 安装最新的
curl -sfL https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
k3s启动成功
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Skipping /usr/local/bin/ctr symlink to k3s, command exists in PATH at /usr/bin/ctr
[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
INSTALL_K3S_VERSION=v1.20.7+k3s1 && INSTALL_K3S_MIRROR=cn && sh k3s-install.sh
指定版本安装,没试过可能有问题
https://docs.rancher.cn/docs/k3s/installation/install-options/_index/#使用脚本安装的选项
# 验证安装是否成功
[root@master ~]# kubectl get nodes
NAME     STATUS   ROLES                  AGE   VERSION
master   Ready    control-plane,master   18m   v1.24.4+k3s1

[root@master ~]# kubectl get pods -n kube-system
NAME                                      READY   STATUS      RESTARTS       AGE
local-path-provisioner-7b7dc8d6f5-k6wng   1/1     Running     0              18m
coredns-b96499967-t6q94                   1/1     Running     1 (13m ago)    18m
helm-install-traefik-crd-98l64            0/1     Completed   1              18m
helm-install-traefik-h25tq                0/1     Completed   2              18m
metrics-server-668d979685-965rv           1/1     Running     2 (105s ago)   18m
svclb-traefik-b4e8c1f8-xjvtz              2/2     Running     0              98s
traefik-7cd4fcff68-nvchn                  1/1     Running     0              98s

[root@master ~]# kubectl top nodes
NAME     CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
master   192m         9%     893Mi           91% 

[root@master ~]# systemctl status k3s
● k3s.service - Lightweight Kubernetes
   Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2022-09-05 19:00:02 CST; 19min ago

部署完成之后,上面所有的进程都由 k3s 这个 service 来创建管理。可以使用 systemctl status k3s 查看。

3.2 k3s集群添加work工作节点

# 1.提取 join token 
[root@master ~]# cat /var/lib/rancher/k3s/server/node-token 
K10a705b6f8de0f13587a6277a19ee3068749d5dc0cfb509e650046473f7af0c5f8::server:bd48ecd96b13ca6c4b7cd969545e1264

# 2.work节点执行
[root@node1 ~]# curl -sfL https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn K3S_URL=https://192.168.10.51:6443 K3S_TOKEN=K10a705b6f8de0f13587a6277a19ee3068749d5dc0cfb509e650046473f7af0c5f8::server:bd48ecd96b13ca6c4b7cd969545e1264 sh -
# 安装完成:
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Skipping /usr/local/bin/ctr symlink to k3s, command exists in PATH at /usr/bin/ctr
[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
[root@master ~]# kubectl get nodes
NAME     STATUS   ROLES                  AGE   VERSION
master   Ready    control-plane,master   72m   v1.24.4+k3s1
node1    Ready    <none>                 64s   v1.24.4+k3s1

[root@master ~]# kubectl get pods -n kube-system
NAME                                      READY   STATUS      RESTARTS      AGE
local-path-provisioner-7b7dc8d6f5-k6wng   1/1     Running     0             72m
coredns-b96499967-t6q94                   1/1     Running     1 (67m ago)   72m
helm-install-traefik-crd-98l64            0/1     Completed   1             72m
helm-install-traefik-h25tq                0/1     Completed   2             72m
metrics-server-668d979685-965rv           1/1     Running     2 (56m ago)   72m
svclb-traefik-b4e8c1f8-xjvtz              2/2     Running     0             55m
traefik-7cd4fcff68-nvchn                  1/1     Running     0             55m
svclb-traefik-b4e8c1f8-gmft9              2/2     Running     0             72s

3.3 k3s卸载

[root@master ~]# ll /usr/local/bin/
总用量 64816
lrwxrwxrwx 1 root root        3 9月   5 18:59 crictl -> k3s
-rwxr-xr-x 1 root root 66363392 9月   5 18:59 k3s
-rwxr-xr-x 1 root root     2024 9月   5 18:59 k3s-killall.sh
-rwxr-xr-x 1 root root     1397 9月   5 18:59 k3s-uninstall.sh
lrwxrwxrwx 1 root root        3 9月   5 18:59 kubectl -> k3s

# 卸载命令
[root@master ~]# /usr/local/bin/k3s-uninstall.sh

 四、k3s安装redis

# 导入镜像
ctr images import redis-master.tar.gz
ctr images import redis-slave.tar.gz

#
[root@master redis]# cat redis-master-deployment.yaml 
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: redis-master
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
      role: master
      tier: backend
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
        role: master
        tier: backend
    spec:
      containers:
      - name: master
        image: docker.io/kubeguide/redis-master:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 6379

[root@master redis]# cat redis-master-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels:
    app: redis
    role: master
    tier: backend
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis
    role: master
    tier: backend

[root@master redis]# kubectl apply -f redis-master-deployment.yaml 
deployment.apps/redis-master configured
[root@master redis]# kubectl apply -f redis-master-service.yaml

# slave
[root@master redis]# cat redis-slave-deployment.yaml 
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: redis-slave
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
      role: slave
      tier: backend
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
        role: slave
        tier: backend
    spec:
      containers:
      - name: slave
        image: docker.io/kubeguide/guestbook-redis-slave:latest
        imagePullPolicy: IfNotPresent
        env:
        - name: GET_HOSTS_FROM
          value: dns
        ports:
        - containerPort: 6379

[root@master redis]# cat redis-slave-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: redis-slave
  labels:
    app: redis
    role: slave
    tier: backend
spec:
  ports:
  - port: 6379
  selector:
    app: redis
    role: slave
    tier: backen

[root@master redis]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
redis-master-7689cf48c9-wms77   1/1     Running   0          4m42s
redis-slave-76c777b89b-42n2v    1/1     Running   0          3m49s

 

posted on 2022-09-05 19:40  杨梅冲  阅读(1030)  评论(0编辑  收藏  举报