Exercise 9.1: Deploy a New Service
CP 노드에 연결된 터미널로 이동
Deployment 생성 시도
cat <<EOF | kubectl apply -f - apiVersion: apps/v1 kind: Deployment metadata: name: nginx-one labels: system: secondary namespace: accounting spec: selector: matchLabels: system: secondary replicas: 2 template: metadata: labels: system: secondary spec: containers: - image: nginx:1.20.1 imagePullPolicy: Always name: nginx ports: - containerPort: 8080 protocol: TCP nodeSelector: system: secondOne EOF
Namespace 생성
kubectl create ns accounting
Deployment 생성
cat <<EOF | kubectl apply -f - apiVersion: apps/v1 kind: Deployment metadata: name: nginx-one labels: system: secondary namespace: accounting spec: selector: matchLabels: system: secondary replicas: 2 template: metadata: labels: system: secondary spec: containers: - image: nginx:1.20.1 imagePullPolicy: Always name: nginx ports: - containerPort: 8080 protocol: TCP nodeSelector: system: secondOne EOF
생성된 Pod 상태 확인
kubectl get pod -n accounting
Pod 상세 내용에서 이벤트 확인
kubectl describe pod POD_NAME -n accounting
OR
kubectl describe pod $(kubectl get pod -l system=secondary -o=jsonpath='{.items[0].metadata.name}' -n accounting) -n accounting
노드에 부여된 Label 확인
kubectl get node --show-labels
Worker 노드에 Label 부여
kubectl label node worker system=secondOne
노드에 부여된 Label 확인
kubectl get node --show-labels
Pod가 배포 되는지 확인
kubectl get pod -n accounting
위에서 생성한 Deployment에 서비스 생성
kubectl expose deployment/nginx-one -n accounting
위에 서비스에 연결된 Endpoints 확인
kubectl get ep nginx-one -n accounting
CURL 명령어를 통해서 Endpoint 주소로 접근 시도
curl ENDPOINT_IP:ENDPOINT_PORT
OR
curl $(kubectl get ep nginx-one -o=jsonpath='{.subsets[0].addresses[0].ip}:{.subsets[0].ports[0].port}' -n accounting)
CURL 명령어를 통해서 Endpoint 주소의 80포트로 접근
curl ENDPOINT_IP:80
OR
curl $(kubectl get ep nginx-one -o=jsonpath='{.subsets[0].addresses[0].ip}:80' -n accounting)
Deployment 수정
cat <<EOF | kubectl apply -f - apiVersion: apps/v1 kind: Deployment metadata: name: nginx-one labels: system: secondary namespace: accounting spec: selector: matchLabels: system: secondary replicas: 2 template: metadata: labels: system: secondary spec: containers: - image: nginx:1.20.1 imagePullPolicy: Always name: nginx ports: - containerPort: 80 protocol: TCP nodeSelector: system: secondOne EOF
생성된 Service의 ClusterIP로 HTTP 요청
curl $(kubectl -n accounting get svc nginx-one -o=jsonpath='{.spec.clusterIP}')
생성된 Service 확인
kubectl -n accounting get svc nginx-one
Port 변경
kubectl -n accounting patch svc nginx-one --type=json -p='[ {"op": "replace", "path": "/spec/ports/0/port", "value": 80}, {"op": "replace", "path": "/spec/ports/0/targetPort", "value": 80}]'
Port가 변경되었는지 확인
kubectl -n accounting get svc nginx-one
Service의 ClusterIP로 HTTP 요청
curl $(kubectl -n accounting get svc nginx-one -o=jsonpath='{.spec.clusterIP}')
Iptable의 모든 규칙 확인
sudo iptables-save
Iptable의 NAT 규칙 확인
sudo iptables -L -t nat
KUBE-SERVICES 규칙 확인
sudo iptables -t nat -L KUBE-SERVICES -n | column -t
생성된 Service의 ClusterIP 확인
kubectl -n accounting get svc nginx-one
Service의 Cluster IP로 연결된 규칙의 상세내용 확인
{ export SERVICE_CHAIN=$(sudo iptables -t nat -L KUBE-SERVICES -n | column -t | grep "accounting/nginx-one" | grep -oE "^KUBE-SVC-[A-Z0-9]+") sudo iptables -t nat -L $SERVICE_CHAIN -n | column -t }
위의 명령어로 나온 결과중의 한개의 Chain 규칙 확인
sudo iptables -t nat -L $(sudo iptables -t nat -L $SERVICE_CHAIN -n | column -t | grep -oE "^KUBE-SEP-[A-Z0-9]+" | head -1) \ -n | column -t
생성된 Pod의 IP주소 확인
kubectl -n accounting get pod -l system=secondary -o wide
생성된 Deployment의 Replica 갯수를 6개로 조정
kubectl -n accounting scale deployment nginx-one --replicas=6
Service의 Cluster IP로 연결된 규칙의 상세내용 확인
sudo iptables -t nat -L $SERVICE_CHAIN -n | column -t
kube-proxy 로그 확인
kubectl -n kube-system logs ds/kube-proxy
kube-proxy 로그 레벨 변경
kubectl -n kube-system patch ds kube-proxy --type=json \ -p='[{"op": "add", "path": "/spec/template/spec/containers/0/command/-", "value": "--v=10"}]'
kube-proxy Pod가 새로 생성되는지 확인
kubectl -n kube-system get pod -l k8s-app=kube-proxy
생성된 Deployment의 Replica 갯수를 2개로 조정
kubectl -n accounting scale deployment nginx-one --replicas=2
Service의 Cluster IP로 연결된 규칙의 상세내용 확인
sudo iptables -t nat -L $SERVICE_CHAIN -n | column -t
kube-proxy 로그 확인
kubectl -n kube-system logs ds/kube-proxy
Last updated