部署所需软件包

https://download.csdn.net/download/qq_42883074/13620940

部署master节点

#软件包地址
https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/

#我们这里下载1.18.3
https://dl.k8s.io/v1.18.3/kubernetes-server-linux-amd64.tar.gz
#打开链接你会发现里面有很多包,下载一个 server 包就够了,包含了 Master 和 Node 二进制文件。

解压软件包

#准备目录
cd
mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}

#解压软件包
tar zxvf kubernetes-server-linux-amd64.tar.gz


#拷贝master节点所需组件的二进制文件
cd kubernetes/server/bin
cp kube-apiserver kube-scheduler kube-controller-manager /opt/kubernetes/bin

#拷贝kubectl管理工具
cp kubectl /usr/bin/


#拷贝证书文件
cp ~/TLS/k8s/*.pem /opt/kubernetes/ssl/

一. 部署 kube-apiserver

1.创建配置文件

#注意ip地址,和etcd地址
cat > /opt/kubernetes/cfg/kube-apiserver.conf << EOF
KUBE_APISERVER_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--etcd-servers=https://192.168.1.20:2379,https://192.168.1.21:2379,https://192.168.1.22:2379 \\
--bind-address=0.0.0.0 \\
--secure-port=6443 \\
--advertise-address=192.168.1.20 \\
--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 安全端口 (默认6443)
--advertise-address   #集群通告地址
--allow-privileged  #启用授权,运行docker使用特权模式
--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     #审计日志

2. 创建token文件

cat > /opt/kubernetes/cfg/token.csv << EOF
c47ffb939f5ca36231d9e3121a252940,kubelet-bootstrap,10001,"system:nodebootstrapper"
EOF

#格式:token,用户名,UID,用户组
#token 也可自行生成替换:
#head -c 16 /dev/urandom | od -An -t x | tr -d ' '

3. 添加api服务到systemd

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. 启动api服务

systemctl daemon-reload
systemctl start kube-apiserver
systemctl enable kube-apiserver

6. 查看服务状态

● kube-apiserver.service - Kubernetes API Server
   Loaded: loaded (/usr/lib/systemd/system/kube-apiserver.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2020-12-10 10:55:21 CST; 18s ago
     Docs: https://github.com/kubernetes/kubernetes
 Main PID: 80493 (kube-apiserver)
   CGroup: /system.slice/kube-apiserver.service
           └─80493 /opt/kubernetes/bin/kube-apiserver --logtostderr=false --v=2 --log-dir=/opt/kubern...

12月 10 10:55:21 k8s-master01 systemd[1]: Started Kubernetes API Server.
12月 10 10:55:25 k8s-master01 kube-apiserver[80493]: E1210 10:55:25.753744   80493 controller.go:1...g:
Hint: Some lines were ellipsized, use -l to show in full.

6. 授权 kubelet-bootstrap 用户允许请求证书

kubectl create clusterrolebinding kubelet-bootstrap \
--clusterrole=system:node-bootstrapper \
--user=kubelet-bootstrap

#返回
clusterrolebinding.rbac.authorization.k8s.io/kubelet-bootstrap created

 

二. 部署 kube-controller-manager

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  #ca证书文件
--cluster-signing-key-file   #ca证书私钥

 #自动为 kubelet 颁发证书的 CA,与 apiserver 保持一致

2. 添加systemd管理

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

3. 启动服务

systemctl daemon-reload
systemctl start kube-controller-manager
systemctl enable kube-controller-manager

4. 查看服务

systemctl status kube-controller-manager.service

返回

● kube-controller-manager.service - Kubernetes Controller Manager
   Loaded: loaded (/usr/lib/systemd/system/kube-controller-manager.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2020-12-10 11:05:31 CST; 3min 1s ago
     Docs: https://github.com/kubernetes/kubernetes
 Main PID: 80693 (kube-controller)
   CGroup: /system.slice/kube-controller-manager.service
           └─80693 /opt/kubernetes/bin/kube-controller-manager --logtostderr=false --v=2 --log-dir=/o...

12月 10 11:05:31 k8s-master01 systemd[1]: Started Kubernetes Controller Manager.
12月 10 11:05:33 k8s-master01 kube-controller-manager[80693]: E1210 11:05:33.173751   80693 core.g...ed
12月 10 11:05:43 k8s-master01 kube-controller-manager[80693]: E1210 11:05:43.192884   80693 core.g...il
12月 10 11:05:44 k8s-master01 kube-controller-manager[80693]: E1210 11:05:44.181517   80693 cluste...in
Hint: Some lines were ellipsized, use -l to show in full.

 

三. 部署 kube-scheduler

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

参数说明

--master        #通过本地非安全本地端口 8080 连接 apiserver。
--leader-elect  #当该组件启动多个时,自动选举(HA)

2. 添加服务文件

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

3. 启动服务

systemctl daemon-reload
systemctl start kube-scheduler
systemctl enable kube-scheduler

4. 查看服务

systemctl status kube-scheduler.service 

返回

● kube-scheduler.service - Kubernetes Scheduler
   Loaded: loaded (/usr/lib/systemd/system/kube-scheduler.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2020-12-10 11:08:16 CST; 2min 56s ago
     Docs: https://github.com/kubernetes/kubernetes
 Main PID: 80791 (kube-scheduler)
   CGroup: /system.slice/kube-scheduler.service
           └─80791 /opt/kubernetes/bin/kube-scheduler --logtostderr=false --v=2 --log-dir=/opt/kubernetes/logs --leader-elect --master=...

12月 10 11:08:16 k8s-master01 systemd[1]: Started Kubernetes Scheduler.
12月 10 11:08:16 k8s-master01 kube-scheduler[80791]: I1210 11:08:16.274857   80791 registry.go:150] Registering EvenPodsSpread p...nction
12月 10 11:08:16 k8s-master01 kube-scheduler[80791]: I1210 11:08:16.275233   80791 registry.go:150] Registering EvenPodsSpread p...nction
Hint: Some lines were ellipsized, use -l to show in full.

四. 查看集群状态

kubectl get cs

返回

#如下正常
[root@k8s-master01 bin]# kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-2               Healthy   {"health":"true"}   
etcd-1               Healthy   {"health":"true"}   
etcd-0               Healthy   {"health":"true"} 

 

Logo

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

更多推荐