需求是在六台服务器上安装k8s服务,三台master节点,三台node节点,服务器的操作系统是BC-Linux,就当Centos用吧。
先给出大佬的文章(我就是看他的):https://zhuanlan.zhihu.com/p/364691278
为了防止使用yum出现Your license is invalid问题,先修改配置
cd /etc/yum/pluginconf.d
vim license-manager.conf
将enable=1改为enable=0
因为BC-linux的yum在下载kubeadm相关安装包的时候会报错,所以先卸载yum重新安装
rpm -qa yum
rpm -qa | grep yum | xargs rpm -e --nodeps
rpm -qa yum
echo 'nameserver 8.8.8.8'>>/etc/resolv.conf
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-3.4.3-168.el7.centos.noarch.rpm
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
rpm -ivh yum-*
rpm -qa yum
cd /etc/yum.repos.d
mv BCLinux-For-LDK.repo BCLinux-For-LDK.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum update //如果报错了,问题不大,可以忽略
yum makecache
bash -c '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'
yum install -y kubeadm-1.21.0 kubectl-1.21.0 kubelet-1.21.0
# 设置开启启动
systemctl enable --now kubelet.service
yum install bash-completion -y
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
mkdir .kube && cd .kube && touch completion.bash.inc
kubectl completion bash > ~/.kube/completion.bash.inc
printf "
# Kubectl shell completion
source '$HOME/.kube/completion.bash.inc'
" >> $HOME/.bash_profile
systemctl stop firewalld && systemctl disable firewalld
iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT
swapoff -a
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0
systemctl restart docker
cat > /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
EOF
# 使文件生效
sysctl -p /etc/sysctl.d/kubernetes.conf
查看kubeadm需要拉取的镜像列表
kubeadm config images list
我修改了大佬的shell脚本,直接运行就行,如果使用的上面版本1.21.0的话,应该可以直接使用,注意版本号。
#!/bin/bash
ALIYUN_KUBE_VERSION=v1.21.0
KOBUBE_VERSION=v1.21.0
KUBE_PAUSE_VERSION=3.4.1
ETCD_VERSION=3.4.13-0
DNS_VERSION=1.8.0
username=registry.cn-hangzhou.aliyuncs.com/google_containers
images=(
kube-proxy:${ALIYUN_KUBE_VERSION}
kube-scheduler:${ALIYUN_KUBE_VERSION}
kube-controller-manager:${ALIYUN_KUBE_VERSION}
kube-apiserver:${ALIYUN_KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
coredns:${DNS_VERSION}
)
for image in ${images[@]}
do
docker pull ${username}/${image}
done
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.0 k8s.gcr.io/kube-apiserver:v1.21.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.0 k8s.gcr.io/kube-proxy:v1.21.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.0 k8s.gcr.io/kube-scheduler:v1.21.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.0 k8s.gcr.io/kube-controller-manager:v1.21.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 k8s.gcr.io/pause:3.4.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.0 k8s.gcr.io/coredns/coredns:v1.8.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0 k8s.gcr.io/etcd:3.4.13-0
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.0
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.0
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.0
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.0
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.0
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0
touch kubeadm-config.yaml
# 内容:
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
kubernetesVersion: v1.21.0
# 指定控制面板的访问端点,这里的ip为keepalived的虚拟ip
controlPlaneEndpoint: "192.168.1.54:6443"
networking:
# This CIDR is a Calico default. Substitute or remove for your CNI provider.
podSubnet: "172.22.0.0/16" # 指定pod所使用的网段
执行初始化命令
kubeadm init --config=kubeadm-config.yaml --upload-certs
执行完上面这条命令,master1节点就算创建完了,然后会打印以下信息,这边就用大佬的信息了。
[init] Using Kubernetes version: v1.21.0
[preflight] Running pre-flight checks
[WARNING Service-Docker]: docker service is not enabled, please run 'systemctl enable docker.service'
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at Container runtimes
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local whdata] and IPs [10.96.0.1 192.168.1.54]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost whdata] and IPs [192.168.1.54 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost whdata] and IPs [192.168.1.54 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[kubelet-check] Initial timeout of 40s passed.
[apiclient] All control plane components are healthy after 65.002661 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.21" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
e2d66ad5b4151f47d0cefa7f6eb2b07d6dda04004e204495e00bcbf60de4ee91
[mark-control-plane] Marking the node whdata as control-plane by adding the labels: [node-role.kubernetes.io/master(deprecated) node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node whdata as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: xlz2ff.cufpkfnxjcazpgxt
[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:
Installing Addons
You can now join any number of the control-plane node running the following command on each as root:
kubeadm join 192.168.1.54:6443 --token xlz2ff.cufpkfnxjcazpgxt \
--discovery-token-ca-cert-hash sha256:d2c74038cee735b8c7b6bef3ed1370e5628f6ee8138c84a2826ca79b43348a23 \
--control-plane --certificate-key e2d66ad5b4151f47d0cefa7f6eb2b07d6dda04004e204495e00bcbf60de4ee91
Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use
"kubeadm init phase upload-certs --upload-certs" to reload certs afterward.
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.1.54:6443 --token xlz2ff.cufpkfnxjcazpgxt \
--discovery-token-ca-cert-hash sha256:d2c74038cee735b8c7b6bef3ed1370e5628f6ee8138c84a2826ca79b43348a23
kubeadm join 192.168.1.54:6443 --token xlz2ff.cufpkfnxjcazpgxt \
--discovery-token-ca-cert-hash sha256:d2c74038cee735b8c7b6bef3ed1370e5628f6ee8138c84a2826ca79b43348a23 \
--control-plane --certificate-key e2d66ad5b4151f47d0cefa7f6eb2b07d6dda04004e204495e00bcbf60de4ee91
这条用于master节点进行添加,添加成功会出现success。这里有一个坑,就是重名问题,需要对其他master节点和node节点进行改名。
改名:
hostnamectl --static set-hostname master2
这条用于node节点进行添加
kubeadm join 192.168.1.54:6443 --token xlz2ff.cufpkfnxjcazpgxt \
--discovery-token-ca-cert-hash sha256:d2c74038cee735b8c7b6bef3ed1370e5628f6ee8138c84a2826ca79b43348a23
如果添加失败了,可以使用kubeadm reset进行重置,然后重新进行初始化。
其中,如果操作间隔时间长会出现certificate-key过期的问题,可以使用以下命令重新生成,然后进行替换。
kubeadm init phase upload-certs --experimental-upload-certs
在master节点上执行如下命令拷贝配置文件:
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get pod --all-namespaces
APIServer健康状态验证,返回ok代表没问题。
curl -k https://192.168.1.54:6443/healthz
这里也有可能出现问题。
The connection to the server localhost:8080 was refused - did you specify the right host or port?
解决办法
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile
source /etc/profile
vim /etc/docker/daemon.json
# 添加下面的内容
{
"registry-mirrors":["https://y0qd3iq.mirror.aliyuncs.com"]
}
# 重启docker
systemctl daemon-reload
systemctl restart docker