Q1. ReplicaSet을 생성하여 Nginx Pod 3개를 배포하시오.
목표
동일한 애플리케이션을 여러 Pod로 복제하여 가용성을 확보한다.
시나리오
개발팀은 트래픽 증가를 대비해 Nginx 웹 서버를 3개의 Pod로 확장하려고 한다.
Pod 이름은 nginx-replicaset으로 하고, 라벨은 app: nginx로 설정하시오.
검증 코드도 작성해야 한다.
$ vim nginx-rs.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx-rs
image: nginx:latest
ports:
- containerPort: 80
$ kubectl get rs
$ kubectl get pods -l app=nginx
$ kubectl get pods --show-labels
Q2. ClusterIP Service를 생성하여 내부 통신을 가능하게 하시오.
목표
클러스터 내부에서 ReplicaSet Pod에 접근할 수 있는 내부용 서비스 생성
시나리오
백엔드 서비스는 외부에 노출되지 않고 내부 통신용으로만 Nginx ReplicaSet에 접근해야 한다.
Service 이름은 nginx-clusterip로 한다.
검증 코드도 작성해야 한다
$ vim clusterip-service.yml
apiVersion: v1
kind: Service
metadata:
name: nginx-clusterip
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
$ kubectl apply -f clusterip-service.yml
service/nginx-clusterip created
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 3d15h
nginx-clusterip ClusterIP 10.233.3.211 <none> 80/TCP 7s
$ kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.56.11:6443 3d15h
nginx-clusterip 10.233.118.97:80,10.233.73.105:80,10.233.74.33:80 11s
$ kubectl run nettool -it --image=ghcr.io/c1t1d0s7/network-multitool --rm bash
If you don't see a command prompt, try pressing enter.
nettool:/#
Q3. NodePort Service를 생성하여 외부 접속을 가능하게 하시오.
목표
클러스터 외부에서 Nginx ReplicaSet에 접속할 수 있도록 NodePort Service 생성
시나리오
운영팀은 외부 테스트를 위해 Nginx ReplicaSet을 노드 IP + 포트 30080으로 접근 가능하게 하고 싶다.
Service 이름은 nginx-nodeport로 설정한다.
검증 코드도 작성해야 한다
$ cp clusterip-service.yml nodeport-service.yml
$ vim nodeport-service.yml
apiVersion: v1
kind: Service
metadata:
name: nginx-nodeport
spec:
type: NodePort
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30000
$ kubectl apply -f nodeport-service.yml
service/nginx-nodeport created
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 3d15h
nginx-clusterip ClusterIP 10.233.3.211 <none> 80/TCP 6m58s
nginx-nodeport NodePort 10.233.13.98 <none> 80:30000/TCP 10s
$ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
kube-control1 Ready control-plane 3d15h v1.31.9 192.168.56.11 <none> Ubuntu 22.04.5 LTS 5.15.0-160-generic containerd://1.7.27
kube-node1 Ready <none> 3d15h v1.31.9 192.168.56.21 <none> Ubuntu 22.04.5 LTS 5.15.0-160-generic containerd://1.7.27
kube-node2 Ready <none> 3d15h v1.31.9 192.168.56.22 <none> Ubuntu 22.04.5 LTS 5.15.0-160-generic containerd://1.7.27
kube-node3 Ready <none> 3d15h v1.31.9 192.168.56.23 <none> Ubuntu 22.04.5 LTS 5.15.0-160-generic containerd://1.7.27
$ curl 192.168.56.21:30000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Q4. LoadBalancer Service를 생성하여 클라우드 환경에서 외부 접속을 가능하게 하시오.
목표
클라우드 환경에서 외부 IP를 통해 ReplicaSet Pod 접근 가능
시나리오
클라우드 팀은 Nginx 웹 서버를 외부 사용자가 접속 가능하도록 LoadBalancer로 노출하려 한다.
서비스 이름은 nginx-lb로 한다.
검증 코드도 작성해야 한다
$ cp nodeport-service.yml loadbalancer.yml
$ vim loadbalancer.yml
apiVersion: v1
kind: Service
metadata:
name: nginx-lb
spec:
type: LoadBalancer
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
$ kubectl apply -f loadbalancer.yml
service/nginx-lb created
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 3d15h
nginx-lb LoadBalancer 10.233.22.116 192.168.56.201 80:31691/TCP 13s
$ kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.56.11:6443 3d15h
nginx-lb 10.233.118.97:80,10.233.73.105:80,10.233.74.33:80 16s
$ curl 192.168.56.201
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Q5. Ingress를 생성하여 도메인 기반 라우팅을 구성하시오.
목표
단일 Ingress로 여러 서비스/ReplicaSet을 도메인/경로 기반으로 라우팅
시나리오
운영팀은 Nginx ReplicaSet을 /web 경로로 접근 가능하도록 Ingress를 구성하고자 한다.
도메인은 example.com, Ingress 이름은 nginx-ingress로 한다.
ClusterIP Service nginx-clusterip를 라우팅 대상으로 지정하시오.
검증 코드도 작성해야 한다
$ vim ingress.yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /web
pathType: Prefix
backend:
service:
name: nginx-clusterip
port:
number: 80
$ kubectl apply -f ingress.yml
ingress.networking.k8s.io/nginx-ingress created
$ kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
myapp-ing <none> myapp.example.com 192.168.56.21,192.168.56.22,192.168.56.23 80 18h
nginx-ingress <none> example.com 80 11s
$ kubectl create -f clusterip-service.yml
service/nginx-clusterip created
$ kubectl describe ingress nginx-ingress
Name: nginx-ingress
Labels: <none>
Namespace: default
Address: 192.168.56.21,192.168.56.22,192.168.56.23
Ingress Class: <none>
Default backend: <default>
Rules:
Host Path Backends
---- ---- --------
example.com
/web nginx-clusterip:80 (10.233.73.105:80,10.233.74.33:80,10.233.118.97:80)
Annotations: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Sync 86s (x2 over 2m15s) nginx-ingress-controller Scheduled for sync
Normal Sync 86s (x2 over 2m15s) nginx-ingress-controller Scheduled for sync
Normal Sync 86s (x2 over 2m15s) nginx-ingress-controller Scheduled for sync