准备工作(所有节点操作)

1. 配置主机名

hostnamectl set-hostname cka01 --static
hostnamectl set-hostname cka02 --static
hostnamectl set-hostname cka03 --static

2. 修改/etc/hosts

以master为例

vim /etc/hosts
192.168.101.102 cka01
192.168.101.103 cka02
192.168.101.103 cka03

3. 修改apt源

清空/etc/apt/sources.list,并添加如下内容

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

执行apt源更新操作
apt update -y

4. 清空防火墙规则

iptables -F

5. 修改内核参数

cat >/etc/sysctl.d/k8s.conf<<EOF
  net.ipv4.ip_forward = 1
  vm.swappiness = 0
EOF

sysctl -p /etc/sysctl.d/k8s.conf

6. 加载内核模块

cat > /etc/modules-load.d/modules.conf<<EOF
br_netfilter
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack_ipv4
EOF

for i in br\_netfilter ip\_vs ip\_vs\_rr ip\_vs\_wrr ip\_vs\_sh nf\_conntrack\_ipv4;do modprobe $i;done

7. 安装docker

apt -y install apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

apt update -y
apt install docker-ce=5:19.03.15~3-0~ubuntu-bionic -y

mkdir -p /etc/docker

cat > /etc/docker/daemon.json<<EOF
{
"exec-opts": \["native.cgroupdriver=systemd"\],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "10"
},
"registry-mirrors": \["https://pqbap4ya.mirror.aliyuncs.com"\]
}
EOF

systemctl restart docker
systemctl enable docker

8. 安装kubeadm、kubectl、kubelet

apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -

cat > /etc/apt/sources.list.d/kubernetes.list<<EOF
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

apt update -y
apt-cache madison kubelet

apt-get install -y kubelet=1.22.4-00 kubectl=1.22.4-00 kubeadm=1.22.4-00

9. 在所有节点设置kubelet开机自启动

systemctl enable kubelet

安装master(只在master上操作)

kubeadm config print init-defaults > kubeadm-config.yaml

apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
\- groups:
  \- system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  \- signing
  \- authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 192.168.0.102                //指定master监听的地址
  bindPort: 6443                                 //指定master监听端口
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: cka01                           //master名称*hosts主机名*
  taints:
  \- effect: NoSchedule
    key: node-role.kubernetes.io/master
\-\-\-
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.20.5  //指定安装版本
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.10.0.0/12    //设置集群内部的网络
  podSubnet: 10.20.0.0/16       //设置pod的网络
scheduler: {}

初始化安装:

kubeadm init --config kubeadm-config.yaml

Your Kubernetes control-plane has initialized successfully! //安装成功提示。

配置master集群访问:

To start using your cluster, you need to run the following as a regular user: //复制此安装完成提示信息,再master节点执行。
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -u) $HOME/.kube/config

添加节点

通过安装完成提示,再node节点上执行
`kubeadm join 192.168.0.102:6443 --token abcdef.0123456789abcdef
--discovery-token-ca-cert-hash sha256:d19eafe0f6ea680a75aca46bdedf9ea20da869a09ff5923d335277ff95d4094e`

部署网络插件

kubectl apply -f https://docs.projectcalico.org/manifests/calicoctl.yaml

安装metrics插件

https://github.com/kubernetes-sigs/metrics-server/releases/download/metrics-server-helm-chart-3.7.0/components.yaml

registry.cn-hangzhou.aliyuncs.com/andyzt/metrics-server-v0.5.2:v0.5.2