👤
Novice Handbook
  • Novice Handbook
  • Guide
  • Internet และ Web
    • HTML
  • Computer Language
    • Basic Computer Language (LV.1)
    • C Language (LV.1)
    • Python3 (LV.1)
  • Operating System
    • Linux
      • Basic Linux (LV.1)
  • TOOLS
    • Text Editor
      • Vim Editor
    • Source Control
      • GitLab
        • GitLab for small site (LV.1)
    • Container
      • Docker
        • Docker (LV.1)
        • Docker (LV.2)
      • Kubernetes
        • Kubernetes Intro (LV.0)
        • Kubernetes Basic (LV.1)
        • Kubernetes Intermediate (LV.2)
        • Helm (LV.2)
        • RKE2 (LV.3)
        • K3S (LV.3)
        • K3D (LV.3)
    • Repository
      • Harbor
        • Harbor for small site (LV.1)
        • Harbor for enterprise (LV.2)
    • Database
      • Redis
        • Redis on Docker Compose (LV.1)
        • Redis on Kubernetes (LV.2)
      • Elastic Stack
        • Elasticsearch & Kibana for small site (LV.1)
    • Observability
      • Prometheus
        • Prometheus for small site (LV.1)
        • Prometheus Operator (LV.2)
    • Security
      • Certbot (LV.1)
      • Falco
      • Hashicorp Vault
    • Collaboration
      • Nextcloud
Powered by GitBook
On this page
  • Consul คืออะไร
  • การติดตั้ง Hashicorp Vault
  • ติดตั้ง Consul
  • ติดตั้ง Vault

Was this helpful?

  1. TOOLS
  2. Security

Hashicorp Vault

HashiCorp Vault เป็นเครื่องมือจัดการความปลอดภัยและการเข้าถึงข้อมูลสำคัญในระบบไอที มันถูกออกแบบมาเพื่อจัดเก็บ จัดการ และควบคุมการเข้าถึงข้อมูลลับต่าง ๆ เช่น รหัสผ่าน คีย์ API และข้อมูลที่เป็นความลับอื่น ๆ โดยมีคุณสมบัติที่สำคัญดังนี้

  1. การจัดเก็บข้อมูลลับ (Secrets Management): Vault ช่วยในการจัดเก็บข้อมูลลับต่าง ๆ ในที่เดียว และควบคุมการเข้าถึงอย่างเข้มงวด ผู้ใช้งานสามารถกำหนดสิทธิ์การเข้าถึงได้ตามบทบาทหรือการกำหนดนโยบายเฉพาะ

  2. Dynamic Secrets: Vault สามารถสร้างข้อมูลลับแบบชั่วคราวสำหรับการเข้าถึงระบบภายนอก เช่น ฐานข้อมูลหรือบริการอื่น ๆ ซึ่งข้อมูลลับเหล่านี้จะถูกสร้างใหม่ในทุกครั้งที่มีการร้องขอ และจะหมดอายุเมื่อใช้งานเสร็จสิ้น

  3. Encryption as a Service: Vault ให้บริการการเข้ารหัสข้อมูลผ่าน API เพื่อใช้ในการเข้ารหัสและถอดรหัสข้อมูลต่าง ๆ โดยที่แอปพลิเคชันหรือระบบต่าง ๆ ไม่จำเป็นต้องมีการจัดการคีย์เข้ารหัสเอง

  4. การควบคุมการเข้าถึงที่ปลอดภัย (Access Control): Vault มีระบบการควบคุมสิทธิ์การเข้าถึงที่ละเอียด ผู้ใช้งานจะสามารถเข้าถึงเฉพาะข้อมูลที่จำเป็นตามที่ได้รับอนุญาต ซึ่งลดความเสี่ยงจากการเปิดเผยข้อมูลที่ไม่จำเป็น

  5. Audit Logging: Vault มีการบันทึกการใช้งานเพื่อให้สามารถตรวจสอบการเข้าถึงและใช้งานข้อมูลลับต่าง ๆ ทำให้มีความโปร่งใสและสามารถติดตามการดำเนินการย้อนหลังได้

Consul คืออะไร

Consul เป็นเครื่องมือที่ให้บริการ service discovery และ distributed key-value store ที่ใช้สำหรับเก็บข้อมูลเช่น การกำหนดค่าและข้อมูลสภาวะการทำงานของระบบแบบกระจายตัว (distributed systems) โดย Consul มีคุณสมบัติหลัก ๆ ดังนี้

  1. High Availability: สามารถทำงานในโหมดกระจายตัวที่มีหลายโหนด (nodes) เพื่อเพิ่มความทนทานต่อการล้มเหลว

  2. Replication: Consul จะทำการ replicates ข้อมูลระหว่างโหนดหลายโหนด เพื่อความทนทานต่อการสูญเสียข้อมูล

  3. Service Discovery: มีคุณสมบัติการค้นหาบริการและการติดตามสุขภาพ (health checking) ของบริการในระบบ

การใช้ Consul เป็น backend สำหรับ HashiCorp Vault เป็นการตั้งค่าที่ช่วยให้ Vault สามารถจัดเก็บข้อมูลที่สำคัญและมีความทนทานในการทำงาน โดย Consul ทำหน้าที่เป็น Storage Backend ที่สามารถจัดเก็บข้อมูลต่าง ๆ ที่ Vault ต้องการเก็บ เช่น Secret, Tokens, และข้อมูลการกำหนดค่าอื่น ๆ การตั้งค่า Consul เป็น backend มีประโยชน์ในด้านการจัดเก็บที่ปลอดภัยและการกระจายโหลดสำหรับการใช้งาน Vault ใน production environment

การติดตั้ง Hashicorp Vault

ติดตั้ง Consul

จากตัวอย่างนี้ จะติดตั้ง consul บน Kubernetes cluster ผ่าน Helm Chart ของ Hashicorp

Architecture

ตาม recommend ของ Hashicorp กำหนดว่า consul server ควรมีอย่างน้อย 5 node และกระจายกันอยู่ใน 3 AZ แต่โดยส่วนมากสำหรับ small production site ใช้ 3 node ก็เพียงพอ เช่นกัน

Hardware Requirement

Size
CPU
Memory
Disk Capacity
Disk IO
Disk Throughput

Small

2-4 core

8-16 GB

100+ GB

3000+ IOPS

75+ MB/s

Large

8-16 core

32-64 GB

200+ GB

7500+ IOPS

250+ MB/s

Network Latency Requirement

round trip time (RTT) ในการสื่อสารกันระหว่าง agent ต้องไม่เกิน ดังนี้

  • RTT ของทุก traffic โดยเฉลี่ย ต้องไม่เกิน 50ms

  • RTT ที่ percentile 99 ต้องไม่เกิน 100ms

Firewall rule

Name
Port / Protocol
Source
Destination
Description

RPC

8300 / TCP

All agents (client & server)

Server agents

Used by servers to handle incoming requests from other agents.

Serf LAN

8301 / TCP & UDP

All agents (client & server)

All agents (client & server)

Used to handle gossip in the LAN. Required by all agents.

Serf WAN

8302 / TCP & UDP

Server agents

Server agents

Used by server agents to gossip over the WAN to other server agents. Only used in multi-cluster environments.

HTTP/HTTPS

8500 & 8501 TCP

Localhost of client or server agent

Localhost of client or server agent

Used by clients to talk to the HTTP API. HTTPS is disabled by default.

DNS

8600 TCP & UDP

Localhost of client or server agent

Localhost of client or server agent

Used to resolve DNS queries.

gRPC (Optional)

8502 TCP

Envoy proxy

Client agent or server agent that manages the sidecar proxies service registration

Used to expose the xDS API to Envoy proxies. Disabled by default.

Sidecar Proxy (Optional)

21000 - 21255 TCP

All agents (client & server)

Client agent or server agent that manages the sidecar proxies service registration

Port range used for automatically assigned sidecar service registrations.

ตัวอย่าง value file

สร้างไฟล์ consul.values.yaml

global:
  name: consul
  datacenter: dc1
  gossipEncryption:
    autoGenerate: true
server:
  replicas: 3
  storage: 10Gi
  resources:
    requests:
      memory: '1Gi'
      cpu: '1'
    limits:
      memory: '1Gi'
      cpu: null # override existing cpu limit value
  topologySpreadConstraints: |
    - maxSkew: 1
      topologyKey: topology.kubernetes.io/zone
      whenUnsatisfiable: DoNotSchedule
      labelSelector:
        matchLabels:
          app: {{ template "consul.name" . }}
          release: "{{ .Release.Name }}"
          component: server
client:
  enabled: true
  resources:
    requests:
      memory: '512Mi'
      cpu: '500m'
    limits:
      memory: '512Mi'
      cpu: null # override existing cpu limit value
ui:
  enabled: true
  service:
    type: ClusterIP

ติดตั้งด้วยคำสั่งดังนี้

helm repo add hashicorp https://helm.releases.hashicorp.com
helm repo update
helm upgrade --install consul hashicorp/consul --values consul.values.yaml --create-namespace --namespace vault

ติดตั้ง Vault

เตรียม YAML file ชื่อ vault.values.yaml ดังนี้

server:
  ha:
    enabled: true
    replicas: 3
  auditStorage:
    enabled: true
    size: 10Gi
  resources:
    requests:
      memory: "1Gi"
      cpu: "250m"
    limits:
      memory: "1Gi"
  topologySpreadConstraints: |
    - maxSkew: 1
      topologyKey: topology.kubernetes.io/zone
      whenUnsatisfiable: DoNotSchedule
      labelSelector:
        matchLabels:
          app.kubernetes.io/name: {{ include "vault.name" . }}
          app.kubernetes.io/instance: {{ .Release.Name }}
          component: server
ui:
  enabled: true

ติดตั้งด้วยคำสั่งดังนี้

helm repo add hashicorp https://helm.releases.hashicorp.com
helm repo update
helm upgrade --install vault hashicorp/vault --values vault.values.yaml --create-namespace --namespace vault

หลังจากติดตั้งเสร็จ ตรวจสอบสถานะ vault

kubectl exec vault-0 -n vault -- vault status

จะขึ้นผลลัพธ์ว่าถูก seal ไว้อยู่

Key                Value
---                -----
Seal Type          shamir
Initialized        false
Sealed             true
Total Shares       0
Threshold          0
Unseal Progress    0/0
Unseal Nonce       n/a
Version            1.17.2
Build Date         2024-07-05T15:19:12Z
Storage Type       consul
HA Enabled         true

ใช้ command ดังนี้เพื่อ generate root key ใช้ในการ bootstrap cluster ครั้งแรกเท่านั้น

kubectl exec vault-0 -n vault -- vault operator init -key-shares=1 -key-threshold=1 -format=json > cluster-keys.json

จากตัวอย่างจากกำหนด key 1 ตัวเท่านั้น เพื่อความง่าย

ใน enterprise อาจจะไม่ควรใส่ -key-shares=1 -key-threshold=1

ทำการ unseal vault server

VAULT_UNSEAL_KEY=$(cat cluster-keys.json | jq -r ".unseal_keys_b64[]")
kubectl exec vault-0 -n vault -- vault operator unseal $VAULT_UNSEAL_KEY

จะได้ผลลัพธ์ดังนี้

Key             Value
---             -----
Seal Type       shamir
Initialized     true
Sealed          false
Total Shares    1
Threshold       1
Version         1.17.2
Build Date      2024-07-05T15:19:12Z
Storage Type    consul
Cluster Name    vault-cluster-7e64d0dc
Cluster ID      05f0b39c-2def-5ce6-5597-71863db4864e
HA Enabled      true
HA Cluster      https://vault-0.vault-internal:8201
HA Mode         active
Active Since    2024-10-11T10:15:00.540042962Z

ทำ port-forward เพื่อทดสอบใช้งาน Vault

kubectl port-forward svc/vault-ui -n vault 8200:8200

ทดสอบ login ผ่าน UI ด้วย token ของ root

root token ได้มาจากตอน bootstrap cluster ด้วยคำสั่ง vault operator init

ดังนั้นสามารถหาได้จากคำสั่ง

cat cluster-keys.json |jq '.root_token'

PreviousFalcoNextCollaboration

Last updated 7 months ago

Was this helpful?

เข้า web browser ผ่าน

http://localhost:8200
consul for production