cloud bee

kubernetes daemonset 이란? 본문

AWS/kubernetes

kubernetes daemonset 이란?

who you 2023. 1. 11. 21:53

오늘은 kubernetes 이론 중 하나인 daemonset에 대해 알아보도록 할 것이다.

Daemonset의 동작과정은 다음과 같다.

1. daemonset은 kubernetes cluster 모든 노드에 특정 파드를 실행하고자 할 때 사용하는 컨트롤러이다.

2. daemonset은 kubernetes cluster 내에서 노드가 하나 생성되었을 때 자동으로 daemonset이 자동으로 생성된 노드에

pod를 실행시킨다.

3. 노드가 사라지면 노드에 위치한 pod도 같이 사라진다.

주로 Daemonset이 어디에 쓰일까?

Daemonset 같은 경우는 주로 cluster 내에 있는 containers log를 수집하는 fluetd에 주로 쓰인다.

Fluentd 이미지이다.

간단하게 amazon console 환경에서 Daemonset을 구현해 보도록 하겠다.

 

블로그 과정에서 필요로 하는 서비스 목록

✅ Amazon ec2( Amazon Elastic Compute Cloud )

✅  IAM ( Identity and Access Management )

✅ AWS EKS

✅ AWS CloudWatch

 

블로그 실습 조건

amazon console account

region: ap-northeast-2( 서울 리전 )

 

오늘 구현할 순서의 단계는 총 6가지이다.

Step 1. Create VPC

Step 2 Create security group

Step 3. EKS setting

Step 4. Create EC2 instance

Step 5. Kuberntes setting

Step 6. TEST


Step 1. Create VPC

yml 파일을 작성하여 VPC를 생성해 주었다. 해당 VPC는 3개의 가용영역을 가진다.( A, B, C )

vpc.yml 파일을 생성하여 아래 코드를 기입한 후, cloudformation stack을 생성해 주도록 한다,

Description:  This template deploys a VPC, with a pair of public and private subnets spread
  across two Availability Zones. It deploys an internet gateway, with a default
  route on the public subnets. It deploys a pair of NAT gateways (one in each AZ),
  and default routes for them in the private subnets.

Parameters:
  EnvironmentName:
    Description: An environment name that is prefixed to resource names
    Type: String
    Default: "skills"

  VpcCIDR:
    Description: Please enter the IP range (CIDR notation) for this VPC
    Type: String
    Default: 10.0.0.0/16

  PublicSubnet1CIDR:
    Description: Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone
    Type: String
    Default: 10.0.3.0/24

  PublicSubnet2CIDR:
    Description: Please enter the IP range (CIDR notation) for the public subnet in the second Availability Zone
    Type: String
    Default: 10.0.4.0/24

  PublicSubnet3CIDR:
    Description: Please enter the IP range (CIDR notation) for the public subnet in the second Availability Zone
    Type: String
    Default: 10.0.5.0/24

  PrivateSubnet1CIDR:
    Description: Please enter the IP range (CIDR notation) for the private subnet in the first Availability Zone
    Type: String
    Default: 10.0.0.0/24

  PrivateSubnet2CIDR:
    Description: Please enter the IP range (CIDR notation) for the private subnet in the second Availability Zone
    Type: String
    Default: 10.0.1.0/24

  PrivateSubnet3CIDR:
    Description: Please enter the IP range (CIDR notation) for the private subnet in the second Availability Zone
    Type: String
    Default: 10.0.2.0/24

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VpcCIDR
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-vpc

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  InternetGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      InternetGatewayId: !Ref InternetGateway
      VpcId: !Ref VPC

  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !GetAZs '' ]
      CidrBlock: !Ref PublicSubnet1CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-public-a

  PublicSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 1, !GetAZs  '' ]
      CidrBlock: !Ref PublicSubnet2CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-public-b

  PublicSubnet3:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 2, !GetAZs  '' ]
      CidrBlock: !Ref PublicSubnet3CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-public-c


  PrivateSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !GetAZs  '' ]
      CidrBlock: !Ref PrivateSubnet1CIDR
      MapPublicIpOnLaunch: false
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-private-a

  PrivateSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 1, !GetAZs  '' ]
      CidrBlock: !Ref PrivateSubnet2CIDR
      MapPublicIpOnLaunch: false
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-private-b

  PrivateSubnet3:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 2, !GetAZs  '' ]
      CidrBlock: !Ref PrivateSubnet3CIDR
      MapPublicIpOnLaunch: false
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-private-c

  NatGateway1EIP:
    Type: AWS::EC2::EIP
    DependsOn: InternetGatewayAttachment
    Properties:
      Domain: vpc

  NatGateway2EIP:
    Type: AWS::EC2::EIP
    DependsOn: InternetGatewayAttachment
    Properties:
      Domain: vpc
  NatGateway3EIP:
    Type: AWS::EC2::EIP
    DependsOn: InternetGatewayAttachment
    Properties:
      Domain: vpc
  NatGateway1:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt NatGateway1EIP.AllocationId
      SubnetId: !Ref PublicSubnet1

  NatGateway2:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt NatGateway2EIP.AllocationId
      SubnetId: !Ref PublicSubnet2
  NatGateway3:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt NatGateway3EIP.AllocationId
      SubnetId: !Ref PublicSubnet3
  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-public-rt

  DefaultPublicRoute:
    Type: AWS::EC2::Route
    DependsOn: InternetGatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  PublicSubnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet1

  PublicSubnet2RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet2

  PublicSubnet3RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet3

  PrivateRouteTable1:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-private-a-rt

  DefaultPrivateRoute1:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable1
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NatGateway1

  PrivateSubnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PrivateRouteTable1
      SubnetId: !Ref PrivateSubnet1

  PrivateRouteTable2:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-private-b-rt

  DefaultPrivateRoute2:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable2
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NatGateway2

  PrivateSubnet2RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PrivateRouteTable2
      SubnetId: !Ref PrivateSubnet2
  PrivateRouteTable3:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-private-c-rt
  DefaultPrivateRoute3:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable3
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NatGateway3

  PrivateSubnet3RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PrivateRouteTable3
      SubnetId: !Ref PrivateSubnet3

다음 결과와 같이 VPC가 성공적으로 생성된 모습을 확인할 수 있다.

이벤트 확인

Step 2 Create security group

이제 EKS와 bastion instance에서 사용할 보안그룹을 하나 생성해 주도록 한다.

우선 먼저 bastion 보안그룹을 생성해 주도록 한다. 보안그룹은 포트 22번을 open 하도록 한다.

bastion 보안그룹 생성

이후 EKS-cluster용 보안그룹을 하나 생성해 주도록 한다.

EKS 보안그룹 생성

Step 3. EKS setting

이제 EKS-cluster를 생성해 주도록 한다.

name: demo-eks

kubernetes version: 1.22

EKS 클러스터 생성

private subnet을 선택하고, 보안그룹을 EKS-SG로 지정해 준다.

cluster endpoint access 방식을 private로 지정해 준다.

private 서브넷 선택
보안그룹 선택

로깅 구성과 같은 경우는 아래 사진과 똑같이 구성해 준다.

로깅 구성하기

이후 생성을 진행해 준다.

생성을 완료하였다면 노드그룹을 하나 생성해 주도록 해준다.

EKS > 클러스터 > demo-eks의 컴퓨팅 란에서 노드 그룹을 추가한다.

node 그룹의 이름을 eks-node-gr로 지정해 준다.

인스턴스 유형을 t3.medium으로 지정한 후 노드 그룹을 생성해 주도록 한다.

EKS 노드그룹 생성하기
노드그룹 컴퓨팅 구성하기

Step 4. Create EC2 instance

이제 bastion instnace를 하나 생성해 주도록 한다.

이름을 bastion-demo로 지정한다.

AMI는 amazon linux2로 지정해 준다.

인스턴스 유형을 t2.micro로 지정한다.

퍼블릭 서브넷을 선택하고, Bastion-SG를 선택해 준다.

bastion host 생성
bastion host 설정

EIP를 연결해 준다.

ec2 console link!: https://console.aws.amazon.com/ec2에 들어가서 EIP를 생성하고 연결해 준다.

EC2 > 탄력적 IP 주소 > 탄력적 IP 주소 연결

탄력적 IP 연결하기

이후 bastion을 재부팅하고 터미널로 접속해 준다.

https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html 다음 링크를 통해 awscli를 다운로드해 준다.

#!/bin/bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
sudo mv /usr/local/bin/aws /bin/

https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/ kubectl을 설치해 준다.

#!/bin/bash
curl -LO https://dl.k8s.io/release/v1.22.0/bin/linux/amd64/kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
chmod +x kubectl
sudo mv ./kubectl /bin/

 

인스턴스 프로파일 설정

사진과 같이 profile 설정을 진행한다.

 

eks를 연결해 주도록 한다.

awscli로 kubeconfig 파일 생성

 

Step 5. Kuberntes setting

다음 yaml 파일을 가지고 DaemonSet을 생성하도록 한다.

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  namespace: kube-system
  labels:
    k8s-app: fluentd-logging
spec:
  selector:
    matchLabels:
      name: fluentd-elasticsearch
  template:
    metadata:
      labels:
        name: fluentd-elasticsearch
    spec:
      tolerations:
      # these tolerations are to have the daemonset runnable on control plane nodes
      # remove them if your control plane nodes should not run pods
      - key: node-role.kubernetes.io/control-plane
        operator: Exists
        effect: NoSchedule
      - key: node-role.kubernetes.io/master
        operator: Exists
        effect: NoSchedule
      containers:
      - name: fluentd-elasticsearch
        image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: varlog
          mountPath: /var/log
      terminationGracePeriodSeconds: 30
      volumes:
      - name: varlog
        hostPath:
          path: /var/log
kubectl apply -f https://k8s.io/examples/controllers/daemonset.yaml

 

Step 6.TEST

테스트 결과

fluentd pod가 정상적으로 생성된 것을 확인할 수 있다.

Comments