CentOS7安装k8s服务--Master节点和Node节点

CentOS7安装k8s服务

需求是在六台服务器上安装k8s服务,三台master节点,三台node节点,服务器的操作系统是BC-Linux,就当Centos用吧。

先给出大佬的文章(我就是看他的):https://zhuanlan.zhihu.com/p/364691278

  1. 配置yum源(如果配置过了请忽略)

为了防止使用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
  1. 配置k8s源
	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'
  1. 下载安装包
	yum install -y kubeadm-1.21.0 kubectl-1.21.0 kubelet-1.21.0
	# 设置开启启动
	systemctl enable --now kubelet.service
  1. 配置kubectl命令补全
	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
  1. 关闭防火墙、swap,重置iptables
	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
  1. 系统参数设置
	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
  1. 拉取安装所需镜像,到这一步所有节点都需要操作完。
    因为有些镜像是国外的,需要手动拉取国内的镜像在使用tag命令进行修改,不然找不到。
查看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

  1. 创建kubeadm用于初始化master节点的配置文件,里面的controlPlaneEndpoint根据实际修改,我直接设置为master1的ip。
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 
  1. 节点添加
  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
  1. 验证
    添加完节点后,可以查看pod信息
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
  1. 配置docker镜像源,因为上面拉取的镜像为阿里云的镜像,需要配置。一般都会配置的,所以放在最后。
vim /etc/docker/daemon.json
# 添加下面的内容
{
        "registry-mirrors":["https://y0qd3iq.mirror.aliyuncs.com"]
}
# 重启docker
systemctl daemon-reload
systemctl restart docker
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安装 Kubernetes 集群需要以下几个步骤: 1. 准备环境:安装 Docker 和 Kubernetes 相关组件 2. 配置 Master 节点安装和配置 kube-apiserver、kube-controller-manager、kube-scheduler 和 etcd 等组件 3. 配置 Worker 节点安装和配置 kubelet 和 kube-proxy 等组件 4. 部署网络插件:安装 Kubernetes 网络插件,如 Flannel、Calico 等 5. 检查集群状态:使用 kubectl 工具检查集群状态 下面是 CentOS 7 上安装 Kubernetes 集群的具体步骤: 1. 安装 Docker 和 Kubernetes 组件 ``` yum install -y docker kubelet kubeadm kubectl kubernetes-cni ``` 2. 初始化 Master 节点 在 Master 节点上执行以下命令: ``` kubeadm init --pod-network-cidr=10.244.0.0/16 ``` 其中 `--pod-network-cidr` 是指定 Pod 网络的 IP 地址段,这里使用 Flannel 网络插件默认的 IP 地址段。 执行完命令后,会输出加入集群的命令,类似于: ``` kubeadm join 172.16.100.10:6443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef ``` 将这个命令复制下来备用。 3. 配置 kubectl 在 Master 节点上执行以下命令: ``` mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config ``` 4. 部署网络插件 在 Master 节点上执行以下命令: ``` kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml ``` 5. 加入 Worker 节点 在 Worker 节点上执行步骤 2 中输出的加入集群的命令。 ``` kubeadm join 172.16.100.10:6443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef ``` 6. 检查集群状态 在 Master 节点上执行以下命令: ``` kubectl get nodes ``` 如果输出类似于以下内容,说明集群安装成功: ``` NAME STATUS ROLES AGE VERSION master-node Ready master 3m51s v1.19.1 worker-node Ready <none> 2m50s v1.19.1 ``` 至此,CentOS 7 上安装 Kubernetes 集群的步骤就完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值