diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..07c4661ad663ac2ea1979c3efedede050df47c1a --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,126 @@ +# +# Example CI/CD pipeline (file .gitlab-ci.yaml) for build and manage simple one-container application +# Please modify code below according corresponding comments and copy it to your custom .gitlab-ci.yaml file +# +# Tag description: +# REQUIRED - job required for build and deploy containers +# OPTIONAL - job useful for manage environment and K8s resources +# EXAMPLE - exaple job showing how to perform some task - after modification from corresponding comments +# change-this-value - be sure to set the values marked with this tag + +variables: + DEV_ENVIROMENT: "development" #change-this-value : The name of environment created in GitLab project - see https://docs.gitlab.com/ee/ci/environments/ + APP_NAME: ps-k8s-app #change-this-value : Your app name - value used in file k8s-manifests/k8s.templ.yml + APP_URL: "pawel.test.software.geant.org" #change-this-value : The URL for your application. This include your application name you provide in request ticket. Full URL will be provided by administrator + +stages: + - build + - deploy + - mgmt + - post_deploy + +#REQUIRED Job for building your image based on Dockerfile in main repo directory. Please do not change code below except parameters/values mentioned in corresponding comments +build_app: #This is an example job name. Please change it change according to your needs + stage: build #This is an example stage name. Please change it change according to your needs + image: + name: gcr.io/kaniko-project/executor:debug + entrypoint: [""] + script: + - echo $CI_REGISTRY + - echo $CI_PROJECT_DIR + - echo $CI_PROJECT_DIR/Dockerfile + - echo $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA + - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json + - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA #You can change --dockerfile and --destination CI_COMMIT_SHA for custom image tag + tags: + - ${RUNNER_NAME} + +#REQUIRED Job for deploy your app to kK8s cluster based on k8s-manifests/k8s.templ.yml manifests. Please do not change code below except parameters/values mentioned in corresponding comments +deploy_dev: #This is an example job name. Please change it change according to your needs + stage: deploy #This is an example stage name. Please change it change according to your needs + image: ${KUBECTL_IMAGE} + script: + - envsubst < k8s-manifests/kubeconfig.templ.yml > kubeconfig.yml + - envsubst < k8s-manifests/k8s.templ.yml > deployment.yml + - kubectl apply -f deployment.yml + - kubectl get pods + artifacts: + expire_in: 1h + paths: + - deployment.yml + environment: #You can remove 'environment' section if no environment is used + name: ${DEV_ENVIROMENT} + url: "http://${APP_URL}" + on_stop: stop_environment + #rules: + # - when: never + tags: + - ${RUNNER_NAME} + +#EXAMPLE job for operations inside running containers. Please do not change code below except parameters/values mentioned in corresponding comments +copy_static_cntent: #This is an example job name. Please change it change according to your needs + stage: post_deploy #This is an example stage name. Please change it change according to your needs + image: ${KUBECTL_IMAGE} + script: + - envsubst < k8s-manifests/kubeconfig.templ.yml > kubeconfig.yml + - APP_POD=`kubectl get pod | awk -v patt=${APP_NAME} '$1 ~ patt {print $1}'` + - echo $APP_POD + - kubectl cp ./../static-html/index2.html $APP_POD:/tmp/ #Example line - copies file from repository to container + - kubectl exec $APP_POD -- /bin/cp /tmp/index2.html /usr/share/nginx/html/index.html #Example line for manipulation inside container - copies files inside container. Command executes inside container starts after '--' + allow_failure: true + tags: + - ${RUNNER_NAME} + rules: + # - when: never + - when: manual + +#OPTIONAL job for delete deployment - it deletes only container (deployment and pods) not services, persistent volumes. etc. Please do not change code below except parameters/values mentioned in corresponding comments +stop_environment: #This is an example job name. Please change it change according to your needs + stage: mgmt #This is an example stage name. Please change it change according to your needs + image: $KUBECTL_IMAGE + script: + - envsubst < k8s-manifests/kubeconfig.templ.yml > kubeconfig.yml + - kubectl delete deployments --all + environment: #You can remove 'environment' section if no environment is used + name: ${DEV_ENVIROMENT} + action: stop + allow_failure: true + rules: + - when: manual + tags: + - ${RUNNER_NAME} + +#OPTIONAL job for destroy whole environment - it deletes all Kubernetes resouces. Please do not change code below except parameters/values mentioned in corresponding comments +destroy_environment: #This is an example job name. Please change it change according to your needs + stage: mgmt #This is an example stage name. Please change it change according to your needs + image: ${KUBECTL_IMAGE} + script: + - envsubst < k8s-manifests/kubeconfig.templ.yml > kubeconfig.yml + - kubectl delete deployments --all + - kubectl delete persistentvolumeclaims --all + - kubectl delete persistentvolume --all + - kubectl delete mappings.getambassador.io --all + - kubectl delete services --all + environment: #You can remove 'environment' section if no environment is used + name: ${DEV_ENVIROMENT} + action: stop + allow_failure: true + rules: + - when: manual + tags: + - ${RUNNER_NAME} + +#EXAMPLE job for manage your K8s resources - here for obtaining pods status. Please do not change code below except parameters/values mentioned in corresponding comments +#You can use this job to perform any 'kubectl' tasks +get_pods_info: #This is an example job name. Please change it change according to your needs + stage: mgmt #This is an example stage name. Please change it change according to your needs + image: ${KUBECTL_IMAGE} + script: + - envsubst < k8s-manifests/kubeconfig.templ.yml > kubeconfig.yml + - kubectl get pods #Here you can use any kubectl command to manage your K8s resources + allow_failure: true + rules: + - when: manual + tags: + - ${RUNNER_NAME} + diff --git a/README.md b/README.md deleted file mode 100644 index 40313eb0c5476de61f9e8bc62c30857f1d1e5725..0000000000000000000000000000000000000000 --- a/README.md +++ /dev/null @@ -1,92 +0,0 @@ -# K8s Templates - - - -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://gitlab.geant.org/gitlab-ci-templates/k8s-templates.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://gitlab.geant.org/gitlab-ci-templates/k8s-templates/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. diff --git a/k8s-manifests/k8s.templ.yml b/k8s-manifests/k8s.templ.yml new file mode 100644 index 0000000000000000000000000000000000000000..fe7b36fad05db1fe2d9577257cc334b99a351982 --- /dev/null +++ b/k8s-manifests/k8s.templ.yml @@ -0,0 +1,92 @@ +# +# This is the main mainfest file used deploy your container (containers). Please read this file carefully and adjust all necessary values according to your needs. Then place this file in your in ./k8s-manifests/ directory. +# Example manifests for one-container application. In case you want deploy more containers you have to add each REQUIRED manifest (except for Mapping) +# +# Tag description: +# REQUIRED - at last one instance of manifest is required +# OPTIONAL - manifest reqiured only in some cases + +#REQUIRED +apiVersion: v1 +kind: Service +metadata: + name: ${APP_NAME} #Do not change value '${APP_NAME} ' except you have more then one Service - in such a case please append appropriate suffix, for example ${APP_NAME}-db_container +spec: + type: ClusterIP + selector: + appsvc: nginx + ports: + - name: "tcp-8080" + protocol: TCP + port: 8080 + targetPort: 80 + +--- +#REQUIRED - but only for containers that required public access to it. One Mapping is for one public URL +apiVersion: getambassador.io/v3alpha1 +kind: Mapping +metadata: + name: ${APP_NAME} #Do not change value '${APP_NAME} ' except you have more then one Mapping - in such a case please append appropriare suffix, for example ${APP_NAME}-nginx +spec: + hostname: ${APP_URL} + prefix: / + service: ${APP_NAME}:8080 #Do not change value '${APP_NAME} ' except you have more then one Mapping - in such a case please append appropriare suffix, for example ${APP_NAME}-nginx + +--- +#OPTIONAL - only when your application required persistent volume. This refers to 'volumes' in corresponding Deployment manifest. Please comment/remove whole this section if your application does not reqiure persisteng volumes +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: ${APP_NAME} #Do not change value '${APP_NAME} ' except you have more then one PersistentVolumeClaim - in such a case please append appropriate suffix, for example ${APP_NAME}-db_container + namespace: ${KUBE_NAMESPACE} #Do not change this! +spec: + storageClassName: nfs-client + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 20Mi + +--- +#REQUIRED +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + appsvc: nginx + name: ${APP_NAME} #Do not change value '${APP_NAME} ' except you have more then one Deployment - in such a case please append appropriate suffix, for example ${APP_NAME}-db_container +spec: + selector: + matchLabels: + appsvc: nginx + replicas: 1 + template: + metadata: + labels: + appsvc: nginx + spec: + containers: + - name: ${APP_NAME} #Do not change value '${APP_NAME} ' except you have more then one Deployment - in such a case please append appropriate suffix, for example ${APP_NAME}-db_container + image: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA} #Do not change value '${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}' except you want to use custom image teag - in such a case please change '${CI_COMMIT_SHA}' + imagePullPolicy: IfNotPresent + volumeMounts: + - name: nginx-vol + mountPath: /usr/share/nginx/html + ports: + - name: "tcp-80" + containerPort: 80 + protocol: TCP + resources: + limits: + cpu: "500m" + memory: "250Mi" + requests: + cpu: "400m" + memory: "200Mi" + volumes: + - name: nginx-vol + persistentVolumeClaim: + claimName: ${APP_NAME} #Do not change value '${APP_NAME} ' except you have more then one Deployment - in such a case please append appropriate suffix, for example ${APP_NAME}-db_container + imagePullSecrets: + - name: gitlab-registry-secret #Do not change this! + \ No newline at end of file diff --git a/k8s-manifests/kubeconfig.templ.yml b/k8s-manifests/kubeconfig.templ.yml new file mode 100644 index 0000000000000000000000000000000000000000..33eda4da0e5e210c0ba859aaae2e601b1a4903c2 --- /dev/null +++ b/k8s-manifests/kubeconfig.templ.yml @@ -0,0 +1,18 @@ +apiVersion: v1 +clusters: +- cluster: + certificate-authority-data: ${KUBECONF_CERT_AUTHORITY_DATA} + server: ${KUBECONF_SERVER} + name: cluster.local +contexts: +- context: + cluster: cluster.local + user: ${KUBECONF_USER} + name: ${KUBECONF_USER}@cluster.local +current-context: ${KUBECONF_USER}@cluster.local +kind: Config +preferences: {} +users: +- name: ${KUBECONF_USER} + user: + token: ${KUBECONF_TOKEN}