Kubernetes 노드 보호
이 페이지는 Calico Enterprise가 관리하는 host endpoints로 Kubernetes 노드 자체를 보호하는 방법을 다뤄요. 자동 host endpoints를 켜고, 커스텀 템플릿으로 세밀하게 구성하고, 노드에 네트워크 정책을 적용하는 과정까지 튜토리얼로 함께 살펴봐요.
출처: 문서
본문
Big picture
Calico Enterprise가 관리하는 host endpoints로 Kubernetes 노드를 안전하게 지켜요.
Value
Calico Enterprise는 Kubernetes 노드를 위한 host endpoints를 자동으로 만들 수 있어요. 즉, 클러스터가 변하면서 host endpoints의 수명 주기를 Calico Enterprise가 관리하고, 노드가 항상 정책으로 보호되도록 보장해요.
Concepts
Host endpoints
각 호스트는 외부와 통신하는 네트워크 인터페이스를 하나 이상 갖고 있어요. Calico Enterprise에서는 이 인터페이스들을 host endpoints로 표현한 뒤, 네트워크 정책으로 보호할 수 있어요.
Calico Enterprise host endpoints는 라벨을 가질 수 있고, workload endpoints의 라벨과 똑같이 동작해요. 네트워크 정책 규칙은 라벨 셀렉터를 사용해 workload와 host endpoints 모두에 적용될 수 있어요.
자동 host endpoints는 호스트의 모든 인터페이스를 보호해요 (즉, 리눅스에서는 host network namespace의 모든 인터페이스). interfaceName: "*" 설정으로 만들어져요.
Automatic host endpoints
Calico Enterprise는 각 노드마다 와일드카드 host endpoint를 만들고, 그 host endpoint는 해당 노드와 같은 라벨과 IP 주소를 갖고 있어요. Calico Enterprise는 주기적인 동기화로 이 관리 host endpoints가 노드와 같은 라벨과 IP 주소를 유지하도록 보장해요. 따라서 이런 자동 host endpoints를 대상으로 하는 정책은 노드를 선택하기 위해 마련해 둔 정책과 함께 올바르게 동작하고, 시간이 지나 노드의 IP나 라벨이 바뀌어도 그래요.
Calico Enterprise는 사용자가 지정한 템플릿을 기반으로 노드용 커스텀 host endpoints를 만들고 관리할 수도 있어요. 덕분에 어떤 인터페이스를 host endpoint에 포함할지, 어떤 노드에 host endpoints를 만들지 세밀하게 조정할 수 있어요. host endpoint 템플릿은 KubeControllersConfig를 업데이트해서 활성화할 수 있어요.
자동 host endpoints는 projectcalico.org/created-by: calico-kube-controllers 라벨로 다른 host endpoints와 구분돼요. 기본 KubeControllersConfiguration 리소스를 구성해서 자동 host endpoints를 켜거나 끌 수 있어요.
Before you begin
지원하지 않음
- GKE
How to
Enable automatic host endpoints
자동 host endpoints를 활성화하려면, 기본 KubeControllersConfiguration 인스턴스를 편집해 spec.controllers.node.hostEndpoint.autoCreate를 true로 설정해요:
kubectl patch kubecontrollersconfiguration default --patch='{"spec": {"controllers": {"node": {"hostEndpoint": {"autoCreate": "Enabled"}}}}}'
성공하면 클러스터의 각 노드마다 host endpoints가 생성돼요:
kubectl get heps -o wide
출력은 대략 이런 모습이에요:
kubectl get heps -o wide
NAME CREATED AT
ip-172-16-101-147.us-west-2.compute.internal-auto-hep 2021-05-12T22:16:47Z
ip-172-16-101-54.us-west-2.compute.internal-auto-hep 2021-05-12T22:16:47Z
ip-172-16-101-79.us-west-2.compute.internal-auto-hep 2021-05-12T22:16:47Z
ip-172-16-101-9.us-west-2.compute.internal-auto-hep 2021-05-12T22:16:47Z
ip-172-16-102-63.us-west-2.compute.internal-auto-hep 2021-05-12T22:16:47Z
Use custom templates for host endpoints
템플릿 HostEndpoints는 맞춤형 자동 HostEndpoint 생성을 가능하게 해줘요. spec.controllers.node.hostEndpoint.templates에 정의하며, 특정 Node나 인터페이스 조건에 따라 생성할 수 있어서 기본 자동 HostEndpoints보다 커스터마이즈 범위가 넓어요.
템플릿 host endpoints를 활성화하고 기본 host endpoints를 비활성화하려면, 기본 KubeControllersConfiguration 인스턴스를 편집해 spec.controllers.node.hostEndpoint.templates에 커스텀 템플릿 spec을 반영해요.
-
커스텀 templates spec을 만들어요:
cat << 'EOF' > hostEndpointTemplate.yaml spec: controllers: node: hostEndpoint: autoCreate: Enabled createDefaultHostEndpoint: Disabled templates: - generateName: custom-host-endpoint interfaceCIDRs: - 192.168.0.0/16 nodeSelector: "!has(node-role.kubernetes.io/control-plane)" labels: custom-label: template-host-endpoint EOF -
templates spec을
kubecontrollersconfiguration리소스에 병합해요:kubectl patch kubecontrollersconfiguration default --patch-file hostEndpointTemplate.yaml --type merge -
kubecontrollersconfiguration리소스가 업데이트됐는지 확인해요:kubectl get kubecontrollersconfiguration default -o yaml출력은 대략 이런 모습이어야 해요:
apiVersion: projectcalico.org/v3 kind: KubeControllersConfiguration metadata: name: default spec: controllers: node: hostEndpoint: autoCreate: Enabled createDefaultHostEndpoint: Enabled templates: - generateName: custom-host-endpoint interfaceCIDRs: - 192.168.0.0/16 labels: custom-label: template-host-endpoint -
성공하면 node selector에 매칭되는 노드에 host endpoints가 생성돼요:
kubectl get heps -o=custom-columns='NAME:.metadata.name,NODE:.spec.node,INTERFACES:.spec.interfaceName,IPS:.spec.expectedIPs,PROFILES:.spec.profiles'출력은 대략 이런 모습이에요:
NAME NODE INTERFACE IPS PROFILES 192.168.83.64.compute.internal-custom-host-endpoint-auto-hep 192.168.83.64.compute.internal 192.168.83.64 projectcalico-default-allow 192.168.0.128.compute.internal-custom-host-endpoint-auto-hep 192.168.0.128.compute.internal 192.168.0.128 projectcalico-default-allow 192.168.157.128.compute.internal-custom-host-endpoint-auto-hep 192.168.157.128.compute.internal 192.168.157.128 projectcalico-default-allow
Apply network policy to automatic host endpoints
모든 Kubernetes 노드를 대상으로 하는 정책을 적용하려면, 먼저 노드에 라벨을 추가해요. 그 라벨은 자동 host endpoints로 동기화돼요.
예를 들어 모든 노드와 그들의 host endpoints에 kubernetes-host 라벨을 추가하려면:
kubectl label nodes --all kubernetes-host=
그리고 정책 스니펫 예시예요:
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
name: all-nodes-policy
spec:
selector: has(kubernetes-host)
#<정책의 나머지 부분>
특정 host endpoints 집합(그리고 해당 Kubernetes 노드들)을 선택하려면, 그 host endpoints 집합에 고유한 라벨을 선택하는 정책 셀렉터를 사용해요. 예를 들어 node1과 node2라는 이름의 노드에 environment=dev 라벨을 추가하고 싶다면:
kubectl label node node1 environment=dev
kubectl label node node2 environment=dev
라벨이 준비되고 자동 host endpoints가 활성화되어 있으면, node1과 node2의 host endpoints가 environment=dev 라벨로 업데이트될 거예요. 셀렉터를 조합해 그 노드 집합을 선택하는 정책을 작성할 수 있어요:
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
name: some-nodes-policy
spec:
selector: has(kubernetes-host) && environment == 'dev'
#<정책의 나머지 부분>
Tutorial
이 튜토리얼은 Kubernetes 노드의 ingress를 SSH와 Kubernetes 동작에 필요한 포트만 허용하도록 잠그는 실습이에요. 정책 두 개를 적용할 거예요. 하나는 컨트롤 플레인 노드용, 다른 하나는 워커 노드용이에요.
SECONDARY: 참고: 이 튜토리얼은 AWS에서 kubeadm v1.18.2로 만든 클러스터, "stacked etcd" 토폴로지로 테스트됐어요. Stacked etcd 토폴로지란 etcd pod가 마스터에서 실행되는 구성이에요. kubeadm은 기본적으로 stacked etcd를 사용해요. Kubernetes 클러스터가 다른 플랫폼에 있거나, Kubernetes 변종을 실행하거나, 외부 etcd 클러스터를 사용하는 토폴로지라면, 클러스터의 컨트롤 플레인과 워커 노드에 필요한 포트를 검토하고 이 튜토리얼의 정책을 필요에 맞게 조정해요.
먼저 컨트롤 플레인 노드로 들어오는 ingress 트래픽을 제한할게요. 아래 ingress 정책에는 세 개의 규칙이 있어요. 첫 번째 규칙은 어디서든 API server 포트로 접근할 수 있게 해줘요. 두 번째 규칙은 localhost로의 모든 트래픽을 허용해 Kubernetes가 컨트롤 플레인 프로세스에 접근할 수 있게 해줘요. 이 컨트롤 플레인 프로세스에는 etcd server client API, 스케줄러, controller-manager가 포함돼요. 이 규칙은 kubelet API와 calico/node 헬스 체크에 대한 localhost 접근도 허용해요. 그리고 마지막 규칙은 etcd pod끼리 피어링할 수 있게 하고, 마스터가 서로의 kubelet API에 접근할 수 있게 해줘요.
failsafe 포트를 수정하지 않았다면, 이 정책을 적용한 뒤에도 SSH 접근은 유지될 거예요. 이제 Kubernetes 마스터의 ingress 정책을 적용해요:
kubectl apply -f - << EOF
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
name: ingress-k8s-masters
spec:
selector: has(node-role.kubernetes.io/control-plane)
# This rule allows ingress to the Kubernetes API server.
ingress:
- action: Allow
protocol: TCP
destination:
ports:
# kube API server
- 6443
# This rule allows all traffic to localhost.
- action: Allow
destination:
nets:
- 127.0.0.0/8
# This rule is required in multi-master clusters where etcd pods are colocated with the masters.
# Allow the etcd pods on the masters to communicate with each other. 2380 is the etcd peer port.
# This rule also allows the masters to access the kubelet API on other masters (including itself).
- action: Allow
protocol: TCP
source:
selector: has(node-role.kubernetes.io/control-plane)
destination:
ports:
- 2380
- 10250
EOF
위 정책은 kubeadm이 컨트롤 플레인 노드에 설정하는 표준 node-role.kubernetes.io/control-plane 라벨을 선택한다는 점만 기억해두면 돼요.
다음은 Kubernetes 워커의 ingress를 제한하는 정책을 적용할 차례예요. 정책을 추가하기 전에 모든 워커 노드에 라벨을 추가할게요. 이 라벨은 노드의 자동 host endpoint에도 추가돼요. 이 튜토리얼에서는 kubernetes-worker를 사용할게요. 워커 노드에 라벨을 추가하는 명령 예시예요:
kubectl get node -l '!node-role.kubernetes.io/control-plane' -o custom-columns=NAME:.metadata.name | tail -n +2 | xargs -I{} kubectl label node {} kubernetes-worker=
워커의 ingress 정책은 두 개의 규칙으로 이루어져요. 첫 번째 규칙은 localhost로의 모든 트래픽을 허용해요. 마스터와 마찬가지로 워커 노드도 자기 localhost의 kubelet API와 calico/node 헬스 체크에 접근해야 하거든요. 두 번째 규칙은 마스터가 워커의 kubelet API에 접근할 수 있게 해줘요. 이제 정책을 적용해요:
kubectl apply -f - << EOF
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
name: ingress-k8s-workers
spec:
selector: has(kubernetes-worker)
# Allow all traffic to localhost.
ingress:
- action: Allow
destination:
nets:
- 127.0.0.0/8
# Allow only the masters access to the nodes kubelet API.
- action: Allow
protocol: TCP
source:
selector: has(node-role.kubernetes.io/control-plane)
destination:
ports:
- 10250
EOF