rocky8.9配置K8S集群
节点 | 主机名 | IP地址 |
---|---|---|
master | tang1 | 192.168.211.101 |
node1 | tang2 | 192.168.211.102 |
node2 | tang3 | 192.168.211.103 |
1)准备工作
全部主机都配置静态ip
vi /etc/sysconfig/network-scriptsTYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ensxxx
UUID=xxxxxxxxxxxxx
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.211.101
DNS1=114.114.114.114
NETMASK=255.255.255.0
nmcli connection reload(表示加载配置信息)
nmcli connection up 网卡名,两条命令都要运行才可以
# 关闭 firewalld 防火墙
$ systemctl stop firewalld
$ systemctl disable firewalld
$ setenforce 0
$ sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config # 永久关闭 SELinux# 临时禁用 swap
$ swapoff -a
$ sed -i '/.*swap.*/d' /etc/fstab # 永久禁用 swap# 配置 /etc/hosts
$ cat > /etc/hosts << EOF
192.168.211.101 tang1
192.168.211.102 tang2
192.168.211.103 tang3
EOF
将桥接的ipv4流量传递到iptables链
modprobe br_netfilter ##生成bridge相关内核参数cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOFsysctl --system # 生效
打开端口转发
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
时间同步
yum install -y chrony;
systemctl start chronyd;
systemctl enable chronyd
2)安装containerd(三个节点上操作)
先安装yum-utils工具
yum install -y yum-utils
配置Docker官方的yum仓库,如果做过,可以跳过
yum-config-manager \--add-repo \https://download.docker.com/linux/centos/docker-ce.repo
安装containerd
yum install containerd.io -y
启动服务
systemctl enable containerd
systemctl start containerd
生成默认配置
containerd config default > /etc/containerd/config.toml
修改配置
vi /etc/containerd/config.toml
sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.8" # 修改为阿里云镜像地址
SystemdCgroup = true
重启containerd服务
systemctl restart containerd
3)配置kubernetes仓库(三个节点都操作)
cat <<EOF > /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
说明:kubernetes用的是RHEL7的源,和8是通用的
4)安装kubeadm和kubelet(三个节点都操作)
查看所有版本
yum --showduplicates list kubeadm #都选择y
安装1.26.2版本,有新版本可以选择新版本
yum install -y kubelet-1.26.2 kubeadm-1.26.2 kubectl-1.26.2
启动kubelet服务
systemctl start kubelet.service
systemctl enable kubelet.service
5)设置crictl连接 containerd(三个节点都操作)
crictl config --set runtime-endpoint=unix:///run/containerd/containerd.sock
6)初始化(master上)
kubeadm init --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --apiserver-advertise-address=192.168.211.103 --kubernetes-version=v1.26.2 --service-cidr=10.15.0.0/16 --pod-network-cidr=10.18.0.0/16
#修改ip地址为master节点,只需要master主机修改就可以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/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/configAlternatively, if you are the root user, you can run:export KUBECONFIG=/etc/kubernetes/admin.confYou 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.211.103:6443 --token 4mvr8t.tqjjac69wg7vtk7x \--discovery-token-ca-cert-hash sha256:6d2f9a8ebc144c3aea5a199a17310adbcfd0fa8605270bc84bd1ae02c82e90b4
说明: 上面这条命令就是如果需要将node节点加入到集群需要执行的命令,这个token有效期为24小时,如果过期,可以使用下面命令获取kubeadm token create --print-join-command
7)创建目录(master)
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
获取节点信息:
kubectl get node
kubectl get pod --all-namespaces
8)node节点上加入master(两个节点主机都执行)
kubeadm join 192.168.211.103:6443 --token 4mvr8t.tqjjac69wg7vtk7x \--discovery-token-ca-cert-hash sha256:6d2f9a8ebc144c3aea5a199a17310adbcfd0fa8605270bc84bd1ae02c82e90b4
安装calico网络(master节点上)
curl https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml -O
下载完后还需要修改⾥⾯定义 Pod ⽹络(CALICO_IPV4POOL_CIDR),与前⾯ kubeadm init 的 --podnetwork-cidr 指定的⼀样
vi calico.yaml
vim calico.yaml
# - name: CALICO_IPV4POOL_CIDR
# value: "192.168.0.0/16"
# 修改为:
- name: CALICO_IPV4POOL_CIDRvalue: "10.18.0.0/16"
部署
kubectl apply -f calico.yaml
查看
kubectl get pods -n kube-system
kubectl get node
#如果想在节点上运行这条命令,要把master创建的目录复制到节点上,需要:
yum install -y rsync
rsync -av $HOME/.kube/ tang2:/root/.kube/
运行,拷贝到哪台主机需要修改主机名,tang?:
scp -r /root/.kube/ tang2:/root/.kube/
9)安装dashboard(master上)
步骤略
- 在K8s里快速部署一个应用
1)创建deployment
kubectl create deployment testdp --image=nginx:1.23.2 ##deploymnet名字为testdp 镜像为nginx:1.23.2
2)查看deployment
kubectl get deployment
3)查看pod
kubectl get pods
4)查看pod详情
kubectl describe pod testdp-68dc848667-d6xhg #根据自己的name值去看
5)创建service,暴漏pod端口到node节点上
kubectl expose deployment testdp --port=80 --type=NodePort --target-port=80 --name=testsvc
6)查看service
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.15.0.1 <none> 443/TCP 156m
testsvc NodePort 10.15.248.16 <none> 80:30360/TCP 8s
192.168.211.102:30360