LinuxK8s

二进制安装-k8s高可用集群08-部署kube-apiserve

2021-08-30  本文已影响0人  Chris0Yang

本文档讲解使用 keepalived 和 haproxy 部署一个 3 节点高可用 master 集群的步骤,对应的 LB VIP 为环境变量 ${MASTER_VIP}。
配置之前需要先安装 kubelet,flannel 等组件,不过前边已经安装,现在直接进入配置

1、创建 kubernetes 证书和私钥

source /opt/k8s/bin/environment.sh
cat > kubernetes-csr.json <<EOF
{
  "CN": "kubernetes",
  "hosts": [
    "127.0.0.1",
    "172.68.96.101",
    "172.68.96.102",
    "172.68.96.103",
    "${MASTER_VIP}",
    "${CLUSTER_KUBERNETES_SVC_IP}",
    "kubernetes",
    "kubernetes.default",
    "kubernetes.default.svc",
    "kubernetes.default.svc.cluster",
    "kubernetes.default.svc.cluster.local"
  ],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "4Paradigm"
    }
  ]
}
EOF
现在还不能进行这样的操作,继续往下配置
[root@k8s-master data]# kubectl get svc kubernetes
NAME         CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   10.254.0.1   <none>        443/TCP   1d

生成证书和私钥:

[root@k8s-master /data/cert]# 
cfssl gencert -ca=/etc/kubernetes/cert/ca.pem \
  -ca-key=/etc/kubernetes/cert/ca-key.pem \
  -config=/etc/kubernetes/cert/ca-config.json \
  -profile=kubernetes kubernetes-csr.json | cfssljson -bare kubernetes

[root@k8s-master cert]# ls kubernetes*pem

将生成的证书和私钥文件拷贝到 master 节点:

cat > /data/script/magic35_distribute_cert_All_service.sh << "EOF"
#!/bin/bash
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}" 
    ssh root@${node_ip} "mkdir -p /etc/kubernetes/cert/ && sudo chown -R root /etc/kubernetes/cert/"
    scp /data/cert/kubernetes*.pem root@${node_ip}:/etc/kubernetes/cert/
done
EOF

2、创建加密配置文件

source /opt/k8s/bin/environment.sh

cat > /data/template/encryption-config.yaml <<EOF
kind: EncryptionConfig
apiVersion: v1
resources:
  - resources:
      - secrets
    providers:
      - aescbc:
          keys:
            - name: key1
              secret: ${ENCRYPTION_KEY}
      - identity: {}
EOF

将加密配置文件拷贝到 master 和 node所有节点上 /etc/kubernetes 目录下:

cat > /data/script/magic36_copy_All_service.sh << "EOF"
#!/bin/bash
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}" 
    scp /data/template/encryption-config.yaml root@${node_ip}:/etc/kubernetes/
done
EOF

3、创建 kube-apiserver systemd unit 模板文件

cat > /data/template/kube-apiserver.service.template <<EOF
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=network.target
[Service]
ExecStart=/opt/k8s/bin/kube-apiserver \\
  --enable-admission-plugins=Initializers,NamespaceLifecycle,NodeRestriction,LimitRanger,ServiceAccount,DefaultStorageClass,ResourceQuota \\
  --anonymous-auth=false \\
  --experimental-encryption-provider-config=/etc/kubernetes/encryption-config.yaml \\
  --advertise-address=##NODE_IP## \\
  --bind-address=##NODE_IP## \\
  --insecure-port=0 \\
  --authorization-mode=Node,RBAC \\
  --runtime-config=api/all \\
  --enable-bootstrap-token-auth \\
  --service-cluster-ip-range=${SERVICE_CIDR} \\
  --service-node-port-range=${NODE_PORT_RANGE} \\
  --tls-cert-file=/etc/kubernetes/cert/kubernetes.pem \\
  --tls-private-key-file=/etc/kubernetes/cert/kubernetes-key.pem \\
  --client-ca-file=/etc/kubernetes/cert/ca.pem \\
  --kubelet-client-certificate=/etc/kubernetes/cert/kubernetes.pem \\
  --kubelet-client-key=/etc/kubernetes/cert/kubernetes-key.pem \\
  --service-account-key-file=/etc/kubernetes/cert/ca-key.pem \\
  --etcd-cafile=/etc/kubernetes/cert/ca.pem \\
  --etcd-certfile=/etc/kubernetes/cert/kubernetes.pem \\
  --etcd-keyfile=/etc/kubernetes/cert/kubernetes-key.pem \\
  --etcd-servers=${ETCD_ENDPOINTS} \\
  --enable-swagger-ui=true \\
  --allow-privileged=true \\
  --apiserver-count=3 \\
  --audit-log-maxage=30 \\
  --audit-log-maxbackup=3 \\
  --audit-log-maxsize=100 \\
  --audit-log-path=/var/log/kube-apiserver-audit.log \\
  --event-ttl=1h \\
  --alsologtostderr=true \\
  --logtostderr=false \\
  --log-dir=/var/log/kubernetes \\
  --v=2
Restart=on-failure
RestartSec=5
Type=notify
User=root
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF

4、为各节点创建和分发 kube-apiserver systemd unit 文件

替换模板文件中的变量,为各节点创建 systemd unit 文件:

cat > /data/script/magic37.sh << "EOF"
#!/bin/bash
# 替换模板文件中的变量,为各节点创建 systemd unit 文件
source /opt/k8s/bin/environment.sh
for (( i=0; i < 3; i++ ))
do
    sed -e "s/##NODE_NAME##/${NODE_NAMES[i]}/" -e "s/##NODE_IP##/${NODE_IPS[i]}/" /data/template/kube-apiserver.service.template > /data/template/kube-apiserver-${NODE_IPS[i]}.service 
done
EOF

分发生成的 systemd unit 文件:

cat > /data/script/magic38_distribute_systemd_unit.sh << "EOF"
#!/bin/bash
# 分发生成的 systemd unit 文件
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}" 
    ssh root@${node_ip} "mkdir -p /var/log/kubernetes && chown -R root /var/log/kubernetes"
    scp /data/template/kube-apiserver-${node_ip}.service root@${node_ip}:/etc/systemd/system/kube-apiserver.service
done
EOF

5、启动 kube-apiserver 服务

cat > /data/script/magic39_start_kubeApiserver.sh << "EOF"
#!/bin/bash
# 启动 kube-apiserver 服务
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}" 
    ssh root@${node_ip} "systemctl daemon-reload && systemctl enable kube-apiserver && systemctl start kube-apiserver"
done
EOF

检查 kube-apiserver 运行状态:

cat > /data/script/magic40_ckeck_kubeApiserver.sh << "EOF"
#!/bin/bash
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}" 
    ssh root@${node_ip} "systemctl status kube-apiserver |grep 'Active:'"
done
EOF

输出如下内容:

[root@k8s-master scriptable]# bash magic.sh
>>> 172.68.96.101
   Active: active (running) since Fri XXXXX  CST; Xh ago
>>> 172.68.96.102
   Active: active (running) since Fri XXXXX  CST; Xh ago
>>> 172.68.96.103
   Active: active (running) since Fri XXXXX  CST; Xh ago

则正常,如果没有,检查日志:

journalctl -xu kube-apiserver

6、打印 kube-apiserver 写入 etcd 的数据

[root@k8s-master data]# source /opt/k8s/bin/environment.sh
ETCDCTL_API=3 etcdctl \
    --endpoints=${ETCD_ENDPOINTS} \
    --cacert=/etc/kubernetes/cert/ca.pem \
    --cert=/etc/etcd/cert/etcd.pem \
    --key=/etc/etcd/cert/etcd-key.pem \
    get /registry/ --prefix --keys-only

7、检查集群信息

[root@k8s-master data]# kubectl cluster-info
Kubernetes master is running at https://172.68.96.110:8443
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'

[root@k8s-master data]# kubectl get all --all-namespaces
NAMESPACE     NAME                           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
default       service/kubernetes             ClusterIP   10.254.0.1       <none>        443/TCP          6h

[root@k8s-master data]# kubectl get componentstatuses
NAME                 STATUS      MESSAGE                                                                                                                                  ERROR
controller-manager   Unhealthy   Get http://127.0.0.1:10252/healthz: net/http: HTTP/1.x transport connection broken: malformed HTTP response "\x15\x03\x01\x00\x02\x02"
etcd-1               Healthy     {"health":"true"}
etcd-0               Healthy     {"health":"true"}
etcd-2               Healthy     {"health":"true"}
scheduler            Healthy     ok

注意:

1)如果执行 kubectl 命令式时输出如下错误信息,则说明使用的 ~/.kube/config 文件不对,请切换到正确的账户后再执行该命令: The connection to the server localhost:8080 was refused – did you specify the right host or port?

2)执行 kubectl get componentstatuses 命令时,apiserver 默认向 127.0.0.1 发送请求。当 controller-manager、scheduler 以集群模式运行时,有可能和 kube-apiserver 不在一台机器上,这时 controller-manager 或 scheduler 的状态为 Unhealthy,但实际上它们工作正常。

8、检查 kube-apiserver 监听的端口

[root@k8s-master data]# sudo netstat -lnpt|grep kube
tcp        0      0 172.68.96.101:6443      0.0.0.0:*               LISTEN      30018/kube-apiserve

9、授予 kubernetes 证书访问 kubelet API 的权限.

在执行 kubectl exec、run、logs 等命令时,apiserver 会转发到 kubelet。这里定义 RBAC 规则,授权 apiserver 调用 kubelet API

[root@k8s-master data]# 
kubectl create clusterrolebinding kube-apiserver:kubelet-apis --clusterrole=system:kubelet-api-admin --user kubernetes

10、参考资料

1)关于证书域名最后字符不能是 . 的问题,实际和 Go 的版本有关,1.9 不支持这种类型的证书:https://github.com/kubernetes/ingress-nginx/issues/2188

上一篇下一篇

猜你喜欢

热点阅读