Friday, December 10, 2021

Deploy flux to kubernetes using gitlab

Flux installation: installation: docs.fluxcd.io/en/stable/references/daemon.html

 commands used: https://github.com/thunderbirds-2021/content-gitops/blob/master/docs/Commands.md



Installing and Configuring Flux with GitLab

Introduction

This lab is for people who do not utilize GitHub, and prefer to use GitLab or some other VCS repository manager with Flux. This lab walks through installing Flux into a Kubernetes cluster, and connecting it to a repository on GitLab.

Set Up A Repository (Project) In GitLab

We need to have a GitLab account, and we've got to set up a repository within that account. The repository should contain two files that are Kubernetes YAML. The first, within the namespaces folder, will create a namespace for the application. The YAML is as follows:

apiVersion: v1
kind: Namespace
metadata:
  labels:
    name: laflux
  name: laflux

The second file is for creating the actual deployment of an NGINX server, and it should probably go in the workloads folder. That YAML is as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: laflux
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

Establish a Terminal Session on the Kubernetes Master and Install Flux

We'll use the credentials on the hands-on lab overview page to log into the Kubernetes host server as cloud_user:

ssh cloud_user@[IP Address Here]

Once we are in the server, we'll make sure Flux is installed:

$ fluxctl version

We should get response of unversioned, which is fine. Now we can make sure our Kubernetes cluster was spun up, and then look at some more details:

$ kubectl get nodes
$ kubectl get pods --all-namespaces

Before we can run Flux, we have to create a namespace for it:

$ kubectl create namespace flux

Then we've got to set the GLUSER environment variable, and check afterward to make sure it was set:

$ export GLUSER=[your GitLab username]
$ env | grep GL

Now we can run Flux:

$ fluxctl install \
--git-user=${GLUSER} \
--git-email=${GLUSER}@gmail.com \
--git-url=git@gitlab.com:${GLUSER}/flux-sample \
--git-path=namespaces,workloads \
--namespace=flux | kubectl apply -f -

We will check on the deployment with the following command:

$ kubectl -n flux rollout status deployment/flux

Obtain the RSA Key Created by fluxctl, and Grant GitLab Write Permission to the Cluster

If everything rolled out properly, we can get the RSA key that was created by the Flux install procedure:

$ fluxctl identity --k8s-fwd-ns flux

Copy that RSA key, then let's head back into GitLab. Go to Settings in the main menu, then click on SSH Keys in the left pane. Paste our key into the Key field, and give it a Title of something like flux identity. Now we can click the Add key button.

Use the fluxctl sync Command to Synchronize the Cluster

After the GitLab account has been granted write access to the Cluster, we can use fluxctl sync to apply the YAML from the repository:

$ fluxctl sync --k8s-fwd-ns flux

Once the sync command has run, we may check that the namespace has been created and that the NGINX deployment has been applied and is running:

$ kubectl get pods --all-namespaces

No comments:

Post a Comment