kubernetes学习笔记-集群搭建篇

发布时间:2023-04-17 18:00

准备

关闭交换分区:
https://blog.csdn.net/u013271384/article/details/125155464

  1. 设置主机名(以主机名为master为例,ip地址192.168.12.141)
hostnamectl set-hostname master --static
  1. 添加host编辑/etc/hosts,添加相应的ip和主机名
192.168.12.141	master
  1. 清空防火墙规则,关闭selinux (只是学习使用,生产环境依照实际情况配置)
 iptables -F                     (清空防火墙,不要关闭)
 iptables -t nat -F              (清空net表防火墙)
 setenforce 0                    (临时关闭selinux,没安装不运行)
 sed -i '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config      (永久关闭selinux)
  1. 转发IPV4,让 iptables 可以监控到桥接的流量(原文:Forwarding IPv4 and letting iptables see bridged traffic,自己理解下…)

方法1

# sysctl params required by setup, params persist across reboots
cat <| sudo tee /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
EOF

sudo sysctl --system

方法2
修改/etc/sysctl.conf,添加

net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1

保存后执行

sysctl -p
  1. 加载内核模块

方法1(通用)

cat <| sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
EOF

sudo modprobe overlay
sudo modprobe br_netfilter
sudo modprobe ip_vs
sudo modprobe ip_vs_rr
sudo modprobe ip_vs_wrr
sudo modprobe ip_vs_sh
sudo modprobe nf_conntrack

方法2 (centos7)

cat > /etc/sysconfig/modules/ipvs.modules << EOF
#!/bin/bash
modprobe -- overlay
modprobe -- br_netfilter
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF

chmod 755 /etc/sysconfig/modules/ipvs.modules
bash /etc/sysconfig/modules/ipvs.modules
lsmod | grep -E "ip_vs|nf_conntrack_ipv4"

ps:使用命令: lsmod | grep br_netfilter 可以查看br_netfilter模块是否加载,使用sudo modprobe br_netfilter可以执行加载。
nf_conntrack_ipv4如果不存在换成nf_conntrack试试

  1. 配置阿里云代理
    https://blog.csdn.net/u013271384/article/details/125508971

docker搭建

https://blog.csdn.net/u013271384/article/details/125355087

kubeadm kubelet kubectl 安装

  1. 安装docker并且配置好相关源以及配置后,安装
ubuntu
sudo apt-get install kubeadm kubelet kubectl


安装指定版本,aliyun仓库可能没有同步最新版本的apiserver,以此推荐安装指定版本

apt install kubeadm=1.23.8-00 kubelet=1.23.8-00 kubectl=1.23.8-00
centos
yum insatll -y kubeadm kubelet kubectl
yum insatll -y kubeadm-1.23.8 kubelet-1.23.8 kubectl-1.23.8
  1. 查看kubectl安装信息
kubectl version --client --output=yaml
  1. 集群初始化,安装master
kubeadm init --kubernetes-version 1.23.8 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --service-cidr 10.96.0.0/16 --pod-network-cidr 10.244.0.0/16

参数说明

--kubernetes-version 指定安装kubernetes版本
--image-repository 指定容器镜像拉取地址,默认地址: k8s.gcr.io
--service-cidr service的ip段. (默认 "10.96.0.0/12")
--pod-network-cidr  pod运行时分配的ip,flannel配置指定10.244.0.0/16  (Specify range of IP addresses for the pod network. If set, the control plane will automatically allocate CIDRs for every node.)

默认情况下kube-apiserver,kube-scheduler,kube-controller-manager,kube-proxy,etcd以镜像形式安装

报错(可忽略):
https://blog.csdn.net/u013271384/article/details/125531746
运行结果(可忽略):

rk-cidr 10.244.0.0/16
[init] Using Kubernetes version: v1.23.8
[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 [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local master] and IPs [10.96.0.1 192.168.12.144]
[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 master] and IPs [192.168.12.144 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master] and IPs [192.168.12.144 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 8.005424 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 master 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 master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: i8qo0l.2ksy6x7hwit6m641
[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 192.168.12.144:6443 --token i8qo0l.2ksy6x7hwit6m641 \
	--discovery-token-ca-cert-hash sha256:4ea11d592fe94958816f58cd67893bbec7c713c3ed393e6282719b71ae77daaf 
  1. 按照提示,敲命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
  1. 查看节点运行情况
    网络相关组件还没安装,因此部分节点没有运行
kubectl get nodes
kubectl get pods -n kube-system

相关命令:
https://blog.csdn.net/u013271384/article/details/125463578

安装网络组件

  1. 安装
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml

或将kube-flannel.yml下载至本地
地址1: https://download.csdn.net/download/u013271384/85836583
地址2:https://github.com/flannel-io/flannel Documentation目录下

kubectl apply -f kube-flannel.yml
  1. 查看节点运行情况
kubectl get nodes
kubectl get pods -n kube-system

节点部署

安装好kubeadm kubelet kubectl 之后,运行下面命令

kubeadm join 192.168.12.144:6443 --token i8qo0l.2ksy6x7hwit6m641 \
	--discovery-token-ca-cert-hash sha256:4ea11d592fe94958816f58cd67893bbec7c713c3ed393e6282719b71ae77daaf 

上面的命令来自kubeadm init后生成的,如果不记得,可以运行下面的命令生成:

kubeadm token create --print-join-command

重置

kubelet清理

systemctl stop kubelet
rm -rf /var/lib/kubelet /etc/kubernetes
apt remove kubelet        或       rpm -e kubelet --nodeps

集群重置

kubeadm reset
rm -rf /etc/kubernetes

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号