Site Tools


techsupport:kube3760

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
techsupport:kube3760 [2022/11/30 19:37] kjohns23techsupport:kube3760 [2023/03/15 17:20] (current) kjohns23
Line 1: Line 1:
-===== Gitlab and Kubernetes in CIS*3760 F23 =====+==== Gitlab and Kubernetes in CIS*3760 F23 ====
  
-This is a guide to assist students in CIS*3760 with the process of deploying an application developed on the SoCS Gitlab Server to our Kubernetes cluster. Each project group will be provided with their own server running RKE2 Kubernetes, on which to deploy. The server will be located at cis3760-XX where XX is your group number.+This is a guide to assist students in CIS*3760 with the process of deploying an application developed on the SoCS Gitlab Server to our Kubernetes cluster. Each project group will be provided with their own server running RKE2 Kubernetes, on which to deploy. The server will be located at cis3760-XX.socs.uoguelph.ca where XX is your group number.
  
 ===== How To Interact With Kubernetes ==== ===== How To Interact With Kubernetes ====
Line 15: Line 15:
 If you prefer to interact with Kubernetes directly from your own machine, the kubectl command can be installed directly on your local machine.  If you prefer to interact with Kubernetes directly from your own machine, the kubectl command can be installed directly on your local machine. 
  
-Follow the instructions for your Operating system from the [[ https://kubernetes.io/docs/tasks/tools/ | official Kubernetes documentation]].+Follow the instructions for your Operating system from the [[ https://kubernetes.io/docs/tasks/tools/|official Kubernetes documentation]].
  
-From the [[ https://kubefarm.socs.uoguelph.ca/ | web interface ]], open the hamburger menu at the top left, and select your cluster listed under Explore Cluster. Select Download KubeConfig from the top right menu on this page. The icon looks like a sheet of paper. You can then copy it to your .kube folder which was created in the installation process.+From the [[ https://kubefarm.socs.uoguelph.ca/ |web interface ]], open the hamburger menu at the top left, and select your cluster listed under Explore Cluster. Select Download KubeConfig from the top right menu on this page. The icon looks like a sheet of paper. You can then copy it to your .kube folder which was created in the installation process.
  
 Unfortunately, due to the current lack of VPN access, this method will only work from off campus, or if you proxy your connection through portkey. Directions to do this are beyond the scope of this guide.  Unfortunately, due to the current lack of VPN access, this method will only work from off campus, or if you proxy your connection through portkey. Directions to do this are beyond the scope of this guide. 
Line 23: Line 23:
 ==== Building your Application Container in Gitlab ==== ==== Building your Application Container in Gitlab ====
  
-Guide to use kaniko here.+Kaniko is a tool that allows you to build your Docker container automatically as part of a CI/CD Pipeline
  
 +1. As a prerequisite your Dockerfile containing all of your container build information must be located in the root of your git repository. If you want to place your Dockerfile in a subdirectory you will have to edit the --dockerfile line in the below configuration to specify the new location.
  
-1You will need to host your Docker containerIf the container can be publicly accessible, this can be hosted in Docker HubIf it must be private, the SoCS Gitlab installation has it's own Docker Container RegistryTo access the SoCS Registryuse ''%% docker login registry.uoguelph.ca %%'' and enter your central login/Gitlab credentialsDirections to tag and push can be found in your Gitlab project under Packages & Registries > Container Registry.+2Add the following lines to your .gitlab-ci.yml in the root of your Gitlab project repository. 
 + 
 +<code> 
 +stages:          # List of stages for jobsand their order of execution 
 +  - build 
 + 
 +build: 
 +  stage: build 
 +  image: 
 +    name: gcr.io/kaniko-project/executor:v1.9.0-debug 
 +    entrypoint: [""
 +  script: 
 +    - /kaniko/executor 
 +      --context "${CI_PROJECT_DIR}" 
 +      --dockerfile "${CI_PROJECT_DIR}/Dockerfile" 
 +      --destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}" 
 +</code> 
 + 
 +Once your container is built it needs to be hosted in a registry to be made available for download. Using the previous configuration file your container is automatically pushed to the Docker Repository integrated into the SoCS Gitlab Server.
  
 ==== Integrating Gitlab and Kubernetes with Gitlab Agent ==== ==== Integrating Gitlab and Kubernetes with Gitlab Agent ====
Line 39: Line 58:
     - id: "<Your Project ID>"     - id: "<Your Project ID>"
       paths:       paths:
-      - glob: '/manifests/*.{yaml,yml,json}'+      - glob: '/manifests/*.{yaml,yml}'
 </code> </code>
  
Line 68: Line 87:
 </code> </code>
  
-Next, run the following command on your kubernetes server to install the gitlab agent in kubernetes and automatically connect it to your gitlab project.  +Next, run the following command on your kubernetes server, specifying your token from step 3, to install the gitlab agent in kubernetes and automatically connect it to your gitlab project.  
 +<code>helm repo add gitlab https://charts.gitlab.io</code>
 <code> helm upgrade --install gitlab-agent gitlab/gitlab-agent --set config.token='TOKEN_FROM_STEP_THREE' --namespace=gitlab-agent -f values.yml </code> <code> helm upgrade --install gitlab-agent gitlab/gitlab-agent --set config.token='TOKEN_FROM_STEP_THREE' --namespace=gitlab-agent -f values.yml </code>
  
-8. In your Gitlab Project go to Settings > Registry > Deploy Tokens. Enter a name to label the token, a username, and check read_registry+==== Deploying your Application ====
  
-9Run the following command, substitution the username you entered in the previous step for DOCKER_USER, the token generated for DOCKER_PASSWORD, and an email address of one of the project members for DOCKER_EMAIL<codekubectl create secret docker-registry <name--docker-server=registry.socs.uoguelph.ca --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL </code>+1Kubernetes needs to be able to connect to the Gitlab Docker RegistryIn your Gitlab Project go to Settings Repository Deploy TokensEnter a name to label the token, a username, and check read_registry
  
-The gitlab agent is now set up and can pull your container from the SoCS container registry.+2. Run the following command, substituting the username you entered in the previous step for DOCKER_USER, the token generated for DOCKER_PASSWORD, and an email address of one of the project members for DOCKER_EMAIL. <code> kubectl create secret docker-registry regcred --namespace=infovis --docker-server=registry.socs.uoguelph.ca --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL </code>
  
-==== Deploying your Application ====+3. A Manifest file needs to be defined in Gitlab. This is a yaml file that lists the kubernetes specifications required to deploy your application within Kubernetes. A sample mainefest file is available in the code block below, and should be placed in manifests/manifest.yml in your Gitlab project repository. The "image:" line will have to be modified with your container URL, and the "host" line will need to be changed to your server URL. The container URL can be found in Gitlab from Packages & Registries > Container Registry. 
 + 
 +<code> 
 +apiVersion: apps/v1 
 +kind: Deployment 
 +metadata: 
 +  name: infovis-deployment 
 +  namespace: infovis  # Can be any namespace managed by you that the agent has access to. 
 +spec: 
 +  selector: 
 +    matchLabels: 
 +      app: infovis 
 +  replicas: 1 
 +  template: 
 +    metadata: 
 +      labels: 
 +        app: infovis 
 +    spec: 
 +      containers: 
 +      - name: nginx-custom 
 +        image: registry.socs.uoguelph.ca/cis3760w23/group1/infovis:latest 
 +      imagePullSecrets: 
 +      - name: regcred 
 + 
 +--- 
 +apiVersion: v1 
 +kind: Service 
 +metadata: 
 +  annotations: 
 +  labels: 
 +    app: infovis 
 +  name: infovis-service 
 +  namespace: infovis 
 +spec: 
 +  ports: 
 +  - name: "80" 
 +    port: 80 
 +    targetPort: 80 
 +  selector: 
 +    app: infovis 
 + 
 +--- 
 +apiVersion: networking.k8s.io/v1 
 +kind: Ingress 
 +metadata: 
 +  name: infovis-ingress 
 +  namespace: infovis 
 +spec: 
 +  rules: 
 +  - host: cis3760-99.socs.uoguelph.ca 
 +    http: 
 +      paths: 
 +      - path: / 
 +        pathType: Prefix 
 +        backend: 
 +          service: 
 +            name: infovis-service 
 +            port: 
 +              number: 80 
 +  ingressClassName: nginx 
 +</code> 
 + 
 +==== Conclusion ==== 
 + 
 +All the pieces are now in place to deploy your application automatically to your gitlab server. You can view your application by connecting to your cis3760xx.socs.uoguelph.ca server in a web browser. Each time your container is updated within Gitlab, the agent will automatically pull the latest version to the kubernetes server. 
  
-Look at official documentation TODO add instructions on how to do this. TODO maybe add a sample repository.+==== Graphical Representation ====
  
-The yaml file should be placed in your project git repository.+The following image is a graphical representation of the SoCS Gitlab and Kubernetes Setup and how the pieces fit together and relate to each other.
  
 +{{:techsupport:gitlab-k8-agent-schematic.png|}}
techsupport/kube3760.1669837055.txt.gz · Last modified: 2022/11/30 19:37 by kjohns23