Kubernetes/K8S 集群环境搭建

前言

最近正在学习k8s集群的搭建过程,记录下来分享给大家。

准备工作

  • visualbox 虚拟机
  • centos7 ISO镜像
  • 练习的电脑配置要高

搭建kube-master虚拟机

  • 名称:kube-master
  • 内存:4G
  • 硬盘:10G
  • 网卡:网卡1和网卡2
  • ip:192.168.56.20

Kubernetes/K8S 集群环境搭建_第1张图片

注意:vboxnet0可以在全局网络配置里面点击添加!

Kubernetes/K8S 集群环境搭建_第2张图片

搭建过程:略

基本环境配置

  • 关闭selinux
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
  • 关闭swap分区或禁用swap文件
swapoff -a
# 注释掉关于swap分区的行
yes | cp /etc/fstab /etc/fstab_bak
cat /etc/fstab_bak |grep -v swap > /etc/fstab
  • 修改网卡配置
$ vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
$ sysctl -p
  • 启用内核模块
$ modprobe -- ip_vs
$ modprobe -- ip_vs_rr
$ modprobe -- ip_vs_wrr
$ modprobe -- ip_vs_sh
$ modprobe -- nf_conntrack_ipv4
$ cut -f1 -d " "  /proc/modules | grep -e ip_vs -e nf_conntrack_ipv4
$ vim /etc/sysconfig/modules/ipvs.modules
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
  • 关闭防火墙
$ systemctl stop firewalld
$ systemctl disable firewalld

kubectl、kubeadm、kubelet的安装

  • 添加Kubernetes的yum源
vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
 http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  • 安装kubelet、kubeadm、kubectl
yum install -y kubelet kubeadm kubectl
  • 启动kubelet服务
systemctl enable kubelet
systemctl start kubelet

此时执行systemctl status kubelet查看服务状态,服务状态应为Error(255), 如果是其他错误可使用journalctl -xe查看错误信息。

Docker安装和配置

  • Docker安装

docker的安装请查看官网文档

$ sudo yum install -y yum-utils

$ sudo yum-config-manager 
    --add-repo 
    https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum install docker-ce docker-ce-cli containerd.io 
$ systemctl enable kubelet
$ systemctl start kubelet
  • 配置cgroup-driver为systemd
#查看cgroup-driver
$ docker info | grep -i cgroup
$ vim /etc/docker/daemon.json
{
  "registry-mirrors": ["https://xxx.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"]
}
$ systemctl restart docker

#再次查看cgroup-driver,看看是否配置成功
$ docker info | grep -i cgroup

kubeadm安装依赖镜像

这里有篇文章,大家直接参考安装就好!kubeadm安装依赖镜像

以上步骤需要在master节点和node节点执行

配置kube-node-1和kube-node-2虚拟机

  • 复制kube-master虚拟机为kube-node-1和kube-node-2

Kubernetes/K8S 集群环境搭建_第3张图片

Kubernetes/K8S 集群环境搭建_第4张图片

  • 修改kube-node-1的ip

以下以kube-node-1为例子
启动kube-node-1和kube-node-2虚拟机并登录,输入下面命令行进行修改:

$ vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
...
IPADDR="10.0.2.21"
...
  • 修改kube-node-1的hostname
$ hostnamectl set-hostname kube-node-1
  • 修改/etc/hosts
$ vim /etc/hosts
192.168.56.20 kube-master
192.168.56.21 kube-node-1
192.168.56.22 kube-node-2
# 重新启动虚拟机
$ poweroff

Master节点的配置

  • Master节点的初始化

首先选用finnel网络插件
Kubernetes/K8S 集群环境搭建_第5张图片

kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.19.4 --apiserver-advertise-address=192.168.56.20

执行完成之后,输出:

╭─root@kube-master ~
╰─# kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.19.4 --apiserver-advertise-addres
s=192.168.56.20
W1122 15:25:25.160860    2916 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.19.4
[preflight] Running pre-flight checks
 [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[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 [kube-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.56.20]
[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 [kube-master localhost] and IPs [192.168.56.20 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [kube-master localhost] and IPs [192.168.56.20 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
[apiclient] All control plane components are healthy after 13.007285 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.19" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node kube-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node kube-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: dcr14g.bn6134xsgfjm4dun
[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

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.56.20:6443 --token dcr14g.bn6134xsgfjm4dun \
    --discovery-token-ca-cert-hash sha256:83e1bbf9c1007f3a0346302ce763b63154694b22e0671f239674352a91c523f3

保存输出中的kubeadm join部分内容,用于添加node节点,或者使用kubeadm token listkubeadm token create --print-join-command查看

kubeadm join 192.168.56.20:6443 --token dcr14g.bn6134xsgfjm4dun \
    --discovery-token-ca-cert-hash sha256:83e1bbf9c1007f3a0346302ce763b63154694b22e0671f239674352a91c523f3

接下来执行剩余的初始化步骤

$ mkdir -p $HOME/.kube
# 替换192.168.56.20为kube-master
$ vim /etc/kubernetes/admin.conf
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

查看nodes和pods:

╭─root@kube-master ~
╰─# kubectl get nodes
NAME          STATUS     ROLES    AGE   VERSION
kube-master   NotReady   master   14m   v1.19.4
╭─root@kube-master ~
╰─# kubectl get pods --all-namespaces
NAMESPACE     NAME                                  READY   STATUS    RESTARTS   AGE
kube-system   coredns-f9fd979d6-5vrhh               0/1     Pending   0          13m
kube-system   coredns-f9fd979d6-bdwnr               0/1     Pending   0          13m
kube-system   etcd-kube-master                      1/1     Running   0          14m
kube-system   kube-apiserver-kube-master            1/1     Running   0          14m
kube-system   kube-controller-manager-kube-master   1/1     Running   0          14m
kube-system   kube-proxy-gcbmf                      1/1     Running   0          13m
kube-system   kube-scheduler-kube-master            1/1     Running   0          14m
  • finnel插件的安装
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
╭─root@kube-master ~
╰─# kubectl apply -f kube-flannel.yml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created

监听查看finnel安装情况:

watch kubectl get pods --all-namespaces
Every 2.0s: kubectl get pods --all-namespaces                                                                                                Sun Nov 22 15:50:24 2020

NAMESPACE     NAME                                  READY   STATUS    RESTARTS   AGE
kube-system   coredns-f9fd979d6-5vrhh               1/1     Running   0          24m
kube-system   coredns-f9fd979d6-bdwnr               1/1     Running   0          24m
kube-system   etcd-kube-master                      1/1     Running   0          24m
kube-system   kube-apiserver-kube-master            1/1     Running   0          24m
kube-system   kube-controller-manager-kube-master   1/1     Running   0          24m
kube-system   kube-flannel-ds-s4pm6                 1/1     Running   0          6m49s
kube-system   kube-proxy-gcbmf                      1/1     Running   0          24m
kube-system   kube-scheduler-kube-master            1/1     Running   0          24m

如果中间出现下载不下来可以参考这篇文章解决:https://blog.csdn.net/zyl9746...

  • 小技巧:

查看kube-flannel-ds-s4pm6安装情况

kubectl describe pods kube-flannel-ds-s4pm6 -n kube-system

Kubernetes/K8S 集群环境搭建_第6张图片

  • 测试
╭─root@kube-master ~
╰─# kubectl get nodes -o wide
NAME          STATUS   ROLES    AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME
kube-master   Ready    master   33m   v1.19.4   192.168.56.20           CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://19.3.13

Node节点的初始化

  • 查看加入集群的命令

要使node节点加入到master节点中,可以先查看master节点生成的加入信息:

╭─root@kube-master ~
╰─# kubeadm token create --print-join-command
W1122 16:01:20.660775   18089 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
kubeadm join kube-master:6443 --token inhs8l.e0otb982o462ruk7     --discovery-token-ca-cert-hash sha256:83e1bbf9c1007f3a0346302ce763b63154694b22e0671f239674352a91c523f3
  • 登录node节点,执行加入集群的命令,完成加入集群操作

以下以kube-node-1为例子

╭─root@kube-node-1 ~
╰─# kubeadm join kube-master:6443 --token inhs8l.e0otb982o462ruk7     --discovery-token-ca-cert-hash sha256:83e1bbf9c1007f3a0346302ce763b63154694b22e0671f239674352a91c523f3
  • 查看flannel插件执行情况
kubectl get pods --all-namespaces -o wide

Kubernetes/K8S 集群环境搭建_第7张图片
这样就完成了k8s集群的所有操作了!

总结

1、我也观看bilibili大佬的视频总结的,有兴趣的可以到这个地址观察视频搭建全过程;
2、k8s需要很多的配置,如果出现问题大家自行搜索解决方案,我在搭建的过程中也参考了好多人的文章;
3、我们首先使用kube-master搭建master虚拟机,而kube-node-1和kube-node-2可以直接复制kube-master虚拟机只要改下ip和hostname即可,比你再去搭建要方便好多;
4、虚拟机可以使用无界面启动,直接在本地通过ssh连接,非常方便。

引用

Kubernetes/K8S 集群环境搭建
K8S安装过程笔记

你可能感兴趣的:(k8s)