ENCORE CLOUD ARCHITECTURE TIL 4/26 Kubernetes

신민창·2021년 4월 26일
0

TIL

목록 보기
46/46

Kubespray로 kubernetes 설치

가상환경 실행 후 명령어를 통해 마스터 노드로 사용할 vm에 접속한다.

https://github.com/kubernetes-sigs/kubespray 해당 git 사이트에서 kuberspray 파일들을 확인할 수 있다. 여기서 2.14 버전 파일을 다운받아 설치할 것이다.

$ git clone --branch release-2.14 https://github.com/kubernetes-sigs/kubespray
$ cd kubespray

이제 설치에 필요한 패키지들을 다운받은 requirements.txt 파일을 통해서 설치할 것이다.

$ sudo apt install python3-pip
$ sudo pip3 install -r requirements.txt

kubespray같은 경우 ansible을 이용해서 설치하기 때문에 ssh 키를 생성하고 생성한 키를 작업 노드에 모두 복사하여야 한다.

# ssh-keygen은 모두 default 값으로 구성한다.
$ ssh-keygen  
$ ssh-copy-id vagrant@192.168.201.11
$ ssh-copy-id vagrant@192.168.201.21
$ ssh-copy-id vagrant@192.168.201.22
$ ssh-copy-id vagrant@192.168.201.23

ansible을 이용하기 때문에 inventory파일도 구성해야한다.

$ cp -r inventory/sample inventory/mycluster
$ vi inventory/mycluster/inventory.ini
[all]
k8s-m1 ansible_host=192.168.201.11 ip=192.168.201.11 ansible_connection=local
k8s-w1 ansible_host=192.168.201.21 ip=192.168.201.21
k8s-w2 ansible_host=192.168.201.22 ip=192.168.201.22
k8s-w3 ansible_host=192.168.201.23 ip=192.168.201.23

[kube-master]
k8s-m1

[etcd]
k8s-m1

[kube-node]
k8s-w1
k8s-w2
k8s-w3

[calico-rr]

[k8s-cluster:children]
kube-master
kube-node
calico-rr

이제 플레이북을 실행하면 된다.

$ ansible-playbook -i inventory/mycluster/inventory.ini cl
uster.yml -b

약 10분이상 기다리면 설치가 완료될 것이다.
이제 사용자에게 kubenetes 권한을 부여하고 자동완성 기능을 추가하기 위해 다음 명령어를 입력한다.

$ mkdir $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
$ kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl
$ exec bash

kubernetes 기본

kubernetes 기본 명령어

kubectl run
kubectl create
kubectl get
kubectl get -o wide
kubectl get -o yaml
kubectl describe
kubectl logs (-c)
kubectl port-forward (-c)
kubectl exec (-c)
kubectl delete

레이블

https://kubernetes.io/ko/docs/concepts/overview/working-with-objects/labels/

  • 검색
  • 리소스 간 연결
...
metadata:
  name: myapp-pod
  labels:
    app: myapp
    env: development
    ...
kubectl label pod web1 app=httpd
kubectl label pod web1 app=myapp --overwrite
kubectl label pod web1 app='' --overwrite
kubectl label pod web1 app-

레이블 확인

kubectl get pods --show-labels
kubectl get pod myapp-pod -o yaml
kubectl describe pod myapp-pod

레이블 셀렉터

kubectl get -l <EXPR>
일치성(Equality Based)
  • Key =Value
  • Key == Value
  • Key != Value
집합성(Set Based)
  • Key in (Value)
  • Key notin (Value)
  • Key
  • !Key

Annotations

https://kubernetes.io/ko/docs/concepts/overview/working-with-objects/annotations/

kubectl annotate pod web1 header=jang
kubectl annotate pod web1 header=kim --overwrite
kubectl annotate pod web1 header-
metadata:
  name: myapp-pod
  labels:
    app: myapp
    env: development
  annotations:
    header: jang
    ...
  • Label: 식별
  • Annotation: 비-식별 정보 (API 또는 라이브러리를 통해 해당 값을 참조)

Namespace

https://kubernetes.io/ko/docs/concepts/overview/working-with-objects/namespaces/

kubectl -n | --namespace
metadata:
  name: myapp-pod
  namespace: kube-system
kubectl create namespace quality-assurance
apiVersion: v1
kind: Namespace
metadata:
  name: quality-assurance

Pod Lifecycle

https://kubernetes.io/ko/docs/concepts/workloads/pods/pod-lifecycle/

파드의 단계

  • Pending

  • Running

  • Succeeded

  • Failed

  • Unknown

  • Terminating

컨테이너의 상태

  • Waiting
  • Running
  • Terminated

Replication Controller(rc)

https://kubernetes.io/ko/docs/concepts/workloads/controllers/replicationcontroller/

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
  • .spec.replicas: 복제본 개수(기본: 1)

  • .spec.selector: 파드 레이블 셀렉터

  • .spec.template: 파드 템플릿

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

0개의 댓글