kubeadm方式部署k8s集群

Posted _雪辉_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kubeadm方式部署k8s集群相关的知识,希望对你有一定的参考价值。

文章目录

一、基础配置

1.1 将master节点和node节点配置到/etc/hosts

1.2 关闭防火墙

systemctl stop firewalld
systemctl disable firewalld
setenforce 0 && sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

1.3 关闭swap缓存

cat>>/etc/sysctl.conf <<EOF
vm.swappiness = 0

二、 软件部署

2.1 内核配置

modprobe br_netfilter
lsmod | grep br_netfilter
 
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
 
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
 
sudo sysctl --system

2.2 启动kubelet

# 启动docker
mkdir /etc/docker
cat >> /etc/docker/daemon.json <<EOF

  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": 
    "max-size": "100m"
  ,
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ],
  "data-root": "/data/docker"

EOF
#要保持docker和kubelet cgroup一致
systemctl enable docker.service
systemctl start docker.service

# 配置镜像源
cat <<EOF | sudo tee /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

#安装相关软件
yum install -y kubelet kubeadm kubectl --nogpgcheck
 
# 设置开机自启动
systemctl enable kubelet
systemctl start kubelet


三、 部署kubernetes集群

3.1 部署kubeadm(master节点执行)

# 初始化kubeadm
kubeadm init --kubernetes-version=1.23.3 --apiserver-advertise-address=10.0.213.157   --image-repository registry.aliyuncs.com/google_containers  --service-cidr=10.1.0.0/16 --pod-network-cidr=10.2.0.0/16
[init] Using Kubernetes version: v1.23.3
[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 sysmessage-mydb01] and IPs [10.1.0.1 10.0.213.157]
[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 sysmessage-mydb01] and IPs [10.0.213.157 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost sysmessage-mydb01] and IPs [10.0.213.157 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.003525 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 sysmessage-mydb01 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 sysmessage-mydb01 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 13bqa9.ohrw3s09v53yks8p
[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.0.213.157:6443 --token 13bqa9.ohrw3s09v53yks8p \\
	--discovery-token-ca-cert-hash sha256:1a59bb40d2682bb6dc9c90c402d30de268eea76fe76e251a5c5cb0f575200354

export KUBECONFIG=/etc/kubernetes/admin.conf
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >>/etc/profile
[root@sysmessage-mydb01 ~]# kubectl get node
NAME                STATUS     ROLES                  AGE     VERSION
sysmessage-mydb01   NotReady   control-plane,master   2m10s   v1.23.3

3.2 安装Pod网络插件

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

Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
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

wait...

# kubectl get node
NAME                STATUS   ROLES                  AGE     VERSION
sysmessage-mydb01   Ready    control-plane,master   8m34s   v1.23.3

3.3 添加node节点(每个node节点执行)

kubeadm join 10.0.213.157:6443 --token 13bqa9.ohrw3s09v53yks8p --discovery-token-ca-cert-hash sha256:1a59bb40d2682bb6dc9c90c402d30de268eea76fe76e251a5c5cb0f575200354
[preflight] Running pre-flight checks
[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.


#kubectl get node
NAME                     STATUS   ROLES                  AGE    VERSION
sysmessage-mydb01        Ready    control-plane,master   135m   v1.23.3
sysmessage-mydb02        Ready    <none>                 13m    v1.23.3
sysmessage-sh05-mydb01   Ready    <none>                 72m    v1.23.3
sysmessage-sh05-mydb02   Ready    <none>                 51s    v1.23.3

四、 部署dashboard

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml

kubectl get pods --all-namespaces
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
kube-system            coredns-6d8c4cb4d-2nzbm                      1/1     Running   0          136m
kube-system            coredns-6d8c4cb4d-rqgxg                      1/1     Running   0          136m
kube-system            etcd-sysmessage-mydb01                       1/1     Running   1          136m
kube-system            kube-apiserver-sysmessage-mydb01             1/1     Running   1          136m
kube-system            kube-controller-manager-sysmessage-mydb01    1/1     Running   1          136m
kube-system            kube-flannel-ds-hjsbg                        1/1     Running   0          14m
kube-system            kube-flannel-ds-kdl24                        1/1     Running   0          128m
kube-system            kube-flannel-ds-knfzn                        1/1     Running   1          83m
kube-system            kube-flannel-ds-tn4jf                        1/1     Running   0          2m15s
kube-system            kube-proxy-4bffc                             1/1     Running   0          2m15s
kube-system            kube-proxy-5c2f9                             1/1     Running   1          83m
kube-system            kube-proxy-k9j9f                             1/1     Running   1          14m
kube-system            kube-proxy-tvb4r                             1/1     Running   0          136m
kube-system            kube-scheduler-sysmessage-mydb01             1/1     Running   1          136m
kubernetes-dashboard   dashboard-metrics-scraper-799d786dbf-q4z44   1/1     Running   0          26s
kubernetes-dashboard   kubernetes-dashboard-6b6b86c4c5-df2sb        1/1     Running   0          26s

以上是关于kubeadm方式部署k8s集群的主要内容,如果未能解决你的问题,请参考以下文章

kubeadm方式部署k8s集群

kubeadm方式部署k8s集群

云原生 • Kubernetes搭建 k8s 集群(Kubeadm 方式)

使用kubeadm的方式部署v1.21.2版本k8s

k8s/kubeadm 生产环境高可用集群部署

kubernetes 概述和kubeadm方式集群搭建