容器编排之Kubernetes网络隔离NetworkPolicy

简介: Kubernetes的一个重要特性就是要把不同node节点的pod(container)连接起来,无视物理节点的限制。但是在某些应用环境中,比如公有云,不同租户的pod不应该互通,这个时候就需要网络隔离。

Kubernetes的一个重要特性就是要把不同node节点的pod(container)连接起来,无视物理节点的限制。但是在某些应用环境中,比如公有云,不同租户的pod不应该互通,这个时候就需要网络隔离。幸好,Kubernetes提供了NetworkPolicy,支持按Namespace级别的网络隔离,这篇文章就带你去了解如何使用NetworkPolicy。

需要注意的是,使用NetworkPolicy需要特定的网络解决方案,如果不启用,即使配置了NetworkPolicy也无济于事。我们这里使用Calico解决网络隔离问题。

互通测试

在使用NetworkPolicy之前,我们先验证不使用的情况下,pod是否互通。这里我们的测试环境是这样的:

Namespace:ns-calico1,ns-calico2

Deployment: ns-calico1/calico1-nginx, ns-calico2/busybox

Service: ns-calico1/calico1-nginx

先创建Namespace:

apiVersion: v1
kind: Namespace
metadata:
 name: ns-calico1
 labels:
 user: calico1
---
apiVersion: v1
kind: Namespace
metadata:
 name: ns-calico2
# kubectl create -f namespace.yaml namespace "ns-calico1" created
namespace "ns-calico2" created
# kubectl get ns
NAME STATUS AGE
default Active 9d
kube-public Active 9d
kube-system Active 9d
ns-calico1 Active 12s
ns-calico2 Active 8s

接着创建ns-calico1/calico1-nginx:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: calico1-nginx
 namespace: ns-calico1
spec:
 replicas: 1 template:
 metadata:
 labels:
 user: calico1
 app: nginx
 spec:
 containers: - name: nginx
 image: nginx
 ports: - containerPort: 80 ---
apiVersion: v1
kind: Service
metadata:
 name: calico1-nginx
 namespace: ns-calico1
 labels: 
 user: calico1
spec:
 selector:
 app: nginx
 ports: - port: 80
# kubectl create -f calico1-nginx.yaml
deployment "calico1-nginx" created
service "calico1-nginx" created
# kubectl get svc -n ns-calico1
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
calico1-nginx 192.168.3.141 <none> 80/TCP 26s # kubectl get deploy -n ns-calico1
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
calico1-nginx 1 1 1 1 34s

最后创建ns-calico2/calico2-busybox:

apiVersion: v1
kind: Pod
metadata:
 name: calico2-busybox
 namespace: ns-calico2
spec:
 containers: - name: busybox
 image: busybox
 command: - sleep
 - "3600"
# kubectl create -f calico2-busybox.yaml
pod "calico2-busybox" created
# kubectl get pod -n ns-calico2
NAME READY STATUS RESTARTS AGE
calico2-busybox 1/1 Running 0 40s

测试服务已经安装完成,现在我们登进calico2-busybox里,看是否能够连通calico1-nginx

# kubectl exec -it calico2-busybox -n ns-calico2 -- wget --spider --timeout=1 calico1-nginx.ns-calico1 Connecting to calico1-nginx.ns-calico1 (192.168.3.141:80)

由此可以看出,在没有设置网络隔离的时候,两个不同Namespace下的Pod是 可以互通的。接下来我们使用Calico进行网络隔离。

网络隔离

先决条件

要想在Kubernetes集群中使用Calico进行网络隔离,必须满足以下条件:

  1. kube-apiserver必须开启运行时extensions/v1beta1/networkpolicies,即设置启动参数:–runtime-config=extensions/v1beta1/networkpolicies=true
  2. kubelet必须启用cni网络插件,即设置启动参数:–network-plugin=cni
  3. kube-proxy必须启用iptables代理模式,这是默认模式,可以不用设置
  4. kube-proxy不得启用–masquerade-all,这会跟calico冲突

注意:配置Calico之后,之前在集群中运行的Pod都要重新启动

安装calico

首先需要安装Calico网络插件,我们直接在Kubernetes集群中安装,便于管理。

# Calico Version v2.1.4 # http://docs.projectcalico.org/v2.1/releases#v2.1.4 # This manifest includes the following component versions: # calico/node:v1.1.3 # calico/cni:v1.7.0 # calico/kube-policy-controller:v0.5.4 # This ConfigMap is used to configure a self-hosted Calico installation.
kind: ConfigMap
apiVersion: v1
metadata:
 name: calico-config
 namespace: kube-system
data: # Configure this with the location of your etcd cluster.
 etcd_endpoints: "https://10.1.2.154:2379,https://10.1.2.147:2379" # Configure the Calico backend to use.
 calico_backend: "bird" # The CNI network configuration to install on each node.
 cni_network_config: |- { "name": "k8s-pod-network", "type": "calico", "etcd_endpoints": "__ETCD_ENDPOINTS__", "etcd_key_file": "__ETCD_KEY_FILE__", "etcd_cert_file": "__ETCD_CERT_FILE__", "etcd_ca_cert_file": "__ETCD_CA_CERT_FILE__", "log_level": "info", "ipam": { "type": "calico-ipam" }, "policy": { "type": "k8s", "k8s_api_root": "https://__KUBERNETES_SERVICE_HOST__:__KUBERNETES_SERVICE_PORT__", "k8s_auth_token": "__SERVICEACCOUNT_TOKEN__" }, "kubernetes": { "kubeconfig": "__KUBECONFIG_FILEPATH__" } } # If you're using TLS enabled etcd uncomment the following. # You must also populate the Secret below with these files.
 etcd_ca: "/calico-secrets/etcd-ca" # "/calico-secrets/etcd-ca"
 etcd_cert: "/calico-secrets/etcd-cert" # "/calico-secrets/etcd-cert"
 etcd_key: "/calico-secrets/etcd-key" # "/calico-secrets/etcd-key" --- # The following contains k8s Secrets for use with a TLS enabled etcd cluster. # For information on populating Secrets, see http://kubernetes.io/docs/user-guide/secrets/
apiVersion: v1
kind: Secret
type: Opaque
metadata:
 name: calico-etcd-secrets
 namespace: kube-system
data: # Populate the following files with etcd TLS configuration if desired, but leave blank if # not using TLS for etcd. # This self-hosted install expects three files with the following names. The values # should be base64 encoded strings of the entire contents of each file.
 etcd-key: base64 key.pem
 etcd-cert: base64 cert.pem
 etcd-ca: base64 ca.pem

--- # This manifest installs the calico/node container, as well # as the Calico CNI plugins and network config on # each master and worker node in a Kubernetes cluster.
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
 name: calico-node
 namespace: kube-system
 labels:
 k8s-app: calico-node
spec:
 selector:
 matchLabels:
 k8s-app: calico-node
 template:
 metadata:
 labels:
 k8s-app: calico-node
 annotations:
 scheduler.alpha.kubernetes.io/critical-pod: ''
 scheduler.alpha.kubernetes.io/tolerations: | [{"key": "dedicated", "value": "master", "effect": "NoSchedule" }, {"key":"CriticalAddonsOnly", "operator":"Exists"}]
 spec:
 hostNetwork: true
 containers: # Runs calico/node container on each Kubernetes node. This # container programs network policy and routes on each # host. - name: calico-node
 image: quay.io/calico/node:v1.1.3
 env: # The location of the Calico etcd cluster. - name: ETCD_ENDPOINTS
 valueFrom:
 configMapKeyRef:
 name: calico-config
 key: etcd_endpoints
 # Choose the backend to use. - name: CALICO_NETWORKING_BACKEND
 valueFrom:
 configMapKeyRef:
 name: calico-config
 key: calico_backend
 # Disable file logging so `kubectl logs` works. - name: CALICO_DISABLE_FILE_LOGGING
 value: "true" # Set Felix endpoint to host default action to ACCEPT. - name: FELIX_DEFAULTENDPOINTTOHOSTACTION
 value: "ACCEPT" # Configure the IP Pool from which Pod IPs will be chosen. - name: CALICO_IPV4POOL_CIDR
 value: "192.168.0.0/16" - name: CALICO_IPV4POOL_IPIP
 value: "always" # Disable IPv6 on Kubernetes. - name: FELIX_IPV6SUPPORT
 value: "false" # Set Felix logging to "info" - name: FELIX_LOGSEVERITYSCREEN
 value: "info" # Location of the CA certificate for etcd. - name: ETCD_CA_CERT_FILE
 valueFrom:
 configMapKeyRef:
 name: calico-config
 key: etcd_ca
 # Location of the client key for etcd. - name: ETCD_KEY_FILE
 valueFrom:
 configMapKeyRef:
 name: calico-config
 key: etcd_key
 # Location of the client certificate for etcd. - name: ETCD_CERT_FILE
 valueFrom:
 configMapKeyRef:
 name: calico-config
 key: etcd_cert
 # Auto-detect the BGP IP address. - name: IP
 value: ""
 securityContext:
 privileged: true #resources: #requests: #cpu: 250m
 volumeMounts: - mountPath: /lib/modules
 name: lib-modules
 readOnly: true - mountPath: /var/run/calico
 name: var-run-calico
 readOnly: false - mountPath: /calico-secrets
 name: etcd-certs
 # This container installs the Calico CNI binaries
 # and CNI network config file on each node.
 - name: install-cni
 image: quay.io/calico/cni:v1.7.0
 command: ["/install-cni.sh"]
 env: # The location of the Calico etcd cluster. - name: ETCD_ENDPOINTS
 valueFrom:
 configMapKeyRef:
 name: calico-config
 key: etcd_endpoints
 # The CNI network config to install on each node. - name: CNI_NETWORK_CONFIG
 valueFrom:
 configMapKeyRef:
 name: calico-config
 key: cni_network_config
 volumeMounts: - mountPath: /host/opt/cni/bin
 name: cni-bin-dir
 - mountPath: /host/etc/cni/net.d
 name: cni-net-dir
 - mountPath: /calico-secrets
 name: etcd-certs
 volumes:
 # Used by calico/node. - name: lib-modules
 hostPath:
 path: /lib/modules
 - name: var-run-calico
 hostPath:
 path: /var/run/calico
 # Used to install CNI. - name: cni-bin-dir
 hostPath:
 path: /opt/cni/bin
 - name: cni-net-dir
 hostPath:
 path: /etc/cni/net.d
 # Mount in the etcd TLS secrets. - name: etcd-certs
 secret:
 secretName: calico-etcd-secrets

--- # This manifest deploys the Calico policy controller on Kubernetes. # See https://github.com/projectcalico/k8s-policy
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: calico-policy-controller
 namespace: kube-system
 labels:
 k8s-app: calico-policy
 annotations:
 scheduler.alpha.kubernetes.io/critical-pod: ''
 scheduler.alpha.kubernetes.io/tolerations: | [{"key": "dedicated", "value": "master", "effect": "NoSchedule" }, {"key":"CriticalAddonsOnly", "operator":"Exists"}]
spec: # The policy controller can only have a single active instance.
 replicas: 1
 strategy:
 type: Recreate template:
 metadata:
 name: calico-policy-controller
 namespace: kube-system
 labels:
 k8s-app: calico-policy
 spec: # The policy controller must run in the host network namespace so that # it isn't governed by policy that would prevent it from working.
 hostNetwork: true
 containers: - name: calico-policy-controller
 image: quay.io/calico/kube-policy-controller:v0.5.4
 env: # The location of the Calico etcd cluster. - name: ETCD_ENDPOINTS
 valueFrom:
 configMapKeyRef:
 name: calico-config
 key: etcd_endpoints
 # Location of the CA certificate for etcd. - name: ETCD_CA_CERT_FILE
 valueFrom:
 configMapKeyRef:
 name: calico-config
 key: etcd_ca
 # Location of the client key for etcd. - name: ETCD_KEY_FILE
 valueFrom:
 configMapKeyRef:
 name: calico-config
 key: etcd_key
 # Location of the client certificate for etcd. - name: ETCD_CERT_FILE
 valueFrom:
 configMapKeyRef:
 name: calico-config
 key: etcd_cert
 # The location of the Kubernetes API. Use the default Kubernetes # service for API access. - name: K8S_API
 value: "https://kubernetes.default:443" # Since we're running in the host namespace and might not have KubeDNS # access, configure the container's /etc/hosts to resolve # kubernetes.default to the correct service clusterIP. - name: CONFIGURE_ETC_HOSTS
 value: "true"
 volumeMounts: # Mount in the etcd TLS secrets. - mountPath: /calico-secrets
 name: etcd-certs
 volumes: # Mount in the etcd TLS secrets. - name: etcd-certs
 secret:
 secretName: calico-etcd-secrets
# kubectl create -f calico.yaml
configmap "calico-config" created
secret "calico-etcd-secrets" created
daemonset "calico-node" created
deployment "calico-policy-controller" created
# kubectl get ds -n kube-system 
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE-SELECTOR AGE
calico-node 1 1 1 1 1 <none> 52s # kubectl get deploy -n kube-system
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
calico-policy-controller 1 1 1 1 6m

这样就搭建了Calico网络,接下来就可以配置NetworkPolicy了。

配置NetworkPolicy

首先,修改ns-calico1的配置:

apiVersion: v1
kind: Namespace
metadata:
 name: ns-calico1
 labels:
 user: calico1
 annotations:
 net.beta.kubernetes.io/network-policy: | { "ingress": { "isolation": "DefaultDeny" } }
# kubectl apply -f ns-calico1.yaml namespace "ns-calico1" configured

如果这个时候再测试两个pod是否连通,一定会不通:

# kubectl exec -it calico2-busybox -n ns-calico2 -- wget --spider --timeout=1 calico1-nginx.ns-calico1 Connecting to calico1-nginx.ns-calico1 (192.168.3.71:80)
wget: download timed out

这就是我们想要的效果,不同Namespace之间的pod不能互通,当然这只是最简单的情况,如果这时候ns-calico1的pod去连接ns-calico2的pod,还是互通的。因为ns-calico2没有设置Namespace annotations。

而且,这时候的ns-calico1会拒绝任何pod的通讯请求。因为,Namespace的annotations只是指定了拒绝所有的通讯请求,还未规定何时接受其他pod的通讯请求。在这里,我们指定只有拥有user=calico1标签的pod可以互联。

apiVersion: extensions/v1beta1
kind: NetworkPolicy
metadata:
 name: calico1-network-policy
 namespace: ns-calico1
spec:
 podSelector:
 matchLabels:
 user: calico1
 ingress: - from: - namespaceSelector:
 matchLabels:
 user: calico1
 - podSelector:
 matchLabels:
 user: calico1
--- 
apiVersion: v1
kind: Pod
metadata:
 name: calico1-busybox
 namespace: ns-calico1
 labels:
 user: calico1
spec:
 containers: - name: busybox
 image: busybox
 command: - sleep
 - "3600"
# kubectl create -f calico1-network-policy.yaml
networkpolicy "calico1-network-policy" created
# kubectl create -f calico1-busybox.yaml
pod "calico1-busybox" created

这时候,如果我通过calico1-busybox连接calico1-nginx,则可以连通。

# kubectl exec -it calico1-busybox -n ns-calico1 -- wget --spider --timeout=1 calico1-nginx.ns-calico1  Connecting to calico1-nginx.ns-calico1 (192.168.3.71:80)

这样我们就实现了Kubernetes的网络隔离。基于NetworkPolicy,可以实现公有云安全组策略。

本文转自中文社区-容器编排之Kubernetes网络隔离NetworkPolicy

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
23天前
|
Kubernetes Cloud Native Docker
云原生时代的容器化实践:Docker和Kubernetes入门
【10月更文挑战第37天】在数字化转型的浪潮中,云原生技术成为企业提升敏捷性和效率的关键。本篇文章将引导读者了解如何利用Docker进行容器化打包及部署,以及Kubernetes集群管理的基础操作,帮助初学者快速入门云原生的世界。通过实际案例分析,我们将深入探讨这些技术在现代IT架构中的应用与影响。
73 2
|
1月前
|
Kubernetes 监控 开发者
掌握容器化:Docker与Kubernetes的最佳实践
【10月更文挑战第26天】本文深入探讨了Docker和Kubernetes的最佳实践,涵盖Dockerfile优化、数据卷管理、网络配置、Pod设计、服务发现与负载均衡、声明式更新等内容。同时介绍了容器化现有应用、自动化部署、监控与日志等开发技巧,以及Docker Compose和Helm等实用工具。旨在帮助开发者提高开发效率和系统稳定性,构建现代、高效、可扩展的应用。
|
25天前
|
存储 Kubernetes Docker
【赵渝强老师】Kubernetes中Pod的基础容器
Pod 是 Kubernetes 中的基本单位,代表集群上运行的一个进程。它由一个或多个容器组成,包括业务容器、基础容器、初始化容器和临时容器。基础容器负责维护 Pod 的网络空间,对用户透明。文中附有图片和视频讲解,详细介绍了 Pod 的组成结构及其在网络配置中的作用。
【赵渝强老师】Kubernetes中Pod的基础容器
|
12天前
|
Kubernetes Cloud Native API
深入理解Kubernetes——容器编排的王者之道
深入理解Kubernetes——容器编排的王者之道
28 1
|
25天前
|
运维 Kubernetes Shell
【赵渝强老师】K8s中Pod的临时容器
Pod 是 Kubernetes 中的基本调度单位,由一个或多个容器组成,包括业务容器、基础容器、初始化容器和临时容器。临时容器用于故障排查和性能诊断,不适用于构建应用程序。当 Pod 中的容器异常退出或容器镜像不包含调试工具时,临时容器非常有用。文中通过示例展示了如何使用 `kubectl debug` 命令创建临时容器进行调试。
|
25天前
|
Kubernetes 调度 容器
【赵渝强老师】K8s中Pod中的业务容器
Pod 是 Kubernetes 中的基本调度单元,由一个或多个容器组成。除了业务容器,Pod 还包括基础容器、初始化容器和临时容器。本文通过示例介绍如何创建包含业务容器的 Pod,并提供了一个视频讲解。示例中创建了一个名为 &quot;busybox-container&quot; 的业务容器,并使用 `kubectl create -f firstpod.yaml` 命令部署 Pod。
|
25天前
|
Kubernetes 容器 Perl
【赵渝强老师】K8s中Pod中的初始化容器
Kubernetes的Pod包含业务容器、基础容器、初始化容器和临时容器。初始化容器在业务容器前运行,用于执行必要的初始化任务。本文介绍了初始化容器的作用、配置方法及优势,并提供了一个示例。
|
1月前
|
Kubernetes 负载均衡 Cloud Native
云原生应用:Kubernetes在容器编排中的实践与挑战
【10月更文挑战第27天】Kubernetes(简称K8s)是云原生应用的核心容器编排平台,提供自动化、扩展和管理容器化应用的能力。本文介绍Kubernetes的基本概念、安装配置、核心组件(如Pod和Deployment)、服务发现与负载均衡、网络配置及安全性挑战,帮助读者理解和实践Kubernetes在容器编排中的应用。
74 4
|
25天前
|
Kubernetes 负载均衡 调度
Kubernetes集群管理与编排实践
Kubernetes集群管理与编排实践
|
25天前
|
Kubernetes 监控 Java
如何在Kubernetes中配置镜像和容器的定期垃圾回收
如何在Kubernetes中配置镜像和容器的定期垃圾回收

推荐镜像

更多