### 🧱 基础配置
---
```shell
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
# 关闭swap
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
yum install -y bash-completion supervisor.noarch nc iftop telnet
yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git bridge-utils dstat tree
yum install -y yum-utils device-mapper-persistent-data lvm2 unzip
```
### 😢 设置变量
---
```shell
ETCD=('x.x.x.x' 'x.x.x.x' 'x.x.x.x')
Master=('x.x.x.x' 'x.x.x.x' 'x.x.x.x')
SLBIP=x.x.x.x
Domain=("" "")
ClusterCidr=
ServiceIPs=
DnsIP=
```
### 😵💫 证书颁发
---
##### etcd 证书
```shell
mkdir -p ~/TLS/etcd
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 -
cat > server-csr.json << EOF
{
"CN": "etcd",
"hosts": [
"${ETCD[0]}",
"${ETCD[1]}",
"${ETCD[2]}"
],
"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
```
##### k8s 证书
```shell
mkdir -p ~/TLS/k8s
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
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
cat > server-csr.json << EOF
{
"CN": "kubernetes",
"hosts": [
"10.0.0.1",
"10.71.0.1",
"127.0.0.1",
"${Master[0]}",
"${Master[1]}",
"${Master[2]}",
"${SLBIP}",
"kubernetes",
"kubernetes.default",
"kubernetes.default.svc",
"kubernetes.default.svc.cluster",
"kubernetes.default.svc.cluster.local"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
```
### 😹 安装etcd
---
```shell
mkdir /home/etcd/{bin,config,ssl} -p
cp ~/TLS/etcd/{ca-key.pem,ca.pem,server-key.pem,server.pem} /home/etcd/ssl/
cp k8s/etcd/bin/etcd /home/etcd/bin/
#这里需要注意不同etcd节点需要修改name 以及ip
cat > /home/etcd/config/etcd.yml << EOF
name: "etcd01"
data-dir: "/home/etcd/data/default.etcd"
listen-peer-urls: "https://x.x.x.x:2380"
listen-client-urls: "https://x.x.x.x:2379"
quota-backend-bytes: 8589934592
auto-compaction-retention: "3"
initial-cluster: "etcd01=https://x.x.x.x:2380,etcd02=https://x.x.x.x:2380,etcd03=https://x.x.x.x:2380"
initial-cluster-token: 'etcd-cluster'
initial-cluster-state: 'new'
initial-advertise-peer-urls: "https://x.x.x.x:2380"
advertise-client-urls: "https://x.x.x.x:2379"
client-transport-security:
cert-file: "/home/etcd/ssl/server.pem"
key-file: "/home/etcd/ssl/server-key.pem"
client-cert-auth: true
trusted-ca-file: "/home/etcd/ssl/ca.pem"
peer-transport-security:
cert-file: "/home/etcd/ssl/server.pem"
key-file: "/home/etcd/ssl/server-key.pem"
client-cert-auth: true
trusted-ca-file: "/home/etcd/ssl/ca.pem"
logger: zap
EOF
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
ExecStart=/home/etcd/bin/etcd --config-file=/home/etcd/config/etcd.yml
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
#配置完成一台etcd之后可以直接scp至其他节点然后修改ip配置启动etcd
#这里需要注意etcd name 01 02 03 都是具体的标示 scp到别的机器上之后需要修改
systemctl start etcd
systemctl enable etcd
ETCDCTL_API=3 etcdctl --cacert=/home/etcd/ssl/ca.pem --cert=/home/etcd/ssl/server.pem --key=/home/etcd/ssl/server-key.pem --endpoints="https://x.x.x.x:2379,https://x.x.x.x:2379,https://x.x.x.x:2379" endpoint health
```
### 🐳 安装docker
---
```shell
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
#如果安装高版本docker containerd会依赖高版本 需要下载rpm安装新版本
#https://download.docker.com/linux/centos/7/x86_64/stable/Packages/
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.4.9-3.1.el7.x86_64.rpm
yum localinstall containerd.io-1.4.9-3.1.el7.x86_64.rpm -y
#安装指定版本docker
yum install docker-ce-20.10.8 -y
#创建docker目录并且挂盘
mkdir /docker-data
mkfs.xfs /dev/vdb
mount /dev/vdb /docker-data
#需要在docker service文件添加--graph /docker-data/docker --bip=10.10.201.1/24 启动参数
mkdir -p /etc/docker
cat > /etc/docker/daemon.json << EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"bip": "10.10.201.1/24",
"data-root": "/docker-data/docker"
}
EOF
systemctl start docker
systemctl enable docker
```
### 🧠 安装master
---
##### apiserver
```shell
mkdir -p /home/kubernetes/{bin,config,ssl}
mkdir -p /www-volume/logs/{kube-apiserver,kube-controller,kube-scheduler,kubelet,kube-proxy}
cp TLS/k8s/{ca.pem,ca-key.pem,server-key.pem,server.pem} /home/kubernetes/ssl/
cp -r k8s/kubernetes/bin/ /home/kubernetes/bin/
cp -r k8s/cni /opt/
cat > /home/kubernetes/config/token.csv << EOF
e537d04b49b03df79cf29c2b53bfcfae,kubelet-bootstrap,10001,"system:node-bootstrapper"
EOF
head -c 16 /dev/urandom | od -An -t x | tr -d ' '
cat > /usr/lib/systemd/system/kube-apiserver.service << EOF
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
[Service]
ExecStart=/home/kubernetes/bin/kube-apiserver --logtostderr=false \
--v=2 \
--log-dir=/www-volume/logs/kube-apiserver \
--log-file=/www-volume/logs/kube-apiserver/kube-apiserver.log \
--log-file-max-size=100 \
--etcd-servers=https://x.x.x.x:2379,https://x.x.x.x:2379,https://x.x.x.x:2379 \
--bind-address=x.x.x.x \
--secure-port=6443 \
--advertise-address=x.x.x.x \
--allow-privileged=true \
--service-cluster-ip-range=10.71.0.0/24 \
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \
--authorization-mode=RBAC,Node \
--enable-bootstrap-token-auth=true \
--token-auth-file=/home/kubernetes/config/token.csv \
--service-node-port-range=30000-32767 \
--kubelet-client-certificate=/home/kubernetes/ssl/server.pem \
--kubelet-client-key=/home/kubernetes/ssl/server-key.pem \
--tls-cert-file=/home/kubernetes/ssl/server.pem \
--tls-private-key-file=/home/kubernetes/ssl/server-key.pem \
--client-ca-file=/home/kubernetes/ssl/ca.pem \
--service-account-key-file=/home/kubernetes/ssl/ca-key.pem \
--service-account-issuer=api \
--service-account-signing-key-file=/home/kubernetes/ssl/server-key.pem \
--etcd-cafile=/home/etcd/ssl/ca.pem \
--etcd-certfile=/home/etcd/ssl/server.pem \
--etcd-keyfile=/home/etcd/ssl/server-key.pem \
--requestheader-client-ca-file=/home/kubernetes/ssl/ca.pem \
--proxy-client-cert-file=/home/kubernetes/ssl/server.pem \
--proxy-client-key-file=/home/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=15 \
--audit-log-maxbackup=15 \
--audit-log-maxsize=512 \
--feature-gates=EphemeralContainers=true \
--audit-log-compress \
--audit-log-mode=batch \
--audit-policy-file=/home/kubernetes/config/kube-api-auditlog.yaml \
--audit-log-path=/www-volume/logs/kubernetes-audit/kuberntes-audit.log
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
```
##### controller-manager
```shell
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
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_CONFIG="/home/kubernetes/config/kube-controller-manager.kubeconfig"
KUBE_APISERVER="https://x.x.x.x:6443"
kubectl config set-cluster kubernetes \
--certificate-authority=/home/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}
cat > /usr/lib/systemd/system/kube-controller-manager.service << EOF
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
[Service]
ExecStart=/home/kubernetes/bin/kube-controller-manager --logtostderr=false \
--v=2 \
--log-dir=/www-volume/logs/kube-controller \
--log-file=/www-volume/logs/kube-controller/kube-controller.log \
--log-file-max-size=100 \
--leader-elect=true \
--kubeconfig=/home/kubernetes/config/kube-controller-manager.kubeconfig \
--bind-address=0.0.0.0 \
--allocate-node-cidrs=true \
--cluster-cidr=10.70.0.0/16 \
--leader-elect-resource-lock=endpoints \
--service-cluster-ip-range=10.71.0.0/24 \
--cluster-signing-cert-file=/home/kubernetes/ssl/ca.pem \
--cluster-signing-key-file=/home/kubernetes/ssl/ca-key.pem \
--root-ca-file=/home/kubernetes/ssl/ca.pem \
--service-account-private-key-file=/home/kubernetes/ssl/ca-key.pem \
--cluster-signing-duration=87600h0m0s
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start kube-controller-manager
systemctl enable kube-controller-manager
```
##### scheduler
```shell
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
# 生成证书
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_CONFIG="/home/kubernetes/config/kube-scheduler.kubeconfig"
KUBE_APISERVER="https://x.x.x.x:6443"
kubectl config set-cluster kubernetes \
--certificate-authority=/home/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}
cat > /usr/lib/systemd/system/kube-scheduler.service << EOF
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
[Service]
ExecStart=/home/kubernetes/bin/kube-scheduler --logtostderr=false \
--v=2 \
--log-dir=/www-volume/logs/kube-scheduler \
--log-file=/www-volume/logs/kube-scheduler/kube-scheduler.log \
--log-file-max-size=100 \
--leader-elect=true \
--leader-elect-resource-lock=endpoints \
--kubeconfig=/home/kubernetes/config/kube-scheduler.kubeconfig \
--bind-address=0.0.0.0
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start kube-scheduler
systemctl enable kube-scheduler
```
##### admin证书制作 kubectl权限
```shell
cat > admin-csr.json <<EOF
{
"CN": "admin",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "system:masters",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin
生成kubeconfig文件:
mkdir /root/.kube
KUBE_CONFIG="/root/.kube/config"
KUBE_APISERVER="https://x.x.x.x:6443"
kubectl config set-cluster kubernetes \
--certificate-authority=/home/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}
kubectl get cs
kubectl create clusterrolebinding kubelet-admin --clusterrole=system:kubelet-api-admin --user=kubernetes
kubectl create clusterrolebinding kubelet-bootstrap \
--clusterrole=system:node-bootstrapper \
--user=kubelet-bootstrap
```
### 😼 安装node
---
##### kubelet
```shell
KUBE_CONFIG="/home/kubernetes/config/bootstrap.kubeconfig"
KUBE_APISERVER="https://x.x.x.x:6443" # apiserver IP:PORT
TOKEN="e537d04b49b03df79cf29c2b53bfcfae" # 与token.csv里保持一致
# 生成 kubelet bootstrap kubeconfig 配置文件
kubectl config set-cluster kubernetes \
--certificate-authority=/home/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}
cat > /usr/lib/systemd/system/kubelet.service << EOF
[Unit]
Description=Kubernetes Kubelet
After=docker.service
[Service]
ExecStart=/home/kubernetes/bin/kubelet --logtostderr=false \
--v=2 \
--log-dir=/www-volume/logs/kubelet \
--log-file=/www-volume/logs/kubelet/kubelet.log \
--log-file-max-size=100 \
--hostname-override=cbd-master01.ali-bj \
--network-plugin=cni \
--kubeconfig=/home/kubernetes/config/kubelet.kubeconfig \
--config=/home/kubernetes/config/kubelet-config.yml \
--bootstrap-kubeconfig=/home/kubernetes/config/bootstrap.kubeconfig \
--cert-dir=/home/kubernetes/ssl \
--system-reserved=memory=1G \
--kube-reserved=memory=1G \
--pod-infra-container-image=harbor.xxx.com/base/pause:3.1
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
cat > /home/kubernetes/config/kubelet-config.yml << EOF
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 0.0.0.0
port: 10250
readOnlyPort: 10255
cgroupDriver: systemd
clusterDNS:
- x.x.x.x
clusterDomain: cluster.local
failSwapOn: false
authentication:
anonymous:
enabled: false
webhook:
cacheTTL: 2m0s
enabled: true
x509:
clientCAFile: /home/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: 55
EOF
systemctl daemon-reload
systemctl start kubelet
systemctl enable kubelet
kubectl get csr | grep Pending | awk '{print $1}' | xargs kubectl certificate approve
```
##### kube-proxy
```shell
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
# 生成证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
KUBE_CONFIG="/home/kubernetes/config/kube-proxy.kubeconfig"
KUBE_APISERVER="https://x.x.x.x:6443"
kubectl config set-cluster kubernetes \
--certificate-authority=/home/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}
cat > /usr/lib/systemd/system/kube-proxy.service << EOF
[Unit]
Description=Kubernetes Proxy
After=network.target
[Service]
ExecStart=/home/kubernetes/bin/kube-proxy --logtostderr=false \
--v=2 \
--log-file=/www-volume/logs/kube-proxy/kube-proxy.log \
--log-file-max-size=100 \
--log-dir=/www-volume/logs/kube-proxy \
--config=/home/kubernetes/config/kube-proxy-config.yml
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
cat > /home/kubernetes/config/kube-proxy-config.yml << EOF
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
clientConnection:
acceptContentTypes: ""
burst: 10
kubeconfig: /home/kubernetes/config/kube-proxy.kubeconfig
qps: 5
clusterCIDR: 10.70.0.0/16
configSyncPeriod: 15m0s
conntrack:
max: null
maxPerCore: 32768
min: 131072
tcpCloseWaitTimeout: 1h0m0s
tcpEstablishedTimeout: 24h0m0s
enableProfiling: false
healthzBindAddress: 0.0.0.0:10256
hostnameOverride: ""
iptables:
masqueradeAll: false
masqueradeBit: 14
minSyncPeriod: 0s
syncPeriod: 30s
ipvs:
excludeCIDRs: null
minSyncPeriod: 0s
scheduler: "rr"
strictARP: false
syncPeriod: 30s
kind: KubeProxyConfiguration
metricsBindAddress: 0.0.0.0:10249
mode: "ipvs"
nodePortAddresses: null
oomScoreAdj: -999
portRange: ""
resourceContainer: /kube-proxy
udpIdleTimeout: 250ms
winkernel:
enableDSR: false
networkName: ""
sourceVip: ""
hostnameOverride: cbd-master01.ali-bj
EOF
systemctl daemon-reload
systemctl start kube-proxy
systemctl enable kube-proxy
```
部署 1.20.9 K8S