dayi的大键盘
dayi的大键盘

云与虚拟化_实验14 K8S

K8S

K8S部署

先创建一个debian模板

https://cmd.dayi.ink/uploads/upload_78895650660d7632aff3fbcb21486a2b.png

选择iso

https://cmd.dayi.ink/uploads/upload_8df897882fe69fd0f064edca9ba7c049.png

快速安装一下即可:

https://cmd.dayi.ink/uploads/upload_bbb72e77d295ed3582a448f661994ff4.png

安装完成克隆成模板:

https://cmd.dayi.ink/uploads/upload_4e01593ed77595a33cb4e83587418c00.png

Master节点

直接克隆一个就可以

https://cmd.dayi.ink/uploads/upload_4f3bd6cd70d408b501442ec9225337f5.png
https://cmd.dayi.ink/uploads/upload_a69e7a1dca5148c6f3d622dca14df4fd.png

克隆两个本地node

https://cmd.dayi.ink/uploads/upload_39e8d8e53fddbb693ee3d49adb0b45cc.png
https://cmd.dayi.ink/uploads/upload_bc570d7a6bc7a88a264b51d73f1520e8.png
sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources
sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list
echo "export PATH=$PATH:/usr/sbin:/home/$(whoami)/.local/bin" >> ~/.bashrc
source ~/.bashrc
apt install ufw

apt install sudo vim wget curl -y

sudo hostnamectl set-hostname dayi-cloud-k8s-master

这里HOSTS用了tailscile 来进行相互访问

curl -fsSL https://tailscale.com/install.sh | sh
https://cmd.dayi.ink/uploads/upload_1b3ee77c25f562ddf352e728368e3648.png
https://cmd.dayi.ink/uploads/upload_d530c8c99423e886ce53e1f8b3629f41.png
https://cmd.dayi.ink/uploads/upload_622bfea7593128a9dab2c1d503649f92.png

这样就配好hosts了

https://cmd.dayi.ink/uploads/upload_19b44997ae0a7ff16d2c4b6ff6c5e73e.png
dayi-cloud-k8s-master
dayi-cloud-k8s-node1
dayi-cloud-k8s-node2

Master

sudo ufw allow 6443/tcp
sudo ufw allow 2379/tcp
sudo ufw allow 2380/tcp
sudo ufw allow 10250/tcp
sudo ufw allow 10251/tcp
sudo ufw allow 10252/tcp
sudo ufw allow 10255/tcp
sudo ufw reload

cat <

Other

sudo ufw allow 10250/tcp
sudo ufw allow 30000:32767/tcp
sudo ufw reload

cat <
https://cmd.dayi.ink/uploads/upload_116f996d93633e33a355d9a3ac9f5770.png

SSH-密钥复制

mkdir ~/.ssh
echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDk1VvdE9ZZFnK5Bwhi+C74B/rLvT7zKHN4/tUhzLFrMVDQZvsF+8ON9w4xHK1AZyb64GQXs9BtX93N+OJ51t5ZbEl960S0o7BYgcdB7q3+wb9E4uTYNFK7akcBgtxa+3gmZTmttr1l2KT3xzfE9BebkRg+C/DO/PwnaPwOWyeYc90fmcpk7voM2e268wmv6V2eZmIKaA/T7GXCe22qKIcrgWZp78BHL1Je+sqQ72FfzGasDj/iDCcqbW6fsQ4v0QHsuQ4SavcT3xvPDPTwCow4CoV8cQiK4s2nak/5z8lZF07FRJsF0oYcg7m+9qizLv3jBi/P1M26Rhpj5fV6XmdBtIpSY8A958U2bPT/16eAJOcyj+0qFL2E0DWs7Hbh8tni4L51mppQfx4VnozzEzPPaEauGA/GNQ+HlKcaEe43bBia7btW9K9c6EHPvolAf/365gv4BcKArlbOVo4kfxqfX7ybjSnK2pz3kZjFDrXwhQ0LsVqlZIvGu+wkHIVFYHM= root@dayi-cloud-k8s-maste" >> ~/.ssh/authorized_keys

安装containerd

apt install containerd
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo vi /etc/containerd/config.toml

scp /etc/containerd/config.toml dayi-cloud-k8s-node1:/etc/containerd/config.toml
scp /etc/containerd/config.toml dayi-cloud-k8s-node2:/etc/containerd/config.toml

sudo systemctl restart containerd
sudo systemctl enable containerd
https://cmd.dayi.ink/uploads/upload_baecd5feeefb1bf34edb3df0888c71e2.png

安装K8S仓库


sudo apt install gnupg gnupg2 curl software-properties-common -y
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/cgoogle.gpg
apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
https://cmd.dayi.ink/uploads/upload_114852975238e7cd633e665ef722960e.png

安装K8S

apt update
apt install kubelet kubeadm kubectl -y
apt-mark hold kubelet kubeadm kubectl
https://cmd.dayi.ink/uploads/upload_90ea9c0bbb630f8671338aa6b5008075.png
https://cmd.dayi.ink/uploads/upload_ac61767116d2c3c0a9d98037026ebf8d.png

初始化集群:

kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.234.0.0/16 --control-plane-endpoint=dayi-cloud-k8s-master --v=5

虚拟化K8S安装

# 设置主机名
hostnamectl set-hostname m1
hostnamectl set-hostname node1
hostnamectl set-hostname node2
、
===== 192.168.0.202 node2

# 使用scp复制/etc/hosts到node1和node2
scp /etc/hosts [email protected]:/etc/hosts
scp /etc/hosts [email protected]:/etc/hosts

# 关闭swap并配置内核参数
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab
echo -e 'net.bridge.bridge-nf-call-iptables = 1 \nnet.bridge.bridge-nf-call-ip6tables = 1' >> /etc/sysctl.conf

# 安装基本软件包
yum -y install wget ntpdate

# 配置时间同步
ntpdate ntp1.aliyun.com
crontab -e
# 添加以下内容
*/1 * * * * /usr/sbin/ntpdate ntp1.aliyun.com
systemctl restart crond.service
reboot

# 配置yum源及下载Kubernetes和Docker相关软件包
cd /etc/yum.repos.d
rm -f CentOS-*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
vi /etc/yum.repos.d/kubernetes.repo
# 添加kubernetes源的配置信息
wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
rpm -import rpm-package-key.gpg
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum clean all
yum makecache fast
cd

# 安装docker-ce并启动服务
yum -y install docker-ce
systemctl start docker
systemctl enable docker

# 配置Docker的镜像加速器
vi /etc/docker/daemon.json
# 添加以下内容
{
    "registry-mirrors": ["https://x3nqjrcg.mirror.aliyuncs.com"]
}

# Docker降级为20.10
yum downgrade --setopt=obsoletes=0 -y docker-ce-20.10.24 docker-ce-selinux-20.10.24 containerd.io

# 修改Docker Cgroup Driver为systemd
vi /etc/docker/daemon.json
# 添加以下内容
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": ["https://x3nqjrcg.mirror.aliyuncs.com"]
}

# 重启docker服务
systemctl daemon-reload
systemctl restart docker

# 安装kubeadm、kubectl、kubelet
yum install -y kubelet-1.23.17 kubeadm-1.23.17 kubectl-1.23.17
systemctl start kubelet
systemctl enable kubelet

# 下载Kubernetes镜像
kubeadm config images list --kubernetes-version v1.23.17
kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers

# 初始化Kubernetes集群
kubeadm init \
--kubernetes-version=v1.23.17 \
--pod-network-cidr=10.244.0.0/16 \
--service-cidr=10.96.0.0/12 \
--apiserver-advertise-address=192.168.8.10 \
--image-repository=registry.aliyuncs.com/google_containers 

# 配置环境变量
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# 安装Flannel网络
wget https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
kubectl apply -f kube-flannel.yml

# 将节点加入到集群
kubeadm join 192.168.8.10:6443 --token [token] --discovery-token-ca-cert-hash [hash]

# 查看集群状态
kubectl get nodes

Debian安装

K8S2

https://cmd.dayi.ink/uploads/upload_c1fdb42fb798fee71613de7d2a23c3bc.png

哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈

换CENTOS

https://cmd.dayi.ink/uploads/upload_e2d3236a6f66e4343aa4203cae778314.png

换大牛

https://cmd.dayi.ink/uploads/upload_4f40ef939a89981e6b70a8d6abe8714e.png

Rencher

https://cmd.dayi.ink/uploads/upload_24643119687b65a3acbe9ea64dab57a8.png
https://cmd.dayi.ink/uploads/upload_9db75971871a496bfffec339582c76f2.png

真男人直接下载:

https://cmd.dayi.ink/uploads/upload_e99cd20291e675fae58175901452b49c.png
https://cmd.dayi.ink/uploads/upload_90a363323e31fc6a36aa2873f756a8d6.png
https://cmd.dayi.ink/uploads/upload_d35cf8cb8f9679946303b3adeb2c9d3b.png
https://cmd.dayi.ink/uploads/upload_082811a423ea27e4c8e0a8ab4e274973.png
https://cmd.dayi.ink/uploads/upload_1cd676126b3b603539eaa6df05831003.png

修改主机名:

https://cmd.dayi.ink/uploads/upload_a76570a7110cca0869b76d847783782d.png
https://cmd.dayi.ink/uploads/upload_a2fc43d67b4d16d8936e9524465ca814.png
ssh-keygen -t ecdsa -b 256
ssh-copy-id node1
ssh-copy-id node2
https://cmd.dayi.ink/uploads/upload_e0e2368ece240b6cab868efeebc8b0e1.png
https://cmd.dayi.ink/uploads/upload_6599744aee4473962bca38145ea878ce.png

复制密钥:

ssh-copy-id node2
https://cmd.dayi.ink/uploads/upload_2fc8f5dec366ca27f5a6e514e459e2fb.png

复制hosts

scp /etc/hosts root@node1:/etc/hosts 
scp /etc/hosts root@node2:/etc/hosts 
https://cmd.dayi.ink/uploads/upload_b13b70c48617c8214c1cdbb753237cdf.png

修改内核配置

/etc/sysctl.conf

vi:

echo -e 'net.bridge.bridge-nf-call-iptables = 1 \nnet.bridge.bridge-nf-call-ip6tables = 1' >> /etc/sysctl.conf

开启模块

modprobe br_netfilter
ls /proc/sys/net/bridge/
echo "br_netfilter" > /etc/modules-load.d/br_netfilter.conf
sysctl -p
https://cmd.dayi.ink/uploads/upload_5296d39e9397f966085b41bf07b1b979.png

发送到从节点:

scp /etc/sysctl.conf root@node1:/etc/sysctl.conf
scp /etc/sysctl.conf root@node2:/etc/sysctl.conf 

#从节点执行:
modprobe br_netfilter
ls /proc/sys/net/bridge/
echo "br_netfilter" > /etc/modules-load.d/br_netfilter.conf
sysctl -p
https://cmd.dayi.ink/uploads/upload_788b9b9a3a9af55d7f44c9cca8c8f99e.png

防火墙 SELINUX:

systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld

setenforce 0
vi /etc/selinux/config
#修改: SELINUX=disabled

reboot

发送到从节点

scp /etc/selinux/config root@node1:/etc/selinux/config
scp /etc/selinux/config root@node2:/etc/selinux/config
https://cmd.dayi.ink/uploads/upload_4d61d1ea945cf9e92ad3b57142d23475.png
https://cmd.dayi.ink/uploads/upload_3d717fddc62295e41f9fffc2a69720a7.png

查看SELINUX状态

sestatus

NTP时间同步

yum -y install wget ntpdate

crontab -e
*/1 * * * * /usr/sbin/ntpdate ntp1.aliyun.com

systemctl restart crond.service

添加源


vi /etc/yum.repos.d/kubernetes.repo

[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
https://cmd.dayi.ink/uploads/upload_1c2e8399f36b05fe9bd108302be8cd83.png

安装

yum downgrade --setopt=obsoletes=0 -y docker-ce-20.10.24 docker-ce-selinux-20.10.24 containerd.io


yum install -y kubelet-1.23.17 kubeadm-1.23.17 kubectl-1.23.17
systemctl start kubelet
systemctl enable kubelet
systemctl status kubelet
kubeadm config images list --kubernetes-version v1.23.17

初始化

mv /etc/containerd/config.toml /root/config.toml.bak systemctl restart containerd

/etc/kubernetes/manifests/kube-scheduler.yaml

rm -rf /etc/kubernetes/manifests/kube-apiserver.yaml rm -rf /etc/kubernetes/manifests/kube-controller-manager.yaml rm -rf /etc/kubernetes/manifests/kube-scheduler.yaml rm -rf /etc/kubernetes/manifests/etcd.yaml

kubeadm init \ --pod-network-cidr=10.244.0.0/16 \ --service-cidr=10.96.0.0/12 \ --apiserver-advertise-address=192.168.59.137 \ --image-repository=registry.aliyuncs.com/google_containers --v=5

rm -f /etc/kubernetes/controller-manager.conf rm -f /etc/kubernetes/scheduler.conf rm -f /etc/kubernetes/scheduler.conf rm -f /etc/kubernetes/admin.conf rm -f /etc/kubernetes/kubelet.conf

mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config

https://cmd.dayi.ink/uploads/upload_7d053e4a60221a5977c6968f88d485d4.png
kubeadm join 192.168.59.137:6443 --token y8m7u7.3ur44mc2ezb87fmq \
        --discovery-token-ca-cert-hash sha256:cb9d1bfa0c3adaabd9a34cd072e1d27c13c9cf9e6e3ef4a0c810b84d6fdf4c9f

节点:

https://cmd.dayi.ink/uploads/upload_82632cfc8cfb3b7e71d88977c973c24e.png
https://cmd.dayi.ink/uploads/upload_5658ec2229ec0a948522f7a641fe6c8d.png

节点:

https://cmd.dayi.ink/uploads/upload_55477cc1d63bf4c588825fd7298d83cc.png
https://cmd.dayi.ink/uploads/upload_77954726917776bb2e82b5f6cafa6a43.png

状态:

https://cmd.dayi.ink/uploads/upload_6e435d93aedbaaa24323ca0600cdb4e9.png

发表回复

textsms
account_circle
email

dayi的大键盘

云与虚拟化_实验14 K8S
# K8S ## K8S部署 ## 先创建一个debian模板 ![](https://cmd.dayi.ink/uploads/upload_78895650660d7632aff3fbcb21486a2b.png) 选择iso ![](https://cmd.dayi.ink/upl…
扫描二维码继续阅读
2023-11-23