실습 설정: Chaos Mesh, 스케일링 및 Pod affinity
이 가이드는 고가용성 관행을 구현하여 UI 서비스의 복원력을 향상시키는 단계를 설명합니다. helm 설치, UI 서비스 스케일링, Pod anti-affinity 구현 및 가용 영역 간 Pod 분산을 시각화하는 헬퍼 스크립트 사용에 대해 다룹니다.
Chaos Mesh 설치
클러스터의 복원력 테스트 기능을 향상시키기 위해 Chaos Mesh를 설치합니다. Chaos Mesh는 Kubernetes 환경을 위한 강력한 카오스 엔지니어링 도구입니다. 이를 통해 다양한 장애 시나리오를 시뮬레이션하고 애플리케이션이 어떻게 응답하는지 테스트할 수 있습니다.
Helm을 사용하여 클러스터에 Chaos Mesh를 설치해 보겠습니다:
Release "chaos-mesh" does not exist. Installing it now.
NAME: chaos-mesh
LAST DEPLOYED: Tue Aug 20 04:44:31 2024
NAMESPACE: chaos-mesh
STATUS: deployed
REVISION: 1
TEST SUITE: None
스케일링 및 Topology Spread Constraints
Kustomize 패치를 사용하여 UI Deployment를 수정하고, 5개의 레플리카로 스케일링하며 topology spread constraints 규칙을 추가합니다. 이를 통해 UI Pod가 서로 다른 노드에 분산되어 노드 장애의 영향을 줄일 수 있습니다.
패치 파일의 내용은 다음과 같습니다:
- Kustomize Patch
- Deployment/ui
- Diff
apiVersion: apps/v1
kind: Deployment
metadata:
name: ui
namespace: ui
spec:
replicas: 5
selector:
matchLabels:
app: ui
template:
metadata:
labels:
app: ui
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: ui
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: ui
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app.kubernetes.io/created-by: eks-workshop
app.kubernetes.io/type: app
name: ui
namespace: ui
spec:
replicas: 5
selector:
matchLabels:
app: ui
app.kubernetes.io/component: service
app.kubernetes.io/instance: ui
app.kubernetes.io/name: ui
template:
metadata:
annotations:
prometheus.io/path: /actuator/prometheus
prometheus.io/port: "8080"
prometheus.io/scrape: "true"
labels:
app: ui
app.kubernetes.io/component: service
app.kubernetes.io/created-by: eks-workshop
app.kubernetes.io/instance: ui
app.kubernetes.io/name: ui
spec:
containers:
- env:
- name: JAVA_OPTS
value: -XX:MaxRAMPercentage=75.0 -Djava.security.egd=file:/dev/urandom
- name: METADATA_KUBERNETES_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: METADATA_KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: METADATA_KUBERNETES_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
envFrom:
- configMapRef:
name: ui
image: public.ecr.aws/aws-containers/retail-store-sample-ui:1.2.1
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 45
periodSeconds: 20
name: ui
ports:
- containerPort: 8080
name: http
protocol: TCP
resources:
limits:
memory: 1.5Gi
requests:
cpu: 250m
memory: 1.5Gi
securityContext:
capabilities:
add:
- NET_BIND_SERVICE
drop:
- ALL
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
volumeMounts:
- mountPath: /tmp
name: tmp-volume
securityContext:
fsGroup: 1000
serviceAccountName: ui
topologySpreadConstraints:
- labelSelector:
matchLabels:
app: ui
maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
- labelSelector:
matchLabels:
app: ui
maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
volumes:
- emptyDir:
medium: Memory
name: tmp-volume
app.kubernetes.io/type: app
name: ui
namespace: ui
spec:
- replicas: 1
+ replicas: 5
selector:
matchLabels:
+ app: ui
app.kubernetes.io/component: service
app.kubernetes.io/instance: ui
app.kubernetes.io/name: ui
template:
[...]
prometheus.io/path: /actuator/prometheus
prometheus.io/port: "8080"
prometheus.io/scrape: "true"
labels:
+ app: ui
app.kubernetes.io/component: service
app.kubernetes.io/created-by: eks-workshop
app.kubernetes.io/instance: ui
app.kubernetes.io/name: ui
[...]
name: tmp-volume
securityContext:
fsGroup: 1000
serviceAccountName: ui
+ topologySpreadConstraints:
+ - labelSelector:
+ matchLabels:
+ app: ui
+ maxSkew: 1
+ topologyKey: topology.kubernetes.io/zone
+ whenUnsatisfiable: ScheduleAnyway
+ - labelSelector:
+ matchLabels:
+ app: ui
+ maxSkew: 1
+ topologyKey: kubernetes.io/hostname
+ whenUnsatisfiable: ScheduleAnyway
volumes:
- emptyDir:
medium: Memory
name: tmp-volume
Kustomize 패치와 Kustomization 파일을 사용하여 변경 사항을 적용합니다:
리테일 스토어 접근성 확 인
이러한 변경 사항을 적용한 후 리테일 스토어에 접근할 수 있는지 확인하는 것이 중요합니다:
Waiting for k8s-ui-ui-5ddc3ba496-721427594.us-west-2.elb.amazonaws.com...
You can now access http://k8s-ui-ui-5ddc3ba496-721427594.us-west-2.elb.amazonaws.com
이 명령이 완료되면 URL이 출력됩니다. 새 브라우저 탭에서 이 URL을 열어 리테일 스토어가 접근 가능하고 올바르게 작동하는지 확인하세요.
리테일 URL이 작동하려면 5-10분이 소요될 수 있습니다.
헬퍼 스크립트: Get Pods by AZ
get-pods-by-az.sh 스크립트는 터미널에서 서로 다른 가용 영역에 걸친 Kubernetes Pod의 분산을 시각화하는 데 도움이 됩니다. GitHub에서 스크립트 파일을 여기에서 볼 수 있습니다.