K8s 网络-基本环境准备 Day01

打印 上一主题 下一主题

主题 1084|帖子 1084|积分 3252

1. 环境准备

1.1 资源准备

   准备3台Ubuntu 20.04,我用的是2C4G。
  

  • 192.168.2.2 bpf1
  • 192.168.2.3 bpf2
  • 192.168.2.4 bpf3
  1.2 环境初始化(所有节点操作)

1.2.1 安装须要工具

  1. apt update
  2. apt install -y net-tools tcpdump chrony bridge-utils tree wget iftop ethtool curl
复制代码
1.2.2 设置hosts文件

  1. ~# cat /etc/hosts
  2. 127.0.0.1       localhost
  3. 192.168.2.2     bpf1
  4. 192.168.2.3     bpf2
  5. 192.168.2.4     bpf3
复制代码
1.2.3 关闭swap

  1. sed -ri 's/.*swap.*/#&/' /etc/fstab
  2. swapoff -a
复制代码
1.2.4 调解内核参数

  1. # vi /etc/sysctl.conf
  2. ……省略部分内容
  3. net.ipv4.ip_forward = 1
  4. net.bridge.bridge-nf-call-ip6tables = 1
  5. net.bridge.bridge-nf-call-iptables = 1
  6. net.bridge.bridge-nf-call-arptables = 1
  7. root@bpf1:~# modprobe br_netfilter # 临时生效
  8. root@bpf1:~# echo br_netfilter > /etc/modules-load.d/br_netfilter.conf # 永久生效
  9. root@bpf1:~# lsmod | grep br_netfilter
  10. br_netfilter           28672  0
  11. bridge                176128  1 br_netfilter
  12. root@bpf1:~# sysctl -p
复制代码
  

  • net.ipv4.ip_forward = 1
    这个参数启用了 IP 转发功能。当设置为 1 时,Linux 内核将会转发收到的、目的地不是本机的数据包。这对于路由器和网关装备来说是须要的。
  • net.bridge.bridge-nf-call-ip6tables = 1
    这个参数允许在 Linux 桥接装备上对 IPv6 数据包应用 ip6tables 规则。默认情况下,桥接装备不会将数据包转达给 ip6tables,设置为 1 可以启用这个功能。
  • net.bridge.bridge-nf-call-iptables = 1
    类似于上面的 IPv6 参数,这个参数允许在 Linux 桥接装备上对 IPv4 数据包应用 iptables 规则。
  • net.bridge.bridge-nf-call-arptables = 1
    这个参数允许在 Linux 桥接装备上对 ARP 数据包应用 arptables 规则。
  1.2.5 添加阿里的repo

  1. apt-get update && apt-get install -y apt-transport-https
  2. curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
  3. cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
  4. deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
  5. EOF
  6. apt-get update
复制代码
1.2.6 安装docker

  1. # 多执行几次,直到返回ok。
  2. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  3. add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  4. apt update
  5. apt install docker-ce docker-ce-cli containerd.io
复制代码
1.2.7 启动docker

  1. systemctl daemon-reload
  2. systemctl restart docker
  3. systemctl enable docker
复制代码
2. Kubeadm安装k8s集群

2.1 安装kubelet kubectl kubeadm并启动

工作节点可以不用安装kubectl
  1. root@bpf1:~# apt-get install -y kubelet=1.23.4-00 kubeadm=1.23.4-00 kubectl=1.23.4-00 --allow-unauthenticated
  2. root@bpf1:~#  systemctl enable kubelet && systemctl restart kubelet
复制代码
2.2 设置 kubelet 的 cgroup 驱动

  1. root@bpf1:~# cat /etc/docker/daemon.json
  2. {
  3.   "exec-opts": ["native.cgroupdriver=systemd"],
  4.   "registry-mirrors": ["https://cu2yw19m.mirror.aliyuncs.com"]
  5. }
复制代码
2.3 调解kubelet设置

  1. root@bpf1:~# cat > /var/lib/kubelet/config.yaml <<EOF      apiVersion: kubelet.config.k8s.io/v1beta1kind: KubeletConfigurationcgroupDriver: systemdEOFsystemctl daemon-reloadsystemctl restart dockersystemctl enable kubelet && systemctl restart kubeletsystemctl daemon-reload
  2. systemctl restart docker
  3. systemctl enable dockerroot@bpf1:~# docker info |grep "Cgroup Driver" Cgroup Driver: systemd
复制代码
2.4 初始化集群(master节点操作)

2.4.1 拉取镜像

  1. kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers
复制代码
2.4.2 初始化master节点

  1. root@bpf1:~# kubeadm init --kubernetes-version=v1.23.5 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap
  2. …………省略部分输出
  3. Your Kubernetes control-plane has initialized successfully!
  4. To start using your cluster, you need to run the following as a regular user:
  5.   mkdir -p $HOME/.kube
  6.   sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  7.   sudo chown $(id -u):$(id -g) $HOME/.kube/config
  8. Alternatively, if you are the root user, you can run:
  9.   export KUBECONFIG=/etc/kubernetes/admin.conf
  10. You should now deploy a pod network to the cluster.
  11. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  12.   https://kubernetes.io/docs/concepts/cluster-administration/addons/
  13. Then you can join any number of worker nodes by running the following on each as root:
  14. kubeadm join 192.168.2.2:6443 --token 7vjk7y.2ztfqonr9miybhbv \
  15.         --discovery-token-ca-cert-hash sha256:42ad7eb8fa479f9b9ee9e93c735863ee64c3c7d3f7ccd403a21dc301987a3d74
复制代码
  1.   mkdir -p $HOME/.kube
  2.   sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3.   sudo chown $(id -u):$(id -g) $HOME/.kube/config
  4. root@bpf1:~# kubectl get no -owide
  5. NAME   STATUS     ROLES                  AGE     VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
  6. bpf1   NotReady   control-plane,master   3m24s   v1.23.4   192.168.2.2   <none>        Ubuntu 20.04.6 LTS   5.4.0-202-generic   docker://27.3.1
复制代码
2.4.3 设置env

  1. root@bpf1:~# kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
  2. root@bpf1:~# kubectl get po -A
  3. NAMESPACE      NAME                           READY   STATUS    RESTARTS      AGE
  4. kube-flannel   kube-flannel-ds-m86vx          1/1     Running   0             17s
  5. kube-system    coredns-6d8c4cb4d-5ll9c        1/1     Running   0             57m
  6. kube-system    coredns-6d8c4cb4d-hh2rk        1/1     Running   0             57m
  7. kube-system    etcd-bpf1                      1/1     Running   3 (19m ago)   57m
  8. kube-system    kube-apiserver-bpf1            1/1     Running   3 (18m ago)   57m
  9. kube-system    kube-controller-manager-bpf1   1/1     Running   3 (19m ago)   57m
  10. kube-system    kube-proxy-pwsv5               1/1     Running   3 (19m ago)   57m
  11. kube-system    kube-scheduler-bpf1            1/1     Running   3 (19m ago)   57m
  12. root@bpf1:~# kubectl get no
  13. NAME   STATUS   ROLES                  AGE   VERSION
  14. bpf1   Ready    control-plane,master   58m   v1.23.4
  15. root@bpf1:~# kubectl taint nodes --all node-role.kubernetes.io/master-
复制代码
2.5 添加工作节点

  1. kubeadm join 192.168.2.2:6443 --token 7vjk7y.2ztfqonr9miybhbv \
  2.         --discovery-token-ca-cert-hash sha256:42ad7eb8fa479f9b9ee9e93c735863ee64c3c7d3f7ccd403a21dc301987a3d74
  3. root@bpf1:~# kubectl get no
  4. NAME   STATUS   ROLES                  AGE   VERSION
  5. bpf1   Ready    control-plane,master   78m   v1.23.4
  6. bpf2   Ready    <none>                 70s   v1.23.4
  7. bpf3   Ready    <none>                 22s   v1.23.4
复制代码
2.6 创建flannel_cni

  1. root@bpf1:~# cat flannel_cni.yaml
  2. apiVersion: apps/v1
  3. kind: DaemonSet
  4. metadata:
  5.   labels:
  6.     app: flannel-udp
  7.   name: flannel-udp
  8. spec:
  9.   #replicas: 2
  10.   selector:
  11.     matchLabels:
  12.       app: flannel-udp
  13.   template:
  14.     metadata:
  15.       labels:
  16.         app: flannel-udp
  17.     spec:
  18.       containers:
  19.       - image: registry.cn-beijing.aliyuncs.com/sanhua-k8s/nettool:latest
  20.         name: nettoolbox
  21.         env:
  22.           - name: NETTOOL_NODE_NAME
  23.             valueFrom:
  24.               fieldRef:
  25.                 fieldPath: spec.nodeName
  26.         securityContext:
  27.           privileged: true
  28. ---
  29. apiVersion: v1
  30. kind: Service
  31. metadata:
  32.   name: flannel-udp
  33. spec:
  34.   type: NodePort
  35.   selector:
  36.     app: flannel-udp
  37.   ports:
  38.   - name: flannel-udp
  39.     port: 80
  40.     targetPort: 80
  41.     nodePort: 32000
  42. root@bpf1:~# kubectl apply -f flannel_cni.yaml
  43. daemonset.apps/flannel-udp created
  44. service/flannel-udp created
  45. root@bpf1:~# kubectl get po -A |grep udp
  46. default        flannel-udp-47tbl              1/1     Running   0             108s
  47. default        flannel-udp-686k4              1/1     Running   0             108s
  48. default        flannel-udp-ffrnz              1/1     Running   0             108s
  49. root@bpf1:~# kubectl get svc -A
  50. NAMESPACE     NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
  51. default       flannel-udp   NodePort    10.100.186.33   <none>        80:32000/TCP             16h
  52. default       kubernetes    ClusterIP   10.96.0.1       <none>        443/TCP                  18h
  53. kube-system   kube-dns      ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP   18h
  54. root@bpf1:~# curl 192.168.2.4:32000
  55. PodName: flannel-udp-rxkbp | PodIP: eth0 10.244.2.6/24
复制代码
3. Kind安装k8s集群

   单独找一个假造机即可,由于kind是在一个节点上模拟安装多套k8s集群。
 
  后续的大部门实验,都是使用kind来切换环境。
  3.1 Kind介绍

官网:kind
   kind 是一种使用 Docker 容器“节点”运行本地 Kubernetes 集群(单个宿主机节点)的工具。重要用与测试或本地开辟。
  3.2 安装docker


3.3 二进制安装kind

  1. root@localhost:~# wget https://github.com/kubernetes-sigs/kind/releases/download/v0.26.0/kind-linux-amd64
  2. root@localhost:~# mv kind-linux-amd64 kind
  3. root@localhost:~# chmod +x kind
  4. root@localhost:~# mv kind /usr/local/bin/
复制代码
3.4 安装k8s集群

3.4.1 克隆所需文件

  1. root@localhost:~# git clone https://gitee.com/rowan-wcni/wcni-kind.git
  2. root@localhost:~/wcni-kind/LabasCode/flannel/5-flannel-ipip# ll
  3. total 32
  4. drwxr-xr-x  3 root root 4096 Jan 30 10:27 ./
  5. drwxr-xr-x 14 root root 4096 Jan 29 06:31 ../
  6. -rwxr-xr-x  1 root root 6028 Jan 29 06:34 1-setup-env.sh*
  7. drwxr-xr-x  2 root root 4096 Jan 29 06:31 2-datapath/
  8. -rw-r--r--  1 root root  690 Jan 29 06:31 cni.yaml
  9. -rw-r--r--  1 root root 4726 Jan 29 06:31 flannel.yaml
复制代码
3.4.2 调解yaml

  1. root@localhost:~/wcni-kind/LabasCode/flannel/5-flannel-ipip# cat 1-setup-env.sh
  2. ……省略部分内容
  3. cat <<EOF | KIND_EXPERIMENTAL_DOCKER_NETWORK=kind kind create cluster --name=flannel-ipip --image=registry.cn-beijing.aliyuncs.com/sanhua-k8s/kindest:v1.27.3 # 把这个镜像改成私有仓库的
  4. ……省略部分内容
  5. root@localhost:~/wcni-kind/LabasCode/flannel/5-flannel-ipip# grep 'image:' cni.yaml # 这个也要改
  6.       - image: registry.cn-beijing.aliyuncs.com/sanhua-k8s/nettool
  7. root@localhost:~/wcni-kind/LabasCode/flannel/5-flannel-ipip# grep 'image:' flannel.yaml # 这个也要改
  8.         image: registry.cn-beijing.aliyuncs.com/sanhua-k8s/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
  9.         image: registry.cn-beijing.aliyuncs.com/sanhua-k8s/mirrored-flannelcni-flannel:v0.19.2
  10.         image: registry.cn-beijing.aliyuncs.com/sanhua-k8s/mirrored-flannelcni-flannel:v0.19.2
复制代码
3.4.3 刷新内核参数

  1. root@localhost:~/wcni-kind/LabasCode/flannel/5-flannel-ipip# modprobe br_netfilter
  2. root@localhost:~/wcni-kind/LabasCode/flannel/5-flannel-ipip# sysctl -p
  3. net.ipv4.ip_forward = 1
  4. net.bridge.bridge-nf-call-ip6tables = 1
  5. net.bridge.bridge-nf-call-iptables = 1
  6. net.bridge.bridge-nf-call-arptables = 1
  7. # 永久生效内核参数
  8. root@localhost:~/wcni-kind/LabasCode/flannel/5-flannel-ipip# echo "br_netfilter" | sudo tee /etc/modules-load.d/br_netfilter.conf
  9. br_netfilter
复制代码
3.4.4 安装集群

脚本实行前,需要调解一下下载kubeclt的那里,否则下载失败,后续kubectl相干的操作都会报错
  1. root@localhost:~/wcni-kind/LabasCode/flannel/5-flannel-ipip# sh 1-setup-env.sh
复制代码

3.4.5 查看集群状态



4. Containerlab与KinD联合使用

4.1 Containerlab介绍

官网:containerlab
   ContainerLab 是一个用于网络实验室环境的开源工具,专注于容器化网络装备的快速部署和测试。

重要就是通过容器技能来模拟各种网络装备(路由交换啥的)。
  4.2 二进制安装containerlab

这个一定要装,后面会用到
  1. wget https://github.com/srl-labs/containerlab/releases/download/v0.64.0/containerlab_0.64.0_linux_amd64.tar.gz
  2. mkdir -p /etc/containerlab
  3. tar -zxvf /tmp/clab.tar.gz -C /etc/containerlab
  4. mv /etc/containerlab/containerlab /usr/bin && chmod a+x /usr/bin/containerlab
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

种地

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表