Study Memory Work
[K8S/CKA 자격증] Services & Networking - Network Policy, Ingress, kube-dns 본문
Infra/Kubernetes
[K8S/CKA 자격증] Services & Networking - Network Policy, Ingress, kube-dns
Hera Choi 2023. 1. 25. 22:03Network Policy
- Kubernetes가 지원하는 Pod 통신 접근제한.
- 일종의 방화벽으로 Pod로 트래픽이 들어오고(Inbound), 나가는(Outbound)것을 설정하는 정책
- Ingress트래픽 : In bound 정책. 들어오는 트래픽을 허용할 것인지를 정의
- Egress트래픽 : Out bound 정책. 트래픽이 나갈 수 있는 허용 범위 정의
- 트래픽컨트롤정의
- ipBlock : CIDR IP 대역으로, 특정 IP 대역에서만 트래픽이 들어오도록 지정할 수 있다.
- podSelector : label을 이용하여, 특정 label을 가지고 있는 Pod들에서 들어오는 트래픽만 받을 수 있다. 예를 들어 DB Pod의 경우에는 API server로 부터 들어오는 트래픽만 받는 것과 같은 정책 정의가 가능하다.
- namespaceSelector : 특정 namespace로 부터 들어오는 트래픽 만을 받는 기능이다. 운영 로깅 서버의 경우에는 운영 환경namespace에서만 들어오는 트래픽을 받거나, 특정 서비스 컴포넌트의 namespace에서의 트래픽만 들어오게 컨트롤이 가능하다. 내부적으로 새로운 서비스 컴포넌트를 오픈했을 때, 베타 서비스를 위해서 특정 서비스나 팀에게만 서비스를 오픈하고자 할 때 유용하게 사용할 수 있다.
- Protocol & Port : 특정 Protocol 또는 Port로 설정된 트래픽만 허용되는 포트를 정의할 수 있다.
- 적용예:
Network Policies
If you want to control traffic flow at the IP address or port level (OSI layer 3 or 4), NetworkPolicies allow you to specify rules for traffic flow within your cluster, and also between Pods and the outside world. Your cluster must use a network plugin tha
kubernetes.io
Ingress
- L7스위치 역할을 논리적으로 수행
- 클러스터로 접근하는 URL별로 다른 서비스에 트래픽을 분산
- Kubernetes Ingress의 능
- Service에 외부 URL을 제공
- 트래픽을 로드밸런싱
- SSL 인증서 처리
- Virtual hosting을 지정 (하나가 아닌 여러개의 host를 지정할 수 있다)

인그레스(Ingress)
기능 상태: Kubernetes v1.19 [stable] 클러스터 내의 서비스에 대한 외부 접근을 관리하는 API 오브젝트이며, 일반적으로 HTTP를 관리함. 인그레스는 부하 분산, SSL 종료, 명칭 기반의 가상 호스팅을 제공
kubernetes.io
kube-dns
- 쿠버네티스 클러스터에서 사용하는 DNS
- 클러스터 내의 모든 Service에는 DNS네임이 할당된다. 범위: 클러스터 내.
- svc-name.namespace.svc.cluster.local
- 클러스터에서 동작되는 모든 Pod의 /etc/resolv.conf에는 kube-dns가 namespace로 정의되어 있다.
$ cat /etc/resolv.conf
###
nameserver 10.32.0.10
search <namespace>.svc.cluster.local svc.cluster.local cluster.local
###
- 특정 Pod에서 servicename이나 podname으로 Access가능
- service name과 clusterIP mapping 정보
- pod name과 podIP mapping 정보
svc-name.namespace.svc.cluster.local // web.namespace.svc.cluster.local
pod-ip.namespace.pod.cluster.local // 172-12-1-3.namespace.pod.cluster.local

[실습]
app:web 레이블을 가진 Pod에 특정 namespace의 Pod들만 접근 허용
$ kubectl config use-context hk8s
$ kubectl run webpod --image=nginx --port=80 --labels=app=web
$ kubectl get pods webpod -o wide
$ kubectl create namespace dev
$ kubectl create namespace prod
$ kubectl label namespace prod purpose=production
$ kubectl label namespace dev purpose=development
$ kubectl get namespaces --show-labels
$ vi web-allow-prod.yaml
###
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: web-allow-prod
spec:
podSelector:
matchLabels:
app: web
ingress:
- from:
- namespaceSelector:
matchLabels:
purpose: production
###
$ kubectl apply -f web-allow-prod.yaml
$ kubectl get networkpolicies.networking.k8s.io
$ kubectl run test --namespace=dev --rm -it --image=alpine -- /bin/sh
/ # wget -qO- --timeout=2 http://web.default.svc.cluster.local
$ kubectl run test --namespace=prod --rm -it --image=alpine -- /bin/sh
/ # wget -qO- --timeout=2 http://web.default.svc.cluster.local
$ kubectl delete networkpolicy web-allow-prod $ kubectl delete pod webpod
$ kubectl delete namespace {prod,dev}
[문제 4] NetworkPolicy
작업 클러스터 : hk8s
- default namespace에 다음과 같은 pod를 생성하세요.
- name: poc
- image: nginx
- port: 80
- label: app=poc
- "partition=customera"를 사용하는 namespace에서만 poc의 80포트로 연결할 수 있도록
default namespace에 'allow-web- from-customera'라는 network Policy를 설정하세요.
보안 정책상 다른 namespace의 접근은 제한합니다.
# 0. 클러스터 변경
$ kubectl config use-context hk8s
# 1. pod 생성
$ kubectl run poc --image=nginx --port=80 --label=app=poc -o yaml
$ kubectl run poc --image=nginx --port=80 --label=app=poc
# 2. default namespace label 조회
$ kubectl get namespaces -L partition
# 3. network policy 설정
$ vi np.yaml
###
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-web- from-customera
namespace: default
spec:
podSelector:
matchLabels:
app: poc
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
partition: customera
ports:
- protocol: TCP
port : 80
###
$ kubectl apply -f np.yaml
# 4. 확인
$ kubectl run testpod -n customerb --image=centos:7 -it --rm -- /bin/bash
/]# curl 192.168.75.100 --실패
/]# exit
$ kubectl run testpod -n customera --image=centos:7 -it --rm -- /bin/bash
/]# curl 192.168.75.100
/]# exit
[문제5] Ingress 구성
작업 클러스터 : k8s
- ingress-nginx namespace에 nginx이미지를 app=nginx 레이블을 가지고 실행하는 nginx pod를 구성하세요.
- 현재 appjs-servic와 nginx 서비스는 이미 동작 중입니다. 별도 구성이 필요 없습니다.
- app-ingress.yaml 파일을 생성하고, 다음 조건의 ingress 를 구성하세요.
- name: app-ingress
- NODE_PORT:30080/ 접속했을 때 nginx 서비스로 연결
- NODE_PORT:30080/app 접속했을 때 appjs-service 서비스로 연결
- Ingress 구성에 다음의 annotations을 포함시키세요.
annotations: kubernetes.io/ingress.class: nginx
# 1. pod 생성
$ kubectl run nginx --image=nginx --labels=app=nginx --namespagress-nginx
$ kubectl get pod -L ingress-nginx
# 2. ingress 생성
$ vi app-ingress.yaml
###
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: ingress-nginx
name: app-ingress
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: nginx
- cchttp:
paths:
- pathType: Prefix
path: "/app"
backend:
service:
name: appjs-service
###
$ kubectl apply -f app-ingress.yaml
# 3. 확인
$ curl k8s-worker1:30080/app
[문제6] Service and DNS Lookup구성
작업 클러스터 : k8s
- image nginx를 사용하는 resolver pod를 생성하고 resolver-service라는 service를 구성합니다.
- 클러스터내에서 service와 pod이름을 조회할 수 있는지 테스트합니다.
- dns 조회에 사용하는 pod 이미지는 busybox:1.28이고, service와 pod 이름 조회는 nlsookup을 사용합니다.
- service 조회 결과는 /var/CKA2022/nginx.svc에 pod name 조회 결과는 /var/CKA2022/nginx.pod 파일에 기록합니다.
$ kubectl config use-context k8s
$ kubectl run resolver --image=nginx --port=80
$ kubectl expose pod resolver --name resolver-service --port=80
$ kubectl get svc resolver-service
$ kubectl run test-nslookup --image=busybox:1.28 -it --restart=Never --rm -- nslookup 10.104.150.11
$ kubectl run test-nslookup --image=busybox:1.28 -it --restart=Never --rm -- nslookup nginx-resolver-service > /var/CKA2022/nginx.svc
# pod name service
$ kubectl get pod ngnix-resolver -o wide
$ kubectl run test-nslookup --image=busybox:1.28 -it --restart=Never --rm -- nslookup 10-244-1-55.default.pod.cluster.local > /var/CKA2022/nginx.pod
$ cat /var/CKA2022/nginx.svc
$ cat /var/CKA2022/nginx.pod
'Infra > Kubernetes' 카테고리의 다른 글
[K8S/CKA 자격증] Storage Class (0) | 2023.01.29 |
---|---|
[K8S/CKA 자격증] Storage - Volume mount (0) | 2023.01.27 |
[K8S/CKA 자격증] Services & Networking - Service 동작 원리, Cluster IP, NodePort (0) | 2023.01.22 |
[K8S/CKA 자격증] Worloads & Scheduling - Secret (0) | 2023.01.22 |
[K8S/CKA 자격증] Worloads & Scheduling - ConfigMap (0) | 2023.01.22 |