# Installing Loki


[Loki](https://grafana.com/oss/loki/) is a horizontally scalable, highly available, multi-tenant log aggregation and storage system. It indexes a set of labels for each log stream rather than the contents of logs.

## Getting started {#before-you-begin}

1. If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

1. [Make sure](../connect/security-groups.md) the security groups for the Managed Service for Kubernetes cluster and its node groups are configured correctly. If a rule is missing, [add it](../../../vpc/operations/security-group-add-rule.md).

    {% note warning %}
    
    The configuration of security groups determines performance and availability of the cluster and the services and applications running in it.
    
    {% endnote %}

1. Get ready to install Loki:

    1. [Create a service account](../../../iam/operations/sa/create.md) with the `storage.uploader` and `storage.viewer` [roles](../../../iam/concepts/access-control/roles.md). You need it to access [Yandex Object Storage](../../../storage/index.md).
    1. [Create a static access key](../../../iam/operations/authentication/manage-access-keys.md#create-access-key) for your [service account](../../../iam/concepts/users/service-accounts.md) in JSON format:

        ```bash
        yc iam access-key create \
          --service-account-name=<service_account_name> \
          --format=json > sa-key.json
        ```

    1. [Create a bucket](../../../storage/operations/buckets/create.md) with restricted access in Object Storage.

## Installation from Yandex Cloud Marketplace {#marketplace-install}

1. In the [management console](https://console.yandex.cloud), select a folder.
1. Navigate to **Managed Service for&nbsp;Kubernetes**.
1. Click the name of the [Managed Service for Kubernetes cluster](../../concepts/index.md#kubernetes-cluster) you need and select the ![image](../../../_assets/console-icons/shopping-cart.svg) **Marketplace** tab.
1. Under **Application available for installation**, select [Loki](https://yandex.cloud/en/marketplace/products/yc/loki) and click **Go to install**.
1. Configure the application:

   * **Namespace**: Create a new [namespace](../../concepts/index.md#namespace), e.g., `loki-space`. If you leave the default namespace, Loki may work incorrectly.
   * **Application name**: Specify the application name.
   * **Bucket name**: Specify the name of the [bucket](../../../storage/concepts/bucket.md) in Object Storage.
   * **Static access key**: Paste the contents of the `sa-key.json` file.
   * **Install Promtail**: Leave the option enabled to deliver local logs to the Grafana Loki instance using the [Promtail agent](https://grafana.com/docs/loki/latest/clients/promtail/). This agent is used for applications that require regular monitoring.

1. Click **Install**.
1. Wait for the application status to change to `Deployed`.

## Installation using a Helm chart {#helm-install}

1. [Install Helm](https://helm.sh/docs/intro/install/) v3.8.0 or higher.

1. [Install kubect](https://kubernetes.io/docs/tasks/tools/install-kubectl) and [configure it to work with the new cluster](../connect/index.md#kubectl-connect).

1. To install a [Helm chart](https://helm.sh/docs/topics/charts/) with Loki, run the following command, specifying the parameters of the resources you created [earlier](#before-you-begin):

    ```bash
    helm pull oci://cr.yandex/yc-marketplace/yandex-cloud/grafana/loki/chart/loki \
      --version 1.2.0-7 \
      --untar && \
    helm install \
      --namespace <namespace> \
      --create-namespace \
      --set global.bucketname=<bucket_name> \
      --set-file global.serviceaccountawskeyvalue=<path_to_sa-key.json> \
      loki ./loki/
    ```

    If you set `namespace` to the default namespace, Loki may work incorrectly. We recommend specifying a value different from all the existing namespaces, e.g., `loki-space`.

    {% note info %}
    
    If you are using a Helm version below 3.8.0, add the `export HELM_EXPERIMENTAL_OCI=1 && \` string at the beginning of the command to enable [Open Container Initiative](https://opencontainers.org/) (OCI) support in the Helm client.
    
    {% endnote %}

1. Make sure all Loki pods switched to `Running`:

    ```bash
    kubectl get pods -A -l "app.kubernetes.io/instance=loki"
    ```

## Connecting to Loki {#loki-connect}

Once deployed, Loki is available within the Managed Service for Kubernetes cluster at the following address:

```text
http://<Loki_gateway_service_name>.<namespace>.svc.cluster.local
```

To find out the namespace and name of the Loki gateway service, run this command:

```bash
kubectl get service -A | grep distributed-gateway
```

Result:

```text
test-namespace   loki-loki-distributed-gateway   ClusterIP   10.96.168.88   <none>   80/TCP    15m
```

## See also {#see-also}

* [Grafana Loki documentation](https://grafana.com/docs/loki/latest/)