如何将 Akka 集群部署到 Kubernetes?

How to deploy Akka Cluster to Kubernetes?

我编写了支持使用 Akka Cluster 进行集群的小型应用程序。我想将它部署到支持缩放的 Kubernetes。这是我的 docker-compose.yml 文件:

version: '3.5'

networks:
  cluster-network:

services:
  seed:
    networks:
      - cluster-network
    image: akka-cluster-server
    ports:
      - '2552:2552'
      - '8000:8000'
    environment:
      SERVER_IP: 0.0.0.0
      CLUSTER_IP: seed
      CLUSTER_SEED_IP: seed

  node1:
    networks:
      - cluster-network
    image: akka-cluster-server
    ports:
      - '8001:8000'
    environment:
      SERVER_IP: 0.0.0.0
      CLUSTER_IP: node1
      CLUSTER_PORT: 1600
      CLUSTER_SEED_IP: seed
      CLUSTER_SEED_PORT: 2552

  node2:
    networks:
      - cluster-network
    image: akka-cluster-server
    ports:
      - '8002:8000'
    environment:
      SERVER_IP: 0.0.0.0
      CLUSTER_IP: node2
      CLUSTER_PORT: 1600
      CLUSTER_SEED_IP: seed
      CLUSTER_SEED_PORT: 2552

三个实例共享网络。请注意 node1node2 使用 seed 的 DNS 名称进行连接。我写了简单的部署和服务。

部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: akka-cluster
spec:
  selector:
    matchLabels:
      app: akka-cluster
  replicas: 2
  template:
    metadata:
      labels:
        app: akka-cluster
    spec:
      containers:
      - name: server-seed
        image: akka-cluster-server
        imagePullPolicy: Never
        ports:
        - containerPort: 8000
        - containerPort: 2552
        env:
        - name: SERVER_IP
          value: 0.0.0.0
        - name: CLUSTER_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: CLUSTER_SEED_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: CLUSTER_PORT
          value: "1600"
        - name: CLUSTER_SEED_PORT
          value: "2552"

服务:

apiVersion: v1
kind: Service
metadata:
  name: akka-cluster-service
spec:
  type: LoadBalancer
  selector:
    app: akka-cluster
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8000

请注意,在这种情况下,当您扩展副本时,容器将连接到自身 (CLUSTER_SEED_IP) 而不是种子节点。

如何正确地将我的 docker-compose.yml 翻译成 Kubernetes deployment/service?

要"translate" docker-正确组合,您应该创建两组部署+服务:一组用于种子,另一组用于节点。

如果您需要将所有实例列为种子,那么可能更好的方法是通过 statefulset 来实现。你可以检查这个,例如 https://github.com/IBM/Akka-cluster-deploy-kubernetes/blob/master/deploy/kubernetes/resources/myapp/myapp-statefulset.json