root@manager:~/k8slab/quiz# k get ns
NAME STATUS AGE
default Active 6d2h
ingress-nginx Active 6d1h
kube-node-lease Active 6d2h
kube-public Active 6d2h
kube-system Active 6d2h
metallb-system Active 6d2h
root@manager:~/k8slab/quiz# ls k8s-quiz/
quiz1 quiz2 quiz3 quiz4 quiz5 quiz6 quiz7 quiz8 quiz9
root@manager:~/k8s-quiz/quiz1# k get pod -n quiz1
NAME READY STATUS RESTARTS AGE
my-nginx-deployment-7484748b57-bdvgp 1/1 Running 0 51s
my-nginx-deployment-7484748b57-crlpd 1/1 Running 0 51s
my-nginx-deployment-7484748b57-rf6zh 1/1 Running 0 51s
apiVersion: v1
kind: Namespace
metadata:
name: quiz1
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx-deployment
namespace: quiz1 # namespace 추가
spec:
replicas: 3
selector:
matchLabels: # 오타 수정
app: my-nginx-pod # label 오타 수정
template:
metadata:
name: my-nginx-pod
labels:
app: my-nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.10
ports:
- containerPort: 80
- quiz1 namespace 에 속하게 namespace 를 명시해준다
- matchlabels -> matchLabels 오타 수정
- matchLabels 에 해당하는 label 을 Pod 의 label 과 동일하게 수정
root@manager:~/k8slab/quiz/k8s-quiz/quiz1# k get pod -n quiz1
NAME READY STATUS RESTARTS AGE
my-nginx-deployment-7569bb4555-5smgk 1/1 Running 0 2m6s
my-nginx-deployment-7569bb4555-q5llt 1/1 Running 0 2m6s
my-nginx-deployment-7569bb4555-rh2q9 1/1 Running 0 2m6s
root@manager:~/k8s-quiz/quiz2# k get pod -n quiz2
NAME READY STATUS RESTARTS AGE
container-env-example 1/1 Running 0 20s
root@manager:~/k8s-quiz/quiz2# kubectl exec container-env-example -n quiz2 -- env
...
container=docker
k8s=kubernetes
...
apiVersion: v1
kind: Namespace
metadata:
name: quiz2
---
apiVersion: v1
kind: Pod
metadata:
namespace: quiz2
name: container-env-example
spec:
containers:
- name: my-container
image: busybox
args: ['tail', '-f', '/dev/null']
envFrom:
- configMapRef:
name: start-k8s
---
apiVersion: v1 # ConfigMap 추가
kind: ConfigMap
metadata:
namespace: quiz2 # Namespace 에 포함시켜야 한다
name: start-k8s
data:
container: docker
k8s: kubernetes
- ConfigMap 을 Pod 가 속한 Namespace 에 배포하지 않으면, Container 배포시 같은 Namespace 에 속하지 않아서 해당 ConfigMap 을 찾지 못하므로 Error 가 생긴다. 주의하자!!!
root@manager:~/k8slab/quiz/k8s-quiz/quiz2# k get pod -n quiz2
NAME READY STATUS RESTARTS AGE
container-env-example 1/1 Running 0 5s
root@manager:~/k8slab/quiz/k8s-quiz/quiz2# kubectl exec container-env-example -n quiz2 -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=container-env-example
container=docker
k8s=kubernetes
root@manager:~/k8s-quiz/quiz3# k get svc -n quiz3
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
webserver-nodeport NodePort 10.104.107.84 <none> 8080:32151/TCP 10s
또한 위와 같은 결과를 확인했을 경우 윈도우에서 브라우저를 열고 주소창에 'http://211.183.3.101:32151' 과 같이 입력했을 경우 nginx 의 페이지가 출력되어야 한다
apiVersion: v1
kind: Namespace
metadata:
name: quiz3
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: quiz3
name: test-deploy
spec:
replicas: 3
selector:
matchLabels:
app: webserver
template:
metadata:
name: my-webserver
labels:
app: webserver
spec:
containers:
- name: my-webserver
image: nginx:1.10 # nginx:1.10 띄어쓰기 고치기
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
namespace: quiz3
name: webserver-nodeport
spec:
selector:
app: webserver
ports:
- name: web-port
port: 8080 # port 를 8080 으로 수정
targetPort: 80 # target port 수정
type: NodePort # 들여쓰기 고치기
- nodeport 의 Port 번호 수정 및 띄여쓰기와 들여쓰기를 고쳐야 한다
root@manager:~/k8slab/quiz/k8s-quiz/quiz3# k get svc -n quiz3
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
webserver-nodeport NodePort 10.99.181.125 <none> 8080:30547/TCP 12s
root@manager:~/k8s-quiz/quiz4# k get svc -n quiz4
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx LoadBalancer 10.98.158.127 211.183.3.202 80:32739/TCP 52s
apiVersion: v1
kind: Namespace
metadata:
name: quiz4
---
apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb-system # namespace 수정
name: config
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- 211.183.3.201-211.183.3.239
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: quiz4
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.10 # 이미지 버전 지정
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
namespace: quiz4
name: nginx
labels:
app: nginx
spec:
externalTrafficPolicy: Local
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
type: LoadBalancer # 오타 수정
- metallb ConfigMap 은 metallb-system 을 지정해야 한다
root@manager:~/k8slab/quiz/k8s-quiz/quiz4# k get svc -n quiz4
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx LoadBalancer 10.96.178.228 211.183.3.201 80:32143/TCP 6s
root@manager:~/k8s-quiz/quiz5# k get ing -n quiz5
NAME CLASS HOSTS ADDRESS PORTS AGE
example-ingress <none> * 211.183.3.201 80 16m
또한 http://211.183.3.201/http 로 접속했을 때에는 httpd 의 >기본페이지 내용인 "It works!" 가 보여야 한다
sudo ufw disable
kubectl delete validatingwebhookconfiguration ingress-nginx-admission
#!/bin/bash
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.2/deploy/static/provider/cloud/deploy.yaml
kubectl delete validatingwebhookconfiguration ingress-nginx-admission
kubectl apply -f ingress-q.yaml
apiVersion: v1
kind: Namespace
metadata:
name: quiz5
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: quiz5
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: testnginx
port:
number: 80
- path: /http
pathType: Prefix
backend:
service:
name: testhttpd-service
port:
number: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: quiz5
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: webserver
template:
metadata:
name: my-webserver
labels:
app: webserver
spec:
containers:
- name: my-webserver
image: nginx:1.10
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: testnginx
namespace: quiz5
spec:
ports:
- name: web-port
port: 80
targetPort: 80
selector:
app: webserver
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: quiz5
name: http-deployment
spec:
replicas: 3
selector:
matchLabels:
app: httpserver
template:
metadata:
name: my-httpserver
labels:
app: httpserver
spec:
containers:
- name: my-httpserver
image: httpd
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: testhttpd-service
namespace: quiz5
spec:
ports:
- name: http-port
port: 80
targetPort: 80
selector:
app: httpserver
type: NodePort
root@manager:~/k8slab/quiz/k8s-quiz/quiz5# k get ing -n quiz5
NAME CLASS HOSTS ADDRESS PORTS AGE
example-ingress <none> * 211.183.3.231 80 12m