使用kubeadm安装kubernetes集群

K8s安装

kubeadm
1、master nodes:安装kubelet kubeadm docker
2、master:kubelet init
3、node:kubelet join

一、准备工作

关闭防火墙,iptables,关闭swap分区

systemctl stop firewalld
swapoff -a
iptables -F

yum install -y bridge-utils.x86_64
modprobe  br_netfilter  # 加载br_netfilter模块,使用lsmod查看开启的模块
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system  # 重新加载所有配置文件

二、准备yum源

1、准备docker的yum源

$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

$ sudo yum install -y yum-utils

$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo                

2、准备K8s的yum源

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

三、安装k8s docker 1个master、2个node

master

yum install docker-ce kubelet kubeadm kubectl bash-completion -y

node节点

yum install docker-ce kubelet kubeadm -y

四、主节点的初始化

1、启动docker和kubelet

vim /etc/sysconfig/kubelet

# KUBELET_EXTRA_ARGS="--fail-swap-on=false"

# kubectl 命令补全
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc

systemctl start docker kubelet 
systemctl enable docker kubelet
kubeadm init --kubernetes-version=stable-1 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap

master初始化完成

[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.19.29:6443 --token rp14ge.sd6dlzilhzik7ga5 \
    --discovery-token-ca-cert-hash sha256:d69202fa7177ceecc3fd2ef899ce18fa78530c91ec4a64fe910dc5674669ec31

2、排错

[root@master ~]# kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS      MESSAGE                                                                                       ERROR
scheduler            Unhealthy   Get "http://127.0.0.1:10251/healthz": dial tcp 127.0.0.1:10251: connect: connection refused   
controller-manager   Unhealthy   Get "http://127.0.0.1:10252/healthz": dial tcp 127.0.0.1:10252: connect: connection refused   
etcd-0               Healthy     {"health":"true"}  

通过搜索应该是禁用端口

vim /etc/kubernetes/manifests/kube-scheduler.yaml
#    - --port=0
vim /etc/kubernetes/manifests/kube-controller-manager.yaml
#    - --port=0

重启kubelet

[root@master manifests]# systemctl restart kubelet
[root@master manifests]# kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok                  
scheduler            Healthy   ok                  
etcd-0               Healthy   {"health":"true"}

3、 安装网络组件

flannel组件主页

flannel For Kubernetes v1.17+

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

查看节点状态

[root@master manifests]# kubectl get nodes
NAME     STATUS   ROLES                  AGE   VERSION
master   Ready    control-plane,master   20m   v1.20.2

五、node节点加入

1、yum源准备

[master]# scp /etc/yum.repos.d/docker-ce.repo node1:/etc/etc/yum.repos.d/docker-ce.repo
[master]# scp /etc/yum.repos.d/kubernetes.repo node1:/etc/etc/yum.repos.d/kubernetes.repo

2、安装docker和kubelet kubeadm

yum install -y docker kubelet kubeadm

3、启动并设置开机自启

systemctl start docker kubelet
systemctl enable docker kubelet

4、加入节点

[root@234c12 ~]# kubeadm join 192.168.19.29:6443 --token rp14ge.sd6dlzilhzik7ga5 \
    --discovery-token-ca-cert-hash sha256:d69202fa7177ceecc3fd2ef899ce18fa78530c91ec4a64fe910dc5674669ec31 --ignore-preflight-errors=Swap

查看集群状态

[root@master ~]# kubectl get nodes
NAME     STATUS   ROLES                  AGE     VERSION
master   Ready    control-plane,master   8m22s   v1.20.2
node1    Ready    <none>                 3m51s   v1.20.2
node2    Ready    <none>                 3m44s   v1.20.2