# Deploy to Kubernetes, EKS included

> Give plinto a namespace of your cluster, EKS's or any other, and deploy a service there as a Docker image, its sidecars in its pods.

An environment can deploy to a namespace of a Kubernetes cluster instead of to hosts: an EKS cluster, another cloud's, or one you run yourself. plinto signs in as a service account of that namespace alone, and the cluster pulls your release's image from plinto's repository, so there's no registry to set up. The release has to be a [Docker image](docker-images.md), on Kubernetes 1.29 or later.

## 1. The account in your cluster {#account}

plinto deploys with a service account of its own, which may change Deployments and Secrets in one namespace and read its pods and their logs, nothing else:

```conf file=plinto-account.yaml
# What plinto deploys to a namespace with: a service account of its own in that namespace alone, which may change
# Deployments and Secrets there and read its pods and their logs, and a token for it, which Target accounts takes. Any
# cluster, EKS's included. Change every "plinto" to your namespace, then:
#
#   kubectl apply -f plinto-account.yaml
#   kubectl -n plinto get secret plinto -o jsonpath='{.data.token}' | base64 -d
apiVersion: v1
kind: Namespace
metadata:
  name: plinto
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: plinto
  namespace: plinto
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: plinto
  namespace: plinto
rules:
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get", "create", "update", "patch", "delete"]
  - apiGroups: [""]
    resources: ["pods", "pods/log"]
    verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: plinto
  namespace: plinto
subjects:
  - kind: ServiceAccount
    name: plinto
    namespace: plinto
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: plinto
---
# Its token, which doesn't expire: delete this Secret to revoke it, and apply it again for a new one.
apiVersion: v1
kind: Secret
metadata:
  name: plinto
  namespace: plinto
  annotations:
    kubernetes.io/service-account.name: plinto
type: kubernetes.io/service-account-token
```

Change every `plinto` to your namespace's name, then apply it and read its token:

```sh
$ kubectl apply -f plinto-account.yaml
$ kubectl -n plinto get secret plinto -o jsonpath='{.data.token}' | base64 -d
```

Deleting the Secret revokes the token. Apply the file again for a new one, then save it in plinto.

## 2. EKS {#eks}

An EKS cluster takes the same account. If you don't have a cluster yet, `eksctl` makes one, with its network and nodes, and points `kubectl` at it:

```sh
$ eksctl create cluster --name billing --region eu-north-1 --version 1.37 --managed --node-type t3.small --nodes 2
```

Choose a version in AWS's standard support: an older one costs more, in extended support. plinto has been checked on EKS 1.37.

> An account on AWS's Free Plan launches Free Tier instance types alone, such as `t3.small`. With another, the node group stays creating, with no instance and no error, until `eksctl` gives up after 25 minutes. A dry run doesn't show it; `RunInstances` in CloudTrail does.

Read its API server's address and certificate authority with the AWS CLI:

```sh
$ aws eks describe-cluster --name billing --query 'cluster.{endpoint: endpoint, ca: certificateAuthority.data}'
```

Two things the cluster needs:

| What | Why |
|---|---|
| its API endpoint public | plinto calls it from outside your VPC. You can limit public access to plinto's addresses |
| its nodes reaching `repository.plinto.build` | they pull the images from there. Nodes in private subnets need a NAT gateway |

## 3. The account in plinto {#target-account}

In **Environments**, **Target accounts** takes the cluster's API server (`https://…`), its CA (the PEM, or `certificate-authority-data` as a kubeconfig has it), the namespace and the token. Only your organization's admins add them. **Check** asks the cluster as the account: its version, and whether the account may read the namespace's Deployments.

plinto keeps the token sealed, and makes a pull token of the account's own, which reads only the images of the packages its environments deploy.

## 4. An environment on it {#environment}

An environment's **Target** is hosts or an account. Choose the account, and its next deployment goes there. Its deploy settings mean what they mean for a cluster:

| Setting | On Kubernetes |
|---|---|
| `replicas` | how many pods, 1 unset |
| `hosts-at-once` | how many pods an update starts beyond them; none is taken down before its replacement is ready |
| `health-url` | the app's readiness probe, as the container sees itself: `http://localhost:8080/health` |
| `health-timeout` | with a minute more, how long an update has before it is rolled back |
| `ports` | the app's container ports |
| `stop-timeout` | each container's share of the pod's grace period |

The configuration's plain values are in the Deployment; its secrets are in a Secret it refers to.

## 5. Sidecars in the pod {#sidecars}

Each [sidecar](../concepts/sidecars.md) the environment names runs in every pod, as a native sidecar: it starts before the app and stops after it. The app and its sidecars reach each other at `localhost`, and share files through `/shared` (`PLINTO_SHARED`). A sidecar's `cpu` and `memory` are its container's, such as `100m` and `64Mi`, and the pod's size adds them up. Sidecars on Kubernetes are images too. Any change, to the app or to a sidecar, restarts the whole pod.

## 6. What a deployment does {#deployment}

Its page has one row, the account's, in four steps:

1. **Store the secrets**: the pull secret and the configuration's secrets.
2. **Apply the Deployment**: each image by its digest, as the registry gives it, so the cluster runs exactly that release.
3. **Wait for its new pods**, until every one is ready. The last lines of each new pod's log go into the deployment's log.
4. **Check its pods.**

An update that doesn't finish in time is rolled back: the Deployment before it is applied again, and the deployment fails saying so. Deleting the environment, or moving it to hosts or to another account, takes its Deployment and Secrets out of the namespace.

## 7. Reaching your service {#reach}

plinto makes a Deployment and its Secrets, and no Service, so until you add one nothing outside the cluster reaches your pods. How traffic gets in is yours to choose: a Service, an Ingress, or what your cluster already has.

The Deployment and its pods' `app` label are named after the environment, in lowercase with `-` for anything else: `Billing/prod` is `billing-prod`. A Service selects on that label, and gives the pods one address:

```conf file=billing-service.yaml
# One address for the pods of the environment Billing/prod. Apply it as yourself: plinto's account can't touch Services.
apiVersion: v1
kind: Service
metadata:
  name: billing
  namespace: plinto
spec:
  type: LoadBalancer
  selector:
    app: billing-prod
  ports:
    - port: 80
      targetPort: 8080
```

```sh
$ kubectl apply -f billing-service.yaml
$ kubectl -n plinto get service billing
```

Its address is under `EXTERNAL-IP`. On EKS, `type: LoadBalancer` makes an AWS load balancer, which AWS bills you for, and that column has its name.

Only ready pods get traffic, as `health-url` decides, and each release's pods come up behind the same address. The Service is yours: deployments don't touch it, and taking the environment off the cluster leaves it there with no pods behind it, so delete it yourself.

To reach the pods from your own machine without a Service:

```sh
$ kubectl -n plinto port-forward deployment/billing-prod 8080:8080
$ curl http://localhost:8080/
```

A port-forward goes to one pod, and ends when that pod does.

## 8. Pipelines and rollbacks {#pipeline}

An environment on an account works like any other. A deploying stage of a [pipeline](../concepts/pipelines.md) deploys to it, so a commit becomes a release and then a deployment in the cluster with nobody starting it. [**Roll back to this**](../concepts/deployments.md#rollback) deploys an earlier release again, as a new deployment.

- [A Docker image](docker-images.md): `packager = Docker`, and bases of your own
- [Sidecars](../concepts/sidecars.md): what a sidecar is
- [Deployments](../concepts/deployments.md): what each deployment records
- [Pipelines](../concepts/pipelines.md): from a commit to a deployment
