Build localstack on local k8s cluster

Localstack on local k8s cluster

  1. Set up your Kubernetes cluster:

    • Install a Kubernetes cluster locally. Follow the respective documentation for installation instructions.
  2. Deploy LocalStack to your Kubernetes cluster:

    • Create a Kubernetes deployment YAML file, for example, localstack-deployment.yaml with the following contents:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      apiVersion: apps/v1
      kind: Deployment
      metadata:
      name: localstack
      spec:
      replicas: 1
      selector:
      matchLabels:
      app: localstack
      template:
      metadata:
      labels:
      app: localstack
      spec:
      containers:
      - name: localstack
      image: localstack/localstack
      ports:
      - containerPort: 4566
      - containerPort: 4571
      env:
      - name: SERVICES
      value: "ec2,apigateway,lambda,s3,sns,dynamodb,route53,cloudwatch"
    • Apply the deployment to your Kubernetes cluster: kubectl apply -f localstack-deployment.yaml.

  3. Expose LocalStack for access:

    • Create a Kubernetes service YAML file, for example, localstack-service.yaml with the following contents:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      apiVersion: v1
      kind: Service
      metadata:
      name: localstack-service
      spec:
      selector:
      app: localstack
      ports:
      - name: port-4566
      protocol: TCP
      port: 4566
      targetPort: 4566
      - name: port-4571
      protocol: TCP
      port: 4571
      targetPort: 4571
      type: NodePort
    • Apply the service to your Kubernetes cluster: kubectl apply -f localstack-service.yaml.

  4. Access LocalStack services:

    • Find the NodePort assigned to the LocalStack service: kubectl get services.

    • Use the NodePort to access LocalStack services. For example, for S3, the endpoint would be http://<cluster-ip>:<node-port>/.

    • Test the S3 service with AWS CLI

      • Install AWS CLI on centos7

        1
        sudo yum install awscli -y
      • Configure AK and SK (Using example AK and SK is good enough )

        1
        2
        3
        4
        5
        [root@centos-0 ansible]# aws configure
        AWS Access Key ID [None]: EXAMPLEACCESSKEY
        AWS Secret Access Key [None]: EXAMPLEACCESSKEY
        Default region name [None]:
        Default output format [None]:
      • Create the s3 bucket

        1
        2
        3
        4
        [root@centos-0 ansible]# aws --endpoint-url=http://10.52.184.221:32661 s3api create-bucket --bucket test
        {
        "Location": "/test"
        }
      • List the s3 bucket

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        [root@centos-0 ansible]# aws --endpoint-url=http://10.52.184.221:32661 s3api list-buckets
        {
        "Owner": {
        "DisplayName": "webfile",
        "ID": "75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a"
        },
        "Buckets": [
        {
        "CreationDate": "2023-12-10T15:22:27.000Z",
        "Name": "test"
        }
        ]
        }
  5. Configure your applications to use LocalStack:

    • Update your application’s AWS SDK or AWS CLI configuration to point to the LocalStack endpoints. For example, set the endpoint for S3 to http://<cluster-ip>:<node-port>/ in your code or AWS CLI configuration.

Build localstack on local k8s cluster
https://blog.excelsre.com/2023/12/10/build-localstack-on-local-k8s-cluster/
作者
Felix Yang
发布于
2023年12月10日
许可协议