• 【云原生】Kubernetes(k8s)Calico 客户端工具 calicoctl


    一、概述

    calicoctl 是 Calico 客户端管理工具。 可以方便的管理 calico 网络,配置和安全策略,calicoctl 命令行提供了许多资源管理命令,允许您创建,修改,删除和查看不同的 Calico 资源,网络资源包含:node,bgpPeer,hostEndpoint,workloadEndpoint,ipPool,policy,profile等。

    官方文档:https://projectcalico.docs.tigera.io/reference/calicoctl/
    关于Calico 可以参考我这篇文章:Kubernetes(k8s)CNI(Calico)网络模型原理

    在这里插入图片描述

    二、calicoctl 安装

    wget https://github.com/projectcalico/calico/releases/download/v3.24.5/calicoctl-linux-amd64
    mv calicoctl-linux-amd64 /usr/local/bin/calicoctl 
    chmod +x /usr/local/bin/calicoctl
    
    # 查看
    calicoctl version
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    三、calicoctl 简单使用

    calicoctl通过读写calico的数据存储系统(datastore)进行查看或者其他各类管理操作,通常,它需要提供认证信息经由相应的数据存储完成认证。在使用Kubernetes API数据存储时,需要使用类似kubectl的认证信息完成认证。它可以通过环境变量声明的DATASTORE_TYPEKUBECONFIG接入集群,例如以下命令格式运行calicoctl:

    1)认证信息配置

    export KUBECONFIG=/path/to/your/kubeconfig
    export DATASTORE_TYPE=kubernetes
    
    # 查看帮助
    calicoctl --help
    # 查看calico节点
    calicoctl get nodes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2)查看 IP 资源池

    calicoctl get ipPools
    calicoctl get ipPool -o yaml
    
    • 1
    • 2

    在这里插入图片描述

    3)配置 IP 池

    IP 池 是 Calico 使用的 IP 地址范围 工作负载终端节点。

    定义两个在此群集中使用的 IP 池。 您可以仅使用一个 池,但我们定义了两个,编排操作如下:

    cat > pool1.yaml <<EOF
    apiVersion: projectcalico.org/v3
    kind: IPPool
    metadata:
      name: pool1
    spec:
      cidr: 10.245.1.0/24
      ipipMode: Never
      natOutgoing: true
      disabled: false
      nodeSelector: all()
    EOF
    
    cat > pool2.yaml <<EOF
    apiVersion: projectcalico.org/v3
    kind: IPPool
    metadata:
      name: pool2
    spec:
      cidr: 10.245.2.0/24
      ipipMode: Never
      natOutgoing: true
      disabled: true
      nodeSelector: all()
    EOF
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    执行并查看

    # 先查看IP资源池
    calicoctl get ipPools
    
    # 创建两个IP资源池
    calicoctl create -f pool1.yaml
    calicoctl create -f pool2.yaml
    
    # 查看
    calicoctl get ipPools
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    如果使用kubectl创建,就必须查询apiVersion和kind

    # 先删除上面新建的两个ip资源池
    calicoctl delete -f pool1.yaml
    calicoctl delete -f pool2.yaml
    calicoctl get ipPools
    
    # 未修改前执行,发现是无法匹配对应的kind
    kubectl create -f pool1.yaml
    
    # 查看apiVersion
    kubectl api-versions|grep calico
    # 查看kind
    kubectl api-resources -o wide|grep calico|grep IPPool
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述
    修改

    # 这里只需要把apiVersion换掉既可
    cat > pool3.yaml <<EOF
    apiVersion: crd.projectcalico.org/v1
    kind: IPPool
    metadata:
      name: pool3
    spec:
      cidr: 10.245.3.0/24
      ipipMode: Never
      natOutgoing: true
      disabled: false
      nodeSelector: all()
    EOF
    
    cat > pool4.yaml <<EOF
    apiVersion: crd.projectcalico.org/v1
    kind: IPPool
    metadata:
      name: pool4
    spec:
      cidr: 10.245.4.0/24
      ipipMode: Never
      natOutgoing: true
      disabled: true
      nodeSelector: all()
    EOF
    
    # 先查看ip资源池
    calicoctl get ipPools
    
    # 执行
    kubectl create -f pool3.yaml
    kubectl create -f pool4.yaml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述

    4)IP 资源池示例演示

    利用注解cni.projectcalico.org/ipv4pools

    cat > ipv4pools-deployment-test.yaml <<EOF
    # apiVersion: projectcalico.org/v3
    apiVersion: crd.projectcalico.org/v1
    kind: IPPool
    metadata:
      name: new-pool1
    spec:
      blockSize: 31
      cidr: 10.244.3.220/24
      ipipMode: Never
      natOutgoing: true
    ---
    # apiVersion: projectcalico.org/v3
    apiVersion: crd.projectcalico.org/v1
    kind: IPPool
    metadata:
      name: new-pool2
    spec:
      blockSize: 31
      cidr: 10.244.4.221/24
      ipipMode: Never
      natOutgoing: true
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: fixed-ip-test2
      namespace: default
      labels:
        k8s-app: cloudnativer-test
    spec:
      replicas: 3
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxUnavailable: 1
      selector:
        matchLabels:
          k8s-app: cloudnativer-test
      template:
        metadata:
          labels:
            k8s-app: cloudnativer-test
          annotations:
            # 【注意】不能使用单引号
            "cni.projectcalico.org/ipv4pools": "[\"new-pool1\",\"new-pool2\"]"
        spec:
          containers:
          - name: fixed-ip-test
            image: nginx:1.7.9
            imagePullPolicy: IfNotPresent
            ports:
            - name: http
              containerPort: 80
    EOF
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    在这里插入图片描述

    5)固定 IP 示例演示

    利用注解 cni.projectcalico.org/ipAddrs

    # vi fixed-ip-test-deployment.yaml
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: fixed-ip-test
      namespace: default
      labels:
        k8s-app: cloudnativer-test
    spec:
      replicas: 1
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxUnavailable: 1
      selector:
        matchLabels:
          k8s-app: cloudnativer-test
      template:
        metadata:
          labels:
            k8s-app: cloudnativer-test
          annotations:
            cni.projectcalico.org/ipAddrs: "[\"10.244.1.220\"]"
        spec:
          containers:
          - name: fixed-ip-test
            image: nginx:1.7.9
            imagePullPolicy: IfNotPresent
            ports:
            - name: http
              containerPort: 80
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    6)网络策略(NetworkPolicy)

    网络策略资源 (NetworkPolicy) 表示应用的一组有序规则 到与 标签选择器。NetworkPolicy 是命名空间资源。 NetworkPolicy 在特定命名空间中 仅适用于 工作负载终端节点资源 在该命名空间中。两个资源位于同一命名空间中,如果 namespace 两者上的值设置相同。 看 全局网络策略资源 对于非命名空间网络策略。

    【示例】此示例策略允许来自 TCP 流量 frontend 端口 6379 的终结点 database 端点。

    # vim networkpolicy-test.yaml
    apiVersion: projectcalico.org/v3
    kind: NetworkPolicy
    metadata:
      name: allow-tcp-6379
      namespace: production
    spec:
      selector: role == 'database'
      types:
      - Ingress
      - Egress
      ingress:
      - action: Allow
        metadata:
          annotations:
            from: frontend
            to: database
        protocol: TCP
        source:
          selector: role == 'frontend'
        destination:
          ports:
          - 6379
      egress:
      - action: Allow
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    执行

    kubectl create ns production
    calicoctl create -f networkpolicy-test.yaml
    # 查看
    calicoctl get networkPolicy --namespace=production -oyaml
    
    • 1
    • 2
    • 3
    • 4

    想了解更多网络策略,可以查看官方文档:https://projectcalico.docs.tigera.io/reference/resources/networkpolicy

    四、Kube-ipam

    Kube-ipam 基于etcd分布式存储实现kubernetes动态IP网络分配管理,确保集群中IP地址的唯一性。Kube-ipam支持给kubernetes集群中的Pod固定IP地址,同时支持resolv.conf的DNS配置。这个需要基于网络插件(例如:macvlanipvlankube-routerbridgecalico等),这里就使用calico网络插件来实现。

    一些场景往往对IP地址有依赖,需要使用固定IP地址的Pod,可以使用kube-ipam轻松解决这类问题。例如,mysql主从架构的时候,主database与从database之间的同步;例如keepalived做集群HA的时候,两个节点之间检测通信等;例如某些安全防护设备,需要基于IP地址进行网络安全访问策略限制的场景等。

    GitHub地址:https://github.com/cloudnativer/kube-ipam

    在这里插入图片描述

    1)安装kube-ipam

    请确保你的kubelet正确的配置了network-plugincni-conf-dircni-bin-dir 参数。下面给出一个kubelet的配置示例供你参考:

    # vi /usr/lib/systemd/system/kubelet.service
    # ...
    ExecStart=/usr/local/bin/kubelet \
    --network-plugin=cni \
    --cni-conf-dir=/etc/cni/net.d \
    --cni-bin-dir=/opt/cni/bin/ \
    
    # 重启kubelet
    systemctl daemon-reload
    systemctl restart kubelet
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    下载安装 kube-ipam

    wget https://github.com/cloudnativer/kube-ipam/releases/download/v0.2.0/kube-ipam-v0.2.0-x86.tgz
    tar -zxvf kube-ipam-v0.2.0-x86.tgz
    mv kube-ipam/kube-ipam /opt/cni/bin/kube-ipam
    
    • 1
    • 2
    • 3

    2)子网和etcd配置

    你可以通过 subnet 参数设置IP子网信息,通过 gateway 设置网关信息。你可以通过 etcdConfig 配置etcd的证书和endpoint地址。编辑所有kubernetes node主机的 /etc/cni/net.d/1-kube-ipam.conf 文件。

    # 类型
    #"type": "calico",
    # 主网卡名称
    #"master": "ens33",
    # cat /etc/cni/net.d/1-kube-ipam.conf
    {
            "cniVersion":"0.3.1",
            "name": "k8snetwork",
            "type": "calico",
            "master": "ens33",
            "ipam": {
                    "name": "kube-subnet",
                    "type": "kube-ipam",
                    "kubeConfig": "/etc/kubernetes/pki/kubectl.kubeconfig"
                    "etcdConfig": {
                            "etcdURL": "https://192.168.1.50:2379,https://192.168.1.58:2379,https://192.168.1.63:2379",
                            "etcdCertFile": "/etc/kubernetes/pki/etcd.pem",
                            "etcdKeyFile": "/etc/kubernetes/pki/etcd-key.pem",
                            "etcdTrustedCAFileFile": "/etc/kubernetes/pki/ca.pem"
                    },
                    "subnet": "10.188.0.0/16",
                    "fixedStart": "10.188.0.10",
                    "fixedEnd": "10.188.0.255",
                    "rangeStart": "10.188.1.0",
                    "rangeEnd": "10.188.255.254",
                    "gateway": "10.188.0.1",
                    "routes": [{
                            "dst": "0.0.0.0/0"
                    }],
                    "resolvConf": "/etc/resolv.conf"
            }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    3)固定IP示例演示

    # cat fixed-ip-test-Deployment.yaml
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: fixed-ip-test
      namespace: default
      labels:
        k8s-app: cloudnativer-test
    spec:
      replicas: 1
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxUnavailable: 1
      selector:
        matchLabels:
          k8s-app: cloudnativer-test
      template:
        metadata:
          labels:
            k8s-app: cloudnativer-test
          annotations:
            kube-ipam.ip: "10.188.0.216"
            kube-ipam.netmask: "255.255.0.0"
            kube-ipam.gateway: "10.188.0.1"
        spec:
          containers:
          - name: fixed-ip-test
            image: nginx:1.7.9
            imagePullPolicy: IfNotPresent
            ports:
            - name: http
              containerPort: 80  
    ---
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    这里没有真正的去验证,只是稍微说一下,还可以通过kube-ipam进行固定IP配置,有兴趣的小伙伴可以去试试;Calico 客户端工具 calicoctl简单使用就先到这里了,有任何疑问欢迎给我留言,后续会持续更新【云原生+大数据】相关的文章~

  • 相关阅读:
    C#:委托与事件
    高通Android 12/13 设置和获取ADB状态
    Linux网络编程-网络基础1
    基于Alexnet深度学习网络的人员口罩识别算法matlab仿真
    什么是时间冒泡?
    C++PrimerPlus 第六章 分支语句和逻辑运算符(复习题含答案)
    SSM整合步骤(重点)
    LeetCode_位运算_递归_中等_779.第K个语法符号
    如何系统地去学python
    AE 的软件、硬件、驱动控制、调试策略(没有算法)
  • 原文地址:https://blog.csdn.net/qq_35745940/article/details/127942781