乐趣区

关于k8s:KubernetesK8S-集群环境搭建

前言

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

筹备工作

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

搭建 kube-master 虚拟机

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





留神:vboxnet0 能够在全局网络配置外面点击增加!

搭建过程:略

根本环境配置

  • 敞开 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

  • 批改 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 网络插件

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

  • 测试
╭─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   <none>        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


这样就实现了 k8s 集群的所有操作了!

总结

1、我也观看 bilibili 大佬的视频总结的,有趣味的能够到这个地址察看视频搭建全过程;
2、k8s 须要很多的配置,如果呈现问题大家自行搜寻解决方案,我在搭建的过程中也参考了好多人的文章;

援用

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

退出移动版