K8s에서 nginx설정을 아무리 해도 60초후에 연결이 끊어지는 현상이 있었다.

서비스에서 Load Balancer에서 설정을 변경해주어야 한다.

 

반응형
Posted by 화니.
,

네이버 클라우드의 K8s는 디스크 용량이 50G제한으로 늘릴 수 없게 되어있다.

기본적으로 용량이 큰 팟은 사용할 수 없는 상태이다.

 

먼저 스토리지를 생성하여 해당 노드에 연결시킨다.

https://guide.ncloud-docs.com/docs/server-storage-use-vpc

 

스토리지 사용

 

guide.ncloud-docs.com

 

 

해당 노드의 쉘로 접속 후

/etc/containerd/config.toml 파일을 수정하여 아래와 같이 컨테이너 경로를 수정한다.

root = "/마운트경로/var/lib/containerd"
state = "/마운트경로/run/containerd"

 

재부팅후 팟을 올리면 새 마운트경로에 저장되는 것을 볼 수 있다.

반응형
Posted by 화니.
,

여기서 클러스터는 Node를 뜻합니다.

Pod 가 증가하거나 다른서버 오류로 노드(서버 컴퓨터가) 부족할때 자동으로 증가했다가 10분(기본값)이상 미사용되면 다시 축소하게 됩니다.

 

일단 노드그룹에서 최대크기를 지정해놔야 합니다.

 

적용할 YAML

---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-addon: cluster-autoscaler.addons.k8s.io
    k8s-app: cluster-autoscaler
  name: cluster-autoscaler
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cluster-autoscaler
  labels:
    k8s-addon: cluster-autoscaler.addons.k8s.io
    k8s-app: cluster-autoscaler
rules:
  - apiGroups: [""]
    resources: ["events", "endpoints"]
    verbs: ["create", "patch"]
  - apiGroups: [""]
    resources: ["pods/eviction"]
    verbs: ["create"]
  - apiGroups: [""]
    resources: ["pods/status"]
    verbs: ["update"]
  - apiGroups: [""]
    resources: ["endpoints"]
    resourceNames: ["cluster-autoscaler"]
    verbs: ["get", "update"]
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["watch", "list", "get", "update"]
  - apiGroups: [""]
    resources:
      - "namespaces"
      - "pods"
      - "services"
      - "replicationcontrollers"
      - "persistentvolumeclaims"
      - "persistentvolumes"
    verbs: ["watch", "list", "get"]
  - apiGroups: ["extensions"]
    resources: ["replicasets", "daemonsets"]
    verbs: ["watch", "list", "get"]
  - apiGroups: ["policy"]
    resources: ["poddisruptionbudgets"]
    verbs: ["watch", "list"]
  - apiGroups: ["apps"]
    resources: ["statefulsets", "replicasets", "daemonsets"]
    verbs: ["watch", "list", "get"]
  - apiGroups: ["storage.k8s.io"]
    resources:
      ["storageclasses", "csinodes", "csidrivers", "csistoragecapacities"]
    verbs: ["watch", "list", "get"]
  - apiGroups: ["batch", "extensions"]
    resources: ["jobs"]
    verbs: ["get", "list", "watch", "patch"]
  - apiGroups: ["coordination.k8s.io"]
    resources: ["leases"]
    verbs: ["create"]
  - apiGroups: ["coordination.k8s.io"]
    resourceNames: ["cluster-autoscaler"]
    resources: ["leases"]
    verbs: ["get", "update"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: cluster-autoscaler
  namespace: kube-system
  labels:
    k8s-addon: cluster-autoscaler.addons.k8s.io
    k8s-app: cluster-autoscaler
rules:
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["create", "list", "watch"]
  - apiGroups: [""]
    resources: ["configmaps"]
    resourceNames:
      ["cluster-autoscaler-status", "cluster-autoscaler-priority-expander"]
    verbs: ["delete", "get", "update", "watch"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cluster-autoscaler
  labels:
    k8s-addon: cluster-autoscaler.addons.k8s.io
    k8s-app: cluster-autoscaler
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-autoscaler
subjects:
  - kind: ServiceAccount
    name: cluster-autoscaler
    namespace: kube-system

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: cluster-autoscaler
  namespace: kube-system
  labels:
    k8s-addon: cluster-autoscaler.addons.k8s.io
    k8s-app: cluster-autoscaler
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: cluster-autoscaler
subjects:
  - kind: ServiceAccount
    name: cluster-autoscaler
    namespace: kube-system

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cluster-autoscaler
  namespace: kube-system
  labels:
    app: cluster-autoscaler
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cluster-autoscaler
  template:
    metadata:
      labels:
        app: cluster-autoscaler
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "8085"
    spec:
      priorityClassName: system-cluster-critical
      securityContext:
        runAsNonRoot: true
        runAsUser: 65534
        fsGroup: 65534
      serviceAccountName: cluster-autoscaler
      containers:
        - image: k8s.gcr.io/autoscaling/cluster-autoscaler:v1.22.3 #k8s버전
          name: cluster-autoscaler
          resources:
            limits:
              cpu: 100m
              memory: 600Mi
            requests:
              cpu: 100m
              memory: 600Mi
          command:
            - ./cluster-autoscaler
            - --v=4
            - --stderrthreshold=info
            - --cloud-provider=aws
            - --skip-nodes-with-local-storage=false
            - --expander=least-waste
            - --node-group-auto-discovery=asg:tag=k8s.io/cluster-autoscaler/enabled,k8s.io/cluster-autoscaler/myclustername #사용하는 클러스터명
            - --balance-similar-node-groups=true 
            - --skip-nodes-with-system-pods=false


          volumeMounts:
            - name: ssl-certs
              mountPath: /etc/ssl/certs/ca-certificates.crt #/etc/ssl/certs/ca-bundle.crt for Amazon Linux Worker Nodes
              readOnly: true
          imagePullPolicy: "Always"
      volumes:
        - name: ssl-certs
          hostPath:
            path: "/etc/ssl/certs/ca-bundle.crt"

 

 

 

참고문서 : https://catalog.us-east-1.prod.workshops.aws/workshops/9c0aa9ab-90a9-44a6-abe1-8dff360ae428/ko-KR/100-scaling/200-cluster-scaling

반응형
Posted by 화니.
,

VPC CNI업데이트 후 외부네트워크에 문제가 생기고 인그레스가 정상적이지 않은 문제가 생겼습니다.

 

VPC CNI 을 제거하고 클러스터에서 이 추가 기능의 기존 구성을 재정의합니다. 를 체크하고 문제가 해결되었습니다.

반응형
Posted by 화니.
,

먼저 Gitlab에서 Kubernetes연동을 해줍니다.

Environment scope는 * 로 지정해줍니다.

다중 클러스터를 사용하시면 별도이름을 지정해 주어야 합니다.

 

그 다음 Gitlab-runner를 등록해야합니다.

Linux방식의 shell executor와 docker방식 2개를 등록했습니다.

docker방식만 사용하면 캐시가 되지 않아 도커빌드가 시간이 오래걸리게 됩니다.

docker만 사용하실분은 https://docs.gitlab.com/ee/ci/docker/using_docker_build.html 참고해주세요.

 

#  .gitlab-ci.yml 파일 예제

stages:
  - test
  - build
  - deploy

test:
  image:
    name: python3 #사용하는 언어에 맞는 이미지
  script:
	- pip install -r requirements.txt
    - python manage.py test
  tags:
    - docker #사용할 runner 테그

build:
  stage: build
  script:
  	# private 저장소를 이용시 로그인. gitlab 변수에 설정해둡니다.
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - echo "Building the app"
    - docker build --tag myimage:latest .
    - docker image push -a $CI_REGISTRY/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME
  tags:
    - shell #사용할 runner 테그

# staging환경. master로 커밋시 작동
deploy_staging:
  stage: deploy
  image:
    name: bitnami/kubectl:latest
    entrypoint: [""]
  only:
    - master
  script:
    - echo "Deploy to staging server"
    - kubectl apply -f devops/deploy-dev.yaml 

  environment:
    name: staging        
  tags:
    - docker #docker로만 쿠버네티스 연동 가능

# production 환경. 배포는 수동으로만 작동
deploy_prod:
  stage: deploy
  image:
    name: bitnami/kubectl:latest
    entrypoint: [""]
  only:
    - master
  script:
    - echo "Deploy to production server"
    - kubectl apply -f devops/deploy-prod.yaml 
  environment:
    name: production        
  tags:
    - docker #docker로만 쿠버네티스 연동 가능

 

 

반응형
Posted by 화니.
,

Django, PHP등의 WAS는 프로그램 결과는 전송하지만 이미지나 CSS등은 처리가 되지 않아 보통 nginx를 통해 전송합니다.

이미지등의 처리는 AWS S3등의 클라우드 스토리지를 통해 많이 사용하지만 Ingress를 통해 전송하는 방식을 정리해보았습니다.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: was  
spec:
  replicas: 1
  selector:
    matchLabels:
      app: was
  template:
    metadata:
      labels:
        app: was
    spec:      
      volumes:
        - name: staticfiles

      containers:
      	# was의 파일을 nginx를 통해 전송
        - name: nginx
          image: nginx:stable
          ports:
            - containerPort: 80
          volumeMounts:          	
            - name: staticfiles
              mountPath: /usr/share/nginx/html/static/          

        - name: was
          image: myimage
          imagePullPolicy: Always
          ports:
            - containerPort: 8000
          volumeMounts:
            - name: staticfiles
              mountPath: /static
          lifecycle:
            postStart:
              exec:
                # was내의 파일을 다른곳으로 복사
                command: ["/bin/sh", "-c", "cp -r /usr/src/app/static /"]

---
apiVersion: v1
kind: Service
metadata:
  name: was-svc  
  labels:
    app: was-svc

spec:
  selector:
    app: was
  type: NodePort

  ports:
    - port: 8000
      targetPort: 8000
      name: was
    - port: 80
      targetPort: 80
      name: nginx

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: "was-ing"  
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-production"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/ssl-passthrough: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"

spec:
  ingressClassName: nginx
  tls:
    - hosts:
        - mydomain.com
      secretName: was-tls
  rules:
    - host: mydomain.com
      http:
        paths:
          - path: /static
            pathType: Prefix
            backend:
              service:
                name: was-svc
                port:
                  number: 80
          - path: /
            pathType: Prefix
            backend:
              service:
                name: was-svc
                port:
                  number: 8000

 

 

 

반응형
Posted by 화니.
,

보통 도커 이미지를 잘못 만들었거나 쿠버네티스 환경 셋팅이 잘못되어 나오지만 오류메세지는 뜨지 않는 경우가 있습니다.

Deployment 파일에 

spec:
  containers:
    - name : err-container
      command: ["sleep"]  # 두줄 추가
      args: ["infinity"]  # 두줄 추가

sleep 을 추가하여 

kubelctl exec pod이름 -it -- /bin/bash 로 쉘에 접속하여 오류 파악을 할 수 있습니다.

반응형

'IT > Kubernetes' 카테고리의 다른 글

WAS에서 Static 파일 전송하기  (0) 2022.03.12
Elastic Cloud on Kubernetes (ECK) 설치  (1) 2022.03.07
AWS 에서 서브 도메인만 연결하기(SSL)  (0) 2022.02.26
Posted by 화니.
,

* AWS EKS(Amazon Elastic Kubernetes Service) 에 설치한 기록입니다.

 

- 기본 설정 적용

kubectl create -f https://download.elastic.co/downloads/eck/2.0.0/crds.yaml
kubectl apply -f https://download.elastic.co/downloads/eck/2.0.0/operator.yaml

- 먼저 PV(PersistentVolume)를 설정해 줍니다. 저렴하게 저장하기위해 st1을 위해 셋팅했습니다.

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: hdd
  annotations:
    storageclass.kubernetes.io/is-default-class: "false"
provisioner: kubernetes.io/aws-ebs
parameters:
  type: st1
  fsType: ext4
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-elastic
  namespace: default

spec:
  accessModes:
    - ReadWriteOnce
  awsElasticBlockStore:
    fsType: ext4
    volumeID: aws://ap-northeast-2c(리젼)/볼륨ID
  capacity:
    storage: 500Gi
  persistentVolumeReclaimPolicy: Delete
  storageClassName: hdd

- ElasticSearch를 생성합니다. 미리 생성한 PV에 자동으로 PVC(PersistentVolumeClaim)가 연결됩니다.

apiVersion: elasticsearch.k8s.elastic.co/v1
kind: Elasticsearch
metadata:
  name: es
spec:
  version: 8.0.1
  nodeSets:
    - name: default
      count: 1      
      podTemplate:
        spec:
          containers:
            - name: elasticsearch
              env:
                - name: ES_JAVA_OPTS
                  value: -Xms1g -Xmx1g
              resources:
                requests:
                  memory: 2Gi
                  cpu: 2
                limits:
                  memory: 4Gi
      volumeClaimTemplates:
        - metadata:
            name: elasticsearch-data 
          spec:
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                storage: 500Gi
            storageClassName: hdd

- Kibana를 셋팅합니다.

Ingress는 nginx-ingress기준으로 설정했습니다.

apiVersion: kibana.k8s.elastic.co/v1
kind: Kibana
metadata:
  name: es
spec:
  version: 8.0.1
  count: 1
  elasticsearchRef:
    name: es
  # Please upgrade your browser 메세지 방지
  config:
    csp.strict: false    
  http:
    tls:
      selfSignedCertificate:
        disabled: true
  podTemplate:
    spec:
      containers:
        - name: kibana
          env:
            - name: NODE_OPTIONS
              value: "--max-old-space-size=1024"
          resources:
            requests:
              memory: 0.5Gi
              cpu: 0.5
            limits:
              memory: 1Gi
              cpu: 1
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: "kibana-ing"
  annotations:    
    cert-manager.io/cluster-issuer: "letsencrypt-production"    
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/ssl-passthrough: "true"
    
    

spec:
  ingressClassName: nginx
  tls:
    - hosts:
        - kibana.hwani.net
      secretName: kibana-tls
  rules:
    - host: kibana.hwani.net
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: es-kb-http
                port:
                  number: 5601

- 리눅스

kubectl get secret es-es-elastic-user -o=jsonpath='{.data.elastic}' | base64 --decode; echo

- 파워쉘

$s=kubectl get secret es-es-elastic-user -o=jsonpath='{.data.elastic}'
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($s))

 

https://kibana.hwani.net(설정 도메인) 으로 로 접속하여 

아이디 : elastic

암호 : 쉘내용

으로 접속합니다.

 

기본 설정이 끝났습니다.

 

반응형
Posted by 화니.
,