k8s学习

k8s学习记录

1.单机容器编排 docker-compose.yml
2.多宿主机容器编排(k8s)

学法

  1. 先安装好玩起来
  2. 学习原理
  3. 学习

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 < /etc/sysctl.d/k8s.conf
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.sh

    bash init-docker.sh

安装k8s的初始化工具 kubeadm 命令(所有节点)

安装k8s集群环境初始化的工具

kubelet-1.19.3 # 组件,对pod增删改查,pod可以运行在主节点、node节点上
kubeadm-1.19.3 # 自动拉取k8s基础组件镜像的一个工具
kubectl-1.19.3 # 管理、维护k8s客户端和服务端交互的一个命令行工具

脚本:

cat < /etc/yum.repos.d/kubernetes.repo
[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

部署网络插件

  1. 下载网络插件,yaml配置文件

git clone --depth l https://github.com/coreos/flannel.git

  1. 在 k8s主节点上,应用这个yaml创建具体的pod

  2. 操作
    下载、解压
    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

  3. 查看
    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

posted @   shog808  阅读(11)  评论(0编辑  收藏  举报
编辑推荐:
· 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语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示