Chris Farmiloe
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # RE Kubernetes Workshop A hands on introduction to the RE Kubernetes platform for potential users using a local [minikube](https://kubernetes.io/docs/setup/minikube/) environment. Incubated during the [2018/2019 Q4 Techops Firebreak](https://trello.com/c/4McS6xzg). ### Overview * assumes all workshop attendees are from RE so there is a bias towards understanding what is happening under-the-hood * want to understand what the infrastructure looks like * we decided that a minimal app must consist of: * A [Helm chart](https://docs.helm.sh/developing_charts/) containing.. * A [Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/) (describing the app container to run) * A [Service](https://kubernetes.io/docs/concepts/services-networking/service/) (describing the internal endpoint for the app) * An [Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/) (exposing the app to the public) * this requires explaining the following concepts: * [Nodes](https://kubernetes.io/docs/concepts/architecture/nodes/), [Pods](https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/), [Deployments](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/), [Services](https://kubernetes.io/docs/concepts/services-networking/service/), [Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/) etc. are all Kubernetes Objects that you can define (usually as YAML) * A [Helm chart](https://docs.helm.sh/developing_charts/) is a collection of [https://kubernetes.io/docs/concepts/#kubernetes-objects](https://kubernetes.io/docs/concepts/#kubernetes-objects) with some simple templating features to allow reusability/composability * [Pods](https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/) are the unit of execution, one or more containers * You will probably never directly define a [Pod](https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/), and instead will define a [Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/) which will manage your [Pods](https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/) * [Services](https://kubernetes.io/docs/concepts/services-networking/service/) are like mini internal load-balancers that point to [Pods](https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/) * [Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/) routes HTTP traffic from the outside world to a [Service](https://kubernetes.io/docs/concepts/services-networking/service/) ### Exercise * Create a [Helm chart](https://docs.helm.sh/developing_charts/) for a simple hello world application based on nginx that can be deployed to a kubernetes cluster. install [homebrew](https://brew.sh/) (if required) to install dependencies ``` /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ``` install minikube and dependencies using [homebrew](https://brew.sh/) ``` brew cask install virtualbox # if it fails, go to system preferences > security and allow Oracle access at the bottom brew cask install minikube brew install kubernetes-cli brew install kubernetes-helm brew install jq ``` start minikube with an [Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/) controller ``` minikube start --vm-driver virtualbox minikube addons enable ingress minikube addons list ``` check minikube is running ``` minikube status ``` check that we can access our minikube cluster using `kubectl` ``` kubectl get nodes ``` check out minikube using the kubernetes web dashboard ``` minikube dashboard --url # get the web address of the dashboard minikube dashboard # launch dashboad in default browser ``` ![](https://i.imgur.com/Ujw9EFh.png) create a directory that will contain our "Chart" (collection of kubernetes resources) ``` mkdir myapp cd myapp ``` A chart is a simple directory with "Chart.yaml" file, "values.yaml" file and a templates dir like: ``` . ├── Chart.yaml ├── templates └── values.yaml ``` So let's create these files ....create a Chart.yaml file that will define some metadata about this chart ``` apiVersion: v1 appVersion: "1.0" description: My first helm chart name: myapp version: 0.1.0 ``` create a "templates" directory which will contain all of our kubernetes object definitions ``` mkdir templates ``` create an values.yaml file that can be used to set defaults for any variables we might use in our chart containing ``` replicas: 2 # the number of pods ``` This sets the default number of pods to create and is referenced in the deployment template using ``{{ .Values.replicas }}``. ok, we now have the boilerplate for an empty "Chart" and can start defining kubernetes resources First we will create a Deployment which will ensure than N replicas (instances) of our application container will be running. In this case the default is 2 as defined in `values.yaml` however this may be overridden. We will use an "nginx" container image to play the role of our application, but you could replace this with another Docker image if you want: Create a minimal `templates/deployment.yaml` that looks like: ``` apiVersion: apps/v1beta2 kind: Deployment metadata: name: {{ .Release.Name }}-myapp labels: app.kubernetes.io/name: myapp app.kubernetes.io/instance: {{ .Release.Name }} spec: replicas: {{ .Values.replicas }} selector: matchLabels: app.kubernetes.io/name: myapp app.kubernetes.io/instance: {{ .Release.Name }} template: metadata: labels: app.kubernetes.io/name: myapp app.kubernetes.io/instance: {{ .Release.Name }} spec: containers: - name: myappcontainer image: "nginx:latest" ports: - name: http containerPort: 80 protocol: TCP ``` The `{{ .Release.Name }}` variables will be populated when we "render" the chart. The ``{{ .Values.replicas }}`` variable is populated from the contents of the `values.yaml` file. Test out rendering the chart using the `helm template` command like: ``` helm template --name example . ``` you should see the rendered version of the kubernetes objects dumped to stdout instead of dumping to stdout, we can write the output to a directory: ``` mkdir output helm template --name example --output-dir=output . ``` we can now use `kubectl` to "apply" the contents of the "output" dir to our minikube cluster ``` kubectl apply -R -f output/ ``` `apply` means, install the declarations into the cluster The `-R` flag means, find all the kubernetes resource definitions recursively in the directory specified by the `-f` flag We should now be able to list the deployments installed in the cluster with: ``` kubectl get deployments ``` The Deployment will manage the creation of Pods for our app image To check the state of pods do ``` kubectl get pods ``` which should show something like ``` NAME READY STATUS RESTARTS AGE example-myapp-6d76dc8fbb-ptnm9 1/1 Running 0 25s example-myapp-c8fbb6d76d-44nm9 1/1 Running 0 25s ``` If you see a status of something other than `Running`, then you may have to try again in a few seconds ... ok so we now have 2x Pods running instances of our app (nginx) in the cluster, but all Pods in the cluster are running on an internal only network and as such it is not accessible from the outside. We can tunnel into the cluster using `kubectl port-forward` to check if our Pod is serving requests: ``` kubectl port-forward deployment/example-myapp 8080:80 ``` this will tunnel http://localhost:8080 -> into the cluster -> pod ... so you can use a browser to check it's returning the "welcome to nginx" default page tunnelling into the cluster isn't a very practical way to expose your service so let's improve that. The first thing we need to do is add a `Service` definition to our Chart. create a `templates/service.yaml` file like: ``` apiVersion: v1 kind: Service metadata: name: {{ .Release.Name }}-myapp labels: app.kubernetes.io/name: myapp app.kubernetes.io/instance: {{ .Release.Name }} spec: type: ClusterIP ports: - port: 80 targetPort: http protocol: TCP name: http selector: app.kubernetes.io/name: myapp app.kubernetes.io/instance: {{ .Release.Name }} ``` The `Service` will act like a mini internal load-balancer giving us a single internal endpoint that points to each running Pod that matches the labels in the `selector` description. We'll need to re-render our template... ``` helm template --name=example --output-dir=output . ``` ...and re-apply it to the cluster... ``` kubectl apply -R -f output/ ``` As before we can use `kubectl port-forward` to tunnel this time to the `Service` endpoint to check it's working: ``` kubectl port-forward service/example-myapp 8080:80 ``` This still hasn't helped us expose our app to the public, but has given us a stable endpoint to direct traffic to To route traffic from the public we need to define an `Ingress` record. Create a `templates/ingress.yaml` file: ``` apiVersion: extensions/v1beta1 kind: Ingress metadata: name: {{ .Release.Name }}-myapp annotations: nginx.ingress.kubernetes.io/rewrite-target: "/" spec: rules: - host: www.myapp.com http: paths: - backend: serviceName: example-myapp servicePort: 80 path: / ``` Re-render the Chart and re-apply to the cluster (we'll do it in one go this time just for fun): ``` helm template --name=example . | kubectl apply -f - ``` Check the ingress is created: ``` kubectl get ingress ``` which should show something like: ``` kubectl get ingress NAME HOSTS ADDRESS PORTS AGE example-myapp www.myapp.com 10.0.2.15 80 3m31s ``` Our ingress route should now be working and routing traffic for the `www.myapp.com` host from the exposed minikube IP to our Service and on to our Pods running nginx... we can test this using curl: ``` curl -k -H 'Host: www.myapp.com' http://$(minikube ip)/ ``` The `minikube ip` command will return the IP address of the minikube virtual machine, which is the ingress point. We add a `Host: www.myapp.com` header to the request so that our request is correctly routed. which should show the "Thank you for using nginx" message! ``` <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> ``` You can check the logs of your application using ``` kubectl logs -f example-myapp-768cd7d675-zmr6vkubectl ``` The `-f` switch steams updates from the log as they happen and the pod name is the value you find in the output from `kubectl get pods` ``` 72.17.0.2 - - [10/Jan/2019:11:10:12 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "192.168.99.1" 172.17.0.2 - - [10/Jan/2019:11:12:44 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.54.0" "192.168.99.1" 172.17.0.2 - - [10/Jan/2019:11:13:03 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.54.0" "192.168.99.1" ``` Now lets scale our deployment up to ten nginx instances. We can override the replicas setting during the templating as folllows: ``` helm template --name=example --set-string replicas=10 . ``` variables passed using `--set-string` override the default in the `values.yaml` file. Note the default replicas setting is now set to 10 in the generated yaml ``` --- # Source: myapp/templates/service.yaml apiVersion: v1 kind: Service metadata: name: example-myapp labels: app.kubernetes.io/name: myapp app.kubernetes.io/instance: example spec: type: ClusterIP ports: - port: 80 targetPort: http protocol: TCP name: http selector: app.kubernetes.io/name: myapp app.kubernetes.io/instance: example --- # Source: myapp/templates/deployment.yaml apiVersion: apps/v1beta2 kind: Deployment metadata: name: example-myapp labels: app.kubernetes.io/name: myapp app.kubernetes.io/instance: example spec: replicas: 10 selector: matchLabels: app.kubernetes.io/name: myapp app.kubernetes.io/instance: example template: metadata: labels: app.kubernetes.io/name: myapp app.kubernetes.io/instance: example spec: containers: - name: myappcontainer image: "nginx:latest" ports: - name: http containerPort: 80 protocol: TCP --- # Source: myapp/templates/ingress.yaml apiVersion: extensions/v1beta1 kind: Ingress metadata: name: example-myapp annotations: nginx.ingress.kubernetes.io/rewrite-target: "/" spec: rules: - host: www.myapp.com http: paths: - backend: serviceName: example-myapp servicePort: 80 path: / ``` So we can now scale up to ten instances using ``` helm template --name=example --set-string replicas=10 . | kubectl apply -f - ``` Check the running pods to confirm there are in fact ten instances ``` kubectl get pods ``` ``` NAME READY STATUS RESTARTS AGE example-myapp-768cd7d675-2vwxg 1/1 Running 0 3m37s example-myapp-768cd7d675-8gwnn 1/1 Running 0 3m37s example-myapp-768cd7d675-9fvgd 1/1 Running 0 3m37s example-myapp-768cd7d675-fj2nw 1/1 Running 0 3m37s example-myapp-768cd7d675-pfgmv 1/1 Running 0 3m37s example-myapp-768cd7d675-qpvfz 1/1 Running 0 3m37s example-myapp-768cd7d675-qw4lj 1/1 Running 0 3m37s example-myapp-768cd7d675-rcjjp 1/1 Running 0 3m37s example-myapp-768cd7d675-tvvwq 1/1 Running 0 3m37s example-myapp-768cd7d675-zsdm7 1/1 Running 0 3m37s ``` Now lets scale back to 3 pods ``` helm template --name=example --set-string replicas=3 . | kubectl apply -f - ``` Check the running pods again to confirm that there are now three ``` kubectl get pods ``` ``` NAME READY STATUS RESTARTS AGE example-myapp-768cd7d675-2vwxg 1/1 Running 0 5m14s example-myapp-768cd7d675-qw4lj 1/1 Running 0 5m14s example-myapp-768cd7d675-tvvwq 1/1 Running 0 5m14s ``` Finally we can remove our deployment, the instances, the service and the ingress ``` helm template --name=example --set-string replicas=3 . | kubectl delete -f - ``` ``` service "myapp" deleted deployment.apps "myapp" deleted ingress.extensions "myapp" deleted ``` ``` kubectl get pods ``` which confirms that its all gone ``` No resources found. ``` Finally we can stop minikube ``` minikube stop ``` and if required remove all the sofware ``` brew cask uninstall virtualbox brew cask uninstall minikube brew uninstall kubernetes-cli brew uninstall kubernetes-helm brew uninstall jq ``` **FIN** --- ### What next? * Mounting a custom file into `/usr/share/nginx/html/index.html` to serve static files from a `ConfigMap`? * Build your own docker image from scratch for your app? * Debugging, describe (broken thing and how to fix it) * Continuous Deployment --- ### References |Topic|Description| |----|-----------| |[kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)| kubernetes command line tool| |[kubernetes](https://kubernetes.io/docs/home/?path=users&persona=app-developer&level=foundational)| k8s | |[helm](https://docs.helm.sh/)| helm package manager for kubernetes| |[jq](https://stedolan.github.io/jq/manual/)| json wrangling filter | |[minikube](https://github.com/kubernetes/minikube)|local kubernetes | |[virtualbox](https://www.virtualbox.org/manual/UserManual.html)|hypervisor

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully