k8s学习
k8s学习记录
1.单机容器编排 docker-compose.yml
2.多宿主机容器编排(k8s)
学法
- 先安装好玩起来
- 学习原理
- 学习
k8s安装
k8s-master
etcd 、 kube-apiserver 、 kube-controller-manager 、 kubectl 、 kubeadm 、 kubelet 、 kube-proxy 、 flannel
k8s-node
kubectl 、 kubelet 、 kube-proxy 、 flannel 、 docker
环境初始化
网络互相访问
cat >>/etc/hosts <<'EOF' 192.168.50.128 centos 192.168.50.129 centos-2 192.168.50.130 centos-3 EOF
开放端口
master节点:6443 2379 2380 60080 60081 UDP协议端口全部打开
node节点:UDP协议端口全部打开
设置iptables
systemctl stop firewalld NetworkManager
systemctl disable firewalld NetworkManager
sed -ri 's#(SELINUX=). *#\ldisabled#' /etc/selinux/config
setenforce 0
systemctl disable firewalld && systemctl stop firewalld
getenforce 0
iptables -F
iptables -X
iptables -Z
iptables -P FORWARD ACCEPT
关闭swap k8s默认禁用swap功能
swapoff -a
防止开机自动挂载 swap 分区
sed -i '/ swap / s/^(.*)$/#\l/g' /etc/fstab
阿里云源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
ed -i '/aliyuncs/d' /etc/yum.repos.d/*.repo
yum clean all && yum makecache fast
ntp配置
yum install chrony -y #时间同步工具
systemctl start chronyd
systemctl enable chronyd
date
hwclock -w
vim /etc/chrony.conf # 加入 server ntp.aliyun.com iburst
ntpdate -u ntp.aliyun.com
hwclock -w
修改内核参数:打开网络转发功能
cat <
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.max_map_count=262144
EOF
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
安装docker
- 脚本:
yum remove docker docker-common docker-selinux docker-engine -y
curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum list docker-ce --showduplicates
yum install docker-ce-19.03.15 docker-ce-cli-19.03.15 -y
配置docker加速器以及cgroup驱动,改为k8s官方推荐的systemd,否则初始化时会有报错
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors":[
"https://ms9glx6x.mirror.aliyuncs.com"],
"exec-opts":["native.cgroupdriver=systemd"]
}
EOF
-
启动
systemctl start docker && systemctl enable docker
docker version -
执行:
vim init-docker.shbash init-docker.sh
安装k8s的初始化工具 kubeadm 命令(所有节点)
安装k8s集群环境初始化的工具
kubelet-1.19.3 # 组件,对pod增删改查,pod可以运行在主节点、node节点上
kubeadm-1.19.3 # 自动拉取k8s基础组件镜像的一个工具
kubectl-1.19.3 # 管理、维护k8s客户端和服务端交互的一个命令行工具
脚本:
- 设置阿里源
curl -o /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
cat <
[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
EOF
yum clean all && yum makecache
yum list kubeadm --showduplicates
- 安装指定版本 kubeadm版本就是拉取k8s的版本
yum install kubelet-1.19.3 kubeadm-1.19.3 kubectl-1.19.3 ipvsadm -y
systemctl enable kubelet # 开机启动
- 执行:
bash init-k8s.sh
查看端口占用状态
netstat -tunlp
初始化k8s-master主节点
脚本:
kubeadm init
--apiserver-advertise-address=192.168.50.128
--image-repository registry.aliyuncs.com/google_containers
--kubernetes-version v1.19.3
--service-cidr=10.01.0.0/16
--pod-network-cidr=10.02.0.0/16
--service-dns-domain=cluster.local
--ignore-preflight-errors=Swap
打印如下表示成功
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.50.128:6443 --token vavtzo.daqsr9jkcqcpvb17 \
--discovery-token-ca-cert-hash sha256:32e230e97f4b91022da7b7f0a277d3fd893e88316564365483237623282aac7b
查看k8s节点
kubectl get nodes
加入node节点
在node节点执行命令:
kubeadm join 192.168.50.128:6443 --token vavtzo.daqsr9jkcqcpvb17
--discovery-token-ca-cert-hash sha256:32e230e97f4b91022da7b7f0a277d3fd893e88316564365483237623282aac7b
配置k8s命令补全
操作节点:k8s-master
yum install bash-completion -y
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
部署网络插件
- 下载网络插件,yaml配置文件
git clone --depth l https://github.com/coreos/flannel.git
-
在 k8s主节点上,应用这个yaml创建具体的pod
-
操作
下载、解压
cd flannel-master/Documentation
vim kube-flannel.yml
a. 修改网段,搜索 Network 修改为 kubeadm init 的 --pod-network-cidr=10.02.0.0/16
b. 修改网卡 containers.args 添加参数 - --iface=ens33
c. 执行创建命令 kubectl create -f ./kube-flannel.yml -
查看
docker ps|grep flannel
5.为啥所有节点都有了 flannel:主节点安装pod(flannel)的命令存到etcd数据库,node节点一直在监听etcd数据库变化
k8s 使用
1.kubectl get nodes -o wide
2.kubectl get pods -o wide
3.kubectl run --help
4.kubectl run linux-test-pod-1-nginx --image=nginx:1.14.1
问题
flannel问题
vim /run/flannel/subnet.env
FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true
scp /etc/kubernetes/admin.conf root@192.168.50.130:/etc/kubernetes
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构