Master节点组件介绍

  • kube-apiserver
    Kubernetes API,集群统一入口,各组件协调者,以RESTful API提供接口服务,所有对象的资源的增删改查和监听操作都交给APIServer处理后在提交给Etcd存储。
  • kube-controller-manager
    处理集群中常规的后台任务,一个资源对应一个控制器,而ControllerManager就是负责管理这些控制器的
  • kube-scheduler
    根据调度算法为新创建的Pod选择一个node节点,可以部署在同一个节点上,也可以部署在不同节点上。
  • etcd(已部署)
    分布式键值存储系统,用于保存群集状态数据,比如Pod、service等对象信息。

实验准备

  • 在实验之前,确保你的etcd flannel docker都得运行正常
  1. 在GitHub网上下载kubernetes-server-linux-amd64.tar.gz文件

  2. 解压软件包并拷贝文件

 tar -zxvf kubernetes-server-linux-amd64.tar.gz
 cd kubernetes/server/bin/
 mkdir -p /opt/kubernetes/{cfg,ssl,bin}
 cp kube-apiserver kube-controller-manager kube-scheduler kubectl /opt/kubernetes/bin/

实验步骤

生成证书

  1. 生成CA证书
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 -

  1. 生成apiserver证书
cat > server-csr.json <<EOF
{
    "CN": "kubernetes",
    "hosts": [
      "10.0.0.1",
      "127.0.0.1",
      "192.168.18.10",
      "192.168.18.40",  #可以添加LB节点ip,harbor等,除了node节点均可添加
      "192.168.18.50",
      "192.168.18.60",
      "192.168.18.70",
      "kubernetes",
      "kubernetes.default",
      "kubernetes.default.svc",
      "kubernetes.default.svc.cluster",
      "kubernetes.default.svc.cluster.local"
    ],
    "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 server-csr.json | cfssljson -bare server
  1. 生成admin证书(此步骤可省略)
cat > admin-csr.json <<EOF
{
  "CN": "admin",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "L": "BeiJing",
      "ST": "BeiJing",
      "O": "system:masters",
      "OU": "System"
    }
  ]
}
EOF

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin
  1. 生成kube-proxy证书
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
  1. 最终生成证书如下图所示:
    在这里插入图片描述
  2. 拷贝证书
cp ca.pem ca-key.pem server.pem server-key.pem /opt/kubernetes/ssl/

部署apiserver组件

  1. 生成apiserver配置文件
vim /opt/kubernetes/cfg/kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.18.10:2379,https://192.168.18.20:2379,https://192.168.18.30:2379 \
--bind-address=192.168.18.10 \
--secure-port=6443 \
--advertise-address=192.168.18.10 \
--allow-privileged=true \
--service-cluster-ip-range=10.0.0.0/24 \
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \
--authorization-mode=RBAC,Node \
--kubelet-https=true \
--enable-bootstrap-token-auth \
--token-auth-file=/opt/kubernetes/cfg/token.csv \
--service-node-port-range=30000-50000 \
--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"
  • 注我们可以修改日志路径:
mkdir -p /opt/kubernetes/log
vim /opt/kubernetes/cfg/kube-apiserver
#true改为false
KUBE_APISERVER_OPTS="--logtostderr=false \
#增加指定目录
--log-dir=/opt/kubernetes/logs
  1. 设置systemctl启动apiserver服务
vim /usr/lib/systemd/system/kube-apiserver.service
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-apiserver
ExecStart=/opt/kubernetes/bin/kube-apiserver $KUBE_APISERVER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target
  1. 生成token文件
BOOTSTRAP_TOKEN=0fb61c46f8991b718eb38d27b605b008
cat > token.csv <<EOF
${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap"
EOF
mv token.csv /opt/kubernetes/cfg/
  • 我们还可以通过命令自动生成
BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | od -An -t x | tr -d ' ')

在这里插入图片描述

  1. 启动kube-apiserver
systemctl daemon-reload
systemctl enable kube-apiserver
systemctl start kube-apiserver

在这里插入图片描述

部署controller-manager组件

  1. 生成controller-manager配置文件
vim /opt/kubernetes/cfg/kube-controller-manager
KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=true \
--v=4 \
--master=127.0.0.1:8080 \
--leader-elect=true \
--address=127.0.0.1 \
--service-cluster-ip-range=10.0.0.0/24 \
--cluster-name=kubernetes \
--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"
  1. 设置systemctl启动controller-manager服务
vim /usr/lib/systemd/system/kube-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-controller-manager
ExecStart=/opt/kubernetes/bin/kube-controller-manager $KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target
  1. 启动controller-manager服务
 systemctl daemon-reload
 systemctl enable kube-controller-manager
 systemctl restart kube-controller-manager

在这里插入图片描述

部署scheduler组件

  1. 生成controller-manager配置文件
 vim /opt/kubernetes/cfg/kube-scheduler
 KUBE_SCHEDULER_OPTS="--logtostderr=true \
 --v=4 \
 --master=127.0.0.1:8080 \
 --leader-elect"
  1. 设置systemctl启动scheduler服务
vim /usr/lib/systemd/system/kube-scheduler.service
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-scheduler
ExecStart=/opt/kubernetes/bin/kube-scheduler $KUBE_SCHEDULER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target
  1. 启动scheduler服务
 systemctl daemon-reload
 systemctl enable kube-scheduler
 systemctl restart kube-scheduler

在这里插入图片描述

部署完成查看集群状态

  • 使用kubectl工具查看当前集群组件状态
mv /opt/kubernetes/bin/kubectl /usr/bin
kubectl get cs
  • 组件健康如下图所示:
    在这里插入图片描述

配置文件参数介绍

  • –logtostderr 启用日志
  • —v 日志等级
  • –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 token文件
  • –service-node-port-range Service Node类型默认分配端口范围
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐