kubenetes 설정

Docker Desktop에서 위의 그림과 같이 세팅하고 적용을 해준다.
(base) jeong-in-ung@jeong-in-ung-ui-MacBookAir k8s-practice % kubectl version
Client Version: version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.9", GitCommit:"a1a87a0a2bcd605820920c6b0e618a8ab7d117d4", GitTreeState:"clean", BuildDate:"2023-04-12T12:16:51Z", GoVersion:"go1.19.8", Compiler:"gc", Platform:"darwin/arm64"}
Kustomize Version: v4.5.7
Server Version: version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.2", GitCommit:"7f6f68fdabc4df88cfea2dcf9a19b2b830f1e647", GitTreeState:"clean", BuildDate:"2023-05-17T14:13:28Z", GoVersion:"go1.20.4", Compiler:"gc", Platform:"linux/arm64"}
kubecrtl version으로 들어가서 kubenetes가 시스템에서 제대로 동작하는 지를 확인한다.
nginx-pod.yaml파일로 들어가서 yaml 파일 명세서를 작성해준다.
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx
nginx를 pod로 띄운다.
kubectl apply -f nginx-pod-yaml
kubectl get pod를 통해 kubenetes상의 pod 상태를 확인해 본다.
(base) jeong-in-ung@jeong-in-ung-ui-MacBookAir k8s-practice % pythonin-ung@jeong-in-ung-ui-MacBookAir k8s-practice % kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 68s
kubectl describe를 통해서도 정보를 얻을 수 있다.
(base) jeong-in-ung@jeong-in-ung-ui-MacBookAir k8s-practice % kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Service Account: default
Node: docker-desktop/192.168.65.4
Start Time: Fri, 22 Sep 2023 21:14:49 +0900
Labels: name=nginx
Annotations: <none>
Status: Running
IP: 10.1.0.6
IPs:
IP: 10.1.0.6
Containers:
nginx:
Container ID: docker://c0169f7f2414c643454e1acfbb09345e3f4382ae87bd4fbd05adfea96ff3d057
Image: nginx
Image ID: docker-pullable://nginx@sha256:32da30332506740a2f7c34d5dc70467b7f14ec67d912703568daff790ab3f755
Port: <none>
Host Port: <none>
State: Running
Started: Fri, 22 Sep 2023 21:14:58 +0900
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6t422 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-6t422:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m2s default-scheduler Successfully assigned default/nginx to docker-desktop
Normal Pulling 2m1s kubelet Pulling image "nginx"
Normal Pulled 113s kubelet Successfully pulled image "nginx" in 7.969528628s (7.969539378s including waiting)
Normal Created 113s kubelet Created container nginx
Normal Started 113s kubelet Started container nginx
외부에서 pod 접근가능하게 하기
pods는 노출되지 않기 때문에 외부에서 접근하기 어렵다. 따라서 서비스를 올려야 접근이 가능하다.
nano편집기로 nginx-service.yaml 파일을 생성해서 아래와 같이 작성해주었다.
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
ports:
- port: 80
protocol: TCP
selector:
name: nginx
type: LoadBalancer
아래의 명령어를 통해 잘 동작함을 확인 할 수 있다.


localhost로 접속해보면 nginx로 외부에서 접속할 수 있음을 알 수 있다.
(base) jeong-in-ung@jeong-in-ung-ui-MacBookAir k8s-practice % kubectl delete service my-nginx
삭제를 해주면 접근이 불가한 것을 확인할 수 있다.
namespace 만들기
아래와 같이 입력하면 만들고 확인을 할 수 있다. cuk라는 네임스페이스가 만들어졌음을 확인할 수 있다.
(base) jeong-in-ung@jeong-in-ung-ui-MacBookAir k8s-practice % kubectl create namespace cuk
namespace/cuk created
(base) jeong-in-ung@jeong-in-ung-ui-MacBookAir k8s-practice % kubectl get ns
NAME STATUS AGE
cuk Active 8s
default Active 46m
kube-node-lease Active 46m
kube-public Active 46m
kube-system Active 46m
지금까지는 nginx를 하나 띄운 것을 알 수 있다.
만약 3개를 띄우고 싶다면 어떻게 해야할까?
디플리먼트를 생성해서 3개의 ngin pod를 불러오기 위한 레플리카셋을 생성한다.nano편집기를 활용하여 nginx-deployment.yaml파일을 생성해보겠다.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
(base) jeong-in-ung@jeong-in-ung-ui-MacBookAir k8s-practice % kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
#적용하기
(base) jeong-in-ung@jeong-in-ung-ui-MacBookAir k8s-practice % kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 40m
nginx-deployment-cbdccf466-4dsbf 0/1 ContainerCreating 0 6s
nginx-deployment-cbdccf466-mmln7 0/1 ContainerCreating 0 6s
nginx-deployment-cbdccf466-x9qqx 0/1 ContainerCreating 0 6s
#아직 생성중인 상태
(base) jeong-in-ung@jeong-in-ung-ui-MacBookAir k8s-practice % kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 40m
nginx-deployment-cbdccf466-4dsbf 1/1 Running 0 21s
nginx-deployment-cbdccf466-mmln7 1/1 Running 0 21s
nginx-deployment-cbdccf466-x9qqx 1/1 Running 0 21s
#모두 생성한 상태
적용을 해준 뒤 확인을 해보면 3개가 작동하는 것을 확인 할 수 있다.
만약 더 수를 늘리고 싶다면 scale 명령어를 이용하여 갯수를 늘릴 수 있다.
(base) jeong-in-ung@jeong-in-ung-ui-MacBookAir k8s-practice % kubectl scale deployment.v1.apps/nginx-deployment --replicas=10
deployment.apps/nginx-deployment scaled
(base) jeong-in-ung@jeong-in-ung-ui-MacBookAir k8s-practice % kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 48m
nginx-deployment-cbdccf466-4dsbf 1/1 Running 0 8m34s
nginx-deployment-cbdccf466-7l9fn 1/1 Running 0 3m27s
nginx-deployment-cbdccf466-bq5dq 1/1 Running 0 3m27s
nginx-deployment-cbdccf466-flslq 1/1 Running 0 3m27s
nginx-deployment-cbdccf466-jrglr 1/1 Running 0 3m27s
nginx-deployment-cbdccf466-mmln7 1/1 Running 0 8m34s
nginx-deployment-cbdccf466-rpbxs 1/1 Running 0 3m27s
nginx-deployment-cbdccf466-x9qqx 1/1 Running 0 8m34s
nginx-deployment-cbdccf466-xgfnc 1/1 Running 0 3m27s
nginx-deployment-cbdccf466-xhqr5 1/1 Running 0 3m27s
10개가 생성되는 것을 확인 할 수 있다.