2020. 10. 18. 13:01ㆍ클라우드/Kubernetes
Kubernetes 실습
1. 클러스터 생성
위치 유형을 리전으로 할 경우 제대로 동작 안 될 수도 있다
마스터 버전은 최신버전 (1.17.12-gke.1501)로 생성
좌측 메뉴 default-pool에서 노드 수 등등 설정할 수 있음
2. 터미널 접속하기
클러스터 목록에서 접속하려는 클러스터 우측의 [연결] 버튼 클릭
3. 터미널 명령어 실행
$ gcloud container clusters get-credentials kube-cluster --zone us-central1-a --project perfect-aura-292902
터미널 접속 시 기본으로 작성되어있는 명령어 바로 실행
4. kubectl 명령어 확인하기
$ kubectl -h
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
create Create a resource from a file or from stdin.
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
run Run a particular image on the cluster
set Set specific features on objects
Basic Commands (Intermediate):
explain Documentation of resources
get Display one or many resources
edit Edit a resource on the server
delete Delete resources by filenames, stdin, resources and names, or by resources and label selector
Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a Deployment, ReplicaSet or Replication Controller
autoscale Auto-scale a Deployment, ReplicaSet, or ReplicationController
Cluster Management Commands:
certificate Modify certificate resources.
cluster-info Display cluster info
top Display Resource (CPU/Memory/Storage) usage.
cordon Mark node as unschedulable
uncordon Mark node as schedulable
drain Drain node in preparation for maintenance
taint Update the taints on one or more nodes
Troubleshooting and Debugging Commands:
describe Show details of a specific resource or group of resources
logs Print the logs for a container in a pod
attach Attach to a running container
exec Execute a command in a container
port-forward Forward one or more local ports to a pod
proxy Run a proxy to the Kubernetes API server
cp Copy files and directories to and from containers.
auth Inspect authorization
Advanced Commands:
diff Diff live version against would-be applied version
apply Apply a configuration to a resource by filename or stdin
patch Update field(s) of a resource using strategic merge patch
replace Replace a resource by filename or stdin
wait Experimental: Wait for a specific condition on one or many resources.
convert Convert config files between different API versions
kustomize Build a kustomization target from a directory or a remote url.
Settings Commands:
label Update the labels on a resource
annotate Update the annotations on a resource
completion Output shell completion code for the specified shell (bash or zsh)
Other Commands:
alpha Commands for features in alpha
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config Modify kubeconfig files
plugin Provides utilities for interacting with plugins.
version Print the client and server version information
Usage:
kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
$ kubectl -h
로 사용 가능한 명령어 확인 가능
5. 노드(Worker Node) 확인하기
클러스터 생성 시 노드 수를 3개(기본값 3개)로 설정했기 때문에 3개가 나옴
-o wide
옵션으로 더 다양한 정보 확인 가능
narae456@cloudshell:~ (perfect-aura-292902)$ kubectl get node
NAME STATUS ROLES AGE VERSION
gke-kube-cluster-default-pool-98a1d7b8-0jqn Ready <none> 8m12s v1.17.12-gke.1501
gke-kube-cluster-default-pool-98a1d7b8-ft4v Ready <none> 8m14s v1.17.12-gke.1501
gke-kube-cluster-default-pool-98a1d7b8-fv5p Ready <none> 8m13s v1.17.12-gke.1501
narae456@cloudshell:~ (perfect-aura-292902)$ kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
gke-kube-cluster-default-pool-98a1d7b8-0jqn Ready <none> 8m24s v1.17.12-gke.1501 10.128.0.3 35.184.104.198 Container-Optimized OS from Google 4.19.112+ docker://19.3.6
gke-kube-cluster-default-pool-98a1d7b8-ft4v Ready <none> 8m26s v1.17.12-gke.1501 10.128.0.4 34.122.22.185 Container-Optimized OS from Google 4.19.112+ docker://19.3.6
gke-kube-cluster-default-pool-98a1d7b8-fv5p Ready <none> 8m25s v1.17.12-gke.1501 10.128.0.2 104.197.104.79 Container-Optimized OS from Google 4.19.112+ docker://19.3.6
6. nginX 파드 생성
narae456@cloudshell:~ (perfect-aura-292902)$ kubectl run nginx --image=nginx:latest --port=80
pod/nginx created
narae456@cloudshell:~ (perfect-aura-292902)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 22s
narae456@cloudshell:~ (perfect-aura-292902)$ kubectl exec -it nginx -- curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
narae456@cloudshell:~ (perfect-aura-292902)$ kubectl expose pod nginx --port=80 --name=frontend --type=LoadBalancer
service/frontend exposed
narae456@cloudshell:~ (perfect-aura-292902)$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend LoadBalancer 10.4.12.203 <pending> 80:32738/TCP 11s
kubernetes ClusterIP 10.4.0.1 <none> 443/TCP 15m
narae456@cloudshell:~ (perfect-aura-292902)$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend LoadBalancer 10.4.12.203 34.121.165.172 80:32738/TCP 46s
kubernetes ClusterIP 10.4.0.1 <none> 443/TCP 16m
svc
: service의 약자
EXTERNAL-IP의 ip주소로 접속하면 nginX의 Welcome 페이지 확인 가능
7. 도커 이미지로 pod 생성
narae456@cloudshell:~ (perfect-aura-292902)$ mkdir kubernetes
narae456@cloudshell:~ (perfect-aura-292902)$ cd kubernetes
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ ll
total 8
drwxr-xr-x 2 narae456 narae456 4096 Oct 18 02:57 ./
drwxr-xr-x 9 narae456 narae456 4096 Oct 18 02:57 ../
yaml 파일을 저장해놓을 폴더 생성
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get pod nginx -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2020-10-18T02:50:56Z"
labels:
run: nginx
name: nginx
namespace: default
resourceVersion: "4964"
selfLink: /api/v1/namespaces/default/pods/nginx
uid: cb88e4fe-e449-4e16-abf5-9ebf2e553feb
spec:
containers:
- image: nginx:latest
imagePullPolicy: Always
name: nginx
ports:
- containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-d22c2
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: gke-kube-cluster-default-pool-98a1d7b8-ft4v
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-d22c2
secret:
defaultMode: 420
secretName: default-token-d22c2
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2020-10-18T02:50:56Z"
apiVersion: v1
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2020-10-18T02:51:02Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2020-10-18T02:51:02Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2020-10-18T02:50:56Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://1d86ab25aa89e69a596823c51cd4577d5a3bf8bc51843b6a658e37fa7b1497e2
image: nginx:latest
imageID: docker-pullable://nginx@sha256:4949aa7259aa6f827450207db5ad94cabaa9248277c6d736d5e1975d200c7e43
lastState: {}
name: nginx
ready: true
restartCount: 0
started: true
state:
running:
startedAt: "2020-10-18T02:51:02Z"
hostIP: 10.128.0.4
phase: Running
podIP: 10.0.2.7
podIPs:
- ip: 10.0.2.7
qosClass: BestEffort
startTime: "2020-10-18T02:50:56Z"
nginX의 yaml 파일 생성
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
app: web-spring-resource
name: web-spring-resource
namespace: default
spec:
containers:
- image: maxeasy2/study-kubernetes:latest
name: study-kube-webspring
ports:
- containerPort: 8080
resources:
requests:
cpu: 0.1
memory: 200M
limits:
cpu: 0.5
memory: 512M
yaml 파일 만들기
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl create -f pod.yaml
pod/web-spring created
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ ll
total 12
drwxr-xr-x 2 narae456 narae456 4096 Oct 18 03:05 ./
drwxr-xr-x 9 narae456 narae456 4096 Oct 18 03:05 ../
-rw-r--r-- 1 narae456 narae456 241 Oct 18 03:05 pod.yaml
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 14m
web-spring 1/1 Running 0 17s
pod.yaml 파일로 파드 생성
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl exec -it web-spring -- curl localhost:8080
<!DOCTYPE html>
<html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>version : latest</div>
<div>argument : <span>web-spring</span></div>
</body>
확인하기
yaml 파일 옵션
emptyDir
hostPath
도커의 Volume과 비슷함
로컬에 있는 폴더에 직접 붙음
스케줄러가 어떤 노드에 파드를 배치할 지 알 수 없음
PVC / PV
config Server 처럼 볼 수 있음
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl delete all --all
pod "nginx" deleted
pod "web-spring" deleted
service "frontend" deleted
service "kubernetes" deleted
모두 지우기
ReplicaSet
1. 레플리카 셋 생성
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ vi replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: web-spring
labels:
app: web-spring
spec:
replicas: 5
selector:
matchLabels:
app: web-spring
template:
metadata:
labels:
app: web-spring
spec:
containers:
- image: maxeasy2/study-kubernetes:latest
name: study-kube-webspring
ports:
- containerPort: 8080
replicas: 5
파드를 몇개를 설정할 건지
selector:
이정보를 가지고 파드를 관리하겠다(??)
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get pod
No resources found in default namespace.
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.4.0.1 <none> 443/TCP 5m31s
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl create -f replicaset.yaml
replicaset.apps/web-spring created
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
web-spring-4rv27 1/1 Running 0 7s
web-spring-6q249 0/1 ContainerCreating 0 7s
web-spring-8cp8g 0/1 ContainerCreating 0 7s
web-spring-jq7tv 1/1 Running 0 7s
web-spring-m4c2l 1/1 Running 0 7s
5개의 파드 생성됨
쿠버네티스를 사용하지 않을 경우 docker run을 5번 해야함
2. 레플리카셋에서 관리중인 파드가 지워진다면?
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl delete pod web-spring-4rv27
pod "web-spring-4rv27" deleted
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
web-spring-6q249 1/1 Running 0 101s
web-spring-8cp8g 1/1 Running 0 101s
web-spring-jq7tv 1/1 Running 0 101s
web-spring-m4c2l 1/1 Running 0 101s
web-spring-rwgl9 1/1 Running 0 40s
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-spring-6q249 1/1 Running 0 2m10s 10.0.1.5 gke-kube-cluster-default-pool-98a1d7b8-0jqn <none> <none>
web-spring-8cp8g 1/1 Running 0 2m10s 10.0.0.6 gke-kube-cluster-default-pool-98a1d7b8-fv5p <none> <none>
web-spring-jq7tv 1/1 Running 0 2m10s 10.0.2.10 gke-kube-cluster-default-pool-98a1d7b8-ft4v <none> <none>
web-spring-m4c2l 1/1 Running 0 2m10s 10.0.2.11 gke-kube-cluster-default-pool-98a1d7b8-ft4v <none> <none>
web-spring-rwgl9 1/1 Running 0 69s 10.0.0.7 gke-kube-cluster-default-pool-98a1d7b8-fv5p <none> <none>
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get node
NAME STATUS ROLES AGE VERSION
gke-kube-cluster-default-pool-98a1d7b8-0jqn Ready <none> 61m v1.17.12-gke.1501
gke-kube-cluster-default-pool-98a1d7b8-ft4v Ready <none> 61m v1.17.12-gke.1501
gke-kube-cluster-default-pool-98a1d7b8-fv5p Ready <none> 61m v1.17.12-gke.1501
레플리카셋의 파드를 하나 지워도 (4rv27
)
레플리카 개수(5개)에 맞춰서 새로 생성함(rwgl9
)
-o wide
옵션으로 NODE도 확인 가능
3. 레플리카 파드 수 늘리기
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl edit rs web-spring
replicaset.apps/web-spring edited
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
web-spring-2wp87 1/1 Running 0 7s
web-spring-6q249 1/1 Running 0 4m18s
web-spring-88lkq 1/1 Running 0 7s
web-spring-8cp8g 1/1 Running 0 4m18s
web-spring-dcm7t 1/1 Running 0 7s
web-spring-jq7tv 1/1 Running 0 4m18s
web-spring-m4c2l 1/1 Running 0 4m18s
web-spring-rwgl9 1/1 Running 0 3m17s
파드 3개가 추가됨
4. 레플리카 파드 수 줄이기
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl edit rs web-spring
replicaset.apps/web-spring edited
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
web-spring-2wp87 1/1 Terminating 0 3m11s
web-spring-6q249 1/1 Running 0 7m22s
web-spring-88lkq 1/1 Terminating 0 3m11s
web-spring-8cp8g 1/1 Running 0 7m22s
web-spring-dcm7t 1/1 Terminating 0 3m11s
web-spring-jq7tv 1/1 Running 0 7m22s
web-spring-m4c2l 1/1 Running 0 7m22s
web-spring-rwgl9 1/1 Running 0 6m21s
파드 3개가 삭제됨 (Terminating)
5. 레플리카셋 삭제
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/web-spring-6q249 1/1 Running 0 8m17s
pod/web-spring-8cp8g 1/1 Running 0 8m17s
pod/web-spring-jq7tv 1/1 Running 0 8m17s
pod/web-spring-m4c2l 1/1 Running 0 8m17s
pod/web-spring-rwgl9 1/1 Running 0 7m16s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.4.0.1 <none> 443/TCP 14m
NAME DESIRED CURRENT READY AGE
replicaset.apps/web-spring 5 5 5 8m18s
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl delete replicaset.apps/web-spring
replicaset.apps "web-spring" deleted
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ kubectl get pod
NAME READY STATUS RESTARTS AGE
web-spring-6q249 1/1 Terminating 0 9m3s
web-spring-8cp8g 1/1 Terminating 0 9m3s
web-spring-jq7tv 1/1 Terminating 0 9m3s
web-spring-m4c2l 1/1 Terminating 0 9m3s
web-spring-rwgl9 1/1 Terminating 0 8m2s
관리중이던 파드 모두 삭제됨 (Terminating)
Deployment
1. 디플로이먼트 생성하기
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ cp replicaset.yaml deployment.yaml
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ vi deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-spring
labels:
app: web-spring
spec:
replicas: 10
selector:
matchLabels:
app: web-spring
template:
metadata:
labels:
app: web-spring
spec:
containers:
- image: maxeasy2/study-kubernetes:latest
name: study-kube-webspring
ports:
- containerPort: 8080
narae456@cloudshell:~/kubernetes (perfect-aura-292902)$ ll
total 20
drwxr-xr-x 2 narae456 narae456 4096 Oct 18 03:50 ./
drwxr-xr-x 9 narae456 narae456 4096 Oct 18 03:50 ../
-rw-r--r-- 1 narae456 narae456 387 Oct 18 03:50 deployment.yaml
-rw-r--r-- 1 narae456 narae456 241 Oct 18 03:05 pod.yaml
-rw-r--r-- 1 narae456 narae456 387 Oct 18 03:38 replicaset.yaml
yaml 파일의 kind
설정만 ReplicaSet
-> Deployment
로 수정.
19p. 서비스의 로드밸런싱
Service : 로드밸런서
Service의 ip만 알고 있으면 접근 가능.
20p. 인그레스를 통한 서비스 오픈
Ingress : L7이라고 보면 됨
같은 도메인이지만 path에 있는 컨텐츠 명에 따라서 다른 파드로 연결 가능
파드에 웹서버(nginX)올림
'클라우드 > Kubernetes' 카테고리의 다른 글
Kubernetes 스터디 (0) | 2020.10.18 |
---|---|
Kubernetes 스터디 일정 (0) | 2020.09.30 |