十、二进制部署k8s集群

原创
2020/10/22 16:28
阅读数 547

步骤概括

  1. 准备机器,安装Linux操作系统
  2. 操作系统初始化
  3. 自签证书
  4. 部署master node组件
    1. etcd
    2. kube-apiserver
    3. kube-controller-manager
    4. kube-scheduler
    5. 部署集群网络
  5. 部署worker node组件

准备工作

和使用kubeadm搭建时相同:

  1. 准备机器,安装Linux操作系统
  2. 操作系统初始化

开始搭建

3. 准备cfssl证书生成工具

cfssl是一个开源的证书管理工具,使用json文件生成证书,相比openssl更方便。

$ wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
$ chmod +x cfssl*
$ mv cfssl_linux-amd64 /usr/local/bin/cfssl
$ mv cfssl-certinfo_linux-amd64 /usr/local/bin/cfssl-certinfo
$ mv cfssljson_linux-amd64 /usr/local/bin/cfssljson

4. 部署master组件——etcd

4.0 自签etcd (CA)证书

工作目录:

$ mkdir ~/k8s/ssl/etcd
$ cd ~/k8s/ssl/etcd

自签CA:

cat > ca-config.json << EOF
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "www": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}
EOF

cat > ca-csr.json << EOF
{
    "CN": "etcd CA",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Beijing",
            "ST": "Beijing"
        }
    ]
}
EOF

生成证书:

$ cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
$ ls *pem
ca-key.pem  ca.pem

4.1 使用自签CA证书签发Etcd HTTPS证书

创建证书申请文件:

cat > server-csr.json << EOF
{
    "CN": "etcd",
    "hosts": [
    "172.16.180.128",
    "172.16.180.129",
    "172.16.180.130"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "BeiJing",
            "ST": "BeiJing"
        }
    ]
}
EOF

<font color=red>注:上述文件hosts字段中IP为所有etcd节点的集群内部通信IP,一个都不能少!为了方便后期扩容可以多写几个预留的IP。</font>

生成证书:

$ cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
$ ls server*pem
server-key.pem  server.pem

4.2 部署etcd集群

4.2.1 下载二进制文件:
$ wget https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
4.2.2 创建工作目录并解压二进制包:
$ mkdir /opt/etcd/{bin,cfg,ssl} -p
$ tar zxvf etcd-v3.4.9-linux-amd64.tar.gz
$ mv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
4.2.3 创建etcd配置文件:
cat > /opt/etcd/cfg/etcd.conf << EOF
#[Member]
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://172.16.180.128:2380"
ETCD_LISTEN_CLIENT_URLS="https://172.16.180.128:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://172.16.180.128:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://172.16.180.128:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://172.16.180.128:2380,etcd-2=https://172.16.180.129:2380,etcd-3=https://172.16.180.130:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF
  • ETCD_NAME:节点名称,集群中唯一
  • ETCD_DATA_DIR:数据目录
  • ETCD_LISTEN_PEER_URLS:集群通信监听地址
  • ETCD_LISTEN_CLIENT_URLS:客户端访问监听地址
  • ETCD_INITIAL_ADVERTISE_PEER_URLS:集群通告地址
  • ETCD_ADVERTISE_CLIENT_URLS:客户端通告地址
  • ETCD_INITIAL_CLUSTER:集群节点地址
  • ETCD_INITIAL_CLUSTER_TOKEN:集群Token
  • ETCD_INITIAL_CLUSTER_STATE:加入集群的当前状态,new是新集群,existing表示加入已有集群
4.2.4 systemd管理etcd集群
cat > /usr/lib/systemd/system/etcd.service << EOF
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=/opt/etcd/cfg/etcd.conf
ExecStart=/opt/etcd/bin/etcd \
--cert-file=/opt/etcd/ssl/server.pem \
--key-file=/opt/etcd/ssl/server-key.pem \
--peer-cert-file=/opt/etcd/ssl/server.pem \
--peer-key-file=/opt/etcd/ssl/server-key.pem \
--trusted-ca-file=/opt/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/opt/etcd/ssl/ca.pem \
--logger=zap
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
4.2.5 拷贝刚才生成的证书
$ cp ~/k8s/cfssl/etcd/*.pem /opt/etcd/ssl/
4.2.6 启动并设置开机自启
$ systemctl daemon-reload
$ systemctl start etcd
$ systemctl enable etcd
4.2.7 部署其他节点

拷贝以下文件到其他etcd节点:

$ scp -r /opt/etcd/ host2:/opt/
$ scp /usr/lib/systemd/system/etcd.service host2:/usr/lib/systemd/system/
$ scp -r /opt/etcd/ host3:/opt/
$ scp /usr/lib/systemd/system/etcd.service host3:/usr/lib/systemd/system/

修改配置文件:

vim /opt/etcd/cfg/etcd.conf
#[Member]
ETCD_NAME="etcd-2"  # 修改此处,节点2改为etcd-2,节点3改为etcd-3
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://current_host_ip:2380"  # 修改此处为当前服务器IP
ETCD_LISTEN_CLIENT_URLS="https://current_host_ip:2379"  # 修改此处为当前服务器IP
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://current_host_ip:2380"  # 修改此处为当前服务器IP
ETCD_ADVERTISE_CLIENT_URLS="https://current_host_ip:2379"  # 修改此处为当前服务器IP
ETCD_INITIAL_CLUSTER="etcd-1=https://172.16.180.128:2380,etcd-2=https://172.16.180.129:2380,etcd-3=https://172.16.180.130:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

启动个节点etcd服务,并加入开机自启!

4.2.8 查看集群状态
$ ETCDCTL_API=3 /opt/etcd/bin/etcdctl --cacert=/opt/etcd/ssl/ca.pem --cert=/opt/etcd/ssl/server.pem --key=/opt/etcd/ssl/server-key.pem --endpoints="https://172.16.180.128:2379,https://172.16.180.129:2379,https://172.16.180.130:2379" endpoint health
https://172.16.180.128:2379 is healthy: successfully committed proposal: took = 19.595348ms
https://172.16.180.130:2379 is healthy: successfully committed proposal: took = 20.361174ms
https://172.16.180.129:2379 is healthy: successfully committed proposal: took = 20.186378ms

部署成功!

5. 部署master组件——kube-api-server

5.0 自签API Server(CA)证书

$ cd /root/k8s/ssl/k8s
$ cat > ca-config.json << EOF
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "kubernetes": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}
EOF

$ cat > ca-csr.json << EOF
{
    "CN": "kubernetes",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Beijing",
            "ST": "Beijing",
            "O": "k8s",
            "OU": "System"
        }
    ]
}
EOF

生成证书:

$ cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
$ ls *pem
ca-key.pem  ca.pem

5.1 使用自签CA证书签发kube-apiverver HTTPS证书

创建证书申请文件:

cat > server-csr.json << EOF
{
    "CN": "kubernetes",
    "hosts": [
      "10.0.0.1",
      "127.0.0.1",
      "kubernetes",
      "kubernetes.default",
      "kubernetes.default.svc",
      "kubernetes.default.svc.cluster",
      "kubernetes.default.svc.cluster.local",
      "172.16.180.128",
      "172.16.180.129",
      "172.16.180.130",
      "172.16.180.131",
      "172.16.180.132",
      "172.16.180.133"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "BeiJing",
            "ST": "BeiJing",
            "O": "k8s",
            "OU": "System"
        }
    ]
}
EOF

<font color=red>注:上述文件hosts字段中IP为所有Master/LB/VIP IP,一个都不能少!为了方便后期扩容可以多写几个预留的IP。</font>

生成证书:

$ cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server

$ ls *pem
ca-key.pem  ca.pem  server-key.pem  server.pem

5.2 部署API Server

5.2.1 下载二进制文件

下载地址:https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.18.md#v1183

注: 打开链接你会发现里面有很多包,下载一个server包就够了,包含了Master和Worker Node二进制文件。

eg:https://dl.k8s.io/v1.18.3/kubernetes-server-linux-amd64.tar.gz

5.2.2 创建工作目录并解压二进制包:
$ mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs} 
$ tar zxf kubernetes-server-linux-amd64.tar.gz
$ cd kubernetes/server/bin
$ cp kube-apiserver kube-scheduler kube-controller-manager /opt/kubernetes/bin
$ cp kubectl /usr/bin/
5.2.3 创建API Server配置文件:
cat > /opt/kubernetes/cfg/kube-apiserver.conf << EOF
KUBE_APISERVER_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--etcd-servers=https://172.16.180.128:2379,https://172.16.180.129:2379,https://172.16.180.130:2379 \\
--bind-address=172.16.180.128 \\
--secure-port=6443 \\
--advertise-address=172.16.180.128 \\
--allow-privileged=true \\
--service-cluster-ip-range=10.0.0.0/24 \\
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \\
--authorization-mode=RBAC,Node \\
--enable-bootstrap-token-auth=true \\
--token-auth-file=/opt/kubernetes/cfg/token.csv \\
--service-node-port-range=30000-32767 \\
--kubelet-client-certificate=/opt/kubernetes/ssl/server.pem \\
--kubelet-client-key=/opt/kubernetes/ssl/server-key.pem \\
--tls-cert-file=/opt/kubernetes/ssl/server.pem  \\
--tls-private-key-file=/opt/kubernetes/ssl/server-key.pem \\
--client-ca-file=/opt/kubernetes/ssl/ca.pem \\
--service-account-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--etcd-cafile=/opt/etcd/ssl/ca.pem \\
--etcd-certfile=/opt/etcd/ssl/server.pem \\
--etcd-keyfile=/opt/etcd/ssl/server-key.pem \\
--audit-log-maxage=30 \\
--audit-log-maxbackup=3 \\
--audit-log-maxsize=100 \\
--audit-log-path=/opt/kubernetes/logs/k8s-audit.log"
EOF

注:上面两个\ \ 第一个是转义符,第二个是换行符,使用转义符是为了使用EOF保留换行符。

  • --logtostderr:启用日志
  • --v:日志等级
  • --log-dir:日志目录
  • --etcd-servers:etcd集群地址
  • --bind-address:监听地址
  • --secure-port:https安全端口
  • --advertise-address:集群通告地址
  • --allow-privileged:启用授权
  • --service-cluster-ip-range:Service虚拟IP地址段
  • --enable-admission-plugins:准入控制模块
  • --authorization-mode:认证授权,启用RBAC授权和节点自管理
  • --enable-bootstrap-token-auth:启用TLS bootstrap机制
  • --token-auth-file:bootstrap token文件
  • --service-node-port-range:Service nodeport类型默认分配端口范围
  • --kubelet-client-xxx:apiserver访问kubelet客户端证书
  • --tls-xxx-file:apiserver https证书
  • --etcd-xxxfile:连接Etcd集群证书
  • --audit-log-xxx:审计日志
5.2.4 拷贝刚才生成的证书
$ cp ~/k8s/ssl/k8s/*pem /opt/kubernetes/ssl/
5.2.5 启用 TLS Bootstrapping 机制

TLS Bootstraping:Master apiserver启用TLS认证后,Node节点kubelet和kube-proxy要与kube-apiserver进行通信,必须使用CA签发的有效证书才可以,当Node节点很多时,这种客户端证书颁发需要大量工作,同样也会增加集群扩展复杂度。为了简化流程,Kubernetes引入了TLS bootstraping机制来自动颁发客户端证书,kubelet会以一个低权限用户自动向apiserver申请证书,kubelet的证书由apiserver动态签署。所以强烈建议在Node上使用这种方式,目前主要用于kubelet,kube-proxy还是由我们统一颁发一个证书。

TLS bootstraping 工作流程:

创建上述配置文件中token文件:

# 格式:token,用户名,UID,用户组
$ cat > /opt/kubernetes/cfg/token.csv << EOF
2a78fdd6b2cdf434b5ae718b9f6a3691,kubelet-bootstrap,10001,"system:node-bootstrapper"
EOF

token也可自行生成替换:

$ head -c 16 /dev/urandom | od -An -t x | tr -d ' '
5.2.6 systemd管理apiserver
$ cat > /usr/lib/systemd/system/kube-apiserver.service << EOF
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-apiserver.conf
ExecStart=/opt/kubernetes/bin/kube-apiserver \$KUBE_APISERVER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
5.2.7 启动并加入开机自启
$ systemctl daemon-reload
$ systemctl enable --now kube-apiserver
5.2.8 授权kubelet-bootstrap用户允许请求证书
$ kubectl create clusterrolebinding kubelet-bootstrap \
--clusterrole=system:node-bootstrapper \
--user=kubelet-bootstrap

6. 部署master组件——kube-controller-manager

6.1 创建配置文件

$ cat > /opt/kubernetes/cfg/kube-controller-manager.conf << EOF
KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--leader-elect=true \\
--master=127.0.0.1:8080 \\
--bind-address=127.0.0.1 \\
--allocate-node-cidrs=true \\
--cluster-cidr=10.244.0.0/16 \\
--service-cluster-ip-range=10.0.0.0/24 \\
--cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \\
--cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem  \\
--root-ca-file=/opt/kubernetes/ssl/ca.pem \\
--service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--experimental-cluster-signing-duration=87600h0m0s"
EOF
  • --master:通过本地非安全本地端口8080连接apiserver
  • --leader-elect:当该组件启动多个时,自动选举(HA)
  • --cluster-signing-cert-file/–cluster-signing-key-file:自动为kubelet颁发证书的CA,与apiserver保持一致

6.2 systemd管理Controller-manager

$ cat > /usr/lib/systemd/system/kube-controller-manager.service << EOF
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-controller-manager.conf
ExecStart=/opt/kubernetes/bin/kube-controller-manager \$KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

6.3 启动并设置开机自启

$ systemctl daemon-reload

$ systemctl enable --now kube-controller-manager

7. 部署master组件——kube-scheduler

7.1 创建配置文件

$ cat > /opt/kubernetes/cfg/kube-scheduler.conf << EOF
KUBE_SCHEDULER_OPTS="--logtostderr=false \
--v=2 \
--log-dir=/opt/kubernetes/logs \
--leader-elect \
--master=127.0.0.1:8080 \
--bind-address=127.0.0.1"
EOF

7.2 systemd管理Scheduler

$ cat > /usr/lib/systemd/system/kube-scheduler.service << EOF
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-scheduler.conf
ExecStart=/opt/kubernetes/bin/kube-scheduler \$KUBE_SCHEDULER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

7.3 启动并设置开机自启

$ systemctl daemon-reload
$ systemctl enable --now kube-scheduler

7.4 查看集群状态

master节点所有组件都已经启动成功,通过kubectl工具查看当前集群组件状态:

$ kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok                  
scheduler            Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   
etcd-1               Healthy   {"health":"true"}   
etcd-2               Healthy   {"health":"true"}

如上输出说明Master节点组件运行正常。

8. 部署Worker node组件——准备

  • kubelet
  • kube-proxy
  • docker
  • etcd

下面还是在Master Node上操作,即同时作为Worker Node。

8.1 创建工作目录并拷贝二进制文件

在所有worker node创建工作目录:

$ mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}

从master节点拷贝:

$ cd ~/k8s/api/kubernetes/server/bin
$ cp kubelet kube-proxy /opt/kubernetes/bin   # 本地拷贝

9. 部署Worker组件——kubelet

9.1 创建配置文件

$ cat > /opt/kubernetes/cfg/kubelet.conf << EOF
KUBELET_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--hostname-override=k8s-master1 \\
--network-plugin=cni \\
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \\
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \\
--config=/opt/kubernetes/cfg/kubelet-config.yml \\
--cert-dir=/opt/kubernetes/ssl \\
--pod-infra-container-image=hub.adaixuezhang.cn/public/pause-amd64:3.0" # 需要提前将镜像k8s.gcr.io/pause-amd64:3.0存到本地仓库
EOF
  • -–hostname-override:显示名称,集群中唯一
  • –-network-plugin:启用CNI
  • –-kubeconfig:空路径,会自动生成,后面用于连接apiserver
  • –-bootstrap-kubeconfig:首次启动向apiserver申请证书
  • –-config:配置参数文件
  • –-cert-dir:kubelet证书生成目录
  • –-pod-infra-container-image:管理Pod网络容器的镜像

9.2 配置参数文件

$ cat > /opt/kubernetes/cfg/kubelet-config.yml << EOF
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 0.0.0.0
port: 10250
readOnlyPort: 10255
cgroupDriver: systemd
clusterDNS:
- 10.0.0.2
clusterDomain: cluster.local 
failSwapOn: false
authentication:
  anonymous:
    enabled: false
  webhook:
    cacheTTL: 2m0s
    enabled: true
  x509:
    clientCAFile: /opt/kubernetes/ssl/ca.pem 
authorization:
  mode: Webhook
  webhook:
    cacheAuthorizedTTL: 5m0s
    cacheUnauthorizedTTL: 30s
evictionHard:
  imagefs.available: 15%
  memory.available: 100Mi
  nodefs.available: 10%
  nodefs.inodesFree: 5%
maxOpenFiles: 1000000
maxPods: 110
EOF

9.3 生成bootstrap.kubeconfig文件

# 配置环境变量
$ vim /etc/profile
KUBE_APISERVER="https://172.16.180.128:6443" # apiserver IP:PORT
TOKEN="2a78fdd6b2cdf434b5ae718b9f6a3691" # 与token.csv里保持一致

$ source /etc/profile

# 生成 kubelet bootstrap kubeconfig 配置文件
$ kubectl config set-cluster kubernetes \
  --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=bootstrap.kubeconfig

$ kubectl config set-credentials "kubelet-bootstrap" \
  --token=${TOKEN} \
  --kubeconfig=bootstrap.kubeconfig

$ kubectl config set-context default \
  --cluster=kubernetes \
  --user="kubelet-bootstrap" \
  --kubeconfig=bootstrap.kubeconfig

$ kubectl config use-context default --kubeconfig=bootstrap.kubeconfig

9.4 systemd管理kubelet

$ cat > /usr/lib/systemd/system/kubelet.service << EOF
[Unit]
Description=Kubernetes Kubelet
After=docker.service
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet.conf
ExecStart=/opt/kubernetes/bin/kubelet \$KUBELET_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF

9.5 启动并设置开机自启

$ systemctl daemon-reload
$ systemctl enable --now kubelet

9.6 批准kubelet证书申请加入集群

# 查看kubelet证书请求
$ kubectl get csr
NAME                                                   AGE     SIGNERNAME                                    REQUESTOR           CONDITION
node-csr-8Kl_J2bUafBYlzjkgVjn-ucxFzp4zibf8f2fZjiJIlA   2m13s   kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Pending

# 批准申请(approve后跟上一步生成的node-*)
$ kubectl certificate approve node-csr-8Kl_J2bUafBYlzjkgVjn-ucxFzp4zibf8f2fZjiJIlA

# 查看节点
$ kubectl get nodes
NAME          STATUS     ROLES    AGE   VERSION
k8s-master1   NotReady   <none>   10m   v1.18.3

注:由于网络插件还没有部署,节点会没有准备就绪 NotReady。

9.7 异常处理

kubelet-config.yml 默认 cgroupDriver: cgroups ,启动kubelet时会报错:

failed to run Kubelet: misconfiguration: kubelet cgroup driver: "systemd\u00a0--runtime-cgroups=/systemd/system.slice" is different from docker cgroup driver: "systemd"
  • 错误原因:kubelet文件驱动默认cgroupfs,而docker使用的文件驱动是systemd(经验证:docker 19.03.12不支持使用cgroupfs),造成不一致, 导致镜像无法启动。

  • 解决办法:官方文档 ,将kubelet和docker的驱动统一使用 systemd ,即修改 kubelet-config.yml cgroupDriver: systemd

10. 部署worker组件——kube-proxy

10.1 创建配置文件

$ cat > /opt/kubernetes/cfg/kube-proxy.conf << EOF
KUBE_PROXY_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--config=/opt/kubernetes/cfg/kube-proxy-config.yml"
EOF

10.2 配置参数文件

$ cat > /opt/kubernetes/cfg/kube-proxy-config.yml << EOF
kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
metricsBindAddress: 0.0.0.0:10249
clientConnection:
  kubeconfig: /opt/kubernetes/cfg/kube-proxy.kubeconfig
hostnameOverride: k8s-master1
clusterCIDR: 10.0.0.0/24
EOF

10.3 生成kube-proxy.kubeconfig文件

生成kube-proxy证书:

$ # 切换工作目录
cd ~/k8s/ssl/k8s

# 创建证书请求文件
$ cat > kube-proxy-csr.json << EOF
{
  "CN": "system:kube-proxy",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "L": "BeiJing",
      "ST": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ]
}
EOF

# 生成证书
$ cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy

$ ls kube-proxy*pem
kube-proxy-key.pem  kube-proxy.pem

生成kubeconfig文件:

$ kubectl config set-cluster kubernetes \
  --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=kube-proxy.kubeconfig

$ kubectl config set-credentials kube-proxy \
  --client-certificate=./kube-proxy.pem \
  --client-key=./kube-proxy-key.pem \
  --embed-certs=true \
  --kubeconfig=kube-proxy.kubeconfig

$ kubectl config set-context default \
  --cluster=kubernetes \
  --user=kube-proxy \
  --kubeconfig=kube-proxy.kubeconfig

$ kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig

拷贝到配置文件指定路径:

$ cp kube-proxy.kubeconfig /opt/kubernetes/cfg/

10.4 systemd管理kube-proxy

cat > /usr/lib/systemd/system/kube-proxy.service << EOF
[Unit]
Description=Kubernetes Proxy
After=network.target
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-proxy.conf
ExecStart=/opt/kubernetes/bin/kube-proxy \$KUBE_PROXY_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF

10.5 启动并设置开机自启

$ systemctl daemon-reload
$ systemctl enable --now kube-proxy

11. 部署CNI网络

下载CNI二进制文件:

https://github.com/containernetworking/plugins/releases/download/v0.8.6/cni-plugins-linux-amd64-v0.8.6.tgz

解压至工作目录:

$ mkdir -p /opt/cni/bin
$ tar zxvf cni-plugins-linux-amd64-v0.8.6.tgz -C /opt/cni/bin/

部署CNI网络:

wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

# 将镜像地址换为本地镜像仓库,需要手动下载镜像到本地仓库
sed -i -r "s#quay.io/coreos/#hub.adaixuezhang.cn/public/#g" kube-flannel.yml
#eg: image: hub.adaixuezhang.cn/public/flannel:v0.13.0

# 部署
$ kubectl apply -f kube-flannel.yml

# 查看k8s集群状态
$ kubectl get pods --all-namespaces
NAMESPACE     NAME                    READY   STATUS    RESTARTS   AGE
kube-system   kube-flannel-ds-nd8nz   1/1     Running   0          20s

$ kubectl get nodes
NAME          STATUS   ROLES    AGE   VERSION
k8s-master1   Ready    <none>   10h   v1.18.3

部署好网络插件,Node准备就绪。

12. 授权api-server访问kubelet

$ cat > apiserver-to-kubelet-rbac.yaml << EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  annotations:
    rbac.authorization.kubernetes.io/autoupdate: "true"
  labels:
    kubernetes.io/bootstrapping: rbac-defaults
  name: system:kube-apiserver-to-kubelet
rules:
  - apiGroups:
      - ""
    resources:
      - nodes/proxy
      - nodes/stats
      - nodes/log
      - nodes/spec
      - nodes/metrics
      - pods/log
    verbs:
      - "*"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: system:kube-apiserver
  namespace: ""
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:kube-apiserver-to-kubelet
subjects:
  - apiGroup: rbac.authorization.k8s.io
    kind: User
    name: kubernetes
EOF

$ kubectl apply -f apiserver-to-kubelet-rbac.yaml

13. 新增Worker Node

13.1 拷贝Node相关文件

拷贝已部署好的Node相关文件到新节点(在k8s-master1执行):

# k8s-node01
$ scp -r /opt/kubernetes root@host2:/opt/
$ scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@host2:/usr/lib/systemd/system
$ scp -r /opt/cni/ root@host2:/opt/
$ scp -r /opt/kubernetes/ssl/ca.pem root@host2:/opt/kubernetes/ssl
# k8s-node02
$ scp -r /opt/kubernetes root@host3:/opt/
$ scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@host3:/usr/lib/systemd/system
$ scp -r /opt/cni/ root@host3:/opt/
$ scp -r /opt/kubernetes/ssl/ca.pem root@host3:/opt/kubernetes/ssl
13.2 删除kubelet证书和kubeconfig文件(node节点)
$ rm -f /opt/kubernetes/cfg/kubelet.kubeconfig
$ rm -f /opt/kubernetes/ssl/kubelet*

注:这几个文件是证书申请审批后自动生成的,每个Node不同,必须删除重新生成。

13.3 修改node节点配置文件中主机名
$ vim /opt/kubernetes/cfg/kubelet.conf
--hostname-override={NodeName}

$ vim /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: {NodeName}
13.4 启动并加入开机自启
$ systemctl daemon-reload
$ systemctl enable --now kubelet
$ systemctl enable --now kube-proxy
13.5 在master上批准新Node kubelet证书申请
$ kubectl get csr
$ kubectl certificate approve Node_csr_Name
13.6 查看Node状态
$ kubectl get node
NAME         STATUS     ROLES    AGE   VERSION
k8s-master1   Ready      <none>   65m   v1.18.3
k8s-node01    Ready      <none>   12m   v1.18.3
k8s-node02    Ready      <none>   81s   v1.18.3

参考

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部