一、前置知识点

1.1 生产环境可部署Kubernetes集群的两种方式

目前生产部署Kubernetes集群主要有两种方式:
•kubeadm
Kubeadm是一个K8s部署工具,提供kubeadm init和kubeadm join,用于快速部署Kubernetes集群。
•二进制包
从github下载发行版的二进制包,手动部署每个组件,组成Kubernetes集群。
这里采用kubeadm搭建集群。
kubeadm工具功能:
•kubeadm init:初始化一个Master节点
•kubeadm join:将工作节点加入集群
•kubeadm upgrade:升级K8s版本
•kubeadm token:管理 kubeadm join 使用的令牌
•kubeadm reset:清空 kubeadm init 或者 kubeadm join 对主机所做的任何更改
•kubeadm version:打印 kubeadm 版本
•kubeadm alpha:预览可用的新功能

1.2 准备环境

服务器要求:
•建议最小硬件配置:2核CPU、2G内存、30G硬盘
•服务器最好可以访问外网,会有从网上拉取镜像需求,如果服务器不能上网,需要提前下载对应镜像并导入节点
软件环境:

软件 版本
操作系统 CentOS7.8_x64 (mini)
Docker 19-ce
Kubernetes 1.20

服务器整体规划:

角色 IP 其他单装组件
k8s-master1 192.168.31.61 docker,etcd,nginx,keepalived
k8s-master2 192.168.31.62 docker,etcd,nginx,keepalived
k8s-node1 192.168.31.63 docker,etcd
负载均衡器对外IP 192.168.31.88 (VIP)

架构图:
image.png
单Master服务器规划:

角色 IP 组件
k8s-master 192.168.31.71 kube-apiserver,kube-controller-manager,kube-scheduler,etcd
k8s-node1 192.168.31.72 kubelet,kube-proxy,docker,etcd
k8s-node2 192.168.31.73 kubelet,kube-proxy,docker,etcd

1.3 操作系统初始化配置

  1. # 关闭防火墙
  2. systemctl stop firewalld
  3. systemctl disable firewalld
  4. # 关闭selinux
  5. sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
  6. setenforce 0 # 临时
  7. # 关闭swap
  8. swapoff -a # 临时
  9. sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
  10. # 根据规划设置主机名
  11. hostnamectl set-hostname <hostname>
  12. # 在master添加hosts
  13. cat >> /etc/hosts << EOF
  14. 192.168.31.71 k8s-master1
  15. 192.168.31.72 k8s-node1
  16. 192.168.31.73 k8s-node2
  17. EOF
  18. # 将桥接的IPv4流量传递到iptables的链
  19. cat > /etc/sysctl.d/k8s.conf << EOF
  20. net.bridge.bridge-nf-call-ip6tables = 1
  21. net.bridge.bridge-nf-call-iptables = 1
  22. EOF
  23. sysctl --system # 生效
  24. # 时间同步
  25. yum install ntpdate -y
  26. ntpdate time.windows.com

二、部署Etcd集群

Etcd 是一个分布式键值存储系统,Kubernetes使用Etcd进行数据存储,所以先准备一个Etcd数据库,为解决Etcd单点故障,应采用集群方式部署,这里使用3台组建集群,可容忍1台机器故障,当然,你也可以使用5台组建集群,可容忍2台机器故障。

节点名称 IP
etcd-1 192.168.31.71
etcd-2 192.168.31.72
etcd-3 192.168.31.73

注:为了节省机器,这里与K8s节点机器复用。也可以独立于k8s集群之外部署,只要apiserver能连接到就行。

2.1 准备cfssl证书生成工具

cfssl是一个开源的证书管理工具,使用json文件生成证书,相比openssl更方便使用。
找任意一台服务器操作,这里用Master节点。

  1. wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
  2. wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
  3. wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
  4. chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
  5. mv cfssl_linux-amd64 /usr/local/bin/cfssl
  6. mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
  7. mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo

2.2 生成Etcd证书

1. 自签证书颁发机构(CA)

创建工作目录:

  1. mkdir -p ~/TLS/{etcd,k8s}
  2. cd ~/TLS/etcd

自签CA:

  1. cat > ca-config.json << EOF
  2. {
  3. "signing": {
  4. "default": {
  5. "expiry": "87600h"
  6. },
  7. "profiles": {
  8. "www": {
  9. "expiry": "87600h",
  10. "usages": [
  11. "signing",
  12. "key encipherment",
  13. "server auth",
  14. "client auth"
  15. ]
  16. }
  17. }
  18. }
  19. }
  20. EOF
  21. cat > ca-csr.json << EOF
  22. {
  23. "CN": "etcd CA",
  24. "key": {
  25. "algo": "rsa",
  26. "size": 2048
  27. },
  28. "names": [
  29. {
  30. "C": "CN",
  31. "L": "Beijing",
  32. "ST": "Beijing"
  33. }
  34. ]
  35. }
  36. EOF

生成证书

  1. cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

会生成ca.pem和ca-key.pem文件。

2. 使用自签CA签发Etcd HTTPS证书

创建证书申请文件:

  1. cat > server-csr.json << EOF
  2. {
  3. "CN": "etcd",
  4. "hosts": [
  5. "192.168.31.71",
  6. "192.168.31.72",
  7. "192.168.31.73"
  8. ],
  9. "key": {
  10. "algo": "rsa",
  11. "size": 2048
  12. },
  13. "names": [
  14. {
  15. "C": "CN",
  16. "L": "BeiJing",
  17. "ST": "BeiJing"
  18. }
  19. ]
  20. }
  21. EOF

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

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

会生成server.pem和server-key.pem文件。

2.3 从Github下载二进制文件

下载地址:https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz

2.4 部署Etcd集群

以下在节点1上操作,为简化操作,待会将节点1生成的所有文件拷贝到节点2和节点3.

1. 创建工作目录并解压二进制包

  1. mkdir /opt/etcd/{bin,cfg,ssl} -p
  2. tar zxvf etcd-v3.4.9-linux-amd64.tar.gz
  3. mv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/

2. 创建etcd配置文件

  1. cat > /opt/etcd/cfg/etcd.conf << EOF
  2. #[Member]
  3. ETCD_NAME="etcd-1"
  4. ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
  5. ETCD_LISTEN_PEER_URLS="https://192.168.31.71:2380"
  6. ETCD_LISTEN_CLIENT_URLS="https://192.168.31.71:2379"
  7. #[Clustering]
  8. ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.31.71:2380"
  9. ETCD_ADVERTISE_CLIENT_URLS="https://192.168.31.71:2379"
  10. ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.31.71:2380,etcd-2=https://192.168.31.72:2380,etcd-3=https://192.168.31.73:2380"
  11. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  12. ETCD_INITIAL_CLUSTER_STATE="new"
  13. EOF

•ETCD_NAME:节点名称,集群中唯一
•ETCD_DATA_DIR:数据目录
•ETCD_LISTEN_PEER_URLS:集群通信监听地址
•ETCD_LISTEN_CLIENT_URLS:客户端访问监听地址
•ETCD_INITIAL_ADVERTISE_PEERURLS:集群通告地址
•ETCD_ADVERTISE_CLIENT_URLS:客户端通告地址
•ETCD_INITIAL_CLUSTER:集群节点地址
•ETCD_INITIALCLUSTER_TOKEN:集群Token
•ETCD_INITIALCLUSTER_STATE:加入集群的当前状态,new是新集群,existing表示加入已有集群

3. systemd管理etcd

  1. cat > /usr/lib/systemd/system/etcd.service << EOF
  2. [Unit]
  3. Description=Etcd Server
  4. After=network.target
  5. After=network-online.target
  6. Wants=network-online.target
  7. [Service]
  8. Type=notify
  9. EnvironmentFile=/opt/etcd/cfg/etcd.conf
  10. ExecStart=/opt/etcd/bin/etcd \
  11. --cert-file=/opt/etcd/ssl/server.pem \
  12. --key-file=/opt/etcd/ssl/server-key.pem \
  13. --peer-cert-file=/opt/etcd/ssl/server.pem \
  14. --peer-key-file=/opt/etcd/ssl/server-key.pem \
  15. --trusted-ca-file=/opt/etcd/ssl/ca.pem \
  16. --peer-trusted-ca-file=/opt/etcd/ssl/ca.pem \
  17. --logger=zap
  18. Restart=on-failure
  19. LimitNOFILE=65536
  20. [Install]
  21. WantedBy=multi-user.target
  22. EOF

4. 拷贝刚才生成的证书

把刚才生成的证书拷贝到配置文件中的路径

  1. cp ~/TLS/etcd/ca*pem ~/TLS/etcd/server*pem /opt/etcd/ssl/

5. 启动并设置开机启动

  1. systemctl daemon-reload
  2. systemctl start etcd
  3. systemctl enable etcd

6. 将上面节点1所有生成的文件拷贝到节点2和节点3

  1. scp -r /opt/etcd/ root@192.168.31.72:/opt/
  2. scp /usr/lib/systemd/system/etcd.service root@192.168.31.72:/usr/lib/systemd/system/
  3. scp -r /opt/etcd/ root@192.168.31.73:/opt/
  4. scp /usr/lib/systemd/system/etcd.service root@192.168.31.73:/usr/lib/systemd/system/

然后在节点2和节点3分别修改etcd.conf配置文件中的节点名称和当前服务器IP:

  1. vi /opt/etcd/cfg/etcd.conf
  2. #[Member]
  3. ETCD_NAME="etcd-1" # 修改此处,节点2改为etcd-2,节点3改为etcd-3
  4. ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
  5. ETCD_LISTEN_PEER_URLS="https://192.168.31.71:2380" # 修改此处为当前服务器IP
  6. ETCD_LISTEN_CLIENT_URLS="https://192.168.31.71:2379" # 修改此处为当前服务器IP
  7. #[Clustering]
  8. ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.31.71:2380" # 修改此处为当前服务器IP
  9. ETCD_ADVERTISE_CLIENT_URLS="https://192.168.31.71:2379" # 修改此处为当前服务器IP
  10. ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.31.71:2380,etcd-2=https://192.168.31.72:2380,etcd-3=https://192.168.31.73:2380"
  11. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  12. ETCD_INITIAL_CLUSTER_STATE="new"

最后启动etcd并设置开机启动,同上。

7. 查看集群状态

  1. 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://192.168.31.71:2379,https://192.168.31.72:2379,https://192.168.31.73:2379" endpoint health --write-out=table
  2. +----------------------------+--------+-------------+-------+
  3. | ENDPOINT | HEALTH | TOOK | ERROR |
  4. +----------------------------+--------+-------------+-------+
  5. | https://192.168.31.71:2379 | true | 10.301506ms | |
  6. | https://192.168.31.73:2379 | true | 12.87467ms | |
  7. | https://192.168.31.72:2379 | true | 13.225954ms | |
  8. +----------------------------+--------+-------------+-------+

如果输出上面信息,就说明集群部署成功。
如果有问题第一步先看日志:/var/log/message 或 journalctl -u etcd

三、安装Docker

这里使用Docker作为容器引擎,也可以换成别的,例如containerd
下载地址:https://download.docker.com/linux/static/stable/x86_64/docker-19.03.9.tgz
以下在所有节点操作。这里采用二进制安装,用yum安装也一样。

3.1 解压二进制包

  1. tar zxvf docker-19.03.9.tgz
  2. mv docker/* /usr/bin

3.2 systemd管理docker

  1. cat > /usr/lib/systemd/system/docker.service << EOF
  2. [Unit]
  3. Description=Docker Application Container Engine
  4. Documentation=https://docs.docker.com
  5. After=network-online.target firewalld.service
  6. Wants=network-online.target
  7. [Service]
  8. Type=notify
  9. ExecStart=/usr/bin/dockerd
  10. ExecReload=/bin/kill -s HUP $MAINPID
  11. LimitNOFILE=infinity
  12. LimitNPROC=infinity
  13. LimitCORE=infinity
  14. TimeoutStartSec=0
  15. Delegate=yes
  16. KillMode=process
  17. Restart=on-failure
  18. StartLimitBurst=3
  19. StartLimitInterval=60s
  20. [Install]
  21. WantedBy=multi-user.target
  22. EOF

3.3 创建配置文件

  1. mkdir /etc/docker
  2. cat > /etc/docker/daemon.json << EOF
  3. {
  4. "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
  5. }
  6. EOF

•registry-mirrors 阿里云镜像加速器

3.4 启动并设置开机启动

  1. systemctl daemon-reload
  2. systemctl start docker
  3. systemctl enable docker

四、部署Master Node

如果你在学习中遇到问题或者文档有误可联系阿良~ 微信: xyz12366699

4.1 生成kube-apiserver证书

1. 自签证书颁发机构(CA)

  1. cd ~/TLS/k8s
  2. cat > ca-config.json << EOF
  3. {
  4. "signing": {
  5. "default": {
  6. "expiry": "87600h"
  7. },
  8. "profiles": {
  9. "kubernetes": {
  10. "expiry": "87600h",
  11. "usages": [
  12. "signing",
  13. "key encipherment",
  14. "server auth",
  15. "client auth"
  16. ]
  17. }
  18. }
  19. }
  20. }
  21. EOF
  22. cat > ca-csr.json << EOF
  23. {
  24. "CN": "kubernetes",
  25. "key": {
  26. "algo": "rsa",
  27. "size": 2048
  28. },
  29. "names": [
  30. {
  31. "C": "CN",
  32. "L": "Beijing",
  33. "ST": "Beijing",
  34. "O": "k8s",
  35. "OU": "System"
  36. }
  37. ]
  38. }
  39. EOF

生成证书:

  1. cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

会生成ca.pem和ca-key.pem文件。

2. 使用自签CA签发kube-apiserver HTTPS证书

创建证书申请文件:

  1. cat > server-csr.json << EOF
  2. {
  3. "CN": "kubernetes",
  4. "hosts": [
  5. "10.0.0.1",
  6. "127.0.0.1",
  7. "192.168.31.71",
  8. "192.168.31.72",
  9. "192.168.31.73",
  10. "192.168.31.74",
  11. "192.168.31.88",
  12. "kubernetes",
  13. "kubernetes.default",
  14. "kubernetes.default.svc",
  15. "kubernetes.default.svc.cluster",
  16. "kubernetes.default.svc.cluster.local"
  17. ],
  18. "key": {
  19. "algo": "rsa",
  20. "size": 2048
  21. },
  22. "names": [
  23. {
  24. "C": "CN",
  25. "L": "BeiJing",
  26. "ST": "BeiJing",
  27. "O": "k8s",
  28. "OU": "System"
  29. }
  30. ]
  31. }
  32. EOF

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

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

会生成server.pem和server-key.pem文件。

4.2 从Github下载二进制文件

下载地址: https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.20.md
注:打开链接你会发现里面有很多包,下载一个server包就够了,包含了Master和Worker Node二进制文件。

4.3 解压二进制包

  1. mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}
  2. tar zxvf kubernetes-server-linux-amd64.tar.gz
  3. cd kubernetes/server/bin
  4. cp kube-apiserver kube-scheduler kube-controller-manager /opt/kubernetes/bin
  5. cp kubectl /usr/bin/

4.4 部署kube-apiserver

1. 创建配置文件

  1. cat > /opt/kubernetes/cfg/kube-apiserver.conf << EOF
  2. KUBE_APISERVER_OPTS="--logtostderr=false \\
  3. --v=2 \\
  4. --log-dir=/opt/kubernetes/logs \\
  5. --etcd-servers=https://192.168.31.71:2379,https://192.168.31.72:2379,https://192.168.31.73:2379 \\
  6. --bind-address=192.168.31.71 \\
  7. --secure-port=6443 \\
  8. --advertise-address=192.168.31.71 \\
  9. --allow-privileged=true \\
  10. --service-cluster-ip-range=10.0.0.0/24 \\
  11. --enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \\
  12. --authorization-mode=RBAC,Node \\
  13. --enable-bootstrap-token-auth=true \\
  14. --token-auth-file=/opt/kubernetes/cfg/token.csv \\
  15. --service-node-port-range=30000-32767 \\
  16. --kubelet-client-certificate=/opt/kubernetes/ssl/server.pem \\
  17. --kubelet-client-key=/opt/kubernetes/ssl/server-key.pem \\
  18. --tls-cert-file=/opt/kubernetes/ssl/server.pem \\
  19. --tls-private-key-file=/opt/kubernetes/ssl/server-key.pem \\
  20. --client-ca-file=/opt/kubernetes/ssl/ca.pem \\
  21. --service-account-key-file=/opt/kubernetes/ssl/ca-key.pem \\
  22. --service-account-issuer=api \\
  23. --service-account-signing-key-file=/opt/kubernetes/ssl/server-key.pem \\
  24. --etcd-cafile=/opt/etcd/ssl/ca.pem \\
  25. --etcd-certfile=/opt/etcd/ssl/server.pem \\
  26. --etcd-keyfile=/opt/etcd/ssl/server-key.pem \\
  27. --requestheader-client-ca-file=/opt/kubernetes/ssl/ca.pem \\
  28. --proxy-client-cert-file=/opt/kubernetes/ssl/server.pem \\
  29. --proxy-client-key-file=/opt/kubernetes/ssl/server-key.pem \\
  30. --requestheader-allowed-names=kubernetes \\
  31. --requestheader-extra-headers-prefix=X-Remote-Extra- \\
  32. --requestheader-group-headers=X-Remote-Group \\
  33. --requestheader-username-headers=X-Remote-User \\
  34. --enable-aggregator-routing=true \\
  35. --audit-log-maxage=30 \\
  36. --audit-log-maxbackup=3 \\
  37. --audit-log-maxsize=100 \\
  38. --audit-log-path=/opt/kubernetes/logs/k8s-audit.log"
  39. 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证书
•1.20版本必须加的参数:—service-account-issuer,—service-account-signing-key-file
•—etcd-xxxfile:连接Etcd集群证书
•—audit-log-xxx:审计日志
•启动聚合层相关配置:—requestheader-client-ca-file,—proxy-client-cert-file,—proxy-client-key-file,—requestheader-allowed-names,—requestheader-extra-headers-prefix,—requestheader-group-headers,—requestheader-username-headers,—enable-aggregator-routing

2. 拷贝刚才生成的证书

把刚才生成的证书拷贝到配置文件中的路径:

  1. cp ~/TLS/k8s/ca*pem ~/TLS/k8s/server*pem /opt/kubernetes/ssl/

3. 启用 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 工作流程:
image.png
创建上述配置文件中token文件:

  1. cat > /opt/kubernetes/cfg/token.csv << EOF
  2. c47ffb939f5ca36231d9e3121a252940,kubelet-bootstrap,10001,"system:node-bootstrapper"
  3. EOF

格式:token,用户名,UID,用户组
token也可自行生成替换:

  1. head -c 16 /dev/urandom | od -An -t x | tr -d ' '

4. systemd管理apiserver

  1. cat > /usr/lib/systemd/system/kube-apiserver.service << EOF
  2. [Unit]
  3. Description=Kubernetes API Server
  4. Documentation=https://github.com/kubernetes/kubernetes
  5. [Service]
  6. EnvironmentFile=/opt/kubernetes/cfg/kube-apiserver.conf
  7. ExecStart=/opt/kubernetes/bin/kube-apiserver \$KUBE_APISERVER_OPTS
  8. Restart=on-failure
  9. [Install]
  10. WantedBy=multi-user.target
  11. EOF

5. 启动并设置开机启动

  1. systemctl daemon-reload
  2. systemctl start kube-apiserver
  3. systemctl enable kube-apiserver

4.5 部署kube-controller-manager

1. 创建配置文件

  1. cat > /opt/kubernetes/cfg/kube-controller-manager.conf << EOF
  2. KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=false \\
  3. --v=2 \\
  4. --log-dir=/opt/kubernetes/logs \\
  5. --leader-elect=true \\
  6. --kubeconfig=/opt/kubernetes/cfg/kube-controller-manager.kubeconfig \\
  7. --bind-address=127.0.0.1 \\
  8. --allocate-node-cidrs=true \\
  9. --cluster-cidr=10.244.0.0/16 \\
  10. --service-cluster-ip-range=10.0.0.0/24 \\
  11. --cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \\
  12. --cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem \\
  13. --root-ca-file=/opt/kubernetes/ssl/ca.pem \\
  14. --service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem \\
  15. --cluster-signing-duration=87600h0m0s"
  16. EOF

•—kubeconfig:连接apiserver配置文件
•—leader-elect:当该组件启动多个时,自动选举(HA)
•—cluster-signing-cert-file/—cluster-signing-key-file:自动为kubelet颁发证书的CA,与apiserver保持一致

2. 生成kubeconfig文件

生成kube-controller-manager证书:

  1. # 切换工作目录
  2. cd ~/TLS/k8s
  3. # 创建证书请求文件
  4. cat > kube-controller-manager-csr.json << EOF
  5. {
  6. "CN": "system:kube-controller-manager",
  7. "hosts": [],
  8. "key": {
  9. "algo": "rsa",
  10. "size": 2048
  11. },
  12. "names": [
  13. {
  14. "C": "CN",
  15. "L": "BeiJing",
  16. "ST": "BeiJing",
  17. "O": "system:masters",
  18. "OU": "System"
  19. }
  20. ]
  21. }
  22. EOF
  23. # 生成证书
  24. cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-controller-manager-csr.json | cfssljson -bare kube-controller-manager

生成kubeconfig文件(以下是shell命令,直接在终端执行):

  1. KUBE_CONFIG="/opt/kubernetes/cfg/kube-controller-manager.kubeconfig"
  2. KUBE_APISERVER="https://192.168.31.71:6443"
  3. kubectl config set-cluster kubernetes \
  4. --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  5. --embed-certs=true \
  6. --server=${KUBE_APISERVER} \
  7. --kubeconfig=${KUBE_CONFIG}
  8. kubectl config set-credentials kube-controller-manager \
  9. --client-certificate=./kube-controller-manager.pem \
  10. --client-key=./kube-controller-manager-key.pem \
  11. --embed-certs=true \
  12. --kubeconfig=${KUBE_CONFIG}
  13. kubectl config set-context default \
  14. --cluster=kubernetes \
  15. --user=kube-controller-manager \
  16. --kubeconfig=${KUBE_CONFIG}
  17. kubectl config use-context default --kubeconfig=${KUBE_CONFIG}

3. systemd管理controller-manager

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

4. 启动并设置开机启动

  1. systemctl daemon-reload
  2. systemctl start kube-controller-manager
  3. systemctl enable kube-controller-manager

4.6 部署kube-scheduler

1. 创建配置文件

  1. cat > /opt/kubernetes/cfg/kube-scheduler.conf << EOF
  2. KUBE_SCHEDULER_OPTS="--logtostderr=false \\
  3. --v=2 \\
  4. --log-dir=/opt/kubernetes/logs \\
  5. --leader-elect \\
  6. --kubeconfig=/opt/kubernetes/cfg/kube-scheduler.kubeconfig \\
  7. --bind-address=127.0.0.1"
  8. EOF

•—kubeconfig:连接apiserver配置文件
•—leader-elect:当该组件启动多个时,自动选举(HA)

2. 生成kubeconfig文件

生成kube-scheduler证书:

  1. # 切换工作目录
  2. cd ~/TLS/k8s
  3. # 创建证书请求文件
  4. cat > kube-scheduler-csr.json << EOF
  5. {
  6. "CN": "system:kube-scheduler",
  7. "hosts": [],
  8. "key": {
  9. "algo": "rsa",
  10. "size": 2048
  11. },
  12. "names": [
  13. {
  14. "C": "CN",
  15. "L": "BeiJing",
  16. "ST": "BeiJing",
  17. "O": "system:masters",
  18. "OU": "System"
  19. }
  20. ]
  21. }
  22. EOF
  23. # 生成证书
  24. cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-scheduler-csr.json | cfssljson -bare kube-scheduler

生成kubeconfig文件(以下是shell命令,直接在终端执行):

  1. KUBE_CONFIG="/opt/kubernetes/cfg/kube-scheduler.kubeconfig"
  2. KUBE_APISERVER="https://192.168.31.71:6443"
  3. kubectl config set-cluster kubernetes \
  4. --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  5. --embed-certs=true \
  6. --server=${KUBE_APISERVER} \
  7. --kubeconfig=${KUBE_CONFIG}
  8. kubectl config set-credentials kube-scheduler \
  9. --client-certificate=./kube-scheduler.pem \
  10. --client-key=./kube-scheduler-key.pem \
  11. --embed-certs=true \
  12. --kubeconfig=${KUBE_CONFIG}
  13. kubectl config set-context default \
  14. --cluster=kubernetes \
  15. --user=kube-scheduler \
  16. --kubeconfig=${KUBE_CONFIG}
  17. kubectl config use-context default --kubeconfig=${KUBE_CONFIG}

3. systemd管理scheduler

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

4. 启动并设置开机启动

  1. systemctl daemon-reload
  2. systemctl start kube-scheduler
  3. systemctl enable kube-scheduler

5. 查看集群状态

生成kubectl连接集群的证书:

  1. cat > admin-csr.json <<EOF
  2. {
  3. "CN": "admin",
  4. "hosts": [],
  5. "key": {
  6. "algo": "rsa",
  7. "size": 2048
  8. },
  9. "names": [
  10. {
  11. "C": "CN",
  12. "L": "BeiJing",
  13. "ST": "BeiJing",
  14. "O": "system:masters",
  15. "OU": "System"
  16. }
  17. ]
  18. }
  19. EOF
  20. cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin

生成kubeconfig文件:

  1. mkdir /root/.kube
  2. KUBE_CONFIG="/root/.kube/config"
  3. KUBE_APISERVER="https://192.168.31.71:6443"
  4. kubectl config set-cluster kubernetes \
  5. --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  6. --embed-certs=true \
  7. --server=${KUBE_APISERVER} \
  8. --kubeconfig=${KUBE_CONFIG}
  9. kubectl config set-credentials cluster-admin \
  10. --client-certificate=./admin.pem \
  11. --client-key=./admin-key.pem \
  12. --embed-certs=true \
  13. --kubeconfig=${KUBE_CONFIG}
  14. kubectl config set-context default \
  15. --cluster=kubernetes \
  16. --user=cluster-admin \
  17. --kubeconfig=${KUBE_CONFIG}
  18. kubectl config use-context default --kubeconfig=${KUBE_CONFIG}

通过kubectl工具查看当前集群组件状态:

  1. kubectl get cs
  2. NAME STATUS MESSAGE ERROR
  3. scheduler Healthy ok
  4. controller-manager Healthy ok
  5. etcd-2 Healthy {"health":"true"}
  6. etcd-1 Healthy {"health":"true"}
  7. etcd-0 Healthy {"health":"true"}

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

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

  1. kubectl create clusterrolebinding kubelet-bootstrap \
  2. --clusterrole=system:node-bootstrapper \
  3. --user=kubelet-bootstrap

五、部署Worker Node

如果你在学习中遇到问题或者文档有误可联系阿良~ 微信: xyz12366699
下面还是在Master Node上操作,即同时作为Worker Node

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

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

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

从master节点拷贝:

  1. cd kubernetes/server/bin
  2. cp kubelet kube-proxy /opt/kubernetes/bin # 本地拷贝

5.2 部署kubelet

1. 创建配置文件

  1. cat > /opt/kubernetes/cfg/kubelet.conf << EOF
  2. KUBELET_OPTS="--logtostderr=false \\
  3. --v=2 \\
  4. --log-dir=/opt/kubernetes/logs \\
  5. --hostname-override=k8s-master1 \\
  6. --network-plugin=cni \\
  7. --kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \\
  8. --bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \\
  9. --config=/opt/kubernetes/cfg/kubelet-config.yml \\
  10. --cert-dir=/opt/kubernetes/ssl \\
  11. --pod-infra-container-image=lizhenliang/pause-amd64:3.0"
  12. EOF

•—hostname-override:显示名称,集群中唯一
•—network-plugin:启用CNI
•—kubeconfig:空路径,会自动生成,后面用于连接apiserver
•—bootstrap-kubeconfig:首次启动向apiserver申请证书
•—config:配置参数文件
•—cert-dir:kubelet证书生成目录
•—pod-infra-container-image:管理Pod网络容器的镜像

2. 配置参数文件

  1. cat > /opt/kubernetes/cfg/kubelet-config.yml << EOF
  2. kind: KubeletConfiguration
  3. apiVersion: kubelet.config.k8s.io/v1beta1
  4. address: 0.0.0.0
  5. port: 10250
  6. readOnlyPort: 10255
  7. cgroupDriver: cgroupfs
  8. clusterDNS:
  9. - 10.0.0.2
  10. clusterDomain: cluster.local
  11. failSwapOn: false
  12. authentication:
  13. anonymous:
  14. enabled: false
  15. webhook:
  16. cacheTTL: 2m0s
  17. enabled: true
  18. x509:
  19. clientCAFile: /opt/kubernetes/ssl/ca.pem
  20. authorization:
  21. mode: Webhook
  22. webhook:
  23. cacheAuthorizedTTL: 5m0s
  24. cacheUnauthorizedTTL: 30s
  25. evictionHard:
  26. imagefs.available: 15%
  27. memory.available: 100Mi
  28. nodefs.available: 10%
  29. nodefs.inodesFree: 5%
  30. maxOpenFiles: 1000000
  31. maxPods: 110
  32. EOF

3. 生成kubelet初次加入集群引导kubeconfig文件

  1. KUBE_CONFIG="/opt/kubernetes/cfg/bootstrap.kubeconfig"
  2. KUBE_APISERVER="https://192.168.31.71:6443" # apiserver IP:PORT
  3. TOKEN="c47ffb939f5ca36231d9e3121a252940" # 与token.csv里保持一致
  4. # 生成 kubelet bootstrap kubeconfig 配置文件
  5. kubectl config set-cluster kubernetes \
  6. --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  7. --embed-certs=true \
  8. --server=${KUBE_APISERVER} \
  9. --kubeconfig=${KUBE_CONFIG}
  10. kubectl config set-credentials "kubelet-bootstrap" \
  11. --token=${TOKEN} \
  12. --kubeconfig=${KUBE_CONFIG}
  13. kubectl config set-context default \
  14. --cluster=kubernetes \
  15. --user="kubelet-bootstrap" \
  16. --kubeconfig=${KUBE_CONFIG}
  17. kubectl config use-context default --kubeconfig=${KUBE_CONFIG}

4. systemd管理kubelet

  1. cat > /usr/lib/systemd/system/kubelet.service << EOF
  2. [Unit]
  3. Description=Kubernetes Kubelet
  4. After=docker.service
  5. [Service]
  6. EnvironmentFile=/opt/kubernetes/cfg/kubelet.conf
  7. ExecStart=/opt/kubernetes/bin/kubelet \$KUBELET_OPTS
  8. Restart=on-failure
  9. LimitNOFILE=65536
  10. [Install]
  11. WantedBy=multi-user.target
  12. EOF

5. 启动并设置开机启动

  1. systemctl daemon-reload
  2. systemctl start kubelet
  3. systemctl enable kubelet

5.3 批准kubelet证书申请并加入集群

  1. # 查看kubelet证书请求
  2. kubectl get csr
  3. NAME AGE SIGNERNAME REQUESTOR CONDITION
  4. node-csr-uCEGPOIiDdlLODKts8J658HrFq9CZ--K6M4G7bjhk8A 6m3s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
  5. # 批准申请
  6. kubectl certificate approve node-csr-uCEGPOIiDdlLODKts8J658HrFq9CZ--K6M4G7bjhk8A
  7. # 查看节点
  8. kubectl get node
  9. NAME STATUS ROLES AGE VERSION
  10. k8s-master1 NotReady <none> 7s v1.18.3

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

5.4 部署kube-proxy

1. 创建配置文件

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

2. 配置参数文件

  1. cat > /opt/kubernetes/cfg/kube-proxy-config.yml << EOF
  2. kind: KubeProxyConfiguration
  3. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  4. bindAddress: 0.0.0.0
  5. metricsBindAddress: 0.0.0.0:10249
  6. clientConnection:
  7. kubeconfig: /opt/kubernetes/cfg/kube-proxy.kubeconfig
  8. hostnameOverride: k8s-master1
  9. clusterCIDR: 10.0.0.0/24
  10. EOF

3. 生成kube-proxy.kubeconfig文件

  1. # 切换工作目录
  2. cd ~/TLS/k8s
  3. # 创建证书请求文件
  4. cat > kube-proxy-csr.json << EOF
  5. {
  6. "CN": "system:kube-proxy",
  7. "hosts": [],
  8. "key": {
  9. "algo": "rsa",
  10. "size": 2048
  11. },
  12. "names": [
  13. {
  14. "C": "CN",
  15. "L": "BeiJing",
  16. "ST": "BeiJing",
  17. "O": "k8s",
  18. "OU": "System"
  19. }
  20. ]
  21. }
  22. EOF
  23. # 生成证书
  24. cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
  25. 生成kubeconfig文件:
  26. KUBE_CONFIG="/opt/kubernetes/cfg/kube-proxy.kubeconfig"
  27. KUBE_APISERVER="https://192.168.31.71:6443"
  28. kubectl config set-cluster kubernetes \
  29. --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  30. --embed-certs=true \
  31. --server=${KUBE_APISERVER} \
  32. --kubeconfig=${KUBE_CONFIG}
  33. kubectl config set-credentials kube-proxy \
  34. --client-certificate=./kube-proxy.pem \
  35. --client-key=./kube-proxy-key.pem \
  36. --embed-certs=true \
  37. --kubeconfig=${KUBE_CONFIG}
  38. kubectl config set-context default \
  39. --cluster=kubernetes \
  40. --user=kube-proxy \
  41. --kubeconfig=${KUBE_CONFIG}
  42. kubectl config use-context default --kubeconfig=${KUBE_CONFIG}

4. systemd管理kube-proxy

  1. cat > /usr/lib/systemd/system/kube-proxy.service << EOF
  2. [Unit]
  3. Description=Kubernetes Proxy
  4. After=network.target
  5. [Service]
  6. EnvironmentFile=/opt/kubernetes/cfg/kube-proxy.conf
  7. ExecStart=/opt/kubernetes/bin/kube-proxy \$KUBE_PROXY_OPTS
  8. Restart=on-failure
  9. LimitNOFILE=65536
  10. [Install]
  11. WantedBy=multi-user.target
  12. EOF

5. 启动并设置开机启动

  1. systemctl daemon-reload
  2. systemctl start kube-proxy
  3. systemctl enable kube-proxy

5.5 部署网络组件

Calico是一个纯三层的数据中心网络方案,是目前Kubernetes主流的网络方案。
部署Calico:

  1. ---
  2. # Source: calico/templates/calico-config.yaml
  3. # This ConfigMap is used to configure a self-hosted Calico installation.
  4. kind: ConfigMap
  5. apiVersion: v1
  6. metadata:
  7. name: calico-config
  8. namespace: kube-system
  9. data:
  10. # Typha is disabled.
  11. typha_service_name: "none"
  12. # Configure the backend to use.
  13. calico_backend: "bird"
  14. # Configure the MTU to use
  15. veth_mtu: "1440"
  16. # The CNI network configuration to install on each node. The special
  17. # values in this config will be automatically populated.
  18. cni_network_config: |-
  19. {
  20. "name": "k8s-pod-network",
  21. "cniVersion": "0.3.1",
  22. "plugins": [
  23. {
  24. "type": "calico",
  25. "log_level": "info",
  26. "datastore_type": "kubernetes",
  27. "nodename": "__KUBERNETES_NODE_NAME__",
  28. "mtu": __CNI_MTU__,
  29. "ipam": {
  30. "type": "calico-ipam"
  31. },
  32. "policy": {
  33. "type": "k8s"
  34. },
  35. "kubernetes": {
  36. "kubeconfig": "__KUBECONFIG_FILEPATH__"
  37. }
  38. },
  39. {
  40. "type": "portmap",
  41. "snat": true,
  42. "capabilities": {"portMappings": true}
  43. }
  44. ]
  45. }
  46. ---
  47. # Source: calico/templates/kdd-crds.yaml
  48. apiVersion: apiextensions.k8s.io/v1beta1
  49. kind: CustomResourceDefinition
  50. metadata:
  51. name: felixconfigurations.crd.projectcalico.org
  52. spec:
  53. scope: Cluster
  54. group: crd.projectcalico.org
  55. version: v1
  56. names:
  57. kind: FelixConfiguration
  58. plural: felixconfigurations
  59. singular: felixconfiguration
  60. ---
  61. apiVersion: apiextensions.k8s.io/v1beta1
  62. kind: CustomResourceDefinition
  63. metadata:
  64. name: ipamblocks.crd.projectcalico.org
  65. spec:
  66. scope: Cluster
  67. group: crd.projectcalico.org
  68. version: v1
  69. names:
  70. kind: IPAMBlock
  71. plural: ipamblocks
  72. singular: ipamblock
  73. ---
  74. apiVersion: apiextensions.k8s.io/v1beta1
  75. kind: CustomResourceDefinition
  76. metadata:
  77. name: blockaffinities.crd.projectcalico.org
  78. spec:
  79. scope: Cluster
  80. group: crd.projectcalico.org
  81. version: v1
  82. names:
  83. kind: BlockAffinity
  84. plural: blockaffinities
  85. singular: blockaffinity
  86. ---
  87. apiVersion: apiextensions.k8s.io/v1beta1
  88. kind: CustomResourceDefinition
  89. metadata:
  90. name: ipamhandles.crd.projectcalico.org
  91. spec:
  92. scope: Cluster
  93. group: crd.projectcalico.org
  94. version: v1
  95. names:
  96. kind: IPAMHandle
  97. plural: ipamhandles
  98. singular: ipamhandle
  99. ---
  100. apiVersion: apiextensions.k8s.io/v1beta1
  101. kind: CustomResourceDefinition
  102. metadata:
  103. name: ipamconfigs.crd.projectcalico.org
  104. spec:
  105. scope: Cluster
  106. group: crd.projectcalico.org
  107. version: v1
  108. names:
  109. kind: IPAMConfig
  110. plural: ipamconfigs
  111. singular: ipamconfig
  112. ---
  113. apiVersion: apiextensions.k8s.io/v1beta1
  114. kind: CustomResourceDefinition
  115. metadata:
  116. name: bgppeers.crd.projectcalico.org
  117. spec:
  118. scope: Cluster
  119. group: crd.projectcalico.org
  120. version: v1
  121. names:
  122. kind: BGPPeer
  123. plural: bgppeers
  124. singular: bgppeer
  125. ---
  126. apiVersion: apiextensions.k8s.io/v1beta1
  127. kind: CustomResourceDefinition
  128. metadata:
  129. name: bgpconfigurations.crd.projectcalico.org
  130. spec:
  131. scope: Cluster
  132. group: crd.projectcalico.org
  133. version: v1
  134. names:
  135. kind: BGPConfiguration
  136. plural: bgpconfigurations
  137. singular: bgpconfiguration
  138. ---
  139. apiVersion: apiextensions.k8s.io/v1beta1
  140. kind: CustomResourceDefinition
  141. metadata:
  142. name: ippools.crd.projectcalico.org
  143. spec:
  144. scope: Cluster
  145. group: crd.projectcalico.org
  146. version: v1
  147. names:
  148. kind: IPPool
  149. plural: ippools
  150. singular: ippool
  151. ---
  152. apiVersion: apiextensions.k8s.io/v1beta1
  153. kind: CustomResourceDefinition
  154. metadata:
  155. name: hostendpoints.crd.projectcalico.org
  156. spec:
  157. scope: Cluster
  158. group: crd.projectcalico.org
  159. version: v1
  160. names:
  161. kind: HostEndpoint
  162. plural: hostendpoints
  163. singular: hostendpoint
  164. ---
  165. apiVersion: apiextensions.k8s.io/v1beta1
  166. kind: CustomResourceDefinition
  167. metadata:
  168. name: clusterinformations.crd.projectcalico.org
  169. spec:
  170. scope: Cluster
  171. group: crd.projectcalico.org
  172. version: v1
  173. names:
  174. kind: ClusterInformation
  175. plural: clusterinformations
  176. singular: clusterinformation
  177. ---
  178. apiVersion: apiextensions.k8s.io/v1beta1
  179. kind: CustomResourceDefinition
  180. metadata:
  181. name: globalnetworkpolicies.crd.projectcalico.org
  182. spec:
  183. scope: Cluster
  184. group: crd.projectcalico.org
  185. version: v1
  186. names:
  187. kind: GlobalNetworkPolicy
  188. plural: globalnetworkpolicies
  189. singular: globalnetworkpolicy
  190. ---
  191. apiVersion: apiextensions.k8s.io/v1beta1
  192. kind: CustomResourceDefinition
  193. metadata:
  194. name: globalnetworksets.crd.projectcalico.org
  195. spec:
  196. scope: Cluster
  197. group: crd.projectcalico.org
  198. version: v1
  199. names:
  200. kind: GlobalNetworkSet
  201. plural: globalnetworksets
  202. singular: globalnetworkset
  203. ---
  204. apiVersion: apiextensions.k8s.io/v1beta1
  205. kind: CustomResourceDefinition
  206. metadata:
  207. name: networkpolicies.crd.projectcalico.org
  208. spec:
  209. scope: Namespaced
  210. group: crd.projectcalico.org
  211. version: v1
  212. names:
  213. kind: NetworkPolicy
  214. plural: networkpolicies
  215. singular: networkpolicy
  216. ---
  217. apiVersion: apiextensions.k8s.io/v1beta1
  218. kind: CustomResourceDefinition
  219. metadata:
  220. name: networksets.crd.projectcalico.org
  221. spec:
  222. scope: Namespaced
  223. group: crd.projectcalico.org
  224. version: v1
  225. names:
  226. kind: NetworkSet
  227. plural: networksets
  228. singular: networkset
  229. ---
  230. # Source: calico/templates/rbac.yaml
  231. # Include a clusterrole for the kube-controllers component,
  232. # and bind it to the calico-kube-controllers serviceaccount.
  233. kind: ClusterRole
  234. apiVersion: rbac.authorization.k8s.io/v1
  235. metadata:
  236. name: calico-kube-controllers
  237. rules:
  238. # Nodes are watched to monitor for deletions.
  239. - apiGroups: [""]
  240. resources:
  241. - nodes
  242. verbs:
  243. - watch
  244. - list
  245. - get
  246. # Pods are queried to check for existence.
  247. - apiGroups: [""]
  248. resources:
  249. - pods
  250. verbs:
  251. - get
  252. # IPAM resources are manipulated when nodes are deleted.
  253. - apiGroups: ["crd.projectcalico.org"]
  254. resources:
  255. - ippools
  256. verbs:
  257. - list
  258. - apiGroups: ["crd.projectcalico.org"]
  259. resources:
  260. - blockaffinities
  261. - ipamblocks
  262. - ipamhandles
  263. verbs:
  264. - get
  265. - list
  266. - create
  267. - update
  268. - delete
  269. # Needs access to update clusterinformations.
  270. - apiGroups: ["crd.projectcalico.org"]
  271. resources:
  272. - clusterinformations
  273. verbs:
  274. - get
  275. - create
  276. - update
  277. ---
  278. kind: ClusterRoleBinding
  279. apiVersion: rbac.authorization.k8s.io/v1
  280. metadata:
  281. name: calico-kube-controllers
  282. roleRef:
  283. apiGroup: rbac.authorization.k8s.io
  284. kind: ClusterRole
  285. name: calico-kube-controllers
  286. subjects:
  287. - kind: ServiceAccount
  288. name: calico-kube-controllers
  289. namespace: kube-system
  290. ---
  291. # Include a clusterrole for the calico-node DaemonSet,
  292. # and bind it to the calico-node serviceaccount.
  293. kind: ClusterRole
  294. apiVersion: rbac.authorization.k8s.io/v1
  295. metadata:
  296. name: calico-node
  297. rules:
  298. # The CNI plugin needs to get pods, nodes, and namespaces.
  299. - apiGroups: [""]
  300. resources:
  301. - pods
  302. - nodes
  303. - namespaces
  304. verbs:
  305. - get
  306. - apiGroups: [""]
  307. resources:
  308. - endpoints
  309. - services
  310. verbs:
  311. # Used to discover service IPs for advertisement.
  312. - watch
  313. - list
  314. # Used to discover Typhas.
  315. - get
  316. - apiGroups: [""]
  317. resources:
  318. - nodes/status
  319. verbs:
  320. # Needed for clearing NodeNetworkUnavailable flag.
  321. - patch
  322. # Calico stores some configuration information in node annotations.
  323. - update
  324. # Watch for changes to Kubernetes NetworkPolicies.
  325. - apiGroups: ["networking.k8s.io"]
  326. resources:
  327. - networkpolicies
  328. verbs:
  329. - watch
  330. - list
  331. # Used by Calico for policy information.
  332. - apiGroups: [""]
  333. resources:
  334. - pods
  335. - namespaces
  336. - serviceaccounts
  337. verbs:
  338. - list
  339. - watch
  340. # The CNI plugin patches pods/status.
  341. - apiGroups: [""]
  342. resources:
  343. - pods/status
  344. verbs:
  345. - patch
  346. # Calico monitors various CRDs for config.
  347. - apiGroups: ["crd.projectcalico.org"]
  348. resources:
  349. - globalfelixconfigs
  350. - felixconfigurations
  351. - bgppeers
  352. - globalbgpconfigs
  353. - bgpconfigurations
  354. - ippools
  355. - ipamblocks
  356. - globalnetworkpolicies
  357. - globalnetworksets
  358. - networkpolicies
  359. - networksets
  360. - clusterinformations
  361. - hostendpoints
  362. - blockaffinities
  363. verbs:
  364. - get
  365. - list
  366. - watch
  367. # Calico must create and update some CRDs on startup.
  368. - apiGroups: ["crd.projectcalico.org"]
  369. resources:
  370. - ippools
  371. - felixconfigurations
  372. - clusterinformations
  373. verbs:
  374. - create
  375. - update
  376. # Calico stores some configuration information on the node.
  377. - apiGroups: [""]
  378. resources:
  379. - nodes
  380. verbs:
  381. - get
  382. - list
  383. - watch
  384. # These permissions are only requried for upgrade from v2.6, and can
  385. # be removed after upgrade or on fresh installations.
  386. - apiGroups: ["crd.projectcalico.org"]
  387. resources:
  388. - bgpconfigurations
  389. - bgppeers
  390. verbs:
  391. - create
  392. - update
  393. # These permissions are required for Calico CNI to perform IPAM allocations.
  394. - apiGroups: ["crd.projectcalico.org"]
  395. resources:
  396. - blockaffinities
  397. - ipamblocks
  398. - ipamhandles
  399. verbs:
  400. - get
  401. - list
  402. - create
  403. - update
  404. - delete
  405. - apiGroups: ["crd.projectcalico.org"]
  406. resources:
  407. - ipamconfigs
  408. verbs:
  409. - get
  410. # Block affinities must also be watchable by confd for route aggregation.
  411. - apiGroups: ["crd.projectcalico.org"]
  412. resources:
  413. - blockaffinities
  414. verbs:
  415. - watch
  416. # The Calico IPAM migration needs to get daemonsets. These permissions can be
  417. # removed if not upgrading from an installation using host-local IPAM.
  418. - apiGroups: ["apps"]
  419. resources:
  420. - daemonsets
  421. verbs:
  422. - get
  423. ---
  424. apiVersion: rbac.authorization.k8s.io/v1
  425. kind: ClusterRoleBinding
  426. metadata:
  427. name: calico-node
  428. roleRef:
  429. apiGroup: rbac.authorization.k8s.io
  430. kind: ClusterRole
  431. name: calico-node
  432. subjects:
  433. - kind: ServiceAccount
  434. name: calico-node
  435. namespace: kube-system
  436. ---
  437. # Source: calico/templates/calico-node.yaml
  438. # This manifest installs the calico-node container, as well
  439. # as the CNI plugins and network config on
  440. # each master and worker node in a Kubernetes cluster.
  441. kind: DaemonSet
  442. apiVersion: apps/v1
  443. metadata:
  444. name: calico-node
  445. namespace: kube-system
  446. labels:
  447. k8s-app: calico-node
  448. spec:
  449. selector:
  450. matchLabels:
  451. k8s-app: calico-node
  452. updateStrategy:
  453. type: RollingUpdate
  454. rollingUpdate:
  455. maxUnavailable: 1
  456. template:
  457. metadata:
  458. labels:
  459. k8s-app: calico-node
  460. annotations:
  461. # This, along with the CriticalAddonsOnly toleration below,
  462. # marks the pod as a critical add-on, ensuring it gets
  463. # priority scheduling and that its resources are reserved
  464. # if it ever gets evicted.
  465. scheduler.alpha.kubernetes.io/critical-pod: ''
  466. spec:
  467. nodeSelector:
  468. beta.kubernetes.io/os: linux
  469. hostNetwork: true
  470. tolerations:
  471. # Make sure calico-node gets scheduled on all nodes.
  472. - effect: NoSchedule
  473. operator: Exists
  474. # Mark the pod as a critical add-on for rescheduling.
  475. - key: CriticalAddonsOnly
  476. operator: Exists
  477. - effect: NoExecute
  478. operator: Exists
  479. serviceAccountName: calico-node
  480. # Minimize downtime during a rolling upgrade or deletion; tell Kubernetes to do a "force
  481. # deletion": https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods.
  482. terminationGracePeriodSeconds: 0
  483. priorityClassName: system-node-critical
  484. initContainers:
  485. # This container performs upgrade from host-local IPAM to calico-ipam.
  486. # It can be deleted if this is a fresh installation, or if you have already
  487. # upgraded to use calico-ipam.
  488. - name: upgrade-ipam
  489. image: calico/cni:v3.10.4
  490. command: ["/opt/cni/bin/calico-ipam", "-upgrade"]
  491. env:
  492. - name: KUBERNETES_NODE_NAME
  493. valueFrom:
  494. fieldRef:
  495. fieldPath: spec.nodeName
  496. - name: CALICO_NETWORKING_BACKEND
  497. valueFrom:
  498. configMapKeyRef:
  499. name: calico-config
  500. key: calico_backend
  501. volumeMounts:
  502. - mountPath: /var/lib/cni/networks
  503. name: host-local-net-dir
  504. - mountPath: /host/opt/cni/bin
  505. name: cni-bin-dir
  506. # This container installs the CNI binaries
  507. # and CNI network config file on each node.
  508. - name: install-cni
  509. image: calico/cni:v3.10.4
  510. command: ["/install-cni.sh"]
  511. env:
  512. # Name of the CNI config file to create.
  513. - name: CNI_CONF_NAME
  514. value: "10-calico.conflist"
  515. # The CNI network config to install on each node.
  516. - name: CNI_NETWORK_CONFIG
  517. valueFrom:
  518. configMapKeyRef:
  519. name: calico-config
  520. key: cni_network_config
  521. # Set the hostname based on the k8s node name.
  522. - name: KUBERNETES_NODE_NAME
  523. valueFrom:
  524. fieldRef:
  525. fieldPath: spec.nodeName
  526. # CNI MTU Config variable
  527. - name: CNI_MTU
  528. valueFrom:
  529. configMapKeyRef:
  530. name: calico-config
  531. key: veth_mtu
  532. # Prevents the container from sleeping forever.
  533. - name: SLEEP
  534. value: "false"
  535. volumeMounts:
  536. - mountPath: /host/opt/cni/bin
  537. name: cni-bin-dir
  538. - mountPath: /host/etc/cni/net.d
  539. name: cni-net-dir
  540. # Adds a Flex Volume Driver that creates a per-pod Unix Domain Socket to allow Dikastes
  541. # to communicate with Felix over the Policy Sync API.
  542. - name: flexvol-driver
  543. image: calico/pod2daemon-flexvol:v3.10.4
  544. volumeMounts:
  545. - name: flexvol-driver-host
  546. mountPath: /host/driver
  547. containers:
  548. # Runs calico-node container on each Kubernetes node. This
  549. # container programs network policy and routes on each
  550. # host.
  551. - name: calico-node
  552. image: calico/node:v3.10.4
  553. env:
  554. # Use Kubernetes API as the backing datastore.
  555. - name: DATASTORE_TYPE
  556. value: "kubernetes"
  557. # Wait for the datastore.
  558. - name: WAIT_FOR_DATASTORE
  559. value: "true"
  560. # Set based on the k8s node name.
  561. - name: NODENAME
  562. valueFrom:
  563. fieldRef:
  564. fieldPath: spec.nodeName
  565. # Choose the backend to use.
  566. - name: CALICO_NETWORKING_BACKEND
  567. valueFrom:
  568. configMapKeyRef:
  569. name: calico-config
  570. key: calico_backend
  571. # Cluster type to identify the deployment type
  572. - name: CLUSTER_TYPE
  573. value: "k8s,bgp"
  574. - name: IP_AUTODETECTION_METHOD
  575. value: "interface=eth0"
  576. # Auto-detect the BGP IP address.
  577. - name: IP
  578. value: "autodetect"
  579. # Enable IPIP
  580. - name: CALICO_IPV4POOL_IPIP
  581. value: "Always"
  582. # Set MTU for tunnel device used if ipip is enabled
  583. - name: FELIX_IPINIPMTU
  584. valueFrom:
  585. configMapKeyRef:
  586. name: calico-config
  587. key: veth_mtu
  588. # The default IPv4 pool to create on startup if none exists. Pod IPs will be
  589. # chosen from this range. Changing this value after installation will have
  590. # no effect. This should fall within `--cluster-cidr`.
  591. - name: CALICO_IPV4POOL_CIDR
  592. value: "192.168.0.0/16"
  593. # Disable file logging so `kubectl logs` works.
  594. - name: CALICO_DISABLE_FILE_LOGGING
  595. value: "true"
  596. # Set Felix endpoint to host default action to ACCEPT.
  597. - name: FELIX_DEFAULTENDPOINTTOHOSTACTION
  598. value: "ACCEPT"
  599. # Disable IPv6 on Kubernetes.
  600. - name: FELIX_IPV6SUPPORT
  601. value: "false"
  602. # Set Felix logging to "info"
  603. - name: FELIX_LOGSEVERITYSCREEN
  604. value: "info"
  605. - name: FELIX_HEALTHENABLED
  606. value: "true"
  607. securityContext:
  608. privileged: true
  609. resources:
  610. requests:
  611. cpu: 250m
  612. livenessProbe:
  613. exec:
  614. command:
  615. - /bin/calico-node
  616. - -felix-live
  617. - -bird-live
  618. periodSeconds: 10
  619. initialDelaySeconds: 10
  620. failureThreshold: 6
  621. readinessProbe:
  622. exec:
  623. command:
  624. - /bin/calico-node
  625. - -felix-ready
  626. - -bird-ready
  627. periodSeconds: 10
  628. volumeMounts:
  629. - mountPath: /lib/modules
  630. name: lib-modules
  631. readOnly: true
  632. - mountPath: /run/xtables.lock
  633. name: xtables-lock
  634. readOnly: false
  635. - mountPath: /var/run/calico
  636. name: var-run-calico
  637. readOnly: false
  638. - mountPath: /var/lib/calico
  639. name: var-lib-calico
  640. readOnly: false
  641. - name: policysync
  642. mountPath: /var/run/nodeagent
  643. volumes:
  644. # Used by calico-node.
  645. - name: lib-modules
  646. hostPath:
  647. path: /lib/modules
  648. - name: var-run-calico
  649. hostPath:
  650. path: /var/run/calico
  651. - name: var-lib-calico
  652. hostPath:
  653. path: /var/lib/calico
  654. - name: xtables-lock
  655. hostPath:
  656. path: /run/xtables.lock
  657. type: FileOrCreate
  658. # Used to install CNI.
  659. - name: cni-bin-dir
  660. hostPath:
  661. path: /opt/cni/bin
  662. - name: cni-net-dir
  663. hostPath:
  664. path: /etc/cni/net.d
  665. # Mount in the directory for host-local IPAM allocations. This is
  666. # used when upgrading from host-local to calico-ipam, and can be removed
  667. # if not using the upgrade-ipam init container.
  668. - name: host-local-net-dir
  669. hostPath:
  670. path: /var/lib/cni/networks
  671. # Used to create per-pod Unix Domain Sockets
  672. - name: policysync
  673. hostPath:
  674. type: DirectoryOrCreate
  675. path: /var/run/nodeagent
  676. # Used to install Flex Volume Driver
  677. - name: flexvol-driver-host
  678. hostPath:
  679. type: DirectoryOrCreate
  680. path: /usr/libexec/kubernetes/kubelet-plugins/volume/exec/nodeagent~uds
  681. ---
  682. apiVersion: v1
  683. kind: ServiceAccount
  684. metadata:
  685. name: calico-node
  686. namespace: kube-system
  687. ---
  688. # Source: calico/templates/calico-kube-controllers.yaml
  689. # See https://github.com/projectcalico/kube-controllers
  690. apiVersion: apps/v1
  691. kind: Deployment
  692. metadata:
  693. name: calico-kube-controllers
  694. namespace: kube-system
  695. labels:
  696. k8s-app: calico-kube-controllers
  697. spec:
  698. # The controllers can only have a single active instance.
  699. replicas: 1
  700. selector:
  701. matchLabels:
  702. k8s-app: calico-kube-controllers
  703. strategy:
  704. type: Recreate
  705. template:
  706. metadata:
  707. name: calico-kube-controllers
  708. namespace: kube-system
  709. labels:
  710. k8s-app: calico-kube-controllers
  711. annotations:
  712. scheduler.alpha.kubernetes.io/critical-pod: ''
  713. spec:
  714. nodeSelector:
  715. beta.kubernetes.io/os: linux
  716. tolerations:
  717. # Mark the pod as a critical add-on for rescheduling.
  718. - key: CriticalAddonsOnly
  719. operator: Exists
  720. - key: node-role.kubernetes.io/master
  721. effect: NoSchedule
  722. serviceAccountName: calico-kube-controllers
  723. priorityClassName: system-cluster-critical
  724. containers:
  725. - name: calico-kube-controllers
  726. image: calico/kube-controllers:v3.10.4
  727. env:
  728. # Choose which controllers to run.
  729. - name: ENABLED_CONTROLLERS
  730. value: node
  731. - name: DATASTORE_TYPE
  732. value: kubernetes
  733. readinessProbe:
  734. exec:
  735. command:
  736. - /usr/bin/check-status
  737. - -r
  738. ---
  739. apiVersion: v1
  740. kind: ServiceAccount
  741. metadata:
  742. name: calico-kube-controllers
  743. namespace: kube-system
  744. ---
  745. # Source: calico/templates/calico-etcd-secrets.yaml
  746. ---
  747. # Source: calico/templates/calico-typha.yaml
  748. ---
  749. # Source: calico/templates/configure-canal.yaml
  1. kubectl apply -f calico.yaml
  2. kubectl get pods -n kube-system

等Calico Pod都Running,节点也会准备就绪:

  1. kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-master Ready <none> 37m v1.20.4

5.6 授权apiserver访问kubelet

应用场景:例如kubectl logs

  1. cat > apiserver-to-kubelet-rbac.yaml << EOF
  2. apiVersion: rbac.authorization.k8s.io/v1
  3. kind: ClusterRole
  4. metadata:
  5. annotations:
  6. rbac.authorization.kubernetes.io/autoupdate: "true"
  7. labels:
  8. kubernetes.io/bootstrapping: rbac-defaults
  9. name: system:kube-apiserver-to-kubelet
  10. rules:
  11. - apiGroups:
  12. - ""
  13. resources:
  14. - nodes/proxy
  15. - nodes/stats
  16. - nodes/log
  17. - nodes/spec
  18. - nodes/metrics
  19. - pods/log
  20. verbs:
  21. - "*"
  22. ---
  23. apiVersion: rbac.authorization.k8s.io/v1
  24. kind: ClusterRoleBinding
  25. metadata:
  26. name: system:kube-apiserver
  27. namespace: ""
  28. roleRef:
  29. apiGroup: rbac.authorization.k8s.io
  30. kind: ClusterRole
  31. name: system:kube-apiserver-to-kubelet
  32. subjects:
  33. - apiGroup: rbac.authorization.k8s.io
  34. kind: User
  35. name: kubernetes
  36. EOF
  37. kubectl apply -f apiserver-to-kubelet-rbac.yaml

5.7 新增加Worker Node

1. 拷贝已部署好的Node相关文件到新节点

在Master节点将Worker Node涉及文件拷贝到新节点192.168.31.72/73

  1. scp -r /opt/kubernetes root@192.168.31.72:/opt/
  2. scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@192.168.31.72:/usr/lib/systemd/system
  3. scp /opt/kubernetes/ssl/ca.pem root@192.168.31.72:/opt/kubernetes/ssl

2. 删除kubelet证书和kubeconfig文件

  1. rm -f /opt/kubernetes/cfg/kubelet.kubeconfig
  2. rm -f /opt/kubernetes/ssl/kubelet*

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

3. 修改主机名

  1. vi /opt/kubernetes/cfg/kubelet.conf
  2. --hostname-override=k8s-node1
  3. vi /opt/kubernetes/cfg/kube-proxy-config.yml
  4. hostnameOverride: k8s-node1

4. 启动并设置开机启动

  1. systemctl daemon-reload
  2. systemctl start kubelet kube-proxy
  3. systemctl enable kubelet kube-proxy

5. 在Master上批准新Node kubelet证书申请

  1. # 查看证书请求
  2. kubectl get csr
  3. NAME AGE SIGNERNAME REQUESTOR CONDITION
  4. node-csr-4zTjsaVSrhuyhIGqsefxzVoZDCNKei-aE2jyTP81Uro 89s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
  5. # 授权请求
  6. kubectl certificate approve node-csr-4zTjsaVSrhuyhIGqsefxzVoZDCNKei-aE2jyTP81Uro

6. 查看Node状态

  1. kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-master1 Ready <none> 47m v1.20.4
  4. k8s-node1 Ready <none> 6m49s v1.20.4

Node2(192.168.31.73 )节点同上。记得修改主机名!

六、部署Dashboard和CoreDNS

6.1 部署Dashboard

  1. kubectl apply -f kubernetes-dashboard.yaml
  2. # 查看部署
  3. kubectl get pods,svc -n kubernetes-dashboard

访问地址:https://NodeIP:30001
创建service account并绑定默认cluster-admin管理员集群角色:

  1. kubectl create serviceaccount dashboard-admin -n kube-system
  2. kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
  3. kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

使用输出的token登录Dashboard。
image.png
image.png

6.2 部署CoreDNS

  1. # __MACHINE_GENERATED_WARNING__
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. name: coredns
  6. namespace: kube-system
  7. labels:
  8. kubernetes.io/cluster-service: "true"
  9. addonmanager.kubernetes.io/mode: Reconcile
  10. ---
  11. apiVersion: rbac.authorization.k8s.io/v1
  12. kind: ClusterRole
  13. metadata:
  14. labels:
  15. kubernetes.io/bootstrapping: rbac-defaults
  16. addonmanager.kubernetes.io/mode: Reconcile
  17. name: system:coredns
  18. rules:
  19. - apiGroups:
  20. - ""
  21. resources:
  22. - endpoints
  23. - services
  24. - pods
  25. - namespaces
  26. verbs:
  27. - list
  28. - watch
  29. - apiGroups:
  30. - ""
  31. resources:
  32. - nodes
  33. verbs:
  34. - get
  35. - apiGroups:
  36. - discovery.k8s.io
  37. resources:
  38. - endpointslices
  39. verbs:
  40. - list
  41. - watch
  42. ---
  43. apiVersion: rbac.authorization.k8s.io/v1
  44. kind: ClusterRoleBinding
  45. metadata:
  46. annotations:
  47. rbac.authorization.kubernetes.io/autoupdate: "true"
  48. labels:
  49. kubernetes.io/bootstrapping: rbac-defaults
  50. addonmanager.kubernetes.io/mode: EnsureExists
  51. name: system:coredns
  52. roleRef:
  53. apiGroup: rbac.authorization.k8s.io
  54. kind: ClusterRole
  55. name: system:coredns
  56. subjects:
  57. - kind: ServiceAccount
  58. name: coredns
  59. namespace: kube-system
  60. ---
  61. apiVersion: v1
  62. kind: ConfigMap
  63. metadata:
  64. name: coredns
  65. namespace: kube-system
  66. labels:
  67. addonmanager.kubernetes.io/mode: EnsureExists
  68. data:
  69. Corefile: |
  70. .:53 {
  71. errors
  72. health {
  73. lameduck 5s
  74. }
  75. ready
  76. kubernetes cluster.local in-addr.arpa ip6.arpa {
  77. pods insecure
  78. fallthrough in-addr.arpa ip6.arpa
  79. ttl 30
  80. }
  81. prometheus :9153
  82. forward . /etc/resolv.conf {
  83. max_concurrent 1000
  84. }
  85. cache 30
  86. loop
  87. reload
  88. loadbalance
  89. }
  90. ---
  91. apiVersion: apps/v1
  92. kind: Deployment
  93. metadata:
  94. name: coredns
  95. namespace: kube-system
  96. labels:
  97. k8s-app: kube-dns
  98. kubernetes.io/cluster-service: "true"
  99. addonmanager.kubernetes.io/mode: Reconcile
  100. kubernetes.io/name: "CoreDNS"
  101. spec:
  102. # replicas: not specified here:
  103. # 1. In order to make Addon Manager do not reconcile this replicas parameter.
  104. # 2. Default is 1.
  105. # 3. Will be tuned in real time if DNS horizontal auto-scaling is turned on.
  106. strategy:
  107. type: RollingUpdate
  108. rollingUpdate:
  109. maxUnavailable: 1
  110. selector:
  111. matchLabels:
  112. k8s-app: kube-dns
  113. template:
  114. metadata:
  115. labels:
  116. k8s-app: kube-dns
  117. spec:
  118. securityContext:
  119. seccompProfile:
  120. type: RuntimeDefault
  121. priorityClassName: system-cluster-critical
  122. serviceAccountName: coredns
  123. affinity:
  124. podAntiAffinity:
  125. preferredDuringSchedulingIgnoredDuringExecution:
  126. - weight: 100
  127. podAffinityTerm:
  128. labelSelector:
  129. matchExpressions:
  130. - key: k8s-app
  131. operator: In
  132. values: ["kube-dns"]
  133. topologyKey: kubernetes.io/hostname
  134. tolerations:
  135. - key: "CriticalAddonsOnly"
  136. operator: "Exists"
  137. nodeSelector:
  138. kubernetes.io/os: linux
  139. containers:
  140. - name: coredns
  141. image: coredns/coredns:1.8.0
  142. imagePullPolicy: IfNotPresent
  143. resources:
  144. limits:
  145. memory: 100Mi
  146. requests:
  147. cpu: 100m
  148. memory: 70Mi
  149. args: [ "-conf", "/etc/coredns/Corefile" ]
  150. volumeMounts:
  151. - name: config-volume
  152. mountPath: /etc/coredns
  153. readOnly: true
  154. ports:
  155. - containerPort: 53
  156. name: dns
  157. protocol: UDP
  158. - containerPort: 53
  159. name: dns-tcp
  160. protocol: TCP
  161. - containerPort: 9153
  162. name: metrics
  163. protocol: TCP
  164. livenessProbe:
  165. httpGet:
  166. path: /health
  167. port: 8080
  168. scheme: HTTP
  169. initialDelaySeconds: 60
  170. timeoutSeconds: 5
  171. successThreshold: 1
  172. failureThreshold: 5
  173. readinessProbe:
  174. httpGet:
  175. path: /ready
  176. port: 8181
  177. scheme: HTTP
  178. securityContext:
  179. allowPrivilegeEscalation: false
  180. capabilities:
  181. add:
  182. - NET_BIND_SERVICE
  183. drop:
  184. - all
  185. readOnlyRootFilesystem: true
  186. dnsPolicy: Default
  187. volumes:
  188. - name: config-volume
  189. configMap:
  190. name: coredns
  191. items:
  192. - key: Corefile
  193. path: Corefile
  194. ---
  195. apiVersion: v1
  196. kind: Service
  197. metadata:
  198. name: kube-dns
  199. namespace: kube-system
  200. annotations:
  201. prometheus.io/port: "9153"
  202. prometheus.io/scrape: "true"
  203. labels:
  204. k8s-app: kube-dns
  205. kubernetes.io/cluster-service: "true"
  206. addonmanager.kubernetes.io/mode: Reconcile
  207. kubernetes.io/name: "CoreDNS"
  208. spec:
  209. selector:
  210. k8s-app: kube-dns
  211. clusterIP: 10.0.0.2
  212. ports:
  213. - name: dns
  214. port: 53
  215. protocol: UDP
  216. - name: dns-tcp
  217. port: 53
  218. protocol: TCP
  219. - name: metrics
  220. port: 9153
  221. protocol: TCP

CoreDNS用于集群内部Service名称解析。

  1. kubectl apply -f coredns.yaml
  2. kubectl get pods -n kube-system
  3. NAME READY STATUS RESTARTS AGE
  4. coredns-5ffbfd976d-j6shb 1/1 Running 0 32s

DNS解析测试:

  1. kubectl run -it --rm dns-test --image=busybox:1.28.4 sh
  2. If you don't see a command prompt, try pressing enter.
  3. / # nslookup kubernetes
  4. Server: 10.0.0.2
  5. Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local
  6. Name: kubernetes
  7. Address 1: 10.0.0.1 kubernetes.default.svc.cluster.local

解析没问题。
至此一个单Master集群就搭建完成了!这个环境就足以满足学习实验了,如果你的服务器配置较高,可继续扩容多Master集群!

七、扩容多Master(高可用架构)

Kubernetes作为容器集群系统,通过健康检查+重启策略实现了Pod故障自我修复能力,通过调度算法实现将Pod分布式部署,并保持预期副本数,根据Node失效状态自动在其他Node拉起Pod,实现了应用层的高可用性。
针对Kubernetes集群,高可用性还应包含以下两个层面的考虑:Etcd数据库的高可用性和Kubernetes Master组件的高可用性。 而Etcd我们已经采用3个节点组建集群实现高可用,本节将对Master节点高可用进行说明和实施。
Master节点扮演着总控中心的角色,通过不断与工作节点上的Kubelet和kube-proxy进行通信来维护整个集群的健康工作状态。如果Master节点故障,将无法使用kubectl工具或者API做任何集群管理。
Master节点主要有三个服务kube-apiserver、kube-controller-manager和kube-scheduler,其中kube-controller-manager和kube-scheduler组件自身通过选择机制已经实现了高可用,所以Master高可用主要针对kube-apiserver组件,而该组件是以HTTP API提供服务,因此对他高可用与Web服务器类似,增加负载均衡器对其负载均衡即可,并且可水平扩容。
多Master架构图:
image.png

7.1 部署Master2 Node

现在需要再增加一台新服务器,作为Master2 Node,IP是192.168.31.74。
为了节省资源你也可以将之前部署好的Worker Node1复用为Master2 Node角色(即部署Master组件)
Master2 与已部署的Master1所有操作一致。所以我们只需将Master1所有K8s文件拷贝过来,再修改下服务器IP和主机名启动即可。

1. 安装Docker

  1. scp /usr/bin/docker* root@192.168.31.74:/usr/bin
  2. scp /usr/bin/runc root@192.168.31.74:/usr/bin
  3. scp /usr/bin/containerd* root@192.168.31.74:/usr/bin
  4. scp /usr/lib/systemd/system/docker.service root@192.168.31.74:/usr/lib/systemd/system
  5. scp -r /etc/docker root@192.168.31.74:/etc
  6. # 在Master2启动Docker
  7. systemctl daemon-reload
  8. systemctl start docker
  9. systemctl enable docker

2. 创建etcd证书目录

在Master2创建etcd证书目录:

  1. mkdir -p /opt/etcd/ssl

3. 拷贝文件(Master1操作)

拷贝Master1上所有K8s文件和etcd证书到Master2:

  1. scp -r /opt/kubernetes root@192.168.31.74:/opt
  2. scp -r /opt/etcd/ssl root@192.168.31.74:/opt/etcd
  3. scp /usr/lib/systemd/system/kube* root@192.168.31.74:/usr/lib/systemd/system
  4. scp /usr/bin/kubectl root@192.168.31.74:/usr/bin
  5. scp -r ~/.kube root@192.168.31.74:~

4. 删除证书文件

删除kubelet证书和kubeconfig文件:

  1. rm -f /opt/kubernetes/cfg/kubelet.kubeconfig
  2. rm -f /opt/kubernetes/ssl/kubelet*

5. 修改配置文件IP和主机名

修改apiserver、kubelet和kube-proxy配置文件为本地IP:

  1. vi /opt/kubernetes/cfg/kube-apiserver.conf
  2. ...
  3. --bind-address=192.168.31.74 \
  4. --advertise-address=192.168.31.74 \
  5. ...
  6. vi /opt/kubernetes/cfg/kube-controller-manager.kubeconfig
  7. server: https://192.168.31.74:6443
  8. vi /opt/kubernetes/cfg/kube-scheduler.kubeconfig
  9. server: https://192.168.31.74:6443
  10. vi /opt/kubernetes/cfg/kubelet.conf
  11. --hostname-override=k8s-master2
  12. vi /opt/kubernetes/cfg/kube-proxy-config.yml
  13. hostnameOverride: k8s-master2
  14. vi ~/.kube/config
  15. ...
  16. server: https://192.168.31.74:6443

6. 启动设置开机启动

  1. systemctl daemon-reload
  2. systemctl start kube-apiserver kube-controller-manager kube-scheduler kubelet kube-proxy
  3. systemctl enable kube-apiserver kube-controller-manager kube-scheduler kubelet kube-proxy

7. 查看集群状态

  1. kubectl get cs
  2. NAME STATUS MESSAGE ERROR
  3. scheduler Healthy ok
  4. controller-manager Healthy ok
  5. etcd-1 Healthy {"health":"true"}
  6. etcd-2 Healthy {"health":"true"}
  7. etcd-0 Healthy {"health":"true"}

8. 批准kubelet证书申请

  1. # 查看证书请求
  2. kubectl get csr
  3. NAME AGE SIGNERNAME REQUESTOR CONDITION
  4. node-csr-JYNknakEa_YpHz797oKaN-ZTk43nD51Zc9CJkBLcASU 85m kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
  5. # 授权请求
  6. kubectl certificate approve node-csr-JYNknakEa_YpHz797oKaN-ZTk43nD51Zc9CJkBLcASU
  7. # 查看Node
  8. kubectl get node
  9. NAME STATUS ROLES AGE VERSION
  10. k8s-master1 Ready <none> 34h v1.20.4
  11. k8s-master2 Ready <none> 2m v1.20.4
  12. k8s-node1 Ready <none> 33h v1.20.4
  13. k8s-node2 Ready <none> 33h v1.20.4

如果你在学习中遇到问题或者文档有误可联系阿良~ 微信: xyz12366699

7.2 部署Nginx+Keepalived高可用负载均衡器

kube-apiserver高可用架构图:
image.png
•Nginx是一个主流Web服务和反向代理服务器,这里用四层实现对apiserver实现负载均衡。
•Keepalived是一个主流高可用软件,基于VIP绑定实现服务器双机热备,在上述拓扑中,Keepalived主要根据Nginx运行状态判断是否需要故障转移(漂移VIP),例如当Nginx主节点挂掉,VIP会自动绑定在Nginx备节点,从而保证VIP一直可用,实现Nginx高可用。
注1:为了节省机器,这里与K8s Master节点机器复用。也可以独立于k8s集群之外部署,只要nginx与apiserver能通信就行。
注2:如果你是在公有云上,一般都不支持keepalived,那么你可以直接用它们的负载均衡器产品,直接负载均衡多台Master kube-apiserver,架构与上面一样。
在两台Master节点操作。

1. 安装软件包(主/备)

  1. yum install epel-release -y
  2. yum install nginx keepalived -y

2. Nginx配置文件(主/备一样)

  1. cat > /etc/nginx/nginx.conf << "EOF"
  2. user nginx;
  3. worker_processes auto;
  4. error_log /var/log/nginx/error.log;
  5. pid /run/nginx.pid;
  6. include /usr/share/nginx/modules/*.conf;
  7. events {
  8. worker_connections 1024;
  9. }
  10. # 四层负载均衡,为两台Master apiserver组件提供负载均衡
  11. stream {
  12. log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
  13. access_log /var/log/nginx/k8s-access.log main;
  14. upstream k8s-apiserver {
  15. server 192.168.31.71:6443; # Master1 APISERVER IP:PORT
  16. server 192.168.31.74:6443; # Master2 APISERVER IP:PORT
  17. }
  18. server {
  19. listen 16443; # 由于nginx与master节点复用,这个监听端口不能是6443,否则会冲突
  20. proxy_pass k8s-apiserver;
  21. }
  22. }
  23. http {
  24. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  25. '$status $body_bytes_sent "$http_referer" '
  26. '"$http_user_agent" "$http_x_forwarded_for"';
  27. access_log /var/log/nginx/access.log main;
  28. sendfile on;
  29. tcp_nopush on;
  30. tcp_nodelay on;
  31. keepalive_timeout 65;
  32. types_hash_max_size 2048;
  33. include /etc/nginx/mime.types;
  34. default_type application/octet-stream;
  35. server {
  36. listen 80 default_server;
  37. server_name _;
  38. location / {
  39. }
  40. }
  41. }
  42. EOF

3. keepalived配置文件(Nginx Master)

  1. cat > /etc/keepalived/keepalived.conf << EOF
  2. global_defs {
  3. notification_email {
  4. acassen@firewall.loc
  5. failover@firewall.loc
  6. sysadmin@firewall.loc
  7. }
  8. notification_email_from Alexandre.Cassen@firewall.loc
  9. smtp_server 127.0.0.1
  10. smtp_connect_timeout 30
  11. router_id NGINX_MASTER
  12. }
  13. vrrp_script check_nginx {
  14. script "/etc/keepalived/check_nginx.sh"
  15. }
  16. vrrp_instance VI_1 {
  17. state MASTER
  18. interface ens33 # 修改为实际网卡名
  19. virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的
  20. priority 100 # 优先级,备服务器设置 90
  21. advert_int 1 # 指定VRRP 心跳包通告间隔时间,默认1秒
  22. authentication {
  23. auth_type PASS
  24. auth_pass 1111
  25. }
  26. # 虚拟IP
  27. virtual_ipaddress {
  28. 192.168.31.88/24
  29. }
  30. track_script {
  31. check_nginx
  32. }
  33. }
  34. EOF

•vrrp_script:指定检查nginx工作状态脚本(根据nginx状态判断是否故障转移)
•virtual_ipaddress:虚拟IP(VIP)
准备上述配置文件中检查nginx运行状态的脚本:

  1. cat > /etc/keepalived/check_nginx.sh << "EOF"
  2. #!/bin/bash
  3. count=$(ss -antp |grep 16443 |egrep -cv "grep|$$")
  4. if [ "$count" -eq 0 ];then
  5. exit 1
  6. else
  7. exit 0
  8. fi
  9. EOF
  10. chmod +x /etc/keepalived/check_nginx.sh

4. keepalived配置文件(Nginx Backup)

  1. cat > /etc/keepalived/keepalived.conf << EOF
  2. global_defs {
  3. notification_email {
  4. acassen@firewall.loc
  5. failover@firewall.loc
  6. sysadmin@firewall.loc
  7. }
  8. notification_email_from Alexandre.Cassen@firewall.loc
  9. smtp_server 127.0.0.1
  10. smtp_connect_timeout 30
  11. router_id NGINX_BACKUP
  12. }
  13. vrrp_script check_nginx {
  14. script "/etc/keepalived/check_nginx.sh"
  15. }
  16. vrrp_instance VI_1 {
  17. state BACKUP
  18. interface ens33
  19. virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的
  20. priority 90
  21. advert_int 1
  22. authentication {
  23. auth_type PASS
  24. auth_pass 1111
  25. }
  26. virtual_ipaddress {
  27. 192.168.31.88/24
  28. }
  29. track_script {
  30. check_nginx
  31. }
  32. }
  33. EOF

准备上述配置文件中检查nginx运行状态的脚本:

  1. cat > /etc/keepalived/check_nginx.sh << "EOF"
  2. #!/bin/bash
  3. count=$(ss -antp |grep 16443 |egrep -cv "grep|$$")
  4. if [ "$count" -eq 0 ];then
  5. exit 1
  6. else
  7. exit 0
  8. fi
  9. EOF
  10. chmod +x /etc/keepalived/check_nginx.sh

注:keepalived根据脚本返回状态码(0为工作正常,非0不正常)判断是否故障转移。

5. 启动并设置开机启动

  1. systemctl daemon-reload
  2. systemctl start nginx keepalived
  3. systemctl enable nginx keepalived

6. 查看keepalived工作状态

  1. ip addr
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. inet6 ::1/128 scope host
  7. valid_lft forever preferred_lft forever
  8. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  9. link/ether 00:0c:29:04:f7:2c brd ff:ff:ff:ff:ff:ff
  10. inet 192.168.31.80/24 brd 192.168.31.255 scope global noprefixroute ens33
  11. valid_lft forever preferred_lft forever
  12. inet 192.168.31.88/24 scope global secondary ens33
  13. valid_lft forever preferred_lft forever
  14. inet6 fe80::20c:29ff:fe04:f72c/64 scope link
  15. valid_lft forever preferred_lft forever

可以看到,在ens33网卡绑定了192.168.31.88 虚拟IP,说明工作正常。

7. Nginx+Keepalived高可用测试

关闭主节点Nginx,测试VIP是否漂移到备节点服务器。
在Nginx Master执行 pkill nginx;在Nginx Backup,ip addr命令查看已成功绑定VIP。

8. 访问负载均衡器测试

找K8s集群中任意一个节点,使用curl查看K8s版本测试,使用VIP访问:

  1. curl -k https://192.168.31.88:16443/version
  2. {
  3. "major": "1",
  4. "minor": "20",
  5. "gitVersion": "v1.20.4",
  6. "gitCommit": "e87da0bd6e03ec3fea7933c4b5263d151aafd07c",
  7. "gitTreeState": "clean",
  8. "buildDate": "2021-02-18T16:03:00Z",
  9. "goVersion": "go1.15.8",
  10. "compiler": "gc",
  11. "platform": "linux/amd64"
  12. }

可以正确获取到K8s版本信息,说明负载均衡器搭建正常。该请求数据流程:curl -> vip(nginx) -> apiserver
通过查看Nginx日志也可以看到转发apiserver IP:

  1. tail /var/log/nginx/k8s-access.log -f
  2. 192.168.31.71 192.168.31.71:6443 - [02/Apr/2021:19:17:57 +0800] 200 423
  3. 192.168.31.71 192.168.31.72:6443 - [02/Apr/2021:19:18:50 +0800] 200 423

7.3 修改所有Worker Node连接LB VIP

试想下,虽然我们增加了Master2 Node和负载均衡器,但是我们是从单Master架构扩容的,也就是说目前所有的Worker Node组件连接都还是Master1 Node,如果不改为连接VIP走负载均衡器,那么Master还是单点故障。
因此接下来就是要改所有Worker Node(kubectl get node命令查看到的节点)组件配置文件,由原来192.168.31.71修改为192.168.31.88(VIP)。
在所有Worker Node执行:

  1. sed -i 's#192.168.31.71:6443#192.168.31.88:16443#' /opt/kubernetes/cfg/*
  2. systemctl restart kubelet kube-proxy

检查节点状态:

  1. kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-master1 Ready <none> 32d v1.20.4
  4. k8s-master2 Ready <none> 10m v1.20.4
  5. k8s-node1 Ready <none> 31d v1.20.4
  6. k8s-node2 Ready <none> 31d v1.20.4