forked from bregman-arie/devops-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
abregman
committed
Aug 13, 2021
1 parent
6e01886
commit 3b05d95
Showing
27 changed files
with
1,973 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
## Ansible - My First Playbook | ||
|
||
1. Write a playbook that will: | ||
a. Install the package zlib | ||
b. Create the file `/tmp/some_file` | ||
2. Run the playbook on a remote host |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Ansible - My First Task | ||
|
||
1. Write a task to create the directory ‘/tmp/new_directory’ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
## My first playbook - Solution | ||
|
||
1. `vi first_playbook.yml` | ||
|
||
``` | ||
- name: Install zlib and create a file | ||
hosts: some_remote_host | ||
tasks: | ||
- name: Install zlib | ||
package: | ||
name: zlib | ||
state: present | ||
become: yes | ||
- name: Create the file /tmp/some_file | ||
path: '/tmp/some_file' | ||
state: touch | ||
``` | ||
|
||
2. First, edit the inventory file: `vi /etc/ansible/hosts` | ||
|
||
``` | ||
[some_remote_host] | ||
some.remoted.host.com | ||
``` | ||
|
||
Run the playbook | ||
|
||
`ansible-playbook first_playbook.yml` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## My First Task - Solution | ||
|
||
``` | ||
- name: Create a new directory | ||
file: | ||
path: "/tmp/new_directory" | ||
state: directory | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Deploy to Kubernetes | ||
|
||
* Write a pipeline that will deploy an "hello world" web app to Kubernete | ||
* The CI/CD system (where the pipeline resides) and the Kubernetes cluster should be on separate systems | ||
* The web app should be accessible remotely and only with HTTPS |
45 changes: 45 additions & 0 deletions
45
exercises/devops/solutions/deploy_to_kubernetes/Jenkinsfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
pipeline { | ||
|
||
agent any | ||
|
||
stages { | ||
|
||
stage('Checkout Source') { | ||
steps { | ||
git url:'https://github.com/<GITHUB_USERNAME>/<YOUR_WEB_APP_REPO>.git', | ||
// credentialsId: 'creds_github', | ||
branch:'master' | ||
} | ||
} | ||
|
||
stage("Build image") { | ||
steps { | ||
script { | ||
myapp = docker.build("<YOUR_DOCKER_USERNAME>/helloworld:${env.BUILD_ID}") | ||
} | ||
} | ||
} | ||
|
||
stage("Push image") { | ||
steps { | ||
script { | ||
docker.withRegistry('https://registry.hub.docker.com', 'dockerhub') { | ||
myapp.push("latest") | ||
myapp.push("${env.BUILD_ID}") | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
stage('Deploy App') { | ||
steps { | ||
script { | ||
sh 'ansible-playbook deploy.yml' | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
## Deploy to Kubernetes | ||
|
||
Note: this exercise can be solved in various ways. The solution described here is just one possible way. | ||
|
||
1. Install Jenkins on one system (follow up the standard Jenkins installation procedure) | ||
2. Deploy Kubernetes on a remote host (minikube can be an easy way to achieve it) | ||
3. Create a simple web app or [page](html) | ||
|
||
4. Create Kubernetes [resoruces](helloworld.yml) - Deployment, Service and Ingress (for HTTPS access) | ||
5. Create an [Ansible inventory](inventory) and insert the address of the Kubernetes cluster | ||
6. Write [Ansible playbook](deploy.yml) to deploy the Kubernetes resources and also generate | ||
7. Create a [pipeline](Jenkinsfile) | ||
|
||
8. Run the pipeline :) | ||
9. Try to access the web app remotely |
42 changes: 42 additions & 0 deletions
42
exercises/devops/solutions/deploy_to_kubernetes/deploy.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
- name: Apply Kubernetes YAMLs | ||
hosts: kubernetes | ||
tasks: | ||
- name: Ensure SSL related directories exist | ||
file: | ||
path: "{{ item }}" | ||
state: directory | ||
loop: | ||
- "/etc/ssl/crt" | ||
- "/etc/ssl/csr" | ||
- "/etc/ssl/private" | ||
|
||
- name: Generate an OpenSSL private key. | ||
openssl_privatekey: | ||
path: /etc/ssl/private/privkey.pem | ||
|
||
- name: generate openssl certficate signing requests | ||
openssl_csr: | ||
path: /etc/ssl/csr/hello-world.app.csr | ||
privatekey_path: /etc/ssl/private/privkey.pem | ||
common_name: hello-world.app | ||
|
||
- name: Generate a Self Signed OpenSSL certificate | ||
openssl_certificate: | ||
path: /etc/ssl/crt/hello-world.app.crt | ||
privatekey_path: /etc/ssl/private/privkey.pem | ||
csr_path: /etc/ssl/csr/hello-world.app.csr | ||
provider: selfsigned | ||
|
||
- name: Create k8s secret | ||
command: "kubectl create secret tls tls-secret --cert=/etc/ssl/crt/hello-world.app.crt --key=/etc/ssl/private/privkey.pem" | ||
register: result | ||
failed_when: | ||
- result.rc == 2 | ||
|
||
- name: Deploy web app | ||
k8s: | ||
state: present | ||
definition: "{{ lookup('file', './helloworld.yml') }}" | ||
kubeconfig: '/home/abregman/.kube/config' | ||
namespace: 'default' | ||
wait: true |
65 changes: 65 additions & 0 deletions
65
exercises/devops/solutions/deploy_to_kubernetes/helloworld.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: hello-blue-whale | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: hello-world-app | ||
version: blue | ||
template: | ||
metadata: | ||
name: hello-blue-whale-pod | ||
labels: | ||
app: hello-world-app | ||
version: blue | ||
spec: | ||
containers: | ||
- name: hello-whale-container | ||
image: abregman2/helloworld:latest | ||
imagePullPolicy: Always | ||
ports: | ||
- containerPort: 80 | ||
- containerPort: 443 | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: hello-world | ||
labels: | ||
app: hello-world-app | ||
spec: | ||
ports: | ||
- port: 80 | ||
targetPort: 80 | ||
protocol: TCP | ||
name: http | ||
selector: | ||
app: hello-world-app | ||
--- | ||
apiVersion: networking.k8s.io/v1 | ||
kind: Ingress | ||
metadata: | ||
name: example-ingress | ||
annotations: | ||
cert-manager.io/cluster-issuer: selfsigned-issuer | ||
nginx.ingress.kubernetes.io/rewrite-target: / | ||
kubernetes.io/ingress.class: nginx | ||
spec: | ||
tls: | ||
- hosts: | ||
- hello-world.app | ||
secretName: shhh | ||
rules: | ||
- host: hello-world.app | ||
http: | ||
paths: | ||
- path: / | ||
pathType: Prefix | ||
backend: | ||
service: | ||
name: hello-world | ||
port: | ||
number: 80 |
Oops, something went wrong.