Network Policy란?

접근을 제한하는 기능, yaml 파일을 통해 기준은 pod이고 ingress(들어오는 것)과 egress(나가는 것)에 대한 설정을 하는게 시험에서 요구함.

Network Policy → The NetworkPolicy resource

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
	name: test-network-policy
	namespace: default
spec:
	podSelector:
		matchLabels:
			role: db
	policyTypes:
  - Ingress
  - Egress
	ingress:
	  -from:
	    -ipBlock:
				cidr: 172.17.0.0/16
				except:
        - 172.17.1.0/24
	    -namespaceSelector:
				matchLabels:
					project: myproject
	    -podSelector:
				matchLabels:
					role: frontend
			ports:
				-protocol: TCP
				port: 6379
	egress:
  -to:
    -ipBlock:
			cidr: 10.0.0.0/24
		ports:
    -protocol: TCP
		 port: 5978
--------------------------------------------------------------------------------------
코드 분석
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
	name: test-network-policy
	namespace: default
spec:
	podSelector:
		matchLabels:
			role: db
	policyTypes:
  - Ingress
  - Egress
=> defualt라는 namespace에서 pod들이 존재하는데 이 pod들은 role:db라는 label을 갖고 있고 이 파드들에 ingress traffic, egress traffic을 제한하는 내용
  -------
	ingress:
	- from:
	  - ipBlock:
		  	cidr: 172.17.0.0/16
				except:
        - 172.17.1.0/24
	  - namespaceSelector:
			 matchLabels:
				 project: myproject
	  - podSelector:
			 matchLabels:
				 role: frontend
		ports:
		- protocol: TCP
			port: 6379
=> ingress는 from과 port가 존재하는데 
	 port는 6379만 허용한다는 의미이고 
	 from은 ipBlcok,namespaceSelector, podSelector 3가지가 존재한다.
	 ipBlock은 위 예시로 172.17.0.0/16에 있는 모든 pod들은 ingress가 허용
											 but 172.17.1.0/24는 예외라는 의미
	 namespaceSelector는 namespace명이 myproject인 pod들은 ingress가 허용
	 podSelector는 role이 frontend라는 이름을 갖고 있는 label pod들만 ingress가 허용
	 => 위의 3가지들은 namespace가 Default이고 port가 6379이며 role이 db인 pod들에게 
	    ingress가 허용이 되는 policy가 생김
	--------   
  egress:
  -to:
    -ipBlock:
			cidr: 10.0.0.0/24
		ports:
    -protocol: TCP
		 port: 5978
=> egress는 특정 port와 ipBlock만 사용 가능
   ipBlock는 위 예시로 10.0.0.0/24의 ip를 가진 pod들은 port번호 5978로만 나가는 것을 허용
   

Untitled


  1. kubectl config use-config hk8s
  2. kubectl get namespace devops ⇒ policy를 지정 할 namespace devops가 존재하는 지 확인
  3. kubectl get pods -n devops —show-labels ⇒ yaml 파일 속 matchLabels: 에 넣어 줄 값 찾기

Untitled

  1. kubectl get namespace migops ⇒ policy를 지정 할 namespace migops가 존재하는 지 확인
  2. Cretae NetworkPolicy
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
	name: test-network-policy
	namespace: default
spec:
	podSelector:
		matchLabels:
			role: db
	policyTypes:
  - Ingress
  - Egress
	ingress:
	  -from:
	    -ipBlock:
				cidr: 172.17.0.0/16
				except:
        - 172.17.1.0/24
	    -namespaceSelector:
				matchLabels:
					project: myproject
	    -podSelector:
				matchLabels:
					role: frontend
			ports:
				-protocol: TCP
				port: 6379
	egress:
  -to:
    -ipBlock:
			cidr: 10.0.0.0/24
		ports:
    -protocol: TCP
		 port: 5978
		 
수정
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
	name: allow-port-from-namespace (수정)
	namespace: devops (수정)
spec:
	podSelector:
		matchLabels:
			app: web
	policyTypes:
  - Ingress
	ingress:
	  -from:
	    -namespaceSelector:
				matchLabels:
					project: migops (수정)
			ports:
				-protocol: TCP
				port: 80
  1. cat > allow-port-from-namespace.yaml
  2. 수정한 코드 복붙 후 ctrl + d