0. 服務(wù)器配置
IP | host_name | ROLES |
---|---|---|
172.27.216.27 | k8s-master | master |
172.27.216.28 | k8s-node1 | node(01) |
172.16.70.32 | k8s-node2 | node(02) |
1. 初始化linux配置:
需要在搭建k8s的服務(wù)器上都要執(zhí)行
# 關(guān)閉防火墻:
systemctl stop firewalld
systemctl disable firewalld
#關(guān)閉 selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 臨時
#關(guān)閉 swap
swapoff -a # 臨時
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
# 重啟
reboot
#根據(jù)規(guī)劃設(shè)置各個主機名(要與hosts文件對應(yīng)上)
hostnamectl set-hostname master
在master中將ip設(shè)置到hosts文件中
cat >> /etc/hosts << EOF
172.27.216.27 k8s-master
172.27.216.28 k8s-node1
172.16.70.32 k8s-node2
EOF
# 將橋接的IPV4流量傳遞到iptables鏈
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness = 0
EOF
sysctl --system # 生效
# 加載br_netfilter模塊
modprobe br_netfilter
# 查看是否加載
lsmod | grep br_netfilter
# 生效
sysctl --system
# 時間同步(注: 需要將ntpd關(guān)掉)
yum install ntpdate -y
ntpdate time.nist.gov
#開啟ipvs
yum -y install ipset ipvsadm
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
# 授權(quán)、運行池颈、檢查是否加載
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
# 檢查是否加載
lsmod | grep -e ipvs -e nf_conntrack_ipv4
2. kubeadm命令部署
2.1 安裝docker
1. 安裝Docker:
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
# 下載對應(yīng)的版本
yum -y install docker-ce-18.06.3.ce-3.el7
systemctl enable docker && systemctl start docker
docker version
2. 設(shè)置docker加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
3. 添加阿里云的YUM軟件源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
4. 安裝kubeadm尾序、kubelet和kubectl
# 安裝
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
# 為了實現(xiàn)Docker使用的cgroup drvier和kubelet使用的cgroup drver一致,建議修改"/etc/sysconfig/kubelet"文件的內(nèi)容:
vim /etc/sysconfig/kubelet
# 修改
KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"
# 設(shè)置為開機啟動即可
systemctl enable kubelet
5.部署k8s的Master節(jié)點(只在master節(jié)點執(zhí)行)
kubeadm init \
--apiserver-advertise-address=172.27.216.27 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
# 在master節(jié)點執(zhí)行躯砰,這里的命令是kubeadm init命令執(zhí)行后每币,提示出來的
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
6. 將node節(jié)點加入到master節(jié)點
# 在node節(jié)點中執(zhí)行(本條命令,在執(zhí)行完kubeadm init會打印出)
kubeadm join 172.27.216.27:6443 --token 4bwnx3.kvc4ans3r5zbwtw3 --discovery-token-ca-cert-hash sha256:7aeddf6aeb2c4e901497ab6a779333238f3a57e8bd06a2bf492467a0619bb765
#檢查節(jié)點狀況
kubectl get nodes
默認的token有效期為24h, 過期后琢歇,可以使用如下命令進行生成和添加
kubeadm token create --print-join-command
7.部署CNI網(wǎng)絡(luò)插件(只在master執(zhí)行)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 查看部署CNI網(wǎng)絡(luò)插件速度
kubectl get pods -n kube-system
# 在狀態(tài)都為Running時兰怠,查看節(jié)點狀態(tài)
kubectl get nodes
##以下為打印結(jié)果, status權(quán)威ready
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 33m v1.18.0
k8s-node1 Ready <none> 30m v1.18.0
k8s-node2 Ready <none> 30m v1.18.0
3. 二進制部署(比較繁瑣,極易出錯)
1. 部署etcd集群
1.1 準備除非雙手了證書生成工具
cfssl
是一個開源的證書管理工具李茫,使用 json 文件生成證書揭保,相比 openssl 更方便使用。 找任意一臺服務(wù)器操作魄宏,這里用 Master 節(jié)點秸侣。
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
mv cfssl_linux-amd64 /usr/local/bin/cfssl
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
- 生成ETCD證書
#創(chuàng)建工作目錄:
自簽 CA:
mkdir -p ~/TLS/{etcd,k8s} && 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
# 生成證書:
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
# 檢查
ls *pem
-
使用自簽 CA 簽發(fā) Etcd HTTPS 證書
創(chuàng)建證書申請文件:
cat > server-csr.json<< EOF
{
"CN":"etcd",
"hosts":[
"172.27.216.27",
"172.16.70.32",
"172.27.216.28"
],
"key":{
"algo":"rsa",
"size":2048
},
"names":[
{
"C":"CN",
"L":"BeiJing",
"ST":"BeiJing"
}
]
}
EOF
- 生成證書:
# 生成證書命令
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json - profile=www server-csr.json | cfssljson -bare server
# 檢查
ls server*pem
2 安裝和部署ETCD
- 下載etcd源碼
wget https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
-
創(chuàng)建工作目錄并解壓二進制文件
mkdir /opt/etcd/{bin,cfg,ssl} –p tar zxvf etcd-v3.4.9-linux-amd64.tar.gz mv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
-
創(chuàng)建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://172.27.216.27:2380" ETCD_LISTEN_CLIENT_URLS="https://172.27.216.27:2379,http://127.0.0.1:2379" #[Clustering] ETCD_INITIAL_ADVERTISE_PEER_URLS="https://172.27.216.27:2380" ETCD_ADVERTISE_CLIENT_URLS="https://172.27.216.27:2379" ETCD_INITIAL_CLUSTER="etcd-1=https://172.27.216.27:2380,etcd-2=https://172.27.216.28:2380,etcd-3=https://172.16.70.32:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_INITIAL_CLUSTER_STATE="new" #[Security] ETCD_CERT_FILE="/opt/etcd/ssl/server.pem" ETCD_KEY_FILE="/opt/etcd/ssl/server-key.pem" ETCD_TRUSTED_CA_FILE="/opt/etcd/ssl/ca.pem" ETCD_CLIENT_CERT_AUTH="true" ETCD_PEER_CERT_FILE="/opt/etcd/ssl/server.pem" ETCD_PEER_KEY_FILE="/opt/etcd/ssl/server-key.pem" ETCD_PEER_TRUSTED_CA_FILE="/opt/etcd/ssl/ca.pem" ETCD_PEER_CLIENT_CERT_AUTH="true" EOF #########以下是注解########## ETCD_NAME:節(jié)點名稱,集群中唯一 ETCD_DATA_DIR:數(shù)據(jù)目錄 ETCD_LISTEN_PEER_URLS:集群通信監(jiān)聽地址 ETCD_LISTEN_CLIENT_URLS:客戶端訪問監(jiān)聽地址 ETCD_INITIAL_ADVERTISE_PEER_URLS:集群通告地址 ETCD_ADVERTISE_CLIENT_URLS:客戶端通告地址 ETCD_INITIAL_CLUSTER:集群節(jié)點地址 ETCD_INITIAL_CLUSTER_TOKEN:集群 Token ETCD_INITIAL_CLUSTER_STATE:加入集群的當前狀態(tài)宠互,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 --name=${ETCD_NAME} --data-dir=${ETCD_DATA_DIR} --listen-peer-urls=${ETCD_LISTEN_PEER_URLS} --listen-client-urls=${ETCD_LISTEN_CLIENT_URLS} --advertise-client-urls=${ETCD_ADVERTISE_CLIENT_URLS} --initial-advertise-peer-urls=${ETCD_INITIAL_ADVERTISE_PEER_URLS} --initial-cluster=${ETCD_INITIAL_CLUSTER} --initial-cluster-token=${ETCD_INITIAL_CLUSTER_TOKEN} --initial-cluster-state=${ETCD_INITIAL_CLUSTER_STATE} --cert-file=${ETCD_CERT_FILE} --key-file=${ETCD_KEY_FILE} --peer-cert-file=${ETCD_PEER_CERT_FILE} --peer-key-file=${ETCD_PEER_KEY_FILE} --trusted-ca-file=${ETCD_TRUSTED_CA_FILE} --client-cert-auth=${ETCD_CLIENT_CERT_AUTH} --peer-client-cert-auth=${ETCD_PEER_CLIENT_CERT_AUTH} --peer-trusted-ca-file=${ETCD_PEER_TRUSTED_CA_FILE} Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target EOF
-
拷貝剛才用命令生成的證書到指定的目錄
cp ~/TLS/etcd/ca*pem ~/TLS/etcd/server*pem /opt/etcd/ssl/
-
啟動并設(shè)置開機啟動
systemctl daemon-reload systemctl start etcd systemctl enable etcd
將上述配置文件copy到其他機器當中,<u>并修改成其對應(yīng)的設(shè)置</u>予跌,然后啟動服務(wù)服務(wù)搏色。
-
查看etcd狀態(tài)
systemctl status etcd # 命令 ####### 以下是返回值 ####### ● etcd.service - Etcd Server Loaded: loaded (/usr/lib/systemd/system/etcd.service; enabled; vendor preset: disabled) Active: active (running) since 五 2021-07-09 12:04:28 CST; 39min ago Main PID: 2006 (etcd) CGroup: /system.slice/etcd.service └─2006 /opt/etcd/bin/etcd
-
查看成員
/opt/etcd/bin/etcdctl member list --write-out=table # 命令 ###### 返回值##### +------------------+---------+--------+----------------------------+----------------------------+------------+ | ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER | +------------------+---------+--------+----------------------------+----------------------------+------------+ | 8c56ace40c5d858 | started | etcd-2 | https://172.27.216.28:2380 | https://172.27.216.28:2379 | false | | 1dd6b1b56c862c6f | started | etcd-1 | https://172.27.216.27:2380 | https://172.27.216.27:2379 | false | | 4d3d854caa726877 | started | etcd-3 | https://172.16.70.32:2380 | https://172.16.70.32:2379 | false | +------------------+---------+--------+----------------------------+----------------------------+------------+
需要注意的是,在node節(jié)點中券册,需要加上對應(yīng)的證書频轿,才能獲取到正確的信息
/opt/etcd/bin/etcdctl --endpoints=https://172.27.216.27:2379 --cert=/opt/etcd/ssl/server.pem --key=/opt/etcd/ssl/server-key.pem --cacert=/opt/etcd/ssl/ca.pem member list --write-out=table
etcd的部署基本完成
2. 安裝docker(需要在每個機器上都裝上docker)
1. 第一種方式yum install docker
2. 可以通過源碼安裝
下載地址:https://download.docker.com/linux/static/stable/x86_64/docker-19.03.9.tgz
-
解壓二進制包
tar zxvf docker-19.03.9.tgz 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
-
創(chuàng)建配置文件
mkdir /etc/docker cat > /etc/docker/daemon.json << EOF { "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"] # 阿里鏡像 } EOF
-
啟動并設(shè)置開機啟動
systemctl daemon-reload systemctl start docker systemctl enable docker
docker 安裝完成
. 部署Master Node
1. 生成 kube-apiserver 證書
1.1 自簽證書頒發(fā)機構(gòu)(CA)
cat > ca-config.json<< EOF
{
"signing":{
"default":{
"expiry":"87600h"
},
"profiles":{
"kubernetes":{
"expiry":"87600h",
"usages":[
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
cat > ca-csr.json<< EOF
{
"CN":"kubernetes",
"key":{
"algo":"rsa",
"size":2048
},
"names":[
{
"C":"CN",
"L":"Beijing",
"ST":"Beijing",
"O":"k8s",
"OU":"System"
}
]
}
EOF
生成證書:
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
1.2 使用自簽 CA 簽發(fā) kube-apiserver HTTPS 證書
創(chuàng)建證書申請文件:
cat > server-csr.json<< EOF
{
"CN":"kubernetes",
"hosts":[
"10.0.0.1",
"127.0.0.1",
"172.27.216.27",
"172.27.216.28",
"172.16.70.32",
"kubernetes",
"kubernetes.default",
"kubernetes.default.svc",
"kubernetes.default.svc.cluster",
"kubernetes.default.svc.cluster.local"
],
"key":{
"algo":"rsa",
"size":2048
},
"names":[
{
"C":"CN",
"L":"BeiJing",
"ST":"BeiJing",
"O":"k8s",
"OU":"System"
}
]
}
EOF
生成server證書:
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
2. 安裝kubernetes
從github上找到對應(yīng)的版本即可https://github.com/kubernetes/kubernetes
注:打開鏈接你會發(fā)現(xiàn)里面有很多包垂涯,下載一個 server 包就夠了,包含了 Master 和 Worker Node 二進制文件航邢。
解壓二進制包:
mkdir -p /opt/k8s/{bin,cfg,ssl,logs}
tar zxvf kubernetes-server-linux-amd64.tar.gz
cd kubernetes/server/bin
cp kube-apiserver kube-scheduler kube-controller-manager /opt/k8s/bin
cp kubectl /usr/bin/
3. 部署kube-apiserver
3.1 創(chuàng)建配置文件:
cat > /opt/k8s/cfg/kube-apiserver.conf << EOF
KUBE_APISERVER_OPTS="\\
--logtostderr=false \\
--v=2 \\
--log-dir=/opt/k8s/logs \\
--etcd-servers=https://172.27.216.27:2379,https://172.27.216.28:2379,https://172.16.70.32:2379 \\
--bind-address=172.27.216.27 \\
--secure-port=6443 \\
--advertise-address=172.27.216.27 \\
--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/k8s/cfg/token.csv \\
--service-node-port-range=30000-32767 \\
--kubelet-client-certificate=/opt/k8s/ssl/server.pem \\
--kubelet-client-key=/opt/k8s/ssl/server-key.pem \\
--tls-cert-file=/opt/k8s/ssl/server.pem \\
--tls-private-key-file=/opt/k8s/ssl/server-key.pem \\
--client-ca-file=/opt/k8s/ssl/ca.pem \\
--service-account-key-file=/opt/k8s/ssl/ca-key.pem \\
--etcd-cafile=/opt/etcd/ssl/ca.pem \\
--etcd-certfile=/opt/etcd/ssl/server.pem \\
--etcd-keyfile=/opt/etcd/ssl/server-key.pem \\
--audit-log-maxage=30 \\
--audit-log-maxbackup=3 \\
--audit-log-maxsize=100 \\
--audit-log-path=/opt/k8s/logs/k8s-audit.log \\
"
EOF
#################關(guān)鍵字解釋說明
–logtostderr:啟用日志
—v:日志等級
–log-dir:日志目錄
–etcd-servers:etcd 集群地址
–bind-address:監(jiān)聽地址
–secure-port:https安全端口
–advertise-address:集群通告地址
–allow-privileged:啟用授權(quán)
–service-cluster-ip-range:Service 虛擬 IP 地址段
–enable-admission-plugins:準入控制模塊
–authorization-mode:認證授權(quán)集币,啟用 RBAC 授權(quán)和節(jié)點自管理
–enable-bootstrap-token-auth:啟用 TLS bootstrap 機制
–token-auth-file:bootstrap token 文件
–service-node-port-range:Service nodeport 類型默認分配端口范圍
–kubelet-client-xxx:apiserver 訪問 kubelet 客戶端證書
–tls-xxx-file:apiserver https 證書
–etcd-xxxfile:連接 Etcd 集群證書
–audit-log-xxx:審計日志
注: 如果這里有問題,請查看反轉(zhuǎn)義字符
3.2 Copy證書:
cp ~/TLS/k8s/ca*pem ~/TLS/k8s/server*pem /opt/k8s/ssl/
3.3 啟用TLS Bootstrapping機制:
TLS Bootstraping:Master apiserver 啟用 TLS 認證后翠忠,Node 節(jié)點 kubelet 和 kube-proxy 要與 kube-apiserver 進行通信鞠苟,必須使用 CA 簽發(fā)的有效證書才可以,當 Node 節(jié)點很多時秽之,這種客戶端證書頒發(fā)需要大量工作当娱,同樣也會增加集群擴展復(fù)雜度。為了簡化流程考榨,Kubernetes 引入了 TLS bootstraping 機制來自動頒發(fā)客戶端證書跨细,kubelet 會以一個低權(quán)限用戶自動向 apiserver 申請證書,kubelet 的證書由 apiserver 動態(tài)簽署河质。
所以強烈建議在 Node 上使用這種方式冀惭,目前主要用于 kubelet,kube-proxy 還是由我們統(tǒng)一頒發(fā)一個證書掀鹅。
3.4 創(chuàng)建并寫入對應(yīng)的token文件
# 生成對應(yīng)的token
head -c 16 /dev/urandom | od -An -t x | tr -d ' '
# 寫入文件
cat > /opt/k8s/cfg/token.csv << EOF
de49791d71b499e45a25d07185d07fe8,kubelet-bootstrap,10001,"system:node-bootstrapper"
EOF
## 格式為
token散休, 用戶名 uid,用戶
3.5 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/k8s/cfg/kube-apiserver.conf
ExecStart=/opt/k8s/bin/kube-apiserver \$KUBE_APISERVER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
3.6 設(shè)置開機啟動
systemctl daemon-reload
systemctl start kube-apiserver
systemctl enable kube-apiserver
3.7授權(quán) kubelet-bootstrap 用戶允許請求證書
kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap
4. 部署kube-controller-manager
4.1 創(chuàng)建配置文件
cat > /opt/k8s/cfg/kube-controller-manager.conf << EOF
KUBE_CONTROLLER_MANAGER_OPTS="\\
--logtostderr=false \\
--v=2 \\
--log-dir=/opt/k8s/cfglogs \\
--leader-elect=true \\
--master=127.0.0.1:8080 \\
--bind-address=127.0.0.1 \\
--allocate-node-cidrs=true \\
--cluster-cidr=10.244.0.0/16 \\
--service-cluster-ip-range=10.0.0.0/24 \\
--cluster-signing-cert-file=/opt/k8s/cfgssl/ca.pem \\
--cluster-signing-key-file=/opt/k8s/cfgssl/ca-key.pem \\
--root-ca-file=/opt/k8s/cfgssl/ca.pem \\
--service-account-private-key-file=/opt/k8s/cfgssl/ca-key.pem \\
--experimental-cluster-signing-duration=87600h0m0s\\
"
EOF
############## 以下是關(guān)鍵字解釋
–master:通過本地非安全本地端口 8080 連接 apiserver乐尊。
–leader-elect:當該組件啟動多個時戚丸,自動選舉(HA)
–cluster-signing-cert-file/–cluster-signing-key-file:自動為 kubelet 頒發(fā)證書 的 CA,與 apiserver 保持一致
4.2 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/k8s/cfg/kube-controller-manager.conf
ExecStart=/opt/k8s/bin/kube-controller-manager \$KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
4.3 啟動并設(shè)置開機啟動
systemctl daemon-reload
systemctl start kube-controller-manager
systemctl enable kube-controller-manager
5. 部署kube-scheduler
5.1 創(chuàng)建配置文件
cat > /opt/k8s/cfg/kube-scheduler.conf << EOF
KUBE_SCHEDULER_OPTS="\\
--logtostderr=false \\
--v=2 \\
--log-dir=/opt/k8s/logs \\
--leader-elect \\
--master=127.0.0.1:8080 \\
--bind-address=127.0.0.1"
EOF
###### 以下是關(guān)鍵詞解釋
–master:通過本地非安全本地端口 8080 連接 apiserver扔嵌。
–leader-elect:當該組件啟動多個時限府,自動選舉(HA)
5.2 systemd 管理 scheduler
cat > /usr/lib/systemd/system/kube-scheduler.service << EOF
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/opt/k8s/cfg/kube-scheduler.conf
ExecStart=/opt/k8s/bin/kube-scheduler \$KUBE_SCHEDULER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
5.3啟動并設(shè)置開機啟動
systemctl daemon-reload
systemctl start kube-scheduler
systemctl enable kube-scheduler
6. 查看集群狀態(tài)
6.1 現(xiàn)在所有組件都已經(jīng)啟動成功,通過 kubectl 工具查看當前集群組件狀態(tài):
kubectl get cs
##### 一下為返回值
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-1 Healthy {"health":"true"}
etcd-0 Healthy {"health":"true"}
etcd-2 Healthy {"health":"true"}
4. 部署Worker Node
下面還是在 Master Node 上操作痢缎,即同時作為 Worker Node
1. 創(chuàng)建工作目錄并拷貝二進制文件
cd kubernetes/server/bin
cp kubelet kube-proxy /opt/k8s/bin # 本地拷貝
2. 部署Kubelet
1. 創(chuàng)建配置文件
cat > /opt/k8s/cfg/kubelet.conf << EOF
KUBELET_OPTS="\\
--logtostderr=false \\
--v=2 \\
--log-dir=/opt/k8s/logs \\
--hostname-override=k8s-master \\
--network-plugin=cni \\
--kubeconfig=/opt/k8s/cfg/kubelet.kubeconfig \\
--bootstrap-kubeconfig=/opt/k8s/cfg/bootstrap.kubeconfig \\
--config=/opt/k8s/cfg/kubelet-config.yml \\
--cert-dir=/opt/k8s/ssl \\
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"
EOF
######以下為名詞解釋
–hostname-override:顯示名稱胁勺,集群中唯一
–network-plugin:啟用 CNI
–kubeconfig:空路徑,會自動生成独旷,后面用于連接 apiserver
–bootstrap-kubeconfig:首次啟動向 apiserver 申請證書
–config:配置參數(shù)文件
–cert-dir:kubelet 證書生成目錄
–pod-infra-container-image:管理 Pod 網(wǎng)絡(luò)容器的鏡像
2. 配置參數(shù)文件
cat > /opt/k8s/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/k8s/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
3. 生成 bootstrap.kubeconfig 文件
KUBE_APISERVER="https://172.27.216.27:6443" # apiserver IP:PORT
TOKEN="de49791d71b499e45a25d07185d07fe8" # 與 token.csv 里保持一致
# 生成 kubelet bootstrap kubeconfig 配置文件
kubectl config set-cluster kubernetes \
--certificate-authority=/opt/k8s/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=bootstrap.kubeconfig
kubectl config set-credentials "kubelet-bootstrap" \
--token=${TOKEN} \
--kubeconfig=bootstrap.kubeconfig
kubectl config set-context default \
--cluster=kubernetes \
--user="kubelet-bootstrap" \
--kubeconfig=bootstrap.kubeconfig
kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
4. 拷貝到配置文件路勁
cp bootstrap.kubeconfig /opt/k8s/cfg
5. systemd 管理 kubelet
cat > /usr/lib/systemd/system/kubelet.service << EOF
[Unit]
Description=Kubernetes Kubelet
After=docker.service
[Service]
EnvironmentFile=/opt/k8s/cfg/kubelet.conf
ExecStart=/opt/k8s/bin/kubelet \$KUBELET_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
6. 啟動并設(shè)置開機啟動
systemctl daemon-reload
systemctl start kubelet
systemctl enable kubelet
7. 比準kubelet證書申請并加入集群
# 查看 kubelet 證書請求
kubectl get csr
####### 返回值
NAME AGE SIGNERNAME REQUESTOR CONDITION
node-csr-yPdpMAQKEse8SuDuey9uJ-LMeuTRaBcsVeNrW6rnfYw 2m38s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
# 批準申請
kubectl certificate approve node-csr-yPdpMAQKEse8SuDuey9uJ-LMeuTRaBcsVeNrW6rnfYw
# 查看節(jié)點
kubectl get nodes
5. 部署 kube-proxy
1. 創(chuàng)建配置文件
cat > /opt/k8s/cfg/kube-proxy.conf << EOF
KUBE_PROXY_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/k8s/logs \\
--config=/opt/k8s/cfg/kube-proxy-config.yml"
EOF
2.配置參數(shù)文件
cat > /opt/k8s/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/k8s/cfg/kube-proxy.kubeconfig
hostnameOverride: k8s-master
clusterCIDR: 10.0.0.0/24
EOF
3. 生成kube-porxy.kubeconfig文件
1. 生成kube-proxy證書
# 切換工作目錄
cd ~/TLS/k8s
# 創(chuàng)建證書請求文件
cat > kube-proxy-csr.json<< EOF
{
"CN":"system:kube-proxy",
"hosts":[
],
"key":{
"algo":"rsa",
"size":2048
},
"names":[
{
"C":"CN",
"L":"BeiJing",
"ST":"BeiJing",
"O":"k8s",
"OU":"System"
}
]
}
EOF
# 生成證書
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
# 查看生成證書狀態(tài)
ls kube-proxy*pem
kube-proxy-key.pem kube-proxy.pem
2. 生成kubeconfig
KUBE_APISERVER="https://172.27.216.27:6443"
kubectl config set-cluster kubernetes \
--certificate-authority=/opt/k8s/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=kube-proxy.kubeconfig
kubectl config set-credentials kube-proxy \
--client-certificate=./kube-proxy.pem \
--client-key=./kube-proxy-key.pem \
--embed-certs=true \
--kubeconfig=kube-proxy.kubeconfig
kubectl config set-context default \
--cluster=kubernetes \
--user=kube-proxy \
--kubeconfig=kube-proxy.kubeconfig
kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
3. 拷貝到配置文件到指定路徑
cp kube-proxy.kubeconfig /opt/k8s/cfg/
4. systemd 管理 kube-proxy
cat > /usr/lib/systemd/system/kube-proxy.service << EOF
[Unit]
Description=Kubernetes Proxy
After=network.target
[Service]
EnvironmentFile=/opt/k8s/cfg/kube-proxy.conf
ExecStart=/opt/k8s/bin/kube-proxy \$KUBE_PROXY_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
5. 啟動并設(shè)置開機啟動
systemctl daemon-reload
systemctl start kube-proxy
systemctl enable kube-proxy
6. 部署CNI網(wǎng)絡(luò)
下載地址: https://github.com/containernetworking/plugins/releases
選擇對應(yīng)的版本即可署穗,這里使用的是v0.8.6
1. 解壓二進制包并移動到默認工作目錄:
mkdir /opt/cni/bin
tar zxvf cni-plugins-linux-amd64-v0.8.6.tgz -C /opt/cni/bin
2. 部署CNI網(wǎng)絡(luò)
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
3.
kubectl get node
4. 授權(quán) 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
5.
kubectl apply -f apiserver-to-kubelet-rbac.yaml
7. 新增worker Node
1. 拷貝已經(jīng)部署好的Node相關(guān)文件到新節(jié)點
scp -r /opt/k8s root@172.16.70.32:/opt/
scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@172.16.70.32:/usr/lib/systemd/system
scp -r /opt/cni/ root@172.16.70.32:/opt/
scp /opt/k8s/ssl/ca.pem root@172.16.70.32://opt/k8s/ssl
2. 刪除 kubelet 證書和 kubeconfig 文件
rm /opt/k8s/cfg/kubelet.kubeconfig
rm -f /opt/k8s/ssl/kubelet*
注:這幾個文件是證書申請審批后自動生成的,每個 Node 不同势告,必須刪除重新生成蛇捌。
3. 修改主機名
vi /opt/k8s/cfg/kubelet.conf
--hostname-override=k8s-node1
vi /opt/k8s/cfg/kube-proxy-config.yml
hostnameOverride: k8s-node1
4. 啟動并設(shè)置開機啟動
systemctl daemon-reload
systemctl start kubelet
systemctl enable kubelet
systemctl start kube-proxy
systemctl enable kube-proxy
5. 在 Master 上批準新 Node kubelet 證書申請
kubectl get csr
kubectl certificate approve node-csr-4zTjsaVSrhuyhIGqsefxzVoZDCNKei-aE2jyTP81Uro
6. 查看nodes
Kubectl get node