├── kubernetes_cluster_resources ├── nginx-chart-0.1.0.tgz ├── service.yaml ├── nginx-chart │ ├── templates │ │ ├── service.yaml │ │ ├── serviceaccount.yaml │ │ ├── tests │ │ │ └── test-connection.yaml │ │ ├── deployment.yaml │ │ ├── hpa.yaml │ │ ├── NOTES.txt │ │ ├── _helpers.tpl │ │ └── ingress.yaml │ ├── .helmignore │ ├── Chart.yaml │ └── values.yaml └── deployment.yaml ├── Tasks ├── pod2.yaml ├── pod3.yaml ├── pod1.yaml ├── database.yaml ├── rs.yaml ├── webservers.yaml └── Tasks.md └── README.md /kubernetes_cluster_resources/nginx-chart-0.1.0.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adesoji1/Configure-resources-on-a-Kubernetes-Cluster/HEAD/kubernetes_cluster_resources/nginx-chart-0.1.0.tgz -------------------------------------------------------------------------------- /Tasks/pod2.yaml: -------------------------------------------------------------------------------- 1 | # pod2.yaml 2 | apiVersion: v1 3 | kind: Pod 4 | metadata: 5 | name: pod2 6 | labels: 7 | os: alpine 8 | spec: 9 | containers: 10 | - name: httpd 11 | image: httpd:alpine 12 | -------------------------------------------------------------------------------- /Tasks/pod3.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Pod 3 | metadata: 4 | name: pod3 5 | namespace: finance 6 | labels: 7 | app: accounting 8 | spec: 9 | containers: 10 | - name: redis 11 | image: redis:latest 12 | -------------------------------------------------------------------------------- /Tasks/pod1.yaml: -------------------------------------------------------------------------------- 1 | # pod1.yaml 2 | #1.Create a pod named pod1 that contains an nginx Docker container image: 3 | apiVersion: v1 4 | kind: Pod 5 | metadata: 6 | name: pod1 7 | spec: 8 | containers: 9 | - name: nginx 10 | image: nginx:latest 11 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/service.yaml: -------------------------------------------------------------------------------- 1 | # service.yaml 2 | 3 | apiVersion: v1 4 | kind: Service 5 | metadata: 6 | name: nginx-service 7 | spec: 8 | selector: 9 | app: nginx 10 | ports: 11 | - protocol: TCP 12 | port: 80 13 | targetPort: 80 14 | type: LoadBalancer -------------------------------------------------------------------------------- /kubernetes_cluster_resources/nginx-chart/templates/service.yaml: -------------------------------------------------------------------------------- 1 | # service.yaml 2 | 3 | apiVersion: v1 4 | kind: Service 5 | metadata: 6 | name: nginx-service 7 | spec: 8 | selector: 9 | app: nginx 10 | ports: 11 | - protocol: TCP 12 | port: 80 13 | targetPort: 80 14 | type: LoadBalancer -------------------------------------------------------------------------------- /Tasks/database.yaml: -------------------------------------------------------------------------------- 1 | # database.yaml 2 | apiVersion: apps/v1 3 | kind: Deployment 4 | metadata: 5 | name: database 6 | spec: 7 | replicas: 1 8 | selector: 9 | matchLabels: 10 | app: db 11 | template: 12 | metadata: 13 | labels: 14 | app: db 15 | spec: 16 | containers: 17 | - name: redis 18 | image: redis:latest 19 | -------------------------------------------------------------------------------- /Tasks/rs.yaml: -------------------------------------------------------------------------------- 1 | # rs.yaml 2 | apiVersion: apps/v1 3 | kind: ReplicaSet 4 | metadata: 5 | name: my-replicaset 6 | spec: 7 | replicas: 3 8 | selector: 9 | matchLabels: 10 | app: my-app 11 | template: 12 | metadata: 13 | labels: 14 | app: my-app 15 | spec: 16 | containers: 17 | - name: my-container 18 | image: my-image 19 | -------------------------------------------------------------------------------- /Tasks/webservers.yaml: -------------------------------------------------------------------------------- 1 | # webservers.yaml 2 | apiVersion: apps/v1 3 | kind: Deployment 4 | metadata: 5 | name: webservers 6 | spec: 7 | replicas: 3 8 | selector: 9 | matchLabels: 10 | app: web 11 | template: 12 | metadata: 13 | labels: 14 | app: web 15 | spec: 16 | containers: 17 | - name: nginx 18 | image: nginx:latest 19 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/nginx-chart/templates/serviceaccount.yaml: -------------------------------------------------------------------------------- 1 | {{- if .Values.serviceAccount.create -}} 2 | apiVersion: v1 3 | kind: ServiceAccount 4 | metadata: 5 | name: {{ include "nginx-chart.serviceAccountName" . }} 6 | labels: 7 | {{- include "nginx-chart.labels" . | nindent 4 }} 8 | {{- with .Values.serviceAccount.annotations }} 9 | annotations: 10 | {{- toYaml . | nindent 4 }} 11 | {{- end }} 12 | {{- end }} 13 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/nginx-chart/.helmignore: -------------------------------------------------------------------------------- 1 | # Patterns to ignore when building packages. 2 | # This supports shell glob matching, relative path matching, and 3 | # negation (prefixed with !). Only one pattern per line. 4 | .DS_Store 5 | # Common VCS dirs 6 | .git/ 7 | .gitignore 8 | .bzr/ 9 | .bzrignore 10 | .hg/ 11 | .hgignore 12 | .svn/ 13 | # Common backup files 14 | *.swp 15 | *.bak 16 | *.tmp 17 | *.orig 18 | *~ 19 | # Various IDEs 20 | .project 21 | .idea/ 22 | *.tmproj 23 | .vscode/ 24 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/nginx-chart/templates/tests/test-connection.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Pod 3 | metadata: 4 | name: "{{ include "nginx-chart.fullname" . }}-test-connection" 5 | labels: 6 | {{- include "nginx-chart.labels" . | nindent 4 }} 7 | annotations: 8 | "helm.sh/hook": test 9 | spec: 10 | containers: 11 | - name: wget 12 | image: busybox 13 | command: ['wget'] 14 | args: ['{{ include "nginx-chart.fullname" . }}:{{ .Values.service.port }}'] 15 | restartPolicy: Never 16 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/deployment.yaml: -------------------------------------------------------------------------------- 1 | # deployment.yaml 2 | apiVersion: apps/v1 3 | kind: Deployment 4 | metadata: 5 | name: nginx-deployment 6 | spec: 7 | replicas: 3 8 | selector: 9 | matchLabels: 10 | app: nginx 11 | template: 12 | metadata: 13 | labels: 14 | app: nginx 15 | spec: 16 | containers: 17 | - name: nginx 18 | image: nginx:1.14.2 19 | ports: 20 | - containerPort: 80 21 | resources: 22 | limits: 23 | cpu: 500m 24 | memory: 256Mi 25 | requests: 26 | cpu: 250m 27 | memory: 128Mi 28 | 29 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/nginx-chart/templates/deployment.yaml: -------------------------------------------------------------------------------- 1 | # deployment.yaml 2 | #Hard Coded 3 | apiVersion: apps/v1 4 | kind: Deployment 5 | metadata: 6 | name: {{ include "nginx-chart.fullname" . }} 7 | spec: 8 | replicas: {{ .Values.replicaCount }} 9 | selector: 10 | matchLabels: 11 | app: {{ include "nginx-chart.fullname" . }} 12 | template: 13 | metadata: 14 | labels: 15 | app: {{ include "nginx-chart.fullname" . }} 16 | spec: 17 | containers: 18 | - name: {{ .Chart.Name }} 19 | image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" 20 | ports: 21 | - containerPort: {{ .Values.containerPort }} 22 | 23 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/nginx-chart/templates/hpa.yaml: -------------------------------------------------------------------------------- 1 | {{- if .Values.autoscaling.enabled }} 2 | apiVersion: autoscaling/v2beta1 3 | kind: HorizontalPodAutoscaler 4 | metadata: 5 | name: {{ include "nginx-chart.fullname" . }} 6 | labels: 7 | {{- include "nginx-chart.labels" . | nindent 4 }} 8 | spec: 9 | scaleTargetRef: 10 | apiVersion: apps/v1 11 | kind: Deployment 12 | name: {{ include "nginx-chart.fullname" . }} 13 | minReplicas: {{ .Values.autoscaling.minReplicas }} 14 | maxReplicas: {{ .Values.autoscaling.maxReplicas }} 15 | metrics: 16 | {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} 17 | - type: Resource 18 | resource: 19 | name: cpu 20 | targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} 21 | {{- end }} 22 | {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} 23 | - type: Resource 24 | resource: 25 | name: memory 26 | targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} 27 | {{- end }} 28 | {{- end }} 29 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/nginx-chart/Chart.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v2 2 | name: nginx-chart 3 | description: A Helm chart for Kubernetes 4 | 5 | # A chart can be either an 'application' or a 'library' chart. 6 | # 7 | # Application charts are a collection of templates that can be packaged into versioned archives 8 | # to be deployed. 9 | # 10 | # Library charts provide useful utilities or functions for the chart developer. They're included as 11 | # a dependency of application charts to inject those utilities and functions into the rendering 12 | # pipeline. Library charts do not define any templates and therefore cannot be deployed. 13 | type: application 14 | 15 | # This is the chart version. This version number should be incremented each time you make changes 16 | # to the chart and its templates, including the app version. 17 | # Versions are expected to follow Semantic Versioning (https://semver.org/) 18 | version: 0.1.0 19 | 20 | # This is the version number of the application being deployed. This version number should be 21 | # incremented each time you make changes to the application. Versions are not expected to 22 | # follow Semantic Versioning. They should reflect the version the application is using. 23 | # It is recommended to use it with quotes. 24 | appVersion: "1.16.0" 25 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/nginx-chart/templates/NOTES.txt: -------------------------------------------------------------------------------- 1 | 1. Get the application URL by running these commands: 2 | {{- if .Values.ingress.enabled }} 3 | {{- range $host := .Values.ingress.hosts }} 4 | {{- range .paths }} 5 | http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} 6 | {{- end }} 7 | {{- end }} 8 | {{- else if contains "NodePort" .Values.service.type }} 9 | export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "nginx-chart.fullname" . }}) 10 | export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") 11 | echo http://$NODE_IP:$NODE_PORT 12 | {{- else if contains "LoadBalancer" .Values.service.type }} 13 | NOTE: It may take a few minutes for the LoadBalancer IP to be available. 14 | You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "nginx-chart.fullname" . }}' 15 | export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "nginx-chart.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") 16 | echo http://$SERVICE_IP:{{ .Values.service.port }} 17 | {{- else if contains "ClusterIP" .Values.service.type }} 18 | export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "nginx-chart.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") 19 | export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") 20 | echo "Visit http://127.0.0.1:8080 to use your application" 21 | kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT 22 | {{- end }} 23 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/nginx-chart/templates/_helpers.tpl: -------------------------------------------------------------------------------- 1 | {{/* 2 | Expand the name of the chart. 3 | */}} 4 | {{- define "nginx-chart.name" -}} 5 | {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} 6 | {{- end }} 7 | 8 | {{/* 9 | Create a default fully qualified app name. 10 | We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). 11 | If release name contains chart name it will be used as a full name. 12 | */}} 13 | {{- define "nginx-chart.fullname" -}} 14 | {{- if .Values.fullnameOverride }} 15 | {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} 16 | {{- else }} 17 | {{- $name := default .Chart.Name .Values.nameOverride }} 18 | {{- if contains $name .Release.Name }} 19 | {{- .Release.Name | trunc 63 | trimSuffix "-" }} 20 | {{- else }} 21 | {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} 22 | {{- end }} 23 | {{- end }} 24 | {{- end }} 25 | 26 | {{/* 27 | Create chart name and version as used by the chart label. 28 | */}} 29 | {{- define "nginx-chart.chart" -}} 30 | {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} 31 | {{- end }} 32 | 33 | {{/* 34 | Common labels 35 | */}} 36 | {{- define "nginx-chart.labels" -}} 37 | helm.sh/chart: {{ include "nginx-chart.chart" . }} 38 | {{ include "nginx-chart.selectorLabels" . }} 39 | {{- if .Chart.AppVersion }} 40 | app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} 41 | {{- end }} 42 | app.kubernetes.io/managed-by: {{ .Release.Service }} 43 | {{- end }} 44 | 45 | {{/* 46 | Selector labels 47 | */}} 48 | {{- define "nginx-chart.selectorLabels" -}} 49 | app.kubernetes.io/name: {{ include "nginx-chart.name" . }} 50 | app.kubernetes.io/instance: {{ .Release.Name }} 51 | {{- end }} 52 | 53 | {{/* 54 | Create the name of the service account to use 55 | */}} 56 | {{- define "nginx-chart.serviceAccountName" -}} 57 | {{- if .Values.serviceAccount.create }} 58 | {{- default (include "nginx-chart.fullname" .) .Values.serviceAccount.name }} 59 | {{- else }} 60 | {{- default "default" .Values.serviceAccount.name }} 61 | {{- end }} 62 | {{- end }} 63 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/nginx-chart/values.yaml: -------------------------------------------------------------------------------- 1 | # Default values for nginx-chart. 2 | # This is a YAML-formatted file. 3 | # Declare variables to be passed into your templates. 4 | 5 | replicaCount: 1 6 | 7 | image: 8 | repository: nginx 9 | pullPolicy: IfNotPresent 10 | # Overrides the image tag whose default is the chart appVersion. 11 | tag: 1.14.2 12 | containerport: 80 13 | 14 | imagePullSecrets: [] 15 | nameOverride: "" 16 | fullnameOverride: "" 17 | 18 | serviceAccount: 19 | # Specifies whether a service account should be created 20 | create: true 21 | # Annotations to add to the service account 22 | annotations: {} 23 | # The name of the service account to use. 24 | # If not set and create is true, a name is generated using the fullname template 25 | name: "" 26 | 27 | podAnnotations: {} 28 | 29 | podSecurityContext: {} 30 | # fsGroup: 2000 31 | 32 | securityContext: {} 33 | # capabilities: 34 | # drop: 35 | # - ALL 36 | # readOnlyRootFilesystem: true 37 | # runAsNonRoot: true 38 | # runAsUser: 1000 39 | 40 | service: 41 | type: ClusterIP 42 | port: 80 43 | 44 | ingress: 45 | enabled: false 46 | className: "" 47 | annotations: {} 48 | # kubernetes.io/ingress.class: nginx 49 | # kubernetes.io/tls-acme: "true" 50 | hosts: 51 | - host: chart-example.local 52 | paths: 53 | - path: / 54 | pathType: ImplementationSpecific 55 | tls: [] 56 | # - secretName: chart-example-tls 57 | # hosts: 58 | # - chart-example.local 59 | 60 | resources: {} 61 | # We usually recommend not to specify default resources and to leave this as a conscious 62 | # choice for the user. This also increases chances charts run on environments with little 63 | # resources, such as Minikube. If you do want to specify resources, uncomment the following 64 | # lines, adjust them as necessary, and remove the curly braces after 'resources:'. 65 | # limits: 66 | # cpu: 100m 67 | # memory: 128Mi 68 | # requests: 69 | # cpu: 100m 70 | # memory: 128Mi 71 | 72 | autoscaling: 73 | enabled: false 74 | minReplicas: 1 75 | maxReplicas: 100 76 | targetCPUUtilizationPercentage: 80 77 | # targetMemoryUtilizationPercentage: 80 78 | 79 | nodeSelector: {} 80 | 81 | tolerations: [] 82 | 83 | affinity: {} 84 | -------------------------------------------------------------------------------- /kubernetes_cluster_resources/nginx-chart/templates/ingress.yaml: -------------------------------------------------------------------------------- 1 | {{- if .Values.ingress.enabled -}} 2 | {{- $fullName := include "nginx-chart.fullname" . -}} 3 | {{- $svcPort := .Values.service.port -}} 4 | {{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} 5 | {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} 6 | {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} 7 | {{- end }} 8 | {{- end }} 9 | {{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} 10 | apiVersion: networking.k8s.io/v1 11 | {{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} 12 | apiVersion: networking.k8s.io/v1beta1 13 | {{- else -}} 14 | apiVersion: extensions/v1beta1 15 | {{- end }} 16 | kind: Ingress 17 | metadata: 18 | name: {{ $fullName }} 19 | labels: 20 | {{- include "nginx-chart.labels" . | nindent 4 }} 21 | {{- with .Values.ingress.annotations }} 22 | annotations: 23 | {{- toYaml . | nindent 4 }} 24 | {{- end }} 25 | spec: 26 | {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} 27 | ingressClassName: {{ .Values.ingress.className }} 28 | {{- end }} 29 | {{- if .Values.ingress.tls }} 30 | tls: 31 | {{- range .Values.ingress.tls }} 32 | - hosts: 33 | {{- range .hosts }} 34 | - {{ . | quote }} 35 | {{- end }} 36 | secretName: {{ .secretName }} 37 | {{- end }} 38 | {{- end }} 39 | rules: 40 | {{- range .Values.ingress.hosts }} 41 | - host: {{ .host | quote }} 42 | http: 43 | paths: 44 | {{- range .paths }} 45 | - path: {{ .path }} 46 | {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} 47 | pathType: {{ .pathType }} 48 | {{- end }} 49 | backend: 50 | {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} 51 | service: 52 | name: {{ $fullName }} 53 | port: 54 | number: {{ $svcPort }} 55 | {{- else }} 56 | serviceName: {{ $fullName }} 57 | servicePort: {{ $svcPort }} 58 | {{- end }} 59 | {{- end }} 60 | {{- end }} 61 | {{- end }} 62 | -------------------------------------------------------------------------------- /Tasks/Tasks.md: -------------------------------------------------------------------------------- 1 | # Task Below 2 | 3 | create a pod named pod1 that contains an nginx docker container image . 4 | 5 | create a pod named pod 2 that contains an httpd docker container image and a label named os=alpine 6 | 7 | display the content of the rs.yaml replicaset definition file. 8 | 9 | create a pod replicaset by using rs.yaml file. display the content of the webservers.yaml deployment definition file. 10 | 11 | create a deployment by using the webservers.yaml file. 12 | 13 | create a deployment file named database.yaml for a deployment named database that contains a redis docker container image. 14 | 15 | create a deployment by using the database.yaml file . determine the number of pods that are running in the default namespace 16 | 17 | ## Solution Below 18 | 19 | 1. Create a pod named pod1 that contains an nginx Docker container image: After defining the manifest file pod1.yaml, Create the pod using the following command: `kubectl apply -f pod1.yaml` 20 | 21 | 2. Create a pod named pod2 that contains an httpd Docker container image and a label named os=alpine: After defining the manifest file pod2.yaml, Create the pod using the following command: `kubectl apply -f pod2.yaml` 22 | 23 | 3. Display the content of the rs.yaml ReplicaSet definition file: define the manifest file rs.yaml: 24 | 25 | 4. Create a pod ReplicaSet using the rs.yaml file: `kubectl apply -f rs.yaml` 26 | 27 | 5. Display the content of the webservers.yaml Deployment definition file: define the manifest file webservers.yaml, 28 | 29 | 6. Create a Deployment using the webservers.yaml file: `kubectl apply -f webservers.yaml` 30 | 31 | 7. Create a Deployment file named database.yaml for a Deployment named database that contains a redis Docker container image:Define the database.yaml 32 | 33 | 8. Create a Deployment using the database.yaml file: `kubectl apply -f database.yaml` 34 | 35 | 9. Determine the number of pods that are running in the default namespace: `kubectl get pods -n default | grep -c 'Running'` . This command will return the number of pods with the status 'Running' in the default namespace. 36 | 37 | 10. To determine the number of ReplicaSets running in the default namespace, you can use the `kubectl` command-line tool. Here's how: `kubectl get replicaset -n default` 38 | 39 | This command will display a table of information, including the ReplicaSet names, the number of desired and current replicas, and more. 40 | 41 | **11.**To get the total number of ReplicaSets in the default namespace, you can use the following command: `kubectl get replicaset -n default | awk 'NR>1 {count++} END {print count}'` 42 | 43 | *12.*This command above retrieves all ReplicaSets in the default namespace and uses the awk tool to count the number of lines (excluding the header line) and print the count. 44 | 45 | **13.**create a namespace called finance create a pod named pod 3 in the finance namespace that contains a redis docker container image and a label named app = accounting. First, let's create a namespace called "finance" using: `kubectl create namespace finance` 46 | 47 | *14.*Now, create a pod named pod3 in the finance namespace that contains a redis Docker container image and a label named app=accounting: lookup the pod3.yaml file. 48 | 49 | $$ 50 | 15. 51 | $$create the pod using this command: `kubectl apply -f pod3.yaml` 52 | 53 | This command above will create a pod named "pod3" with a redis container and the label app=accounting in the finance namespace. 54 | 55 | *** Take Note that the `awk` command and `sed` commands are two powerful used text manipulation tools in `Linux` 56 | 57 | awk and sed are two powerful and widely used text manipulation tools in Linux. 58 | 59 | awk stands for "Aho, Weinberger, and Kernighan," named after the three computer scientists who created it. It is a command-line utility used for processing and manipulating text files in Linux. awk reads input files line by line, and for each line, it performs a set of actions specified by the user. It can be used to extract specific columns of data from files, perform calculations, and transform data into a different format. awk is particularly useful for working with structured data files, such as CSV files or log files. 60 | 61 | Here's an example of how to use awk to extract the second column of a CSV file below 62 | 63 | ```markdown 64 | awk -F "," '{print $2}' myfile.csv 65 | 66 | ``` 67 | 68 | In this example, the -F option specifies the delimiter (comma in this case), and '{print $2}' tells awk to print the second column of each line. 69 | 70 | sed stands for "Stream Editor" and is another text manipulation tool commonly used in Linux. sed can be used to perform various text transformations on input data, such as substituting text, deleting lines, and inserting or appending text to a file. Like awk, sed reads input files line by line and applies a set of rules or commands to each line. 71 | 72 | Here's an example of how to use sed to replace all occurrences of the word "hello" with "hi" in a file: 73 | 74 | ```markdown 75 | sed 's/hello/hi/g' myfile.txt 76 | ``` 77 | 78 | In this example, the s command substitutes "hello" with "hi" (s/hello/hi/), and the g option tells sed to replace all occurrences on each line. 79 | 80 | Both awk and sed are powerful tools for manipulating text data in Linux, and mastering them can save you a lot of time and effort when working with text files. 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Configure resources on a Kubernetes Cluster : Turing AWS Devops Test :smile: 2 | 3 | In this challenge, you will deploy resources in a Kubernetes cluster. First, you will retrieve information about the cluster. Next, you will create cluster resources, and then you will organize cluster resources in a namespace. Finally, you will update cluster resources, and then you will roll back a Deployment. 4 | write the correct manifest files too 5 | 6 | In this chalenge, I will guide you through the following steps to configure resources in a Kubernetes cluster: 7 | How do i install kubectl? 8 | 9 | kubectl is a command-line tool for running commands against Kubernetes clusters. It is essential for managing and deploying applications on a Kubernetes cluster. Here are the steps to install kubectl on different operating systems: 10 | 11 | macOS: 12 | 13 | You can use Homebrew to install kubectl: 14 | 15 | ```console markdown 16 | brew install kubectl 17 | 18 | ``` 19 | 20 | Or, you can install kubectl using the following curl command: 21 | 22 | ```markdown 23 | curl -LO " -s " 24 | 25 | ``` 26 | 27 | ```markdown 28 | chmod +x ./kubectl 29 | 30 | ``` 31 | 32 | ```markdown 33 | sudo mv ./kubectl /usr/local/bin/kubectl 34 | 35 | ``` 36 | 37 | `Windows:` 38 | 39 | You can use Chocolatey to install kubectl: 40 | 41 | ```markdown 42 | choco install kubernetes-cli 43 | 44 | ``` 45 | 46 | Alternatively, you can download the latest release with this command: 47 | 48 | ```markdown 49 | curl -LO -s 50 | 51 | ``` 52 | 53 | Then, add the binary in to your PATH. 54 | 55 | `Linux:` 56 | 57 | You can install kubectl using the following curl command: 58 | 59 | ```markdown 60 | curl -LO " -s " 61 | 62 | ``` 63 | 64 | ```markdown 65 | chmod +x ./kubectl 66 | 67 | ``` 68 | 69 | ```markdown 70 | sudo mv ./kubectl /usr/local/bin/kubectl 71 | 72 | ``` 73 | 74 | After installation, you can verify that kubectl is installed by running: 75 | 76 | ```markdown 77 | kubectl version --client 78 | 79 | ``` 80 | 81 | If you don't want to install kubectl locally, you can use an alternative like Kubernetes Dashboard or Lens IDE to manage your cluster. However, kubectl is widely used, and it's recommended to have it installed for managing Kubernetes clusters efficiently. 82 | 83 | ```markdown 84 | 1. Retrieve information about the cluster 85 | 86 | ``` 87 | 88 | ```markdown 89 | kubectl get nodes 90 | 91 | ``` 92 | 93 | ```markdown 94 | kubectl get namespaces 95 | 96 | ``` 97 | 98 | ```markdown 99 | kubectl cluster-info 100 | 101 | ``` 102 | 103 | ```markdown 104 | 2. Create cluster resources 105 | 106 | ``` 107 | 108 | ```markdown 109 | kubectl apply -f deployment.yaml 110 | 111 | ``` 112 | 113 | ```markdown 114 | kubectl apply -f service.yaml 115 | 116 | ``` 117 | 118 | ```markdown 119 | 3. Organize cluster resources in a namespace 120 | 121 | ``` 122 | 123 | ## Create a new namespace 124 | 125 | ```markdown 126 | kubectl create namespace kubenamespace 127 | 128 | ``` 129 | 130 | ## Update the YAML files to include the namespace 131 | 132 | ## Add the following line under the metadata section in both deployment.yaml and service.yaml 133 | 134 | namespace: kubenamespace 135 | 136 | ## Apply the updated YAML files 137 | 138 | ```markdown 139 | kubectl apply -f deployment.yaml 140 | 141 | ``` 142 | 143 | ```markdown 144 | kubectl apply -f service.yaml 145 | 146 | ``` 147 | 148 | ```markdown 149 | 4. Update cluster resources 150 | 151 | ``` 152 | 153 | ## Update the deployment YAML file, for example, change the nginx image version to 1.19.10 154 | 155 | image: nginx:1.19.10 156 | 157 | ## Apply the updated YAML file 158 | 159 | ```markdown 160 | kubectl apply -f deployment.yaml 161 | 162 | ``` 163 | 164 | ```markdown 165 | 5. Roll back a Deployment 166 | 167 | ``` 168 | 169 | ## Check the rollout history 170 | 171 | ```markdown 172 | kubectl rollout history deployment/nginx-deployment -n kubenamespace 173 | 174 | ``` 175 | 176 | ## Roll back to the previous revision 177 | 178 | ```markdown 179 | kubectl rollout undo deployment/nginx-deployment -n kubenamespace 180 | 181 | ``` 182 | 183 | It's a good practice to set resource limits for containers in a Kubernetes deployment to avoid resource starvation for other processes. You can set resource limits by adding the `resources` field with limits and `requests` subfields under the container definition. Here's an example of how to set resource limits for CPU and memory in your `deployment.yaml` file: 184 | 185 | ## Using helm Chart 186 | 187 | ```markdown 188 | 6. Using helm Chart 189 | 190 | ``` 191 | 192 | Helm is a package manager for Kubernetes that simplifies the deployment and management of applications in a Kubernetes cluster. It uses "charts" as a packaging format, which is a collection of files that describe a related set of Kubernetes resources. A chart is like a blueprint for deploying and managing applications in a Kubernetes cluster. 193 | 194 | A Helm chart typically consists of the following components: 195 | 196 | Chart.yaml: Contains metadata about the chart, such as name, version, and description. 197 | values.yaml: Contains default configuration values that can be overwritten during the installation or upgrade of a chart. 198 | templates: A directory containing one or more Kubernetes manifest files (like deployment.yaml and service.yaml) that define the Kubernetes resources to be deployed. 199 | Helm can help you manage the complexity of deploying and updating applications by automating the process and providing a versioning mechanism for your releases. Here's how you can use Helm in your current scenario: 200 | 201 | Install Helm by following the instructions for your specific operating system:[Here](https://helm.sh/docs/intro/install) 202 | 203 | Create a Helm chart for your application by running the following command: 204 | 205 | ```markdown 206 | helm create nginx-chart 207 | 208 | ``` 209 | 210 | This command creates a directory named `nginx-chart` with the following structure: 211 | 212 | 1. nginx-chart/ 213 | 2. Chart.yaml 214 | 3. values.yaml 215 | 4. charts/ 216 | 5. templates/ 217 | 218 | Replace the contents of `nginx-chart/templates/deployment.yaml` and `nginx-chart/templates/service.yaml` with your deployment and service manifests. 219 | 220 | Modify the `nginx-chart/values.yaml` file to define any configurable parameters for your application. These values can be referenced in the template files using the `{{ .Values.parameterName }}` syntax. 221 | 222 | Package the Helm chart: 223 | 224 | ```markdown 225 | helm package nginx-chart 226 | 227 | ``` 228 | 229 | This command creates a (`nginx-chart-0.1.0.tgz`) file in the current directory containing your helm chart. 230 | 231 | To install the Helm chart on your Kubernetes cluster, use the helm install command: 232 | 233 | ```markdown 234 | helm install my-nginx-release nginx-chart-0.1.0.tgz 235 | 236 | ``` 237 | 238 | To update your application, modify the templates or values in your Helm chart, increase the version in `Chart.yaml`, re-package the chart, and then use `helm upgrade` to apply the changes: 239 | 240 | `helm upgrade my-nginx-release nginx-chart-0.1.1.tgz` 241 | 242 | To uninstall the Helm chart, use the helm uninstall command: 243 | 244 | ```markdown 245 | helm uninstall my-nginx-release 246 | 247 | ``` 248 | 249 | If you need to roll back a release, use the helm rollback command: 250 | 251 | ```markdown 252 | helm rollback my-nginx-release 1 253 | ``` 254 | 255 | This command rolls back the release to the previous version. 256 | 257 | In summary, Helm simplifies the deployment and management of applications on Kubernetes by providing a versioning mechanism and automating the deployment process. It uses charts, which are collections of Kubernetes manifest files (like deployment.yaml and service.yaml), to define the resources to be deployed. 258 | 259 | ## Question 260 | 261 | how do i do these? Modify the nginx-chart/values.yaml file to define any configurable parameters for your application. These values can be referenced in the template files using the {{ .Values.parameterName }} syntax? 262 | 263 | To modify the nginx-chart/values.yaml file to define configurable parameters for your application, follow these steps: 264 | 265 | Open the nginx-chart/values.yaml file in a text editor. 266 | 267 | Add the parameters you want to make configurable. For example, let's make the replica count, the container image, and the container port configurable: 268 | 269 | - replicaCount: 3 270 | - image: 271 | - repository: nginx 272 | - tag: 1.14.2 273 | - containerPort: 80 274 | 275 | Save and close the values.yaml file. 276 | 277 | Now, open the nginx-chart/templates/deployment.yaml file in a text editor. 278 | 279 | Replace the hardcoded values with the configurable parameters using the {{ .Values.parameterName }} syntax. For example, replace the replica count, the container image, and the container port with the values from the values.yaml file: 280 | 281 | ```markdown 282 | apiVersion: apps/v1 283 | kind: Deployment 284 | metadata: 285 | name: {{ include "nginx-chart.fullname" . }} 286 | spec: 287 | replicas: {{ .Values.replicaCount }} 288 | selector: 289 | matchLabels: 290 | app: {{ include "nginx-chart.fullname" . }} 291 | template: 292 | metadata: 293 | labels: 294 | app: {{ include "nginx-chart.fullname" . }} 295 | spec: 296 | containers: 297 | - name: {{ .Chart.Name }} 298 | image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" 299 | ports: 300 | - containerPort: {{ .Values.containerPort }} 301 | ``` 302 | 303 | Save and close the deployment.yaml file. 304 | Now, when you install or upgrade your Helm chart, you can provide different values for these parameters using the --set flag, or by creating a custom values.yaml file and passing it with the -f flag. For example, to change the replica count and container port during installation: 305 | 306 | ```markdown 307 | helm install my-nginx-release nginx-chart-0.1.0.tgz --set replicaCount=5,containerPort=8080 308 | ``` 309 | 310 | Or, create a custom values.yaml file with your desired settings: 311 | 312 | ```markdown 313 | replicaCount: 5 314 | image: 315 | repository: nginx 316 | tag: 1.14.2 317 | containerPort: 8080 318 | ``` 319 | 320 | Then, install the Helm chart using this custom values.yaml file below 321 | 322 | ```markdown 323 | 324 | helm install my-nginx-release nginx-chart-0.1.0.tgz -f my-custom-values.yaml 325 | 326 | ``` 327 | 328 | By making parameters configurable in your Helm chart, you can more easily deploy different configurations of your application without modifying the Kubernetes manifests directly. 329 | 330 | After deploying your application using Helm, you need to expose the application to the internet or your internal network to access the website. There are multiple ways to do this, but one common method is to use a Kubernetes Service of type LoadBalancer or NodePort. 331 | 332 | Assuming you have a Service manifest file in your Helm chart (e.g., nginx-chart/templates/service.yaml), you can follow these steps: 333 | 334 | Open the nginx-chart/templates/service.yaml file in a text editor. 335 | 336 | Modify the spec section of the Service to use the LoadBalancer or NodePort type. Here's an example using LoadBalancer: 337 | 338 | ```markdown 339 | apiVersion: v1 340 | kind: Service 341 | metadata: 342 | name: {{ include "nginx-chart.fullname" . }} 343 | spec: 344 | type: LoadBalancer 345 | ports: 346 | - port: 80 347 | targetPort: {{ .Values.containerPort }} 348 | selector: 349 | app: {{ include "nginx-chart.fullname" . }} 350 | ``` 351 | 352 | Note that you should replace {{ .Values.containerPort }} with the actual port number if you haven't defined it in your values.yaml file. 353 | 354 | Save and close the service.yaml file. 355 | 356 | Update your Helm chart version in Chart.yaml, package the chart, and then upgrade the release with the command below: 357 | 358 | ```markdown 359 | helm upgrade my-nginx-release nginx-chart-0.1.1.tgz 360 | ``` 361 | 362 | Get the external IP address (for LoadBalancer type) or the node IP and port (for NodePort type) of your Service: 363 | 364 | ```markdown 365 | kubectl get services 366 | ``` 367 | 368 | This command will display a list of services running in your cluster. Look for the external IP address (in the EXTERNAL-IP column) if you're using a LoadBalancer, or the node port (in the PORT(S) column, after the colon) if you're using a NodePort. 369 | 370 | Access the website using the external IP address and port (for LoadBalancer) or the node IP address and node port (for NodePort). For example 371 | 372 | ```markdown 373 | # :80> 374 | ``` 375 | 376 | or 377 | 378 | ```markdown 379 | # :> 380 | 381 | ``` 382 | 383 | Port forwarding is not always necessary, but it can be helpful in certain scenarios. Whether you need to use port forwarding depends on your specific use case and your Kubernetes environment. 384 | 385 | Port forwarding can be useful when: 386 | 387 | You are running a Kubernetes cluster locally (e.g., using minikube, kind, or k3s) and want to access a service running inside the cluster from your host machine or other devices on your local network. 388 | You are using a Kubernetes cluster without built-in support for external load balancers, and you want to access a service from outside the cluster without using a NodePort service. 389 | You want to temporarily access a specific pod within the cluster for debugging purposes without exposing it through a service. 390 | If your use case falls into any of these categories, you might want to use port forwarding. To do this with kubectl, use the port-forward command, specifying the resource (such as a pod, deployment, or service) and the ports you want to forward: 391 | 392 | ```markdown 393 | kubectl port-forward : 394 | 395 | ``` 396 | 397 | For example, to forward local port 8080 to a deployment named nginx-deployment on port 80, you can run: 398 | 399 | kubectl port-forward deployment/nginx-deployment 8080:80 400 | 401 | Now, you can access the website at . or Then, you can access the service in your browser using the local port: 402 | 403 | 404 | 405 | Keep in mind that port forwarding is not a long-term solution for exposing services to external users, as it creates a direct connection between your machine and the Kubernetes resource, bypassing any security mechanisms provided by services, ingresses, or network policies. It's best used for temporary access, testing, or debugging purposes. For production scenarios, it's recommended to use LoadBalancer, NodePort, or Ingress resources to expose your applications. 406 | 407 | ## More Tasks for creating kuberenetes resources is found in the Tasks/Tasks.md directory 408 | 409 | The End :smile: 410 | --------------------------------------------------------------------------------