1 环境介绍

虽然 kubeadm, kops, kubespray 以及 rke, kubesphere 等工具可以快速部署 K8s 集群,但是依然会有很多人热衷与使用二进制部署 K8s 集群。

二进制部署可以加深对 K8s 各组件的理解,可以灵活地将各个组件部署到不同的机器,以满足自身的要求。还可以生成一个超长时间自签证书,比如 99 年,免去忘记更新证书过期带来的生产事故。

本文基于当前(2021-12-31)最新版本 K8s 1.23.1,总体和网上的 1.20,1.22 等版本的部署方式没有太大的区别,主要参考了韩先超老师的 K8s 1.20 版本的二进制部署教程。

另外,我的环境是使用 m1 芯片的 macbook 运行的 ubuntu 20.04 TLS 虚拟机搭建,因此本次环境搭建 K8s 是基于 arm64 架构的。

1.0 书写约定

  • 命令行输入,均以 符号表示
  • 注释使用 #// 表示
  • 执行命令输出结果,以空行分隔

1.1 规划

角色主机名IP组件
master nodeubuntu-k8s-master-0110.211.55.4etcd, kube-apiserver, kube-controller-manager,
kube-scheduler, kube-proxy, kubelet
worker nodeubuntu-k8s-worker-0110.211.55.5kubelet, kube-proxy

1.2 环境配置

  • 设置主机名

    # 10.211.55.4 主机
    ➜ sudo hostnamectl set-hostname ubuntu-k8s-master-01
    # 10.211.55.5 主机
    ➜ sudo hostnamectl set-hostname ubuntu-k8s-worker-01

- 阅读剩余部分 -

Question 1

Task weight: 1%

You have access to multiple clusters from your main terminal through kubectl contexts. Write all context names into /opt/course/1/contexts, one per line.

From the kubeconfig extract the certificate of user restricted@infra-prod and write it decoded to /opt/course/1/cert.

题目解析:

  • 考点

    • kubectl
  • 解题

    ➜ kubectl config get-contexts -o name > /opt/course/1/contexts
    
    # 从 .kube/config 文件中找到
    - name: restricted@infra-prod
      user
        client-certificate-data: LS0tLS1CRUdJ...
    ➜ echo LS0tLS1CRUdJ... | base64 -d > /opt/course/1/cert

- 阅读剩余部分 -

Question 1

Task weight: 1%

You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts.

Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh, the command should use kubectl.

Finally write a second command doing the same thing into /opt/course/1/context_default_no_kubectl.sh, but without the use of kubectl.

题目解析:

  • 考点

  • 解题

    • 根据题意:Write all those context names into /opt/course/1/contexts

      ➜ kubectl config get-contexts -o name > /opt/course/1/contexts
    • 根据题意:Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh, the command should use kubectl

      ➜ vim /opt/course/1/context_default_kubectl.sh
      kubectl config current-context
    • 根据题意:Finally write a second command doing the same thing into /opt/course/1/context_default_no_kubectl.sh, but without the use of kubectl

      ➜ vim /opt/course/1/context_default_no_kubectl.sh
      grep "current-context: " ~/.kube/config | awk '{print $2}'

- 阅读剩余部分 -

# coding: utf8

# 使用说明:
# 1. 默认参数启动
#    python loggenerator.py
# 2. 自定义参数启动
#    KEY1=VALUE1 KEY2=VALUE2 python loggenerator.py
#    支持参数:
#       TYPE: 日志类型,默认json,可选txt
#       MAX: 日志总量,默认10000000
#       SPEED: 每秒生成日志量,默认15000
#       OUTPUT: 日志文件,默认logs/sum.log
#       MAXSIZE: 日志文件大小,超过此大小将会进行轮转
import os
import gzip
import shutil
from random import random
import time
import json


def logCompressor(filepath):
    offset = 0
    maxsize = os.getenv('MAXSIZE', 50 * 1024 * 1024)
    r = ''
    while True:
        n = yield r
        if not n:
            return
        size = os.path.getsize(filepath)
        if size >= maxsize:
            tmpfile = "%s%d_tmp" % (filepath, int(random() * 1e17))
            shutil.move(filepath, tmpfile)
            gzip.GzipFile(filename="", mode='wb', compresslevel=9, fileobj=open(
                "%s-%s.%d.log.gz" % (filepath.split('.')[0], time.strftime("%Y.%m.%d", time.localtime()), offset),
                'wb')).write(open(tmpfile, 'rb').read())
            os.remove(tmpfile)
            offset += 1
            r = '200'
        else:
            r = '0'


def logGenerator(c, maxline, speed, filepath, logtype):
    if not os.path.exists(os.path.dirname(filepath)):
        os.mkdir(os.path.dirname(filepath))
    fb = open(filepath, 'a+')
    c.send(None)
    n = 0
    while n < maxline:
        start = time.time()
        s = 0 # 控制速率
        while s < speed:
            if logtype == 'json':
                m = {
                    "level": "INFO",
                    "date": time.strftime("%Y.%m.%d %H:%M:%S", time.localtime()),
                    "message": "time:%s, nothing to do!" % time.time(),
                    "business": "logGenerator:19",
                    "service": "loggenerator",
                    "hostname": "fluentd1"
                }
                m = json.dumps(m)
            else:
                m = '%s [INFO] [logGenerator:19] - time:%s, nothing to do!' % (time.strftime("%Y.%m.%d %H:%M:%S", time.localtime()), time.time())
            fb.write(m + "\n")
            n += 1
            s += 1
            r = c.send(n)
            if r == '200':
                fb.close()
                fb = open(filepath, 'w+')
        end = time.time()
        if end - start < 1:
            # 写入耗时小于1秒,控制写入速度
            time.sleep(1 - (end - start))
    c.close()


if __name__ == "__main__":
    maxline = os.getenv('MAX', 10000000)
    speed = os.getenv('SPEED', 15000)
    logfile = os.getenv('OUTPUT', 'logs/sum.log')
    logtype = os.getenv('TYPE', 'json')
    c = logCompressor(logfile)
    logGenerator(c, maxline, speed, logfile, logtype)

nf_conntrack 调优

netfilter

netfilter是linux内在的一个软件框架,用来管理网络数据包。

netfilter提供了5个hook来进行管理网络包。如下图:

netfilter-hooks

  • PREROUTING, 所有包都会经过这个hook
  • LOCAL INPUT, 进入本机的包会经过这个hook
  • FORWARD, 不进入本机的包,做转发的包会经过这个hook
  • LOCAL OUTPUT, 从本机出去的包会经过这个hook
  • POSTROUTING, 所有出去的包都会经过这个hook

netfilter进行包的管理,则需要记录每个连接的状态信息。这就是nf_conntrack的工作了。

nf_conntrack

nf_conntrack是netfilter的一个子系统。它记录了每个连接的状态信息。

nf_conntrack记录的信息包括,源ip、端口,目标ip、端口,连接状态,协议等。

连接状态包含以下几种:

  • NEW, 新创建的连接,发起连接方发出包后,还没收到回包,都处理这种状态。
  • ESTABLISHED, 已建立的连接,发起连接后,收到回包,这时处理已连接状态。
  • RELATED, 与其他连接相关联,其他的连接与此连接有关联。如ftp的控制连接和数据连接。
  • INVALID, 非法的连接,比如包的行为不合法。

nf_conntrack需要保存这些信息在它自己的数据结构中。其数据结构如下:

connection-tracking-structure

它是一个开链的哈希表,链表是一个双向表。每个哈希节点称为一个bucket,计算出同样哈希值的连接放到链表里连起来。 每个节点记录了请求方向、响应方向的消息。

哈希表的大小,也就是说哈希表的节点数,由nf_conntrack_buckets配置。

nf_conntrack能跟踪的最大连接数由nf_conntrack_max配置。

- 阅读剩余部分 -