본문 바로가기

Cloud/Oracle Cloud Infrastructure (OCI)

OCI Kubernetes와 MySQL 서비스를 활용한 어플리케이션 배포 데모 - #3. 쿠버네티스 접속 환경 구성

글 순서

#1. OKE 클러스터 설치

#2. MySQL 서비스 배포

#3. 쿠버네티스 접속 환경 구성

#4. OCI 레지스트리 설정, 도커 이미지 빌드

#5. 도커 이미지 OCIR에 등록, OKE 클러스터에 컨테이너 배포

 

쿠버네티스 접속 환경 구성 (on Bastion Host)

이제 OKE 쿠버네티스 접속을 위한 유틸리티 설치와 환경 설정 파일을 생성하겠습니다. 그 전에 어플리케이션 소스를 가져오기 위한 git과 어플리케이션 컨테이너 빌드를 위한 도커를 먼저 설치하겠습니다.

 

git 설치

아래와 같이 git을 설치합니다.

[opc@bastion ~]$ sudo yum install git -y
...
Installed:
  git.x86_64 0:1.8.3.1-23.el7_8

Dependency Installed:
  perl-Error.noarch 1:0.17020-2.el7               perl-Git.noarch 0:1.8.3.1-23.el7_8               perl-TermReadKey.x86_64 0:2.30-20.el7

Complete!

 

도커 설치

도커를 설치합니다. root 유저 뿐만 아니라 opc 유저도 사용할 수 있도록 opc 유저를 도커 그룹에 포함시킵니다.

[opc@bastion ~]$ sudo -s
[root@bastion opc]# yum install docker-engine -y
...
Installed:
  docker-engine.x86_64 0:19.03.11.ol-13.el7

Dependency Installed:
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8 containerd.x86_64 0:1.4.8-1.el7 criu.x86_64 0:3.12-2.el7  docker-cli.x86_64 0:19.03.11.ol-13.el7
  libnet.x86_64 0:1.1.6-7.el7                        protobuf-c.x86_64 0:1.0.2-3.el7 runc.x86_64 3:1.0.2-1.el7

Complete!
[root@bastion opc]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@bastion opc]# systemctl start docker
[root@bastion opc]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-05-26 04:38:58 GMT; 5s ago
     Docs: <https://docs.docker.com>
 Main PID: 18853 (dockerd)
    Tasks: 9
   Memory: 30.1M
   CGroup: /system.slice/docker.service
           └─18853 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
...
[root@bastion opc]# usermod -aG docker opc
[root@bastion opc]# systemctl restart docker
[root@bastion opc]# exit
exit
[opc@bastion ~]$ exit
logout
...
[opc@bastion ~]$ docker --version
Docker version 19.03.11-ol, build 9bb540d

 

OCI CLI 설치 및 설정

쿠버네티스 관리 유틸리티 kubectl 사용을 위해서는 OCI CLI 설치 및 설정 작업이 우선되어야 합니다. 다음과 같이 OCI CLI를 설치하고, “oci setup config”를 수행합니다

먼저 설치 스크립트를 실행해서 OCI CLI 최신 버전을 설치합니다. opc 유저로 설치했으며, 설치 과정에 나오는 옵션들에 대해서는 모드 엔터를 쳐서 디폴트로 설치했습니다.

[opc@demo ~]$ whoami
opc
[opc@demo ~]$ bash -c "$(curl -L <https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh>)"
...
===> Modify profile to update your $PATH and enable shell/tab completion now? (Y/n): Y

===> Enter a path to an rc file to update (file will be created if it does not exist) (leave blank to use '/home/opc/.bashrc'):
-- Backed up '/home/opc/.bashrc' to '/home/opc/.bashrc.backup'
-- Tab completion set up complete.
-- If tab completion is not activated, verify thYat '/home/opc/.bashrc' is sourced by your shell.
--
-- ** Run `exec -l $SHELL` to restart your shell. **
--
-- Installation successful.
-- Run the CLI with /home/opc/bin/oci --help
[opc@demo ~]$

 

OCI CLI 설치를 확인합니다.

[opc@demo ~]$ oci --version
3.9.1

 

OCI CLI를 사용하기 전에 OCI 작업에 필요한 자격 증명이 포함된 구성 파일을 생성해야 합니다. 아래와 같이 대화식 "oci setup config” 명령을 사용하거나 텍스트 편집기를 사용하여 수동으로 이 파일을 만들 수 있습니다.

CLI 구성 파일을 생성하기 위해서 사전에 User OCID, Tenancy OCID, Region 식별자를 확인해야 합니다.

  • User OCID는 OCI 웹콘솔 오른쪽 상단의 User Profile > User Settings 에서 가져올 수 있습니다.
  • Tenancy OCID는 OCI 웹콘솔 오른쪽 상단의 User Profile > Tenancy 화면에서 가져올 수 있습니다.
  • Region 식별자: Regions and Availability Domains를 참조합니다.

그리고 OCI CLI 설정 과정에서 API Signing RSA key pair를 신규 생성했습니다.

[opc@demo ~]$ oci setup config
    This command provides a walkthrough of creating a valid CLI config file.

    The following links explain where to find the information required by this
    script:

    User API Signing Key, OCID and Tenancy OCID:

        <https://docs.cloud.oracle.com/Content/API/Concepts/apisigningkey.htm#Other>

    Region:

        <https://docs.cloud.oracle.com/Content/General/Concepts/regions.htm>

    General config documentation:

        <https://docs.cloud.oracle.com/Content/API/Concepts/sdkconfig.htm>

Enter a location for your config [/home/opc/.oci/config]:
Enter a user OCID: ocid1.user.oc1..aaaaaaaa____________________________________________________
Enter a tenancy OCID: ocid1.tenancy.oc1..aaaaaaaa____________________________________________________
Enter a region by index or name(e.g.
1: af-johannesburg-1, 2: ap-chiyoda-1, 3: ap-chuncheon-1, 4: ap-dcc-canberra-1, 5: ap-hyderabad-1,
6: ap-ibaraki-1, 7: ap-melbourne-1, 8: ap-mumbai-1, 9: ap-osaka-1, 10: ap-seoul-1,
11: ap-singapore-1, 12: ap-sydney-1, 13: ap-tokyo-1, 14: ca-montreal-1, 15: ca-toronto-1,
16: eu-amsterdam-1, 17: eu-frankfurt-1, 18: eu-marseille-1, 19: eu-milan-1, 20: eu-stockholm-1,
21: eu-zurich-1, 22: il-jerusalem-1, 23: me-abudhabi-1, 24: me-dcc-muscat-1, 25: me-dubai-1,
26: me-jeddah-1, 27: sa-santiago-1, 28: sa-saopaulo-1, 29: sa-vinhedo-1, 30: uk-cardiff-1,
31: uk-gov-cardiff-1, 32: uk-gov-london-1, 33: uk-london-1, 34: us-ashburn-1, 35: us-gov-ashburn-1,
36: us-gov-chicago-1, 37: us-gov-phoenix-1, 38: us-langley-1, 39: us-luke-1, 40: us-phoenix-1,
41: us-sanjose-1): us-ashburn-1
Do you want to generate a new API Signing RSA key pair? (If you decline you will be asked to supply the path to an existing key.) [Y/n]: Y
Enter a directory for your keys to be created [/home/opc/.oci]:
Enter a name for your key [oci_api_key]:
Public key written to: /home/opc/.oci/oci_api_key_public.pem
Enter a passphrase for your private key (empty for no passphrase):
Private key written to: /home/opc/.oci/oci_api_key.pem
Fingerprint: 18:b6:ce:60:47:95:e1:e1:79:59:f1:8a:32:10:c1:77
Config written to /home/opc/.oci/config

    If you haven't already uploaded your API Signing public key through the
    console, follow the instructions on the page linked below in the section
    'How to upload the public key':

        <https://docs.cloud.oracle.com/Content/API/Concepts/apisigningkey.htm#How2>

 

이제 API 키 중에서 public 키 내용을 OCI 웹콘솔의 사용자 정보에 등록해야 합니다. public API 키 내용을 복사합니다.

[opc@demo ~]$ cat .oci/oci_api_key_public.pem
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArjGTCD3jcU5sQw2OKz8v
NTZCUvd+wlZlcyCmUDR+XoYeTYyoMW6rF2dk9Pgxkc3Aa+gCQoA3kwRdqay8f3Da
JVWnPdz8K84hCq+qdx3Uj5d6/4THOubjisvcZSmJoABufTemCknxO+jkZC8qVxgt
zeRMoK/XzGPR0oLpiMNpWrfXlFD770eOtBGuNjg1xCdbBuRxtoK5lPx0b7ClTYaj
Nj5wayA59nZt/HzG7xWCdGzJ0mP8A+z9Ptla5V8rcDGzl2gCxtpOgcYGRlXl7g0d
bngpKXZh9yMF/LHTuc1u+PAb5yg2mCp3PxLCXwitDgKTpFsHj9dGnHX5lzbryYuS
mwIDAQAB
-----END PUBLIC KEY-----

 

복사한 API public 키를 OCI 웹콘솔의 오른쪽 상단 User Profile > User Settings 의 API Keys 화면에서 등록합니다. Add API Key를 클릭해서 나타난 팝업창에서 Paste Public Key를 선택하고, public API 키를 붙여넣고 Add를 클릭합니다.

 

이제 터미널에서 oci 명령을 수행해서 OCI CLI가 정상적으로 수행되는지 확인합니다. 아래 명령은 오브젝트 스토리지 네임스페이스를 출력하는 명령입니다.

[opc@demo ~]$ **oci os ns get**
{
  "data": "apackrsct01"
}

 

kubectl 설치

kubectl 설치 파일을 다운로드 받고 설치를 진행합니다.

“kubectl version” 명령으로 버전 정보 조회시 나타나는 메세지 “The connection to the server localhost:8080 was refused - did you specify the right host or port?”는 아직 kubeconfig 파일을 설정하지 않아서 발생하는 에러입니다. 지금 단계에서는 무시해도 되는 메세지입니다.

[opc@bastion ~]$ curl -LO "<https://dl.k8s.io/release/$>(curl -L -s <https://dl.k8s.io/release/stable.txt>)/bin/linux/amd64/kubectl"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   154  100   154    0     0    595      0 --:--:-- --:--:-- --:--:--   596
100 43.5M  100 43.5M    0     0  77.9M      0 --:--:-- --:--:-- --:--:-- 77.9M
[opc@bastion ~]$ sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
[opc@bastion ~]$ kubectl version
WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.  Use --output=yaml|json to get the full version.
Client Version: version.Info{Major:"1", Minor:"24", GitVersion:"v1.24.1", GitCommit:"3ddd0f45aa91e2f30c70734b175631bec5b5825a", GitTreeState:"clean", BuildDate:"2022-05-24T12:26:19Z", GoVersion:"go1.18.2", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v4.5.4
The connection to the server localhost:8080 was refused - did you specify the right host or port?

 

OKE 클러스터 접속 설정

생성한 OKE 클러스터 상세화면에서 Access Cluster를 클릭합니다.

 

OKE 클러스터에 접속하기 위해서는 Cloud Shell을 이용하는 방법과 로컬에서 접속하는 방법이 있습니다. 여기에서는 로컬에서 접속하는 방식으로 진행했습니다. Local Access를 클릭해서 나오는 하단의 설명된 내용대로 따라하면 됩니다. 그리고 여기에서는 VCN-Native 프라이빗 엔드포인트로 OKE 클러스터에 접근하도록 설정했습니다.

 

위 화면에서 설명된 대로 kubectl 설정파일을 생성합니다.

[opc@bastion ~]$ oci -v
3.10.0
[opc@bastion ~]$ mkdir -p $HOME/.kube
[opc@bastion ~]$ oci ce cluster create-kubeconfig --cluster-id ocid1.cluster.oc1.iad.aaaaaaaaydgk47ug3bk2ykt3mjvjgduliah74rwznb2pafophckbcz27n3pa --file $HOME/.kube/config --region us-ashburn-1 --token-version 2.0.0  --kube-endpoint PRIVATE_ENDPOINT
New config written to the Kubeconfig file /home/opc/.kube/config
[opc@bastion ~]$ export KUBECONFIG=$HOME/.kube/config
[opc@bastion ~]$ cat .bash_profile
...
export KUBECONFIG=$HOME/.kube/config

 

OKE 클러스터 접속을 확인해 봅니다.

kubectl cluster-info dump 명령을 사용하면 클러스터 정보를 파일로 내려받을 수 있으며, 보다 상세한 정보를 확인할 수 있습니다.

[opc@bastion ~]$ kubectl cluster-info
Kubernetes control plane is running at <https://10.0.0.10:6443>
CoreDNS is running at <https://10.0.0.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy>

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[opc@bastion ~]$ kubectl cluster-info dump --output-directory=/home/opc/cluster-state
Cluster info dumped to /home/opc/cluster-state
[opc@bastion ~]$ ls -l /home/opc/cluster-state/
total 40
drwxr-xr-x.  2 opc opc  4096 May 26 05:43 default
drwxr-xr-x. 18 opc opc  4096 May 26 05:43 kube-system
-rw-rw-r--.  1 opc opc 30737 May 26 05:43 nodes.json
[opc@bastion ~]$ ls -l /home/opc/cluster-state/kube-system/
total 328
drwxr-xr-x. 2 opc opc     22 May 26 05:43 coredns-7dcb98c8fd-7jt8j
drwxr-xr-x. 2 opc opc     22 May 26 05:43 coredns-7dcb98c8fd-b8x64
drwxr-xr-x. 2 opc opc     22 May 26 05:43 coredns-7dcb98c8fd-gqvvq
drwxr-xr-x. 2 opc opc     22 May 26 05:43 csi-oci-node-69t92
drwxr-xr-x. 2 opc opc     22 May 26 05:43 csi-oci-node-hg7z5
drwxr-xr-x. 2 opc opc     22 May 26 05:43 csi-oci-node-sn62h
-rw-rw-r--. 1 opc opc  49905 May 26 05:43 daemonsets.json
-rw-rw-r--. 1 opc opc  14865 May 26 05:43 deployments.json
-rw-rw-r--. 1 opc opc    130 May 26 05:43 events.json
drwxr-xr-x. 2 opc opc     22 May 26 05:43 kube-dns-autoscaler-79cb6894d6-h72qg
drwxr-xr-x. 2 opc opc     22 May 26 05:43 kube-flannel-ds-fxxhx
drwxr-xr-x. 2 opc opc     22 May 26 05:43 kube-flannel-ds-gcmw2
drwxr-xr-x. 2 opc opc     22 May 26 05:43 kube-flannel-ds-krbcd
drwxr-xr-x. 2 opc opc     22 May 26 05:43 kube-proxy-cb4rq
drwxr-xr-x. 2 opc opc     22 May 26 05:43 kube-proxy-rtflh
drwxr-xr-x. 2 opc opc     22 May 26 05:43 kube-proxy-zkx5n
-rw-rw-r--. 1 opc opc 237150 May 26 05:43 pods.json
drwxr-xr-x. 2 opc opc     22 May 26 05:43 proxymux-client-hqtxb
drwxr-xr-x. 2 opc opc     22 May 26 05:43 proxymux-client-jsfw2
drwxr-xr-x. 2 opc opc     22 May 26 05:43 proxymux-client-pjh8r
-rw-rw-r--. 1 opc opc  13838 May 26 05:43 replicasets.json
-rw-rw-r--. 1 opc opc    146 May 26 05:43 replication-controllers.json
-rw-rw-r--. 1 opc opc   1899 May 26 05:43 services.json

 

kubectl 설정 파일의 내용을 확인하는 명령입니다.

[opc@bastion ~]$ kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: <https://10.0.0.10:6443>
  name: cluster-ckbcz27n3pa
contexts:
- context:
    cluster: cluster-ckbcz27n3pa
    user: user-ckbcz27n3pa
  name: context-ckbcz27n3pa
current-context: context-ckbcz27n3pa
kind: Config
preferences: {}
users:
- name: user-ckbcz27n3pa
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1beta1
      args:
      - ce
      - cluster
      - generate-token
      - --cluster-id
      - ocid1.cluster.oc1.iad.aaaaaaaaydgk47ug3bk2ykt3mjvjgduliah74rwznb2pafophckbcz27n3pa
      - --region
      - us-ashburn-1
      command: oci
      env: []
      interactiveMode: IfAvailable
      provideClusterInfo: false

 

OKE 클러스터의 노드 정보, 네임스페이스, 파드, 디플로이먼트, 서비스 정보를 확인해 봅니다.

[opc@bastion ~]$ kubectl get nodes
NAME          STATUS   ROLES   AGE   VERSION
10.0.10.116   Ready    node    22h   v1.23.4
10.0.10.135   Ready    node    22h   v1.23.4
10.0.10.137   Ready    node    22h   v1.23.4
[opc@bastion ~]$ kubectl get ns
NAME              STATUS   AGE
default           Active   22h
kube-node-lease   Active   22h
kube-public       Active   22h
kube-system       Active   22h
[opc@bastion ~]$ kubectl get pods --all-namespaces
NAMESPACE     NAME                                   READY   STATUS    RESTARTS        AGE
kube-system   coredns-7dcb98c8fd-7jt8j               1/1     Running   1               22h
kube-system   coredns-7dcb98c8fd-b8x64               1/1     Running   1               22h
kube-system   coredns-7dcb98c8fd-gqvvq               1/1     Running   1               22h
kube-system   csi-oci-node-69t92                     1/1     Running   2               22h
kube-system   csi-oci-node-hg7z5                     1/1     Running   2               22h
kube-system   csi-oci-node-sn62h                     1/1     Running   3 (5h27m ago)   22h
kube-system   kube-dns-autoscaler-79cb6894d6-h72qg   1/1     Running   1               22h
kube-system   kube-flannel-ds-fxxhx                  1/1     Running   3 (5h27m ago)   22h
kube-system   kube-flannel-ds-gcmw2                  1/1     Running   3 (5h28m ago)   22h
kube-system   kube-flannel-ds-krbcd                  1/1     Running   3 (5h27m ago)   22h
kube-system   kube-proxy-cb4rq                       1/1     Running   1               22h
kube-system   kube-proxy-rtflh                       1/1     Running   1               22h
kube-system   kube-proxy-zkx5n                       1/1     Running   1               22h
kube-system   proxymux-client-hqtxb                  1/1     Running   1               22h
kube-system   proxymux-client-jsfw2                  1/1     Running   1               22h
kube-system   proxymux-client-pjh8r                  1/1     Running   1               22h
[opc@bastion ~]$ kubectl get deployments --all-namespaces
NAMESPACE     NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kube-system   coredns               3/3     3            3           22h
kube-system   kube-dns-autoscaler   1/1     1            1           22h
[opc@bastion ~]$ kubectl get services --all-namespaces
NAMESPACE     NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
default       kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP                  22h
kube-system   kube-dns     ClusterIP   10.96.5.5    <none>        53/UDP,53/TCP,9153/TCP   22h

 

<END>