Linux企业应用——kubernates(一)之kubernates集群部署,初步搭建k8s

发布于:2022-11-27 ⋅ 阅读:(333) ⋅ 点赞:(0)

一、什么是kubernates

kubernetes,简称K8s,是用8代替名字中间的8个字符“ubernete”而成的缩写。是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效(powerful),Kubernetes提供了应用部署,规划,更新,维护的一种机制。
传统的应用部署方式是通过插件或脚本来安装应用。这样做的缺点是应用的运行、配置、管理、所有生存周期将与当前操作系统绑定,这样做并不利于应用的升级更新/回滚等操作,当然也可以通过创建虚拟机的方式来实现某些功能,但是虚拟机非常重,并不利于可移植性。
新的方式是通过部署容器方式实现,每个容器之间互相隔离,每个容器有自己的文件系统 ,容器之间进程不会相互影响,能区分计算资源。相对于虚拟机,容器能快速部署,由于容器与底层设施、机器文件系统解耦的,所以它能在不同云、不同版本操作系统间进行迁移。
容器占用资源少、部署快,每个应用可以被打包成一个容器镜像,每个应用与容器间成一对一关系也使容器有更大优势,使用容器可以在build或release 的阶段,为应用创建容器镜像,因为每个应用不需要与其余的应用堆栈组合,也不依赖于生产环境基础结构,这使得从研发到测试、生产能提供一致环境。类似地,容器比虚拟机轻量、更“透明”,这更便于监控和管理。

二、kubernates集群部署之准备

1.环境准备:实验主机

搭载harbor仓库:server1:10.244.3.11
节点1:k8s1:10.244.3.12
节点2:k8s2:10.244.3.13
节点3:k8s3:10.244.3.14

关闭所有节点的selinux和iptables防火墙,所有节点部署docker引擎。
清理所有服务器之前的docker实验产物,保证实验环境崭新
关闭掉节点上的swap分区注释掉/etc/fstab文件里swap分区的部分

setenforce 0
systemctl disable --now firewalld
swapoff -a
vim /etc/fstab

2.环境准备:时间同步

yum install -y chrony
vim /etc/chrony.conf
输入:server ntp1.aliyun.com iburst
systemctl restart chronyd
chronyc  sources -v

3.每个k8s节点上安装并配置Docker

docker安装配置见:此篇链接
每个节点上,根据安装 Docker 引擎 为你的 Linux 发行版安装 Docker。
配置 Docker 守护程序,尤其是使用 systemd 来管理容器的 cgroup。
因为要做本地化处理,其中的仓库镜像地址替换为自己的harbor仓库域名

sudo mkdir /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "registry-mirrors: ["https://www.hanxuan.com"]"
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF

systemctl restart docker重新启动 Docker 并在启动时启用:

 sudo systemctl enable docker
 sudo systemctl daemon-reload
 sudo systemctl restart docker

3.环境准备:harbor私有仓库上传镜像,做本地化处理

建立harbor仓库见此文:链接

注意:一定要先拉起harbor仓库!!

systemctl restart docker
cd /harbor
docker-compose up -d
docker-compose start

解压镜像打包文件

docker load -i k8s.tar

参考推送命令,harbor仓库上传镜像到新建的公开项目:k8s与rancher目录里

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

[root@server1 harbor]# docker images | grep flannel
reg.westos.org/rancher/mirrored-flannelcni-flannel              v0.19.2    8b675dda11bb   6 weeks ago     62.3MB
reg.westos.org/rancher/mirrored-flannelcni-flannel-cni-plugin   v1.1.0     fcecffc7ad4a   4 months ago    8.09MB
[root@server1 harbor]# docker tag reg.westos.org/rancher/mirrored-flannelcni-flannel:v0.19.2 www.hanxuan.com/rancher/mirrored-flannelcni-flannel:v0.19.2
[root@server1 harbor]# docker push www.hanxuan.com/rancher/mirrored-flannelcni-flannel:v0.19.2
The push refers to repository [www.hanxuan.com/rancher/mirrored-flannelcni-flannel]
83f8e343131f: Pushed
835dd64c8553: Pushed
ff282a43c8ba: Pushed
5699696bec4d: Pushed
65f0081cfe88: Pushed
3f5278879490: Pushed
7df5bd7bd262: Pushed
v0.19.2: digest: sha256:79c2231af0453bc38a8ad71ddf6116ade906a43f72460838f3435fb2ec19d88c size: 1785
[root@server1 harbor]# docker tag reg.westos.org/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0 www.hanxuan.com/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
[root@server1 harbor]# docker push www.hanxuan.com/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
The push refers to repository [www.hanxuan.com/rancher/mirrored-flannelcni-flannel-cni-plugin]
10a68dbd8595: Pushed
64242626f2eb: Pushed
v1.1.0: digest: sha256:190ba8db6e14fd49578bc7993cd44dc332ad31fd871acc0af0038bd026791795 size: 738
[root@server1 harbor]#

上传完成
在这里插入图片描述
在这里插入图片描述

三、kubernates集群部署之节点部署

你需要在每台机器上安装以下的软件包:

kubeadm:用来初始化集群的指令。

kubelet:在集群中的每个节点上用来启动 Pod 和容器等。

kubectl:用来与集群通信的命令行工具。

kubeadm 不能帮你安装或者管理 kubelet 或 kubectl, 所以你需要确保它们与通过 kubeadm 安装的控制平面的版本相匹配。 如果不这样做,则存在发生版本偏差的风险,可能会导致一些预料之外的错误和问题。 然而,控制平面与 kubelet 之间可以存在一个次要版本的偏差,但 kubelet 的版本不可以超过 API 服务器的版本。 例如,1.7.0 版本的 kubelet 可以完全兼容 1.8.0 版本的 API 服务器,反之则不可以。

1.节点一

安装工具

所有节点编写yum源,以便下载k8s工具

vim /etc/yum.repos.d/k8s.repo

[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-
x86_64/
enabled=1
gpgcheck=0

所有节点安装 kubeadm、kubelet 和 kubectl1.23.12版本

工具 用途
kubeadm 用来初始化集群的指令。
kubelet 在集群中的每个节点上用来启动 Pod 和容器等。
kubectl 用来与集群通信的命令行工具。
yum list --showduplicates kubeadm
yum install kubeadm-1.23.12-0 kubelet-1.23.12-0 kubectl-1.23.12-0

master节点列出所需镜像,指定版本

kubeadm config images list --image-repository registry.aliyuncs.com/google_containers --kubernetes-version 1.23.12
[root@k8s1 ~]# [root@k8s1 ~]# kubeadm config images list --image-repository registry.aliyuncs.com/google_containers --kubernetes-version 1.23.12
registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.12
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.12
registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.12
registry.aliyuncs.com/google_containers/kube-proxy:v1.23.12
registry.aliyuncs.com/google_containers/pause:3.6
registry.aliyuncs.com/google_containers/etcd:3.5.1-0
registry.aliyuncs.com/google_containers/coredns:v1.8.6

拉取镜像

kubeadm config images pull --image-repository www.hanxuan.com/k8s --kubernetes-version 1.23.12
[root@k8s1 ~]# kubeadm config images pull --image-repository www.hanxuan.com/k8s --kubernetes-version 1.23.12
[config/images] Pulled www.hanxuan.com/k8s/kube-apiserver:v1.23.12
[config/images] Pulled www.hanxuan.com/k8s/kube-controller-manager:v1.23.12
[config/images] Pulled www.hanxuan.com/k8s/kube-scheduler:v1.23.12
[config/images] Pulled www.hanxuan.com/k8s/kube-proxy:v1.23.12
[config/images] Pulled www.hanxuan.com/k8s/pause:3.6
[config/images] Pulled www.hanxuan.com/k8s/etcd:3.5.1-0
[config/images] Pulled www.hanxuan.com/k8s/coredns:v1.8.6

初始化集群,执行命令:

 kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository www.hanxuan.com/k8s --kubernetes-version 1.23.12
[root@k8s1 ~]#  kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository registry.aliyuncs.com/google_containers
I1016 11:54:46.764077   35606 version.go:255] remote version is much newer: v1.25.3; falling back to: stable-1.23
[init] Using Kubernetes version: v1.23.13
[preflight] Running pre-flight checks
[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 [k8s1 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.244.3.12]
[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 [k8s1 localhost] and IPs [10.244.3.12 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s1 localhost] and IPs [10.244.3.12 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 6.504632 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.23" in namespace kube-system with the configuration for the kubelets in the cluster
NOTE: The "kubelet-config-1.23" naming of the kubelet ConfigMap is deprecated. Once the UnversionedKubeletConfigMap feature gate graduates to Beta the default name will become just "kubelet-config". Kubeadm upgrade will handle this transition transparently.
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node k8s1 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 k8s1 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 8hyzif.vrfrlrs9qqcr2p0c
[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 10.244.3.12:6443 --token 8hyzif.vrfrlrs9qqcr2p0c \
        --discovery-token-ca-cert-hash sha256:b719b4afc1d20390047682c638844dc1556396ebdb4873132cf567c055646eeb

此处执行vim ~/.bash_profile
将命令export KUBECONFIG=/etc/kubernetes/admin.conf加入环境变量,以便下次重启生效
在这里插入图片描述
在这里插入图片描述

配置网络策略

配置k8s master节点网络,我们使用flannel
下载yaml文件

[root@k8s1 ~]# wget https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
--2022-10-16 12:03:19--  https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.110.133, 185.199.111.133, 185.199.108.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4583 (4.5K) [text/plain]
Saving to: ‘kube-flannel.yml’

100%[====================================================================================>] 4,583       --.-K/s   in 0.001s

2022-10-16 12:03:20 (8.52 MB/s) - ‘kube-flannel.yml’ saved [4583/4583]

[root@k8s1 ~]# ls
anaconda-ks.cfg  kube-flannel.yml  nginx-1.21.1  nginx-1.21.1.tar.gz  original-ks.cfg

在这里插入图片描述
编辑yaml文件

vim kube-flannel.yml
image: rancher/mirrored-flannelcni-flannel:v0.19.2
image: rancher/mirrored-flannelcni-flannel:v0.19.2
image: rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
[root@k8s1 ~]# kubectl apply -f kube-flannel.yml
namespace/kube-flannel 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
[root@k8s1 ~]# kubectl get pod -A
NAMESPACE      NAME                           READY   STATUS    RESTARTS   AGE
kube-flannel   kube-flannel-ds-4jfqv          1/1     Running   0          4m30s
kube-system    coredns-6d8c4cb4d-4756k        1/1     Running   0          125m
kube-system    coredns-6d8c4cb4d-4p7v8        1/1     Running   0          125m
kube-system    etcd-k8s1                      1/1     Running   0          126m
kube-system    kube-apiserver-k8s1            1/1     Running   0          126m
kube-system    kube-controller-manager-k8s1   1/1     Running   0          126m
kube-system    kube-proxy-wb4kg               1/1     Running   0          125m
kube-system    kube-scheduler-k8s1            1/1     Running   0          126m

2.其余节点网络策略

(其他节点也要安装 kubeadm、kubelet 和 kubectl1.23.12版本)

其他节点执行添加命令kubeadm join 10.244.3.12:6443 --token 8hyzif.vrfrlrs9qqcr2p0c --discovery-token-ca-cert-hash sha256:b719b4afc1d20390047682c638844dc1556396ebdb4873132cf567c055646eeb,加入k8s集群

[root@k8s2 ~]# kubeadm join 10.244.3.12:6443 --token 8hyzif.vrfrlrs9qqcr2p0c         --discovery-token-ca-cert-hash sha256:b719b4afc1d20390047682c638844dc1556396ebdb4873132cf567c055646eeb
[preflight] Running pre-flight checks
        [WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
[root@k8s3 ~]# kubeadm join 10.244.3.12:6443 --token 8hyzif.vrfrlrs9qqcr2p0c         --discovery-token-ca-cert-hash sha256:b719b4afc1d20390047682c638844dc1556396ebdb4873132cf567c055646eeb
[preflight] Running pre-flight checks
        [WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.


怎么查看添加命令

使用kubeadm token create --print-join-command

[root@k8s1 ~]# kubeadm token create --print-join-command
kubeadm join 10.244.3.12:6443 --token 6cg4q7.27y0t8dnov68tq8z --discovery-token-ca-cert-hash sha256:b719b4afc1d20390047682c638844dc1556396ebdb4873132cf567c055646eeb

在这里插入图片描述

本文含有隐藏内容,请 开通VIP 后查看