brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Aug 16. 2021

160. GCP  쿠버네티스  Pod사용해보자


<1>  Cloud shell 실행

<2>  디플로이먼트 생성 , 서비스 사용하기

<3>  Pod 알아보기

<4>  Pod 사용해보기

<5>  서비스 유형  3가지 

<6> 디플로이먼트에 배포하기

<7> 개인 정리 

<10> 깉이 보면 좋을 사이트




<1>  Cloud shell 실행


인증 설정 참고하기

https://brunch.co.kr/@topasvga/1132


1

gcloud auth list

           Credentialed Accounts

ACTIVE  ACCOUNT

*       xxxxxxxxx@xxxxxxxxxx


To set the active account, run:

    $ gcloud config set account `ACCOUNT`


2

gcloud config list project

[core]

project =xxxxxxxxx-gcp-xxxxxxxx

Your active configuration is: [cloudshell-32548]


3

gcloud config set compute/zone us-central1-b

Updated property [compute/zone].


4

gcloud container clusters create seo-k8s-2

WARNING: Starting in January 2021, clusters will use the Regular release channel by default when `--cluster-version`, `--release-channel`, `--no-enable-autoupgrade`, and `--no-enable-autorepair` flags are not specified.

WARNING: Currently VPC-native is not the default mode during cluster creation. In the future, this will become the default mode and can be disabled using `--no-enable-ip-alias` flag. Use `--[no-]enable-ip-alias` flag to suppress this warning.

WARNING: Starting with version 1.18, clusters will have shielded GKE nodes by default.

WARNING: Your Pod address range (`--cluster-ipv4-cidr`) can accommodate at most 1008 node(s).

WARNING: Starting with version 1.19, newly created clusters and node-pools will have COS_CONTAINERD as the default node image when no image type is specified.

Creating cluster seo-k8s-2 in us-central1-b...done.

Created [https://container.googleapis.com/v1/projects/xxxxxxxx-gcp-xxxxxxx/zones/us-central1-b/clusters/seo-k8s-2].

To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1-b/seo-k8s-2?project=xxxxxxxxx-gcp-xxxxxxxxxxx

kubeconfig entry generated for seo-k8s-2.


NAME       LOCATION       MASTER_VERSION  MASTER_IP      MACHINE_TYPE  NODE_VERSION    NUM_NODES  STATUS

seo-k8s-2  us-central1-b  1.20.8-gke.900  35.188.125.26  e2-medium     1.20.8-gke.900  3          RUNNING




<2>  디플로이먼트 생성 , 서비스 사용하기


1

pod 모니터링


alias k=kubectl

complete -F __start_kubectl k

k get ns,no,po,svc,deploy,rs,ing,ep  

watch -d 'kubectl get pods,svc,ep -o wide'

or

watch -d 'k get pods,svc,ep -o wide'  


pod 상세 정보 보기

k  get pod -o wide


2

디플로이먼트 생성

kubectl create deployment nginx --image=nginx:1.10.0


deployment.apps/nginx created


3

생성 pod 확인

kubectl get pods


NAME                    READY   STATUS    RESTARTS   AGE

nginx-56cd7f6b6-dc9pt   1/1     Running   0          14s



https://brunch.co.kr/@topasvga/1656


4

컨테이너를 로드밸런서를 통해 외부에 노출

kubectl expose deployment nginx --port 80 --type LoadBalancer


service/nginx exposed


5

서비스 확인

kubectl get services


NAME         TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE

kubernetes   ClusterIP      10.16.0.1     <none>        443/TCP        4m25s

nginx        LoadBalancer   10.16.6.178   <pending>     80:30436/TCP   8s


10분후 서비스 확인

EXTERNAL-IP가 나온다.


kubectl get services

NAME         TYPE           CLUSTER-IP    EXTERNAL-IP    PORT(S)        AGE

kubernetes   ClusterIP      10.16.0.1     <none>         443/TCP        5m14s

nginx        LoadBalancer   10.16.6.178   34.72.37.139   80:30436/TCP   57s


6

curl로 확인

웹 브라우저로 접속 확인

curl http://34.72.37.139:80


<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>




<3>  Pod 알아보기


Pod는 IP를 하나 가지고 있다.  

Pod안에 컨테이너가 위치한다.

컨테이너는 1개 , 2개 가능하다.

2개 컨테이너는 통신이 가능하다.

첨부된 볼륨도 공유가능하다.

컨테이너는 Pod의 IP를 공유한다.



1

monolith.yaml 로 pod 만들기


kubectl create -f pods/monolith.yaml


pod/monolith created


2

생성된 Pod 확인

kubectl get pods


NAME                    READY   STATUS    RESTARTS   AGE

monolith                1/1     Running   0          11s

nginx-56cd7f6b6-dc9pt   1/1     Running   0          4m36s


3

Pod 상세 보기

Pod는 IP를 하나 가지고 있다.

kubectl describe pods monolith


Name:         monolith

Namespace:    default

Priority:     0

Node:         gke-seo-k8s-2-default-pool-ef2effca-s34c/10.128.0.3

Start Time:   Mon, 16 Aug 2021 08:45:47 +0000

Labels:       app=monolith

Annotations:  <none>

Status:       Running

IP:           10.12.1.3

IPs:

  IP:  10.12.1.3



4

포드에는 비공개 IP 주소 부여, 클러스터 밖에서는 접근 불가

kubectl port-forward 로 포트로 매핑


5

터미널 2개로 확인

접속 시도 ->  접속 불가

curl http://127.0.0.1:10080

curl: (7) Failed to connect to 127.0.0.1 port 10080: Connection refused


6

10080 포트를  내부 80 포트와 매핑 하기

kubectl port-forward monolith 10080:80


Forwarding from 127.0.0.1:10080 -> 80


7

접속 시도 => 접속 가능

curl http://127.0.0.1:10080


{"message":"Hello"}





<4>  Pod 사용해보기


1

보안 설정된 엔드포인트 조회해 보기 = 인증 실패

curl http://127.0.0.1:10080/secure


authorization failed


2

인증 토큰 받기위해 로그인 시도

curl -u user http://127.0.0.1:10080/login

Enter host password for user 'user':


password

로 로그인


3

토큰을 위한 인증 변수 만들기

TOKEN=$(curl http://127.0.0.1:10080/login -u user|jq -r '.token')


Enter host password for user 'user':

password  입력

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100   222  100   222    0     0    504      0 --:--:-- --:--:-- --:--:--   503


4

로그 확인

kubectl logs monolith


2021/08/16 08:45:51 Starting server...

2021/08/16 08:45:51 Health service listening on 0.0.0.0:81

2021/08/16 08:45:51 HTTP service listening on 0.0.0.0:80

127.0.0.1:58630 - - [Mon, 16 Aug 2021 08:48:09 UTC] "GET / HTTP/1.1" curl/7.64.0

127.0.0.1:58700 - - [Mon, 16 Aug 2021 08:48:58 UTC] "GET /secure HTTP/1.1" curl/7.64.0

127.0.0.1:58770 - - [Mon, 16 Aug 2021 08:49:41 UTC] "GET /login HTTP/1.1" curl/7.64.0



5

새창

에서 실시간 로그 가져 오기

kubectl logs -f monolith


2021/08/16 08:45:51 Starting server...

2021/08/16 08:45:51 Health service listening on 0.0.0.0:81

2021/08/16 08:45:51 HTTP service listening on 0.0.0.0:80

127.0.0.1:58630 - - [Mon, 16 Aug 2021 08:48:09 UTC] "GET / HTTP/1.1" curl/7.64.0

127.0.0.1:58700 - - [Mon, 16 Aug 2021 08:48:58 UTC] "GET /secure HTTP/1.1" curl/7.64.0

127.0.0.1:58770 - - [Mon, 16 Aug 2021 08:49:41 UTC] "GET /login HTTP/1.1" curl/7.64.0


6

culr로 로그 추가 되는거 확인 하기

curl http://127.0.0.1:10080


kubectl logs -f monolith

2021/08/16 08:45:51 Starting server...

2021/08/16 08:45:51 Health service listening on 0.0.0.0:81

2021/08/16 08:45:51 HTTP service listening on 0.0.0.0:80

127.0.0.1:58630 - - [Mon, 16 Aug 2021 08:48:09 UTC] "GET / HTTP/1.1" curl/7.64.0

127.0.0.1:58700 - - [Mon, 16 Aug 2021 08:48:58 UTC] "GET /secure HTTP/1.1" curl/7.64.0

127.0.0.1:58770 - - [Mon, 16 Aug 2021 08:49:41 UTC] "GET /login HTTP/1.1" curl/7.64.0

127.0.0.1:58912 - - [Mon, 16 Aug 2021 08:51:25 UTC] "GET / HTTP/1.1" curl/7.64.0

127.0.0.1:58958 - - [Mon, 16 Aug 2021 08:51:50 UTC] "GET / HTTP/1.1" curl/7.64.0



7

컨테이너 내부로 들어가 명령어 실행해보기

kubectl exec monolith --stdin --tty -c monolith /bin/sh


kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.

/ # 

ping 1.1.1.1


exit




<5>  서비스 유형  3가지 


서비스 알아보자


1

유형 3가지

ClusterIP(내부) -- 기본,  클러스터 안에서만 볼 수 있음

NodePort -  외부에서 액세스 가능한 IP 주소를 제공

LoadBalancer -   LB 추가하여 유입 트래픽을 node로 전달



2

보안 설정된 Pod, configmap 만들기

cat pods/secure-monolith.yaml


kubectl create secret generic tls-certs --from-file tls/

kubectl create configmap nginx-proxy-conf --from-file nginx/proxy.conf

kubectl create -f pods/secure-monolith.yaml


3

구성 파일 확인

cat services/monolith.yaml


kind: Service

apiVersion: v1

metadata:

  name: "monolith"

spec:

  selector:

    app: "monolith"

    secure: "enabled"

  ports:

    - protocol: "TCP"

      port: 443

      targetPort: 443

      nodePort: 31000

  type: NodePort


4

서비스 만들기

kubectl create -f services/monolith.yaml

service/monolith created


5

노드포트로 보낸다.

노출된 NodePort의 서비스에 대한 트래픽 허용

gcloud compute firewall-rules create allow-monolith-nodeport \

  --allow=tcp:31000


Creating firewall...⠹Created [https://www.googleapis.com/compute/v1/projects/xxxxx-gcp-01-xxxxxxx9/global/firewalls/allow-monolith-nodeport].

Creating firewall...done.

NAME                     NETWORK  DIRECTION  PRIORITY  ALLOW      DENY  DISABLED

allow-monolith-nodeport  default  INGRESS    1000      tcp:31000        False




6

외부 IP 가져오기

gcloud compute instances list


NAME                                      ZONE           MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS

gke-seo-k8s-2-default-pool-ef2effca-6820  us-central1-b  e2-medium                  10.128.0.2   34.136.123.210  RUNNING

gke-seo-k8s-2-default-pool-ef2effca-hjj6  us-central1-b  e2-medium                  10.128.0.4   34.123.166.129  RUNNING

gke-seo-k8s-2-default-pool-ef2effca-s34c  us-central1-b  e2-medium                  10.128.0.3   34.133.171.141  RUNNING


7

외부 사이트 접속 테스트

시간 테스트

curl -k https://<EXTERNAL_IP>:31000

curl -k https://34.136.123.210:31000


8

포드에 라벨  조회하기

kubectl get pods -l "app=monolith"


NAME              READY   STATUS    RESTARTS   AGE

monolith          1/1     Running   0          14m

secure-monolith   2/2     Running   0          5m11s



9

포드에 라벨  조회하기

kubectl get pods -l "app=monolith,secure=enabled"


No resources found in default namespace.


kubectl get pods

NAME                    READY   STATUS    RESTARTS   AGE

monolith                1/1     Running   0          14m

nginx-56cd7f6b6-dc9pt   1/1     Running   0          19m

secure-monolith         2/2     Running   0          5m35s


10

포드에 라벨  추가하기

kubectl label pods secure-monolith 'secure=enabled'


pod/secure-monolith labeled


확인

kubectl get pods secure-monolith --show-labels


NAME              READY   STATUS    RESTARTS   AGE     LABELS

secure-monolith   2/2     Running   0          6m10s   app=monolith,secure=enabled


11

endpoint 확인

kubectl describe services monolith | grep Endpoints


Endpoints:                10.12.1.4:443



12 

테스트

gcloud compute instances list


NAME                                      ZONE           MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS

gke-seo-k8s-2-default-pool-ef2effca-6820  us-central1-b  e2-medium                  10.128.0.2   34.136.123.210  RUNNING

gke-seo-k8s-2-default-pool-ef2effca-hjj6  us-central1-b  e2-medium                  10.128.0.4   34.123.166.129  RUNNING

gke-seo-k8s-2-default-pool-ef2effca-s34c  us-central1-b  e2-medium                  10.128.0.3   34.133.171.141  RUNNING


13

curl -k https://<EXTERNAL_IP>:31000


curl -k https://34.136.123.210:31000





<6> 디플로이먼트에 배포하기



1

디플로이먼트,  Pod수와 동일하게 생성

pod3개

리플리카 3개


2

그림


3

Node3이 중단되면, Pod 중단 된다.

하지만, 디플로이먼트가 새로운 포드를 만들고 다른 node에서 Pod를 자동 생성해 서비스 되게 한다.


4

auth 디플로이 먼트, 서비스  만들기

kubectl create -f deployments/auth.yaml

kubectl create -f services/auth.yaml


5

hello 디플로이 먼트, 서비스  만들기

kubectl create -f deployments/hello.yaml

kubectl create -f services/hello.yaml


6

frontend 디플로이 먼트, 서비스  만들기

kubectl create configmap nginx-frontend-conf --from-file=nginx/frontend.conf

kubectl create -f deployments/frontend.yaml

kubectl create -f services/frontend.yaml



7

외부 주소 확인

kubectl get services frontend

NAME       TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)         AGE

frontend   LoadBalancer   10.16.7.189   <pending>     443:30944/TCP   13s


$ kubectl get services frontend

NAME       TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)         AGE

frontend   LoadBalancer   10.16.7.189   34.72.69.33   443:30944/TCP   42s


8

외부 접속


https://<EXTERNAL-IP>


curl -k https://34.72.69.33

{"message":"Hello"}



기타

스토리지 관리 툴 gsutil 다운로드

구글 클라우드 계정 이 있어야 한다.

windows 용

https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe







<10> 같이 보면 좋을 사이트


(몰아보기) 구글 클라우드 쿠버네티스 


https://brunch.co.kr/@topasvga/1800

감사합니다.

매거진의 이전글 159. GCP 쿠버네티스 CLI로 만들기-2021

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari