Kubernetes部署Calico实战

                                              作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.Calico网络插件对应K8S的版本说明

1.Calico和K8S1.23版本对应关系

如上图所示,k8s  1.23.17推荐使用Calico 3.25-版本。


推荐阅读:
	https://archive-os-3-25.netlify.app/calico/3.25/getting-started/kubernetes/requirements#kubernetes-requirements

2.Calico和K8S1.31版本对应关系

如上图所示,K8S 1.31.3推荐使用Calico 3.29+版本。

推荐阅读:
	https://docs.tigera.io/calico/latest/getting-started/kubernetes/requirements#kubernetes-requirements

二.K8S部署Calico的CNI网络插件

1.下载资源清单

[root@node-exporter41 ~]# wget https://raw.githubusercontent.com/projectcalico/calico/v3.29.1/manifests/tigera-operator.yaml


SVIP:
wget http://192.168.13.253/Resources/Kubernetes/Add-ons/cni/calico/calico-v3.29.1/tigera-operator.yaml

2.安装Tigera Calico操作符和自定义资源定义

[root@node-exporter41 ~]# kubectl create -f tigera-operator.yaml 

3.下载自定义资源清单

[root@node-exporter41 ~]# wget https://raw.githubusercontent.com/projectcalico/calico/v3.29.1/manifests/custom-resources.yaml


SVIP:
wget http://192.168.13.253/Resources/Kubernetes/Add-ons/cni/calico/calico-v3.29.1/custom-resources.yaml

4.修改自定义资源的Pod网段

[root@node-exporter41 ~]# grep cidr custom-resources.yaml 
      cidr: 192.168.0.0/16
[root@node-exporter41 ~]# 
[root@node-exporter41 ~]# sed -i '/cidr/s#192.168#10.100#' custom-resources.yaml 
[root@node-exporter41 ~]# 
[root@node-exporter41 ~]# grep cidr custom-resources.yaml 
      cidr: 10.100.0.0/16
[root@node-exporter41 ~]# 

5.创建资源

[root@node-exporter41 ~]# kubectl create -f custom-resources.yaml

6.检查Pod是否部署成功

[root@node-exporter41 ~]# kubectl get pods -A
NAMESPACE          NAME                                       READY   STATUS    RESTARTS   AGE
calico-apiserver   calico-apiserver-6db8b74566-mcmt7          1/1     Running   0          51s
calico-apiserver   calico-apiserver-6db8b74566-qbbzk          1/1     Running   0          51s
calico-system      calico-kube-controllers-59d844fb78-9mxsj   1/1     Running   0          34s
calico-system      calico-node-5pmrf                          1/1     Running   0          33s
calico-system      calico-node-lzxmw                          1/1     Running   0          33s
calico-system      calico-node-xjk4p                          1/1     Running   0          33s
calico-system      calico-typha-6c6c97f598-bdbg8              1/1     Running   0          34s
calico-system      calico-typha-6c6c97f598-l4c8m              1/1     Running   0          34s
calico-system      csi-node-driver-7lxrn                      2/2     Running   0          32s
calico-system      csi-node-driver-8fjkn                      2/2     Running   0          33s
calico-system      csi-node-driver-x7dk7                      2/2     Running   0          32s
tigera-operator    tigera-operator-76c4976dd7-7rwhs           1/1     Running   0          40m
[root@node-exporter41 ~]# 


SVIP: (可以批量导入镜像)
cat > import-calico-v3.29.1.sh <<'EOF'
#!/bin/bash

URL=http://192.168.13.253/Resources/Kubernetes/Add-ons/cni/calico/calico-v3.29.1
IMAGES=(yinzhengjie-cni-v3.29.1.tar.gz  yinzhengjie-csi-v3.29.1.tar.gz yinzhengjie-kube-controllers-v3.29.1.tar.gz yinzhengjie-node-driver-registrar-v3.29.1.tar.gz yinzhengjie-node-v3.29.1.tar.gz yinzhengjie-pod2daemon-flexvol-v3.29.1.tar.gz yinzhengjie-typha-v3.29.1.tar.gz yinzhengjie-apiserver-v3.29.1.tar.gz)

# echo ${IMAGES[*]}

for pkg in ${IMAGES[@]}
  do 
    echo  "Downdowning image ---> ${URL}/${pkg}"
    wget ${URL}/${pkg} 

    echo  "importing image ---> ${URL}/${pkg}"
    ctr -n k8s.io  i import ${pkg}
done 
EOF

bash import-calico-v3.29.1.sh

7.再次查看节点状态

[root@node-exporter41 ~]# kubectl get nodes -o wide
NAME              STATUS   ROLES    AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION       CONTAINER-RUNTIME
node-exporter41   Ready    <none>   95m   v1.31.3   10.0.0.41     <none>        Ubuntu 22.04.4 LTS   5.15.0-125-generic   containerd://1.6.36
node-exporter42   Ready    <none>   95m   v1.31.3   10.0.0.42     <none>        Ubuntu 22.04.4 LTS   5.15.0-125-generic   containerd://1.6.36
node-exporter43   Ready    <none>   95m   v1.31.3   10.0.0.43     <none>        Ubuntu 22.04.4 LTS   5.15.0-125-generic   containerd://1.6.36
[root@node-exporter41 ~]# 
 
	
推荐阅读:
	https://docs.tigera.io/calico/latest/getting-started/kubernetes/quickstart

8.检查集群节点是否有污点

[root@node-exporter41 ~]# kubectl describe nodes  | grep Taints
Taints:             <none>
Taints:             <none>
Taints:             <none>
[root@node-exporter41 ~]# 

9.卸载calico插件

[root@node-exporter41 ~]# kubectl delete -f custom-resources.yaml  -f  tigera-operator.yaml 

三.测试集群是否正常

1.配置自动补全功能

kubectl completion bash > ~/.kube/completion.bash.inc
echo "source '$HOME/.kube/completion.bash.inc'" >> $HOME/.bashrc
source $HOME/.bashrc

2.启动deployment资源测试

cat > deploy-apps.yaml  <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: yinzhengjie-app01
spec:
  replicas: 1
  selector:
    matchLabels:
      apps: v1 
  template:
    metadata:
      labels:
        apps: v1
    spec:
      nodeName: node-exporter42
      containers:
      - name: c1
        image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1 

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: yinzhengjie-app02
spec:
  replicas: 1
  selector:
    matchLabels:
      apps: v1 
  template:
    metadata:
      labels:
        apps: v1
    spec:
      nodeName: node-exporter43
      containers:
      - name: c1
        image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2
EOF

3.测试验证

[root@node-exporter41 ~]# kubectl get pods -o wide
NAME                                 READY   STATUS    RESTARTS   AGE   IP               NODE              NOMINATED NODE   READINESS GATES
yinzhengjie-app01-f5cd494c9-bzfvg    1/1     Running   0          21s   10.100.173.69    node-exporter42   <none>           <none>
yinzhengjie-app02-5d77969f8f-q7m25   1/1     Running   0          21s   10.100.246.196   node-exporter43   <none>           <none>
[root@node-exporter41 ~]# 
[root@node-exporter41 ~]# curl 10.100.173.69 
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>yinzhengjie apps v1</title>
    <style>
       div img {
          width: 900px;
          height: 600px;
          margin: 0;
       }
    </style>
  </head>

  <body>
    <h1 style="color: green">凡人修仙传 v1 </h1>
    <div>
      <img src="1.jpg">
    <div>
  </body>

</html>
[root@node-exporter41 ~]# 
[root@node-exporter41 ~]# curl 10.100.246.196 
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>yinzhengjie apps v2</title>
    <style>
       div img {
          width: 900px;
          height: 600px;
          margin: 0;
       }
    </style>
  </head>

  <body>
    <h1 style="color: red">凡人修仙传 v2 </h1>
    <div>
      <img src="2.jpg">
    <div>
  </body>

</html>
[root@node-exporter41 ~]# 

4.删除资源

kubectl delete -f deploy-apps.yaml 

5.关机,拍快照

略,见视频。
posted @   尹正杰  阅读(506)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示