k8s学习一:使用kubeadm安装k8s

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: k8s学习一:使用kubeadm安装k8s

写在开头

在学习整个k8s之前,先想办法搭建个k8s出现成果,然后根据这个成果进行深入学习,才会让人有学习的动力,本文将记录自己的安装k8s教程

准备工作:

一台ubuntu服务器(虚拟机)

k8s环境配置

host配置

我们先给服务器定义好hosts,便于直接找到该服务器ip

192.168.192.9 master

注意,后面如果需要增加集群,也需要配置其他的hosts

主机名修改(非必要)

修改 /etc/hostname 改为 master

关闭防火墙


由于k8s的防火墙规则和系统的冲突,所以需要关闭系统的防火墙

sudo ufw disable
systemctl stop ufw

关闭selinux

关闭selinux以允许容器访问宿主机的文件系统  (新装的Ubuntu好像没这个东西,可以自行百度)

禁用swap

swap会在内存不足的时候使用磁盘当做内存,但是效率会非常低,导致服务器直接卡死

sudo swapoff -a
vi /etc/fstab  # 将swap一行前面增加个# 号注释掉

网络参数

vi /etc/sysctl.d/k8s.conf  #将下面的3行(去掉#号写入到此文件)
#net.bridge.bridge-nf-call-ip6tables = 1
#net.bridge.bridge-nf-call-iptables = 1
#net.ipv4.ip_forward = 1
modprobe br_netfilter  
sysctl -p /etc/sysctl.d/k8s.conf

安装docker

apt-get install docker.io -y
# 设置开机启动并启动docker  
sudo systemctl start docker
sudo systemctl enble docker
# 修改docker运行时,并且增加镜像
cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": \["native.cgroupdriver=systemd"\],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "registry-mirrors": \[
    "https://reg-mirror.qiniu.com/"
  \]
  "storage-driver": "overlay2"
}
EOF

配置ubuntu k8s 安装源

#使得 apt 支持 ssl 传输
apt-get update && apt-get install -y apt-transport-https
#下载 gpg 密钥
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
#添加 k8s 镜像源
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
#更新
apt-get update

安装 kubeadm、kubelet、kubectl

注意!这里最好是只安装 1.24之前的版本,新版本可能安装很多问题,导致无心学习,我这里选择的是1.23.10

apt list--all-versions package_name  ##查看版本 
apt-get install -y kubeadm=1.23.10-00 kubelet=1.23.10-00 kubectl=1.23.10-00
# 阻止自动更新(apt upgrade时忽略)。所以更新的时候先unhold,更新完再hold。
apt-mark hold kubelet kubeadm kubectl

通过kubeadm 进行初始化k8s集群

kubeadm init \
--apiserver-advertise-address 192.168.192.9 \
--apiserver-bind-port 6443 \
--pod-network-cidr 10.244.0.0/16 \
--image-repository registry.aliyuncs.com/google_containers

如果初始化出问题,就需要自己百度多解决了

网络异常,图片无法展示
|

初始化成功之后:

o/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: mdutmg.pbecp9mqcowc4b0u
\[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.192.9:6443 --token mdutmg.pbecp9mqcowc4b0u \
        --discovery-token-ca-cert-hash sha256:61f8d9b13b94a3c7eff88e25faf1c873cfd559d1ee2f2988009ac85de11ec730

保存最后的kubeadm join 代码,后面加入集群有用

查看集群是否成功部署:

kubectl get nodes

如果提示:The connection to the server localhost:8080 was refused - did you specify the right host or port?

是因为没有绑定好集群环境

echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile
source /etc/profile

即可正常显示

image.png

这边可以看到,STATUS 是NotReady状态,因为还有网络插件没装

安装 Pod Network(flannel网络插件)

wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

可以查看到kube-flannel的配置项

image.png

自己手动拉下镜像,避免部署的时候失败:

root@test02:/home/tioncico# docker pull docker.io/rancher/mirrored-flannelcni-flannel:v0.19.2

部署flannel插件

root@tioncico-pc:/home/tioncico# 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@tioncico-pc:/home/tioncico# 
root@test02:/home/tioncico# kubectl get pod --all-namespaces
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
kube-flannel           kube-flannel-ds-rfx4q                        1/1     Running   0          4s
kube-system            coredns-6d8c4cb4d-575sw                      1/1     Running   0          65m
kube-system            coredns-6d8c4cb4d-p4nv8                      1/1     Running   0          65m
kube-system            etcd-master                                  1/1     Running   0          66m
kube-system            kube-apiserver-master                        1/1     Running   0          66m
kube-system            kube-controller-manager-master               1/1     Running   0          66m
kube-system            kube-proxy-n2n4n                             1/1     Running   0          65m
kube-system            kube-scheduler-master                        1/1     Running   0          66m
kubernetes-dashboard   dashboard-metrics-scraper-6f669b9c9b-86hm6   1/1     Running   0          28m
kubernetes-dashboard   kubernetes-dashboard-54c5fb4776-qb2lf        1/1     Running   0          28m
root@test02:/home/tioncico#

可看到nodes,pods一切正常

root@test02:/home/tioncico# kubectl get pods --all-namespaces
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
kube-flannel           kube-flannel-ds-rfx4q                        1/1     Running   0          58s
kube-system            coredns-6d8c4cb4d-575sw                      1/1     Running   0          66m
kube-system            coredns-6d8c4cb4d-p4nv8                      1/1     Running   0          66m
kube-system            etcd-master                                  1/1     Running   0          67m
kube-system            kube-apiserver-master                        1/1     Running   0          67m
kube-system            kube-controller-manager-master               1/1     Running   0          67m
kube-system            kube-proxy-n2n4n                             1/1     Running   0          66m
kube-system            kube-scheduler-master                        1/1     Running   0          67m
kubernetes-dashboard   dashboard-metrics-scraper-6f669b9c9b-86hm6   1/1     Running   0          29m
kubernetes-dashboard   kubernetes-dashboard-54c5fb4776-qb2lf        1/1     Running   0          29m
root@test02:/home/tioncico# kubectl get nodes
NAME     STATUS   ROLES                  AGE   VERSION
master   Ready    control-plane,master   67m   v1.23.10
root@test02:/home/tioncico#

安装失败重置集群

#在master节点之外的节点进行操作
kubeadm reset
systemctl stop kubelet
systemctl stop docker
rm -rf /var/lib/cni/
rm -rf /var/lib/kubelet/*
rm -rf /etc/cni/
ifconfig cni0 down
ifconfig flannel.1 down
ifconfig docker0 down
ip link delete cni0
ip link delete flannel.1
##重启kubelet
systemctl restart kubelet
##重启docker
systemctl restart docker
相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
17天前
|
Kubernetes Ubuntu jenkins
超详细实操教程!在现有K8S集群上安装JenkinsX,极速提升CI/CD体验!
超详细实操教程!在现有K8S集群上安装JenkinsX,极速提升CI/CD体验!
|
19天前
|
存储 Kubernetes Cloud Native
云原生 - Kubernetes基础知识学习
云原生 - Kubernetes基础知识学习
23 0
|
2月前
|
Kubernetes 负载均衡 应用服务中间件
k8s 二进制安装 优化架构之 部署负载均衡,加入master02
k8s 二进制安装 优化架构之 部署负载均衡,加入master02
|
2月前
|
Kubernetes 网络安全 API
k8s 二进制安装 详细安装步骤(二)
k8s 二进制安装 详细安装步骤(二)
|
2月前
|
Kubernetes 算法 API
k8s 二进制安装 详细安装步骤(一)
k8s 二进制安装 详细安装步骤(一)
|
2月前
|
Kubernetes 数据安全/隐私保护 Docker
kubeadm 工具实验 k8s一键安装
kubeadm 工具实验 k8s一键安装
|
2月前
|
Kubernetes 应用服务中间件 nginx
Kubernetes学习-深入Pod篇(一) 创建Pod,Pod配置文件详解
Kubernetes学习-深入Pod篇(一) 创建Pod,Pod配置文件详解
|
1月前
|
Kubernetes 微服务 容器
Aspire项目发布到远程k8s集群
Aspire项目发布到远程k8s集群
406 2
Aspire项目发布到远程k8s集群
|
1月前
|
Kubernetes Cloud Native 微服务
微服务实践之使用 kube-vip 搭建高可用 Kubernetes 集群
微服务实践之使用 kube-vip 搭建高可用 Kubernetes 集群
219 3
|
5天前
|
存储 Kubernetes 监控
Kubernetes 集群的持续性能优化策略
【5月更文挑战第70天】 随着容器化技术的普及,Kubernetes 已成为管理微服务架构的首选平台。然而,在大规模部署和长期运行过程中,集群往往会遭遇性能瓶颈,影响服务的响应速度和稳定性。本文将探讨针对 Kubernetes 集群的性能优化策略,包括资源调度优化、网络延迟降低、存储效率提升及监控与日志分析等方面,旨在为运维工程师提供一套系统化的持续优化方法,确保集群性能的长期稳定。