28 KiB
stage | group | info |
---|---|---|
Configure | Configure | To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments |
GitLab Kubernetes Agent (PREMIUM)
- Introduced in GitLab Premium 13.4.
- Introduced in GitLab 13.6,
grpcs
is supported.- Introduced in GitLab 13.10, KAS became available on GitLab.com under
wss://kas.gitlab.com
through an Early Adopter Program.- Introduced in GitLab 13.11, the GitLab Kubernetes Agent became available to every project on GitLab.com.
The GitLab Kubernetes Agent is an active in-cluster component for solving GitLab and Kubernetes integration tasks in a secure and cloud-native way. It enables:
- GitLab integration with a Kubernetes cluster behind a firewall or NAT (network address translation).
- Pull-based GitOps deployments.
- Inventory object to keep track of objects applied to your cluster.
- Real-time access to API endpoints in a cluster.
- Alert generation based on Container network policy.
- CI/CD Tunnel that enables users to access Kubernetes clusters from GitLab CI/CD jobs even if there is no network connectivity between GitLab Runner and a cluster.
Many more features are planned. Please review our roadmap and our development documentation.
GitLab Agent GitOps workflow
The GitLab Agent, herein Agent, uses multiple GitLab projects to provide a flexible workflow that can suit various needs. This diagram shows these repositories and the main actors involved in a deployment:
sequenceDiagram
participant D as Developer
participant A as Application code repository
participant M as Manifest repository
participant K as Kubernetes Agent
participant C as Agent configuration repository
loop Regularly
K-->>C: Grab the configuration
end
D->>+A: Pushing code changes
A->>M: Updating manifest
loop Regularly
K-->>M: Watching changes
M-->>K: Pulling and applying changes
end
There are several components that work in concert for the Agent to accomplish GitOps deployments:
- A properly-configured Kubernetes cluster where the Agent is running.
- A configuration repository that contains a
config.yaml
file, which tells the Agent the repositories to synchronize with the cluster. - A manifest repository that contains manifest files. Any changes to manifest files are applied to the cluster.
You can use the same GitLab project or projects for configuration and manifest files, as follows:
- Single GitLab project (recommended): when you use a single repository to hold both the manifest and the configuration files, these projects can be either private or public, as you prefer.
- Two GitLab projects: when you opt to use two different GitLab projects, one for manifest files, and another for configuration files, the manifests project must be public, while the configuration project can be either private or public. Our backlog contains issues for adding support for private manifest repositories outside of the configuration project and group level agents in the future.
For more details, please refer to our full architecture documentation in the Agent project.
Get started with GitOps and the GitLab Agent
The setup process involves a few steps to enable GitOps deployments:
- Set up the Kubernetes Agent Server for your GitLab instance.
- Define a configuration repository.
- Create an Agent record in GitLab.
- Install the Agent into the cluster.
- Generate and copy a Secret token used to connect to the Agent.
- Create manifest files.
Watch a GitLab 14.2 walking-through video with this process.
Upgrades and version compatibility
As the GitLab Kubernetes Agent is a new product, we are constantly adding new features
to it. As a result, while shipped features are production ready, its internal API is
neither stable nor versioned yet. For this reason, GitLab only guarantees compatibility
between corresponding major.minor (X.Y) versions of GitLab and its cluster side
component, agentk
.
Upgrade your agent installations together with GitLab upgrades. To decide which version of agentk
to install follow:
- Open the
GITLAB_KAS_VERSION
file from the GitLab Repository, which contains the latestagentk
version associated with themaster
branch. - Change the
master
branch and select the Git tag associated with your version. For instance, you could change it to GitLab v13.5.3-ee release
The available agentk
and kas
versions can be found in
the container registry.
Set up the Kubernetes Agent Server
Introduced in GitLab Premium 13.10, the GitLab Kubernetes Agent Server (KAS) became available on GitLab.com under
wss://kas.gitlab.com
.
To use the KAS:
- If you are a self-managed user, follow the instructions to install the Kubernetes Agent Server.
- If you are a GitLab.com user, when you set up the configuration repository for your agent, use
wss://kas.gitlab.com
as the--kas-address
.
Define a configuration repository
- Introduced in GitLab 13.7, the Agent manifest configuration can be added to multiple directories (or subdirectories) of its repository.
- Group authorization was introduced in GitLab 14.3.
To configure an Agent, you need:
- A GitLab repository to hold the configuration file.
- Install the Agent in a cluster.
After installed, when you update the configuration file, GitLab transmits the information to the cluster automatically without downtime.
In your repository, add the Agent configuration file under:
.gitlab/agents/<agent-name>/config.yaml
Your config.yaml
file specifies all configurations of the Agent, such as:
- The manifest projects to synchronize.
- The groups that can access this Agent via the CI/CD Tunnel.
- The address of the
hubble-relay
for the Network Security policy integrations.
As an example, a minimal Agent configuration that sets up only the manifest synchronizations is:
gitops:
manifest_projects:
- id: "path-to/your-manifest-project-1"
paths:
- glob: '/**/*.{yaml,yml,json}'
All the options for the Kubernetes Agent configuration repository are documented separately.
Create an Agent record in GitLab
Introduced in GitLab 14.1, you can create a new Agent record directly from the GitLab UI.
Next, create a GitLab Rails Agent record to associate it with the configuration repository project. Creating this record also creates a Secret needed to configure the Agent in subsequent steps.
In GitLab:
- From your project's sidebar, select Infrastructure > Kubernetes clusters.
- Select the GitLab Agent managed clusters tab.
- Select Integrate with the GitLab Agent.
- From the Select an Agent dropdown menu, select the Agent you want to connect and select Next to access the installation form.
- The form reveals your registration token. Securely store this secret token as you cannot view it again.
- Copy the command under Recommended installation method.
In your computer:
- Open your local terminal and connect to your cluster.
- Run the command you copied from the installation form.
Install the Agent into the cluster
To install the in-cluster component of the Agent, first you need to define a namespace. To create a new namespace,
for example, gitlab-kubernetes-agent
, run:
kubectl create namespace gitlab-kubernetes-agent
To perform a one-liner installation, run the command below. Make sure to replace:
your-agent-token
with the token received from the previous step (identified assecret
in the JSON output).gitlab-kubernetes-agent
with the namespace you defined in the previous step.wss://kas.gitlab.example.com
with the configured access of the Kubernetes Agent Server (KAS). For GitLab.com users, the KAS is available underwss://kas.gitlab.com
.--agent-version=vX.Y.Z
with the latest released patch version matching your GitLab installation's major and minor versions. For example, for GitLab v13.9.0, use--agent-version=v13.9.1
. You can find your GitLab version under the "Help/Help" menu.
docker run --pull=always --rm registry.gitlab.com/gitlab-org/cluster-integration/gitlab-agent/cli:stable generate --agent-token=your-agent-token --kas-address=wss://kas.gitlab.example.com --agent-version=vX.Y.Z --namespace gitlab-kubernetes-agent | kubectl apply -f -
WARNING:
--agent-version stable
can be used to refer to the latest stable release at the time when the command runs. It's fine for
testing purposes but for production please make sure to specify a matching version explicitly.
To find out the various options the above Docker container supports, run:
docker run --pull=always --rm registry.gitlab.com/gitlab-org/cluster-integration/gitlab-agent/cli:stable generate --help
Advanced installation
For more advanced configurations, we recommend to use the kpt
based installation method.
Otherwise, follow the manual installation steps described below.
Create the Kubernetes secret
After generating the token, you must apply it to the Kubernetes cluster.
To create your Secret, run:
kubectl create secret generic -n gitlab-kubernetes-agent gitlab-kubernetes-agent-token --from-literal=token='YOUR_AGENT_TOKEN'
The following example file contains the
Kubernetes resources required for the Agent to be installed. You can modify this
example resources.yml
file in the following ways:
- Replace
namespace: gitlab-kubernetes-agent
withnamespace: <YOUR-DESIRED-NAMESPACE>
. - You can configure
kas-address
(Kubernetes Agent Server) in several ways. The agent can use the WebSockets or gRPC protocols to connect to the Agent Server. Select the option appropriate for your cluster configuration and GitLab architecture:- The
wss
scheme (an encrypted WebSockets connection) is specified by default after you install thegitlab-kas
sub-chart, or enablegitlab-kas
for Omnibus GitLab. When using the sub-chart, you must setwss://kas.host.tld:443
askas-address
, wherehost.tld
is the domain you've setup for your GitLab installation. When using Omnibus GitLab, you must setwss://GitLab.host.tld:443/-/kubernetes-agent/
askas-address
, whereGitLab.host.tld
is your GitLab hostname. - When using the sub-chart, specify the
ws
scheme (such asws://kas.host.tld:80
) to use an unencrypted WebSockets connection. When using the Omnibus GitLab, specify thews
scheme (such asws://GitLab.host.tld:80/-/kubernetes-agent/
). - Specify the
grpc
scheme if both Agent and Server are installed in one cluster. In this case, you may specifykas-address
value asgrpc://gitlab-kas.<your-namespace>:8150
) to use gRPC directly, wheregitlab-kas
is the name of the service created bygitlab-kas
chart, and<your-namespace>
is the namespace where the chart was installed. - Specify the
grpcs
scheme to use an encrypted gRPC connection. - When deploying KAS through the GitLab chart, it's possible to customize the
kas-address
forwss
andws
schemes to whatever you need. Check the chart's KAS Ingress documentation to learn more about it. - In the near future, Omnibus GitLab intends to provision
gitlab-kas
under a sub-domain by default, instead of the/-/kubernetes-agent/
path. Please follow this issue for details.
- The
- If you defined your own secret name, replace
gitlab-kubernetes-agent-token
with your secret name in thesecretName:
section.
To apply this file, run the following command:
kubectl apply -n gitlab-kubernetes-agent -f ./resources.yml
To review your configuration, run the following command:
$ kubectl get pods -n gitlab-kubernetes-agent
NAMESPACE NAME READY STATUS RESTARTS AGE
gitlab-kubernetes-agent gitlab-kubernetes-agent-77689f7dcb-5skqk 1/1 Running 0 51s
Example resources.yml
file
---
apiVersion: v1
kind: Namespace
metadata:
name: gitlab-kubernetes-agent
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: gitlab-kubernetes-agent
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: gitlab-kubernetes-agent
spec:
replicas: 1
selector:
matchLabels:
app: gitlab-kubernetes-agent
template:
metadata:
labels:
app: gitlab-kubernetes-agent
spec:
serviceAccountName: gitlab-kubernetes-agent
containers:
- name: agent
# Make sure to specify a matching version for production
image: "registry.gitlab.com/gitlab-org/cluster-integration/gitlab-agent/agentk:vX.Y.Z
args:
- --token-file=/config/token
- --kas-address
- wss://kas.host.tld:443 # replace this line with the line below if using Omnibus GitLab or GitLab.com.
# - wss://gitlab.host.tld:443/-/kubernetes-agent/
# - wss://kas.gitlab.com # for GitLab.com users, use this KAS.
# - grpc://host.docker.internal:8150 # use this attribute when connecting from Docker.
volumeMounts:
- name: token-volume
mountPath: /config
volumes:
- name: token-volume
secret:
secretName: gitlab-kubernetes-agent-token
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: gitlab-kubernetes-agent-write
rules:
- resources:
- '*'
apiGroups:
- '*'
verbs:
- create
- update
- delete
- patch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: gitlab-kubernetes-agent-write-binding
roleRef:
name: gitlab-kubernetes-agent-write
kind: ClusterRole
apiGroup: rbac.authorization.k8s.io
subjects:
- name: gitlab-kubernetes-agent
kind: ServiceAccount
namespace: gitlab-kubernetes-agent
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: gitlab-kubernetes-agent-read
rules:
- resources:
- '*'
apiGroups:
- '*'
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: gitlab-kubernetes-agent-read-binding
roleRef:
name: gitlab-kubernetes-agent-read
kind: ClusterRole
apiGroup: rbac.authorization.k8s.io
subjects:
- name: gitlab-kubernetes-agent
kind: ServiceAccount
namespace: gitlab-kubernetes-agent
Create manifest files
In a previous step, you configured a config.yaml
to point to the GitLab projects
the Agent should synchronize. Agent monitors each of those projects for changes to the manifest files it contains. You can auto-generate manifest files with a
templating engine or other means.
The agent is authorized to download manifests for the configuration project, and public projects. Support for other private projects is planned in the issue Agent authorization for private manifest projects.
Each time you push a change to a monitored manifest repository, the Agent logs the change:
2020-09-15_14:09:04.87946 gitlab-k8s-agent : time="2020-09-15T10:09:04-04:00" level=info msg="Config: new commit" agent_id=1 commit_id=e6a3651f1faa2e928fe6120e254c122451be4eea
Example manifest file
This file creates a minimal ConfigMap
:
apiVersion: v1
kind: ConfigMap
metadata:
name: demo-map
namespace: gitlab-kubernetes-agent # Can be any namespace managed by you that the agent has access to.
data:
key: value
Example projects
The following example projects can help you get started with the Kubernetes Agent.
- Configuration repository
- This basic GitOps example deploys NGINX: Manifest repository
GitLab Runner Deployment with the Agent
You can use the Kubernetes Agent to deploy GitLab Runner in a Kubernetes cluster.
Kubernetes Network Security Alerts
The GitLab Agent also provides an integration with Cilium. This integration provides a simple way to generate network policy-related alerts and to surface those alerts in GitLab.
There are several components that work in concert for the Agent to generate the alerts:
- A working Kubernetes cluster.
- Cilium integration through either of these options:
- Installation through cluster management template.
- Enablement of hubble-relay on an existing installation.
- One or more network policies through any of these options:
- Use the Container Network Policy editor to create and manage policies.
- Use an AutoDevOps configuration.
- Add the required labels and annotations to existing network policies.
- A configuration repository with Cilium configured in
config.yaml
The setup process follows the same steps as GitOps, with the following differences:
- When you define a configuration repository, you must do so with Cilium settings.
- You do not need to specify the
gitops
configuration section.
Management interfaces
Users with at least the Developer can access the user interface for the GitLab Kubernetes agent at Infrastructure > Kubernetes clusters, under the GitLab Agent managed clusters tab. This page lists all registered agents for the current project, and the configuration directory for each agent:
Additional management interfaces are planned for the GitLab Kubernetes Agent. Provide more feedback in the related epic.
Remove the GitLab Kubernetes Agent
-
Remove an Agent record with GraphQL by deleting the
clusterAgent
and theclusterAgentToken
.mutation deleteAgent { clusterAgentDelete(input: { id: "<cluster-agent-id>" } ) { errors } } mutation deleteToken { clusterAgentTokenDelete(input: { id: "<cluster-agent-token-id>" }) { errors } }
-
Verify whether the removal occurred successfully. If the output in the Pod logs includes
unauthenticated
, it means that the agent was successfully removed:{ "level": "warn", "time": "2021-04-29T23:44:07.598Z", "msg": "GetConfiguration.Recv failed", "error": "rpc error: code = Unauthenticated desc = unauthenticated" }
-
Delete the GitLab Kubernetes Agent in your cluster:
kubectl delete -n gitlab-kubernetes-agent -f ./resources.yml
Troubleshooting
If you face any issues while using GitLab Kubernetes Agent, you can read the service logs with the following command
kubectl logs -f -l=app=gitlab-kubernetes-agent -n gitlab-kubernetes-agent
GitLab administrators can additionally view the Kubernetes Agent Server logs.
Agent logs
Transport: Error while dialing failed to WebSocket dial
{
"level": "warn",
"time": "2020-11-04T10:14:39.368Z",
"msg": "GetConfiguration failed",
"error": "rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing failed to WebSocket dial: failed to send handshake request: Get \\\"https://gitlab-kas:443/-/kubernetes-agent\\\": dial tcp: lookup gitlab-kas on 10.60.0.10:53: no such host\""
}
This error is shown if there are some connectivity issues between the address
specified as kas-address
, and your Agent pod. To fix it, make sure that you
specified the kas-address
correctly.
{
"level": "error",
"time": "2021-06-25T21:15:45.335Z",
"msg": "Reverse tunnel",
"mod_name": "reverse_tunnel",
"error": "Connect(): rpc error: code = Unavailable desc = connection error: desc= \"transport: Error while dialing failed to WebSocket dial: expected handshake response status code 101 but got 301\""
}
This error occurs if the kas-address
doesn't include a trailing slash. To fix it, make sure that the
wss
or ws
URL ends with a training slash, such as wss://GitLab.host.tld:443/-/kubernetes-agent/
or ws://GitLab.host.tld:80/-/kubernetes-agent/
.
ValidationError(Deployment.metadata)
{
"level": "info",
"time": "2020-10-30T08:56:54.329Z",
"msg": "Synced",
"project_id": "root/kas-manifest001",
"resource_key": "apps/Deployment/kas-test001/nginx-deployment",
"sync_result": "error validating data: [ValidationError(Deployment.metadata): unknown field \"replicas\" in io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta, ValidationError(Deployment.metadata): unknown field \"selector\" in io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta, ValidationError(Deployment.metadata): unknown field \"template\" in io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta]"
}
This error is shown if a manifest file is malformed, and Kubernetes can't create specified objects. Make sure that your manifest files are valid. You may try using them to create objects in Kubernetes directly for more troubleshooting.
Error while dialing failed to WebSocket dial: failed to send handshake request
{
"level": "warn",
"time": "2020-10-30T09:50:51.173Z",
"msg": "GetConfiguration failed",
"error": "rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing failed to WebSocket dial: failed to send handshake request: Get \\\"https://GitLabhost.tld:443/-/kubernetes-agent\\\": net/http: HTTP/1.x transport connection broken: malformed HTTP response \\\"\\\\x00\\\\x00\\\\x06\\\\x04\\\\x00\\\\x00\\\\x00\\\\x00\\\\x00\\\\x00\\\\x05\\\\x00\\\\x00@\\\\x00\\\"\""
}
This error is shown if you configured wss
as kas-address
on the agent side,
but KAS on the server side is not available via wss
. To fix it, make sure the
same schemes are configured on both sides.
It's not possible to set the grpc
scheme due to the issue
It is not possible to configure KAS to work with grpc
without directly editing GitLab KAS deployment. To use grpc
while the
issue is in progress, directly edit the deployment with the
kubectl edit deployment gitlab-kas
command, and change --listen-websocket=true
to --listen-websocket=false
. After running that command, you should be able to use
grpc://gitlab-kas.<YOUR-NAMESPACE>:8150
.
Decompressor is not installed for grpc-encoding
{
"level": "warn",
"time": "2020-11-05T05:25:46.916Z",
"msg": "GetConfiguration.Recv failed",
"error": "rpc error: code = Unimplemented desc = grpc: Decompressor is not installed for grpc-encoding \"gzip\""
}
This error is shown if the version of the agent is newer that the version of KAS.
To fix it, make sure that both agentk
and KAS use the same versions.
Certificate signed by unknown authority
{
"level": "error",
"time": "2021-02-25T07:22:37.158Z",
"msg": "Reverse tunnel",
"mod_name": "reverse_tunnel",
"error": "Connect(): rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing failed to WebSocket dial: failed to send handshake request: Get \\\"https://GitLabhost.tld:443/-/kubernetes-agent/\\\": x509: certificate signed by unknown authority\""
}
This error is shown if your GitLab instance is using a certificate signed by an internal CA that
is unknown to the agent. One approach to fixing it is to present the CA certificate file to the agent
via a Kubernetes configmap
and mount the file in the agent /etc/ssl/certs
directory from where it
will be picked up automatically.
For example, if your internal CA certificate is myCA.pem
:
kubectl -n gitlab-kubernetes-agent create configmap ca-pemstore --from-file=myCA.pem
Then in resources.yml
:
spec:
serviceAccountName: gitlab-kubernetes-agent
containers:
- name: agent
image: "registry.gitlab.com/gitlab-org/cluster-integration/gitlab-agent/agentk:<version>"
args:
- --token-file=/config/token
- --kas-address
- wss://kas.host.tld:443 # replace this line with the line below if using Omnibus GitLab or GitLab.com.
# - wss://gitlab.host.tld:443/-/kubernetes-agent/
# - wss://kas.gitlab.com # for GitLab.com users, use this KAS.
# - grpc://host.docker.internal:8150 # use this attribute when connecting from Docker.
volumeMounts:
- name: token-volume
mountPath: /config
- name: ca-pemstore-volume
mountPath: /etc/ssl/certs/myCA.pem
subPath: myCA.pem
volumes:
- name: token-volume
secret:
secretName: gitlab-kubernetes-agent-token
- name: ca-pemstore-volume
configMap:
name: ca-pemstore
items:
- key: myCA.pem
path: myCA.pem
Alternatively, you can mount the certificate file at a different location and include it using the
--ca-cert-file
agent parameter:
containers:
- name: agent
image: "registry.gitlab.com/gitlab-org/cluster-integration/gitlab-agent/agentk:<version>"
args:
- --ca-cert-file=/tmp/myCA.pem
- --token-file=/config/token
- --kas-address
- wss://kas.host.tld:443 # replace this line with the line below if using Omnibus GitLab or GitLab.com.
# - wss://gitlab.host.tld:443/-/kubernetes-agent/
# - wss://kas.gitlab.com # for GitLab.com users, use this KAS.
# - grpc://host.docker.internal:8150 # use this attribute when connecting from Docker.
volumeMounts:
- name: token-volume
mountPath: /config
- name: ca-pemstore-volume
mountPath: /tmp/myCA.pem
subPath: myCA.pem