部署一套完整的K8s高可用集群(二進制)

整體架構

1639193639164.png

單Master服務器規劃

角色 IP 組件
k8s-m1 192.168.153.25 kube-apiserver,kube-controller-manager,kube-scheduler,etcd,kubelet,kube-proxy,docker
k8s-node1 192.168.153.26 kubelet,kube-proxy,docker

系統初始化設置

#設置主機名:
[root@localhost ~]# hostnamectl set-hostname k8s-m1
[root@localhost ~]# bash

#關閉防火墻
[root@k8s-m1 ~]# systemctl stop firewalld 
[root@k8s-m1 ~]# systemctl disable firewalld  

# 關閉selinux 
# 永久 
[root@k8s-m1 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config 
# 臨時 
[root@k8s-m1 ~]# setenforce 0 
#重啟后
[root@k8s-m1 ~]# getenforce
Disabled


#關閉swap:
#臨時
[root@k8s-m1 ~]# swapoff -a
#永久
[root@k8s-m1 ~]# vi /etc/fstab 
#UUID=ff5b128d-f66c-40a5-86d7-ce69d2c0da61 swap  swap    defaults        0 0

# 在master添加hosts 
[root@k8s-m1 ~]# cat >> /etc/hosts << EOF
192.168.153.25 k8s-m1
192.168.153.26 k8s-node1
192.168.153.27 k8s-m2
EOF


#將橋接的IPv4流量傳遞到iptables的鏈:
[root@k8s-m1 ~]# cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
#執行生效
[root@k8s-m1 ~]# sysctl --system

--遇到問題
sysctl --system并未出現相關配置
使用sysctl -p /etc/sysctl.d/k8s.conf,報錯:
sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-ip6tables: 沒有那個文件或目錄
sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-iptables: 沒有那個文件或目錄
--解決問題
[root@k8s-m1 ~]# modprobe br_netfilter
--然后重新執行
[root@k8s-m1 ~]# sysctl -p /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1

# 時間同步 
[root@k8s-m1 ~]# yum install ntpdate -y 
[root@k8s-m1 ~]# ntpdate time.windows.com

#開機時同步時間
[root@k8s-m1 ~]# vi /usr/lib/systemd/system/updatetime.service
--------------------------------------------------------------------------
[Unit]
Description=updatetime
[Service]
ExecStart=/usr/sbin/ntpdate time.windows.com
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
--------------------------------------------------------------------------
[root@k8s-m1 ~]# systemctl start updatetime
[root@k8s-m1 ~]# systemctl enable updatetime

#看日志:journalctl -u updatetime
        cat /var/log/messages |grep updatetime

部署Etcd集群

規劃

節點名稱 IP
etcd-1 192.168.153.25
etcd-2 192.168.153.26
etcd-3 192.168.153.27

準備cfssl證書生成工具

[root@k8s-m1 tls]# cp -rf cfssl cfssl-certinfo cfssljson /usr/local/bin
[root@k8s-m1 tls]# chmod +x /usr/local/bin/cfssl*

生成Etcd證書

自簽證書頒發機構(CA)

[root@k8s-m1 tls]# mkdir -p ~/TLS/{etcd,k8s}
[root@k8s-m1 ~]# cd ~/TLS/etcd


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

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


#生成證書:
[root@k8s-m1 etcd]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

#會生成ca.pem和ca-key.pem文件
[root@k8s-m1 etcd]# ls
ca-config.json  ca.csr  ca-csr.json  ca-key.pem  ca.pem

使用自簽CA簽發Etcd HTTPS證書

#創建證書申請文件

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


[root@k8s-m1 etcd]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server

#會生成 server-key.pem  server.pem

部署Etcd集群

創建工作目錄并解壓二進制包

[root@k8s-m1 ~]# mkdir /opt/etcd/{bin,cfg,ssl} -p
[root@k8s-m1 ~]# tar zxvf etcd-v3.4.9-linux-amd64.tar.gz
[root@k8s-m1 ~]# mv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/

創建etcd配置文件

cat > /opt/etcd/cfg/etcd.conf << EOF
#[Member]
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.153.25:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.153.25:2379"

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.153.25:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.153.25:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.153.25:2380,etcd-2=https://192.168.153.26:2380,etcd-3=https://192.168.153.27:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF

-------------------------------------------------------------
?   ETCD_NAME:節點名稱,集群中唯一
?   ETCD_DATA_DIR:數據目錄
?   ETCD_LISTEN_PEER_URLS:集群通信監聽地址
?   ETCD_LISTEN_CLIENT_URLS:客戶端訪問監聽地址
?   ETCD_INITIAL_ADVERTISE_PEERURLS:集群通告地址
?   ETCD_ADVERTISE_CLIENT_URLS:客戶端通告地址
?   ETCD_INITIAL_CLUSTER:集群節點地址
?   ETCD_INITIALCLUSTER_TOKEN:集群Token
?   ETCD_INITIALCLUSTER_STATE:加入集群的當前狀態,new是新集群,existing表示加入已有集群


systemd管理etcd

cat > /usr/lib/systemd/system/etcd.service << EOF
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
EnvironmentFile=/opt/etcd/cfg/etcd.conf
ExecStart=/opt/etcd/bin/etcd \
--cert-file=/opt/etcd/ssl/server.pem \
--key-file=/opt/etcd/ssl/server-key.pem \
--peer-cert-file=/opt/etcd/ssl/server.pem \
--peer-key-file=/opt/etcd/ssl/server-key.pem \
--trusted-ca-file=/opt/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/opt/etcd/ssl/ca.pem \
--logger=zap
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

拷貝剛才生成的證書

[root@k8s-m1 bin]# cp ~/TLS/etcd/ca*pem ~/TLS/etcd/server*pem /opt/etcd/ssl/
 
ca-key.pem  ca.pem  server-key.pem  server.pem

將上面節點1所有生成的文件拷貝到節點2和節點3

[root@k8s-m1 ~]# scp -r /opt/etcd/ root@192.168.153.26:/opt/
[root@k8s-m1 ~]# scp /usr/lib/systemd/system/etcd.service root@192.168.153.26:/usr/lib/systemd/system/

[root@k8s-m1 ~]# scp -r /opt/etcd/ root@192.168.153.27:/opt/
[root@k8s-m1 ~]# scp /usr/lib/systemd/system/etcd.service root@192.168.153.27:/usr/lib/systemd/system/

在節點2和節點3分別修改etcd.conf配置文件

# vi /opt/etcd/cfg/etcd.conf

#[Member]
ETCD_NAME="etcd-2"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.153.26:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.153.26:2379"

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.153.26:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.153.26:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.153.25:2380,etcd-2=https://192.168.153.26:2380,etcd-3=https://192.168.153.27:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"


#[Member]
ETCD_NAME="etcd-3"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.153.27:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.153.27:2379"

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.153.27:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.153.27:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.153.25:2380,etcd-2=https://192.168.153.26:2380,etcd-3=https://192.168.153.27:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

啟動并設置開機啟動[所有節點]

[root@k8s-m1 ~]# systemctl daemon-reload
[root@k8s-m1 ~]# systemctl start etcd
[root@k8s-m1 ~]# systemctl enable etcd

查看集群狀態

[root@k8s-m1 ~]# 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.153.25:2379,https://192.168.153.26:2379,https://192.168.153.27:2379" endpoint health --write-out=table
+-----------------------------+--------+-------------+-------+
|          ENDPOINT           | HEALTH |    TOOK     | ERROR |
+-----------------------------+--------+-------------+-------+
| https://192.168.153.27:2379 |   true | 16.155174ms |       |
| https://192.168.153.25:2379 |   true | 16.894839ms |       |
| https://192.168.153.26:2379 |   true | 17.295779ms |       |
+-----------------------------+--------+-------------+-------+

查看日志

如果有問題第一步先看日志:/var/log/message 或 journalctl -u etcd

安裝Docker

解壓二進制包

[root@k8s-m1 ~]# tar zxvf docker-19.03.9.tgz
[root@k8s-m1 ~]# mv docker/* /usr/bin

systemd管理docker

cat > /usr/lib/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target
EOF


創建配置文件

[root@k8s-m1 ~]# mkdir /etc/docker

[root@k8s-m1 ~]# vi /etc/docker/daemon.json
{
  "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}

?   registry-mirrors 阿里云鏡像加速器

啟動并設置開機啟動

[root@k8s-m1 ~]# systemctl daemon-reload
[root@k8s-m1 ~]# systemctl start docker
[root@k8s-m1 ~]# systemctl enable docker

部署Master

生成kube-apiserver證書

自簽證書頒發機構(CA)

[root@k8s-m1 ~]# cd ~/TLS/k8s

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


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

#生成證書,會生成ca.pem和ca-key.pem文件
[root@k8s-m1 k8s]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -


使用自簽CA簽發kube-apiserver HTTPS證書

cat > server-csr.json << EOF
{
    "CN": "kubernetes",
    "hosts": [
      "10.0.0.1",
      "127.0.0.1",
      "kubernetes",
      "kubernetes.default",
      "kubernetes.default.svc",
      "kubernetes.default.svc.cluster",
      "kubernetes.default.svc.cluster.local",
      "192.168.153.16",
      "192.168.153.17",
      "192.168.153.18",
      "192.168.153.19",
      "192.168.153.20",
      "192.168.153.21",
      "192.168.153.22",
      "192.168.153.25",
      "192.168.153.26",
      "192.168.153.27"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "BeiJing",
            "ST": "BeiJing",
            "O": "k8s",
            "OU": "System"
        }
    ]
}
EOF

#生產證書,會生成server.pem和server-key.pem文件
[root@k8s-m1 k8s]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server

解壓二進制包

[root@k8s-m1 k8s]# mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs} 
[root@k8s-m1 ~]# tar xzvf kubernetes-server-linux-amd64.tar.gz 
[root@k8s-m1 ~]# cd kubernetes/server/bin
[root@k8s-m1 bin]# cp kube-apiserver kube-scheduler kube-controller-manager /opt/kubernetes/bin
[root@k8s-m1 bin]# cp kubectl /usr/bin/

部署kube-apiserver

創建配置文件

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.153.25:2379,https://192.168.153.26:2379,https://192.168.153.27:2379 \\
--bind-address=192.168.153.25 \\
--secure-port=6443 \\
--advertise-address=192.168.153.25 \\
--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 \\
--service-account-issuer=api \\
--service-account-signing-key-file=/opt/kubernetes/ssl/server-key.pem \\
--etcd-cafile=/opt/etcd/ssl/ca.pem \\
--etcd-certfile=/opt/etcd/ssl/server.pem \\
--etcd-keyfile=/opt/etcd/ssl/server-key.pem \\
--requestheader-client-ca-file=/opt/kubernetes/ssl/ca.pem \\
--proxy-client-cert-file=/opt/kubernetes/ssl/server.pem \\
--proxy-client-key-file=/opt/kubernetes/ssl/server-key.pem \\
--requestheader-allowed-names=kubernetes \\
--requestheader-extra-headers-prefix=X-Remote-Extra- \\
--requestheader-group-headers=X-Remote-Group \\
--requestheader-username-headers=X-Remote-User \\
--enable-aggregator-routing=true \\
--audit-log-maxage=30 \\
--audit-log-maxbackup=3 \\
--audit-log-maxsize=100 \\
--audit-log-path=/opt/kubernetes/logs/k8s-audit.log"
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

拷貝剛才生成的證書

[root@k8s-m1 k8s]# cp ~/TLS/k8s/ca*pem ~/TLS/k8s/server*pem /opt/kubernetes/ssl/

啟用 TLS Bootstrapping 機制

#生產token
[root@k8s-m1 k8s]# head -c 16 /dev/urandom | od -An -t x | tr -d ' '
b61fe1f11d8e48a974399405a2af809e

#創建token文件
cat > /opt/kubernetes/cfg/token.csv << EOF
b61fe1f11d8e48a974399405a2af809e,kubelet-bootstrap,10001,"system:node-bootstrapper"
EOF


systemd管理apiserver

cat > /usr/lib/systemd/system/kube-apiserver.service << EOF
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes

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

[Install]
WantedBy=multi-user.target
EOF

啟動并設置開機啟動

[root@k8s-m1 k8s]# systemctl daemon-reload
[root@k8s-m1 k8s]# systemctl start kube-apiserver 
[root@k8s-m1 k8s]# systemctl enable kube-apiserver

部署kube-controller-manager

創建配置文件

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 \\
--kubeconfig=/opt/kubernetes/cfg/kube-controller-manager.kubeconfig \\
--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 \\
--cluster-signing-duration=87600h0m0s"
EOF

?   --kubeconfig:連接apiserver配置文件
?   --leader-elect:當該組件啟動多個時,自動選舉(HA)
?   --cluster-signing-cert-file/--cluster-signing-key-file:自動為kubelet頒發證書的CA,與apiserver保持一致

生成kubeconfig文件

#生成kube-controller-manager證書:

# 切換工作目錄
cd ~/TLS/k8s

# 創建證書請求文件
cat > kube-controller-manager-csr.json << EOF
{
  "CN": "system:kube-controller-manager",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "L": "BeiJing", 
      "ST": "BeiJing",
      "O": "system:masters",
      "OU": "System"
    }
  ]
}
EOF

# 生成證書
[root@k8s-m1 k8s]# 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

#生成兩個文件
kube-controller-manager.pem   kube-controller-manager-key.pem

生成kubeconfig文件(以下是shell命令,直接在終端執行)

KUBE_CONFIG="/opt/kubernetes/cfg/kube-controller-manager.kubeconfig"
KUBE_APISERVER="https://192.168.153.25:6443"

kubectl config set-cluster kubernetes \
  --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-credentials kube-controller-manager \
  --client-certificate=./kube-controller-manager.pem \
  --client-key=./kube-controller-manager-key.pem \
  --embed-certs=true \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-context default \
  --cluster=kubernetes \
  --user=kube-controller-manager \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}

systemd管理controller-manager

cat > /usr/lib/systemd/system/kube-controller-manager.service << EOF
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes

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

[Install]
WantedBy=multi-user.target
EOF

啟動并設置開機啟動

[root@k8s-m1 k8s]# systemctl daemon-reload
[root@k8s-m1 k8s]# systemctl start kube-controller-manager
[root@k8s-m1 k8s]# systemctl enable kube-controller-manager

部署kube-scheduler

創建配置文件

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


?   --kubeconfig:連接apiserver配置文件
?   --leader-elect:當該組件啟動多個時,自動選舉(HA)


生成kubeconfig文件

#生成kube-scheduler證書:
# 切換工作目錄
[root@k8s-m1 k8s]# cd ~/TLS/k8s

# 創建證書請求文件
cat > kube-scheduler-csr.json << EOF
{
  "CN": "system:kube-scheduler",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "L": "BeiJing",
      "ST": "BeiJing",
      "O": "system:masters",
      "OU": "System"
    }
  ]
}
EOF

# 生成證書
[root@k8s-m1 k8s]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-scheduler-csr.json | cfssljson -bare kube-scheduler

#生成兩個文件
kube-scheduler.pem kube-scheduler-key.pem

生成kubeconfig文件(以下是shell命令,直接在終端執行)

KUBE_CONFIG="/opt/kubernetes/cfg/kube-scheduler.kubeconfig"
KUBE_APISERVER="https://192.168.153.25:6443"

kubectl config set-cluster kubernetes \
  --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-credentials kube-scheduler \
  --client-certificate=./kube-scheduler.pem \
  --client-key=./kube-scheduler-key.pem \
  --embed-certs=true \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-context default \
  --cluster=kubernetes \
  --user=kube-scheduler \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}


systemd管理scheduler

cat > /usr/lib/systemd/system/kube-scheduler.service << EOF
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes

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

[Install]
WantedBy=multi-user.target
EOF


啟動并設置開機啟動

[root@k8s-m1 k8s]# systemctl daemon-reload
[root@k8s-m1 k8s]# systemctl start kube-scheduler
[root@k8s-m1 k8s]# systemctl enable kube-scheduler

查看集群狀態

#生成kubectl連接集群的證書

# 切換工作目錄
[root@k8s-m1 k8s]# cd ~/TLS/k8s

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


[root@k8s-m1 k8s]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin



生成kubeconfig文件

mkdir /root/.kube

KUBE_CONFIG="/root/.kube/config"
KUBE_APISERVER="https://192.168.153.25:6443"

kubectl config set-cluster kubernetes \
  --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-credentials cluster-admin \
  --client-certificate=./admin.pem \
  --client-key=./admin-key.pem \
  --embed-certs=true \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-context default \
  --cluster=kubernetes \
  --user=cluster-admin \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}


查看當前集群組件狀態

[root@k8s-m1 ~]# kubectl get cs

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

#如上輸出說明Master節點組件運行正常
#如不正常,同步時間,重啟相關服務

授權kubelet-bootstrap用戶允許請求證書

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

  kubectl create--insecure-skip-tls-verifyclusterrolebinding kubelet-bootstrap \ 
  --clusterrole=system:node-bootstrapper \ 
  --user=kubelet-bootstrap
 

部署Worker Node

創建工作目錄并拷貝二進制文件

所有worker node創建工作目錄

[root@k8s-node1 ~]# mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs} 

下面還是在Master Node上操作,即同時作為WorkerNode

[root@k8s-m1 ~]# cd kubernetes/server/bin
[root@k8s-m1 bin]# cp kubelet kube-proxy /opt/kubernetes/bin

部署kubelet

創建配置文件

cat > /opt/kubernetes/cfg/kubelet.conf << EOF
KUBELET_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--hostname-override=k8s-m1 \\
--network-plugin=cni \\
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \\
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \\
--config=/opt/kubernetes/cfg/kubelet-config.yml \\
--cert-dir=/opt/kubernetes/ssl \\
--pod-infra-container-image=lizhenliang/pause-amd64:3.0"
EOF

?   --hostname-override:顯示名稱,集群中唯一
?   --network-plugin:啟用CNI
?   --kubeconfig:空路徑,會自動生成,后面用于連接apiserver
?   --bootstrap-kubeconfig:首次啟動向apiserver申請證書
?   --config:配置參數文件
?   --cert-dir:kubelet證書生成目錄
?   --pod-infra-container-image:管理Pod網絡容器的鏡像


配置參數文件

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


生成kubelet初次加入集群引導kubeconfig文件

KUBE_CONFIG="/opt/kubernetes/cfg/bootstrap.kubeconfig"
KUBE_APISERVER="https://192.168.153.25:6443" # apiserver IP:PORT
TOKEN="b61fe1f11d8e48a974399405a2af809e" # 與token.csv里保持一致

# 生成 kubelet bootstrap kubeconfig 配置文件
kubectl config set-cluster kubernetes \
  --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-credentials "kubelet-bootstrap" \
  --token=${TOKEN} \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-context default \
  --cluster=kubernetes \
  --user="kubelet-bootstrap" \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}


systemd管理kubelet

cat > /usr/lib/systemd/system/kubelet.service << EOF
[Unit]
Description=Kubernetes Kubelet
After=docker.service

[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet.conf
ExecStart=/opt/kubernetes/bin/kubelet \$KUBELET_OPTS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF


啟動并設置開機啟動

[root@k8s-m1 ~]# systemctl daemon-reload
[root@k8s-m1 ~]# systemctl start kubelet
[root@k8s-m1 ~]# systemctl enable kubelet

批準kubelet證書申請并加入集群

# 查看kubelet證書請求

[root@k8s-m1 ~]# kubectl get csr
NAME                                                   AGE   SIGNERNAME                                    REQUESTOR           CONDITION
node-csr-MO8PB_glfjCy_w6JKHJD5x89D1rUXq6UTOoXfdXWwTc   2s    kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Pending

# 批準申請
[root@k8s-m1 ~]# kubectl certificate approve node-csr-MO8PB_glfjCy_w6JKHJD5x89D1rUXq6UTOoXfdXWwTc

#再次查看,Approved,Issued為正常
[root@k8s-m1 ~]# kubectl get csr
NAME                                                   AGE   SIGNERNAME                                    REQUESTOR           CONDITION
node-csr-MO8PB_glfjCy_w6JKHJD5x89D1rUXq6UTOoXfdXWwTc   65m   kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Approved,Issued


[root@k8s-m1 ~]# kubectl get node
NAME     STATUS     ROLES    AGE     VERSION
k8s-m1   NotReady   <none>   6m28s   v1.20.4

部署kube-proxy

創建配置文件

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


配置參數文件

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


生成kube-proxy.kubeconfig文件

# 切換工作目錄
cd ~/TLS/k8s

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

# 生成證書
[root@k8s-m1 k8s]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy


#生成kubeconfig文件:
KUBE_CONFIG="/opt/kubernetes/cfg/kube-proxy.kubeconfig"
KUBE_APISERVER="https://192.168.153.25:6443"

kubectl config set-cluster kubernetes \
  --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-credentials kube-proxy \
  --client-certificate=./kube-proxy.pem \
  --client-key=./kube-proxy-key.pem \
  --embed-certs=true \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-context default \
  --cluster=kubernetes \
  --user=kube-proxy \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}


systemd管理kube-proxy

cat > /usr/lib/systemd/system/kube-proxy.service << EOF
[Unit]
Description=Kubernetes Proxy
After=network.target

[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-proxy.conf
ExecStart=/opt/kubernetes/bin/kube-proxy \$KUBE_PROXY_OPTS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF


啟動并設置開機啟動

[root@k8s-m1 k8s]# systemctl daemon-reload
[root@k8s-m1 k8s]# systemctl start kube-proxy
[root@k8s-m1 k8s]# systemctl enable kube-proxy

部署網絡組件

[root@k8s-m1 ~]# mkdir -p /opt/cni/bin
[root@k8s-m1 ~]# tar zxvf cni-plugins-linux-amd64-v0.8.6.tgz -C /opt/cni/bin

[root@k8s-m1 YAML]# kubectl apply -f kube-flannel.yaml 

[root@k8s-m1 YAML]# kubectl get pods -n kube-system
NAME                          READY   STATUS    RESTARTS   AGE
kube-flannel-ds-amd64-n4pg6   1/1     Running   0          2m24s

[root@k8s-m1 YAML]# kubectl get node
NAME     STATUS   ROLES    AGE   VERSION
k8s-m1   Ready    <none>   27m   v1.20.4


授權apiserver訪問kubelet

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

[root@k8s-m1 YAML]# kubectl apply -f apiserver-to-kubelet-rbac.yaml


新增加Worker Node

在所有worker node創建工作目錄

[root@k8s-node1 ~]# mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs} 

拷貝已部署好的Node相關文件到新節點

[root@k8s-m1 ~]# scp -r /opt/kubernetes root@192.168.153.26:/opt/

[root@k8s-m1 ~]# scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@192.168.153.26:/usr/lib/systemd/system

[root@k8s-m1 ~]# scp -r /opt/cni/ root@192.168.153.26:/opt/

[root@k8s-m1 ~]# scp /opt/kubernetes/ssl/ca.pem root@192.168.153.26:/opt/kubernetes/ssl


刪除kubelet證書和kubeconfig文件

[root@k8s-node1 ~]# rm -f /opt/kubernetes/cfg/kubelet.kubeconfig 
[root@k8s-node1 ~]# rm -f /opt/kubernetes/ssl/kubelet*

注:這幾個文件是證書申請審批后自動生成的,每個Node不同,必須刪除

修改主機名

[root@k8s-node1 ~]# vi /opt/kubernetes/cfg/kubelet.conf
--hostname-override=k8s-node1

[root@k8s-node1 ~]# vi /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: k8s-node1


啟動并設置開機啟動

[root@k8s-node1 ~]# systemctl daemon-reload
[root@k8s-node1 ~]# systemctl start kubelet kube-proxy
[root@k8s-node1 ~]# systemctl enable kubelet kube-proxy

在Master上批準新Node kubelet證書申請

# 查看證書請求
[root@k8s-m1 ~]# kubectl get csr
NAME                                                   AGE     SIGNERNAME                                    REQUESTOR           CONDITION
node-csr-KEU59N0A2o_0k5FK37DxQbs0-stkWpY33oLVJeYT5aU   71s     kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Pending
node-csr-Z8rR8y-Mq2Gdc-rRr7AhTjvssM4OuZ2qVYVZ7sNtMjs   3h32m   kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Approved,Issued

# 授權請求
[root@k8s-m1 ~]# kubectl certificate approve node-csr-KEU59N0A2o_0k5FK37DxQbs0-stkWpY33oLVJeYT5aU


查看Node狀態

[root@k8s-m1 ~]# kubectl get node
NAME        STATUS   ROLES    AGE     VERSION
k8s-m1      Ready    <none>   3h39m   v1.20.4
k8s-node1   Ready    <none>   99s     v1.20.4

部署Dashboard和CoreDNS

部署Dashboard

[root@k8s-m1 YAML]# kubectl apply -f dashboard.yaml   

[root@k8s-m1 YAML]# kubectl apply -f dashboard-adminuser.yaml 

[root@k8s-m1 YAML]# kubectl get pods,svc -n kubernetes-dashboard
NAME                                             READY   STATUS    RESTARTS   AGE
pod/dashboard-metrics-scraper-7b9b99d599-slmmr   1/1     Running   0          5m33s
pod/kubernetes-dashboard-6d4799d74-9fqq4         1/1     Running   0          5m33s

NAME                                TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)         AGE
service/dashboard-metrics-scraper   ClusterIP   10.0.0.189   <none>        8000/TCP        5m33s
service/kubernetes-dashboard        NodePort    10.0.0.39    <none>        443:30001/TCP   5m33s

[root@k8s-m1 YAML]# kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

eyJhbGciOiJSUzI1NiIsImtpZCI6Ikdfd3YySzdYdmxOVWFnSFJiWW5BYXlIeEhYazdYNE4wbmZlT21fN0p0akEifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJmbGFubmVsLXRva2VuLXp2d2p0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImZsYW5uZWwiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJlYzExYzAyYS1hZGVlLTQ4ZDktYmE3NS1iMDg0ODhjMWMwMzIiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06Zmxhbm5lbCJ9.fESAKVNFGzzlGmJSF9yemPUno8AGBmzof0a42ARgCMR8l41tPme1clRERigoSG7R16FgUVFJcYwK06OhQD6MCFt08t7yq0c-pf5Xott_jrig3zYj-nfuN_vbztiyQoXIMT3k2m3sWeJHP0j73glENYb2P7Pi8Dn8OotofUB81x_cQK5Xswz5_GIgrdgThuW3c8ocRavfNEO2NcFhzhpIxyK8rI70TUp_TDsjWUjMz5Au5fGfFoNBsRsP_0aaBy3hfK-sBRzI0Tq-0qEL6MdYETza1pxUB0lkfyvFIIbnUswTxyCWtjHUYDandyx9hYhKsroOdEPv8xZtnPqBfBV2Lw


https://192.168.153.25:30001/

1639118894166.png

部署CoreDNS

[root@k8s-m1 YAML]# kubectl apply -f coredns.yaml 

[root@k8s-m1 YAML]# kubectl get pods -n kube-system  
NAME                          READY   STATUS    RESTARTS   AGE
coredns-58d8cd457b-t4xqp      1/1     Running   0          27s
kube-flannel-ds-amd64-hhswr   1/1     Running   0          17m
kube-flannel-ds-amd64-n4pg6   1/1     Running   1          3h31m

[root@k8s-m1 YAML]# kubectl run -it --rm dns-test --image=busybox:1.28.4 sh 
If you don't see a command prompt, try pressing enter.
/ # nslookup kubernetes 
Server:    10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local

Name:      kubernetes
Address 1: 10.0.0.1 kubernetes.default.svc.cluster.local

至此一個單Master集群就搭建完成了!

擴容多Master(高可用架構)

多Master服務器規劃

角色 IP 組件
k8s-m1 192.168.153.25 kube-apiserver,kube-controller-manager,kube-scheduler,etcd,kubelet,kube-proxy,docker,nginx,keepalived
k8s-node1 192.168.153.26 kubelet,kube-proxy,docker
k8s-m2 192.168.153.27 kube-apiserver,kube-controller-manager,kube-scheduler,kubelet,kube-proxy,docker,nginx,keepalived

部署Master2 Node

安裝Docker

[root@k8s-m1 ~]# scp /usr/bin/docker* root@192.168.153.27:/usr/bin
[root@k8s-m1 ~]# scp /usr/bin/runc root@192.168.153.27:/usr/bin
[root@k8s-m1 ~]# scp /usr/bin/containerd* root@192.168.153.27:/usr/bin
[root@k8s-m1 ~]# scp /usr/lib/systemd/system/docker.service root@192.168.153.27:/usr/lib/systemd/system
[root@k8s-m1 ~]# scp -r /etc/docker root@192.168.153.27:/etc

# 在Master2啟動Docker
systemctl daemon-reload
systemctl start docker
systemctl enable docker


Master2創建etcd證書目錄

[root@k8s-m2 ~]# mkdir -p /opt/etcd/ssl

拷貝Master1上所有K8s文件和etcd證書到Master2

[root@k8s-m1 ~]# scp -r /opt/kubernetes root@192.168.153.27:/opt
[root@k8s-m1 ~]# scp -r /opt/cni/ root@192.168.153.27:/opt
[root@k8s-m1 ~]# scp -r /opt/etcd/ssl root@192.168.153.27:/opt/etcd
[root@k8s-m1 ~]# scp /usr/lib/systemd/system/kube* root@192.168.153.27:/usr/lib/systemd/system
[root@k8s-m1 ~]# scp /usr/bin/kubectl  root@192.168.153.27:/usr/bin
[root@k8s-m1 ~]# scp -r ~/.kube root@192.168.153.27:~


刪除證書文件

#刪除kubelet證書和kubeconfig文件

[root@k8s-m2 ~]# rm -f /opt/kubernetes/cfg/kubelet.kubeconfig 
[root@k8s-m2 ~]# rm -f /opt/kubernetes/ssl/kubelet*


修改配置文件IP和主機名

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

[root@k8s-m2 ~]# vi /opt/kubernetes/cfg/kube-apiserver.conf 
...
--bind-address=192.168.153.27 \
--advertise-address=192.168.153.27 \
...

[root@k8s-m2 ~]# vi /opt/kubernetes/cfg/kube-controller-manager.kubeconfig
server: https://192.168.153.27:6443

[root@k8s-m2 ~]# vi /opt/kubernetes/cfg/kube-scheduler.kubeconfig
server: https://192.168.153.27:6443

[root@k8s-m2 ~]# vi /opt/kubernetes/cfg/kubelet.conf
--hostname-override=k8s-m2

[root@k8s-m2 ~]# vi /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: k8s-m2

[root@k8s-m2 ~]# vi ~/.kube/config
...
server: https://192.168.153.27:6443

啟動設置開機啟動

[root@k8s-m2 ~]# systemctl daemon-reload
[root@k8s-m2 ~]# systemctl start kube-apiserver kube-controller-manager kube-scheduler kubelet kube-proxy
[root@k8s-m2 ~]# systemctl enable kube-apiserver kube-controller-manager kube-scheduler kubelet kube-proxy


查看集群狀態

[root@k8s-m2 ~]# kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok                  
scheduler            Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   

批準kubelet證書申請

# 查看證書請求
[root@k8s-m2 ~]# kubectl get csr
NAME                                                   AGE     SIGNERNAME                                    REQUESTOR           CONDITION
node-csr-Q8r8Z2xb8lfqlFmWZEGcoeHJV1Dcz-DZM7TO5m8zN-w   2m22s   kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Pending

# 授權請求
[root@k8s-m2 ~]# kubectl certificate approve node-csr-Q8r8Z2xb8lfqlFmWZEGcoeHJV1Dcz-DZM7TO5m8zN-w

# 查看Node
[root@k8s-m2 ~]# kubectl get node
NAME        STATUS   ROLES    AGE     VERSION
k8s-m1      Ready    <none>   5h59m   v1.20.4
k8s-m2      Ready    <none>   3m20s   v1.20.4
k8s-node1   Ready    <none>   140m    v1.20.4

部署Nginx+Keepalived高可用負載均衡器

1639139216145.png

安裝軟件包(主/備)

 yum install epel-release -y
 yum install nginx keepalived -y
 yum install nginx-mod-stream

Nginx配置文件(主/備一樣)

cat > /etc/nginx/nginx.conf << "EOF"
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

# 四層負載均衡,為兩臺Master apiserver組件提供負載均衡
stream {

    log_format  main  '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';

    access_log  /var/log/nginx/k8s-access.log  main;

    upstream k8s-apiserver {
       server 192.168.153.25:6443;   # Master1 APISERVER IP:PORT
       server 192.168.153.27:6443;   # Master2 APISERVER IP:PORT
    }
    
    server {
       listen 16443; # 由于nginx與master節點復用,這個監聽端口不能是6443,否則會沖突
       proxy_pass k8s-apiserver;
    }
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    server {
        listen       80 default_server;
        server_name  _;

        location / {
        }
    }
}
EOF


keepalived配置文件(Nginx Master)

cat > /etc/keepalived/keepalived.conf << EOF
global_defs { 
   notification_email { 
     acassen@firewall.loc 
     failover@firewall.loc 
     sysadmin@firewall.loc 
   } 
   notification_email_from Alexandre.Cassen@firewall.loc  
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30 
   router_id NGINX_MASTER
} 

vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
}

vrrp_instance VI_1 { 
    state MASTER 
    interface ens32  # 修改為實際網卡名
    virtual_router_id 51 # VRRP 路由 ID實例,每個實例是唯一的 
    priority 100    # 優先級,備服務器設置 90 
    advert_int 1    # 指定VRRP 心跳包通告間隔時間,默認1秒 
    authentication { 
        auth_type PASS      
        auth_pass 1111 
    }  
    # 虛擬IP
    virtual_ipaddress { 
        192.168.153.16/24
    } 
    track_script {
        check_nginx
    } 
}
EOF

#畫重點:虛擬IP必須是證書里授權的

keepalived配置文件(Nginx Backup)

cat > /etc/keepalived/keepalived.conf << EOF
global_defs { 
   notification_email { 
     acassen@firewall.loc 
     failover@firewall.loc 
     sysadmin@firewall.loc 
   } 
   notification_email_from Alexandre.Cassen@firewall.loc  
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30 
   router_id NGINX_BACKUP
} 

vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
}

vrrp_instance VI_1 { 
    state BACKUP 
    interface ens32
    virtual_router_id 51 # VRRP 路由 ID實例,每個實例是唯一的 
    priority 90
    advert_int 1
    authentication { 
        auth_type PASS      
        auth_pass 1111 
    }  
    virtual_ipaddress { 
        192.168.153.16/24
    } 
    track_script {
        check_nginx
    } 
}
EOF

#畫重點:虛擬IP必須是證書里授權的

nginx運行狀態的腳本(主/備)

cat > /etc/keepalived/check_nginx.sh  << "EOF"
#!/bin/bash
count=$(ss -antp |grep 16443 |egrep -cv "grep|$$")

if [ "$count" -eq 0 ];then
    exit 1
else
    exit 0
fi
EOF

chmod +x /etc/keepalived/check_nginx.sh

#注:keepalived根據腳本返回狀態碼(0為工作正常,非0不正常)判斷是否故障轉移


啟動并設置開機啟動(主/備)

systemctl daemon-reload
systemctl start nginx keepalived
systemctl enable nginx keepalived


查看keepalived工作狀態

[root@k8s-m2 cfg]# ip addr

2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:f9:3d:cf brd ff:ff:ff:ff:ff:ff
    inet 192.168.153.27/24 brd 192.168.153.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet 192.168.153.16/24 scope global secondary ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fef9:3dcf/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

訪問負載均衡器測試

#找K8s集群中任意一個節點,使用curl查看K8s版本測試,使用VIP訪問:
curl -k https://192.168.153.16:16443/version

{
  "major": "1",
  "minor": "20",
  "gitVersion": "v1.20.4",
  "gitCommit": "e87da0bd6e03ec3fea7933c4b5263d151aafd07c",
  "gitTreeState": "clean",
  "buildDate": "2021-02-18T16:03:00Z",
  "goVersion": "go1.15.8",
  "compiler": "gc",
  "platform": "linux/amd64"
}

#訪問192.168.153.16:16443后映射到192.168.153.25:6443和192.168.153.27:6443
[root@k8s-m2 ~]# tail /var/log/nginx/k8s-access.log -f
192.168.153.25 192.168.153.25:6443 - [10/Dec/2021:23:12:19 +0800] 200 422
192.168.153.25 192.168.153.27:6443 - [10/Dec/2021:23:12:30 +0800] 200 422

修改所有Worker Node連接LB VIP(所有節點執行)

 

[root@k8s-m1 ~]# sed -i 's#192.168.153.25:6443#192.168.153.16:16443#' /opt/kubernetes/cfg/*
[root@k8s-m1 ~]# systemctl restart kubelet kube-proxy

[root@k8s-node1 ~]# sed -i 's#192.168.153.25:6443#192.168.153.16:16443#' /opt/kubernetes/cfg/*
[root@k8s-node1 ~]# systemctl restart kubelet kube-proxy

[root@k8s-m2 ~]# sed -i 's#192.168.153.27:6443#192.168.153.16:16443#' /opt/kubernetes/cfg/*
[root@k8s-m2 ~]# systemctl restart kubelet kube-proxy

 

檢查節點狀態

[root@k8s-m1 ~]# kubectl get node
NAME        STATUS   ROLES    AGE     VERSION
k8s-m1      Ready    <none>   11h     v1.20.4
k8s-m2      Ready    <none>   5h32m   v1.20.4
k8s-node1   Ready    <none>   7h50m   v1.20.4

[root@k8s-m2 ~]# kubectl get node
NAME        STATUS   ROLES    AGE     VERSION
k8s-m1      Ready    <none>   11h     v1.20.4
k8s-m2      Ready    <none>   5h32m   v1.20.4
k8s-node1   Ready    <none>   7h50m   v1.20.4

訪問測試

[root@k8s-m1 ~]# kubectl create deployment web --image=nginx

[root@k8s-m1 ~]# kubectl expose deployment web --port=80 --target-port=80 --type=NodePort

[root@k8s-m1 ~]# kubectl get pod,svc
NAME                       READY   STATUS    RESTARTS   AGE
pod/web-6bc4dfc596-hj2vl   1/1     Running   0          30s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP        12h
service/web          NodePort    10.0.0.165   <none>        80:32248/TCP   3s

#每訪問一次可以查看到日志
[root@k8s-m1 ~]# kubectl logs pod/web-6bc4dfc596-hj2vl -f

  

日常查看日志

比如:執行命令`kubectl apply -f nginx-deployment.yaml`并沒有創建Pod
查看日志,尋找原因
使用kubectl apply -f nginx-deployment.yaml命令后,會交給api-server,然后以對象形式存到etcd里,這時候kube-controller-manager會通過循環的方式來編排工作,創建相應的Pod。所以我們應該看 kube-controller-manager服務的日志。
001 我們使用journalctl查看使用systemctl啟動的service。
 journalctl -u kube-controller-manager 
002 查看系統日志
 tail -f /var/log/messages

至此,一套完整的 Kubernetes 高可用集群就部署完成了!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
禁止轉載,如需轉載請通過簡信或評論聯系作者。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,606評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,582評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,540評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,028評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,801評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,223評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,294評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,442評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,976評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,800評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,996評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,543評論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,233評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,926評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,702評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,991評論 2 374

推薦閱讀更多精彩內容