Search

Cilium Demo

kubectl create -f https://raw.githubusercontent.com/cilium/cilium/refs/heads/main/examples/minikube/http-sw-app.yaml
Shell
복사
적용한 정책이 없어서 ingress, egress 정책 실행이 disable 되어 있음
따라서 2가지 요청 모두 허용된다.
kubectl exec xwing -- curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing kubectl exec tiefighter -- curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing
Shell
복사
Cilium network 정책을 정의할 때 endpoint ip보단 pod에 할당된 label 기반으로 정책을 정의
apiVersion: "cilium.io/v2" kind: CiliumNetworkPolicy metadata: name: "rule1" spec: description: "L3-L4 policy to restrict deathstar access to empire ships only" endpointSelector: matchLabels: org: empire class: deathstar ingress: - fromEndpoints: - matchLabels: org: empire toPorts: - ports: - port: "80" protocol: TCP
YAML
복사
위 network policy는 org=empire label이 있는 pod에서 org=empire, class=deathstar pod로 ip/tcp 등의 라우팅을 컨트롤하는 하는 L3/L4 network policy
kubectl apply -f network-policy.yaml
Shell
복사
정상적으로 차단되는 모습 확인 가능
HTTP 기반 L7 정책으로 최소 권한 적용
kubectl exec tiefighter -- curl -s -XPUT deathstar.default.svc.cluster.local/v1/exhaust-port
Shell
복사
적용 하고자 하는 정책은 아래처럼 /v1/request-landing의 POST 요청만 허용
아래 정책을 사용하면 /v1/request-landing POST 호출을 제외한 나머지 요청이 불가능
apiVersion: "cilium.io/v2" kind: CiliumNetworkPolicy metadata: name: "rule1" spec: description: "L7 policy to restrict access to specific HTTP call" endpointSelector: matchLabels: org: empire class: deathstar ingress: - fromEndpoints: - matchLabels: org: empire toPorts: - ports: - port: "80" protocol: TCP rules: http: - method: "POST" path: "/v1/request-landing"
YAML
복사
kubectl exec xwing -- curl --max-time 3 -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing kubectl exec tiefighter -- curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing
Shell
복사
Put 요청 또는 /v1/exhaust-port가 아니더라도, /v1/request-landing + POST 요청이 아니라면 전부 block
/v1/* 이게 아니라 /v1/.* 이다.
path: "/v1/.*"
YAML
복사
아래 명령어로 policy 조회 가능
kubectl -n kube-system exec {cilium_podname} -- cilium-dbg policy get
Shell
복사
참고로 cilium network policy가 namespace scoped resource라서 적용 source, target 모두 미설정 시 network policy를 생성한 namespace의 리소스 만 대상으로 함
때문에 여러 namespace나 일부 namespace에 적용하려면 아래처럼 하면 된다.
apiVersion: "cilium.io/v2" kind: CiliumNetworkPolicy metadata: name: "rule1" spec: description: "L7 policy to restrict access to specific HTTP calls" endpointSelector: matchLabels: org: empire class: deathstar ingress: - fromEndpoints: - matchLabels: org: empire k8s:io.kubernetes.pod.namespace: default - matchLabels: org: empire k8s:io.kubernetes.pod.namespace: staging - matchLabels: org: empire k8s:io.kubernetes.pod.namespace: prod toPorts: - ports: - port: "80" protocol: TCP rules: http: - method: "GET" path: "/v1/request-landing" - method: "POST" path: "/v1/request-landing" - method: "POST" path: "/v1/approve-landing" - method: "POST" path: "/v1/cancel-landing"
YAML
복사
아래의 명령어를 통해서 http 요청의 실시간 모니터링도 가능하다.
kubectl -n kube-system exec {cilium_podname} -- cilium-dbg monitor -v --type l7
Shell
복사
아래의 명령어를 통해서 DROP된 요청 확인 가능
hubble observe --pod deathstar --verdict DROPPED
Shell
복사