<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aymen EL Amri</title>
    <description>The latest articles on DEV Community by Aymen EL Amri (@eon01).</description>
    <link>https://dev.to/eon01</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F165012%2F0c6aebbd-08a8-4669-ba55-1479ffa7c091.jpeg</url>
      <title>DEV Community: Aymen EL Amri</title>
      <link>https://dev.to/eon01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eon01"/>
    <language>en</language>
    <item>
      <title>A Gentle Introduction to Kubernetes</title>
      <dc:creator>Aymen EL Amri</dc:creator>
      <pubDate>Tue, 20 Aug 2019 18:23:34 +0000</pubDate>
      <link>https://dev.to/eon01/a-gentle-introduction-to-kubernetes-52en</link>
      <guid>https://dev.to/eon01/a-gentle-introduction-to-kubernetes-52en</guid>
      <description>&lt;p&gt;Slide available &lt;a href="https://slides.com/eon01/kubernetes-workshop#/"&gt;here&lt;/a&gt;.&lt;br&gt;
Original article posted &lt;a href="https://medium.com/faun/a-gentle-introduction-to-kubernetes-4961e443ba26"&gt;here&lt;/a&gt;.&lt;br&gt;
Source code &lt;a href="https://github.com/eon01/kubernetes-workshop"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In this workshop, we're going to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deploy Kubernetes services and an Ambassador API gateway.&lt;/li&gt;
&lt;li&gt;Examine the difference between Kubernetes proxies and service mesh like Istio.&lt;/li&gt;
&lt;li&gt;Access the Kubernetes API from the outside and from a Pod.&lt;/li&gt;
&lt;li&gt;Understand what API to choose.&lt;/li&gt;
&lt;li&gt;See how Service Accounts and RBAC works&lt;/li&gt;
&lt;li&gt;Discover some security pitfalls when building Docker images and many interesting things.&lt;/li&gt;
&lt;li&gt;Other things :-)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will start by developing then deploying a simple Python application (a Flask API that returns the list of trending repositories by programming language).&lt;/p&gt;
&lt;h2&gt;
  
  
  Development Environment
&lt;/h2&gt;

&lt;p&gt;We are going to use Python 3.6.7&lt;/p&gt;

&lt;p&gt;We are using Ubuntu 18.04 that comes with Python 3.6 by default. You should be able to invoke it with the command python3. (Ubuntu 17.10 and above also come with Python 3.6.7)&lt;/p&gt;

&lt;p&gt;If you use Ubuntu 16.10 and 17.04, you should be able to install it with the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get update
sudo apt-get install python3.6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you are using Ubuntu 14.04 or 16.04, you need to get Python 3 from a Personal Package Archive (PPA):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For the other operating systems, visit &lt;a href="https://realpython.com/installing-python/"&gt;this guide&lt;/a&gt;, follow the instructions and install Python3.&lt;/p&gt;

&lt;p&gt;Now install PIP, the package manager:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install python3-pip
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Follow this by the installation of Virtualenvwrapper, which is a virtual environment manager:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo pip3 install virtualenvwrapper
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Create a folder for your virtualenvs (I use ~/dev/PYTHON_ENVS) and set it as WORKON_HOME:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir  ~/dev/PYTHON_ENVS
export WORKON_HOME=~/dev/PYTHON_ENVS
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In order to source the environment details when the user login, add the following lines to ~/.bashrc:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source "/usr/local/bin/virtualenvwrapper.sh"
export WORKON_HOME="~/dev/PYTHON_ENVS"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Make sure to adapt the WORKON_HOME to your real WORKON_HOME.&lt;br&gt;
Now we need to create then activate the new environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkvirtualenv --python=/usr/bin/python3 trendinggitrepositories
workon trendinggitrepositories
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's create the application directories:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;trendinggitrepositories
&lt;span class="nb"&gt;cd &lt;/span&gt;trendinggitrepositories
&lt;span class="nb"&gt;mkdir &lt;/span&gt;api
&lt;span class="nb"&gt;cd &lt;/span&gt;api
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once the virtual environment is activated, we can install Flask:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;flask
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Developing a Trending Git Repositories API (Flask)
&lt;/h2&gt;

&lt;p&gt;Inside the API folder &lt;code&gt;api&lt;/code&gt;, create a file called &lt;code&gt;app.py&lt;/code&gt; and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will return a hello world message when a user requests the "/" route.&lt;/p&gt;

&lt;p&gt;Now run it using: &lt;code&gt;python app.py&lt;/code&gt; and you will see a similar output to the following one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* Serving Flask app "api" (lazy loading)
* Environment: production
  WARNING: This is a development server. Do not use it in a production deployment.
  Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 465-052-587
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vh5KmUFf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AKiSZZKqTjMbJctp2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vh5KmUFf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AKiSZZKqTjMbJctp2.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We now need to install PyGithub since we need it to communicate with Github API v3.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install PyGithub
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Go to Github and &lt;a href="https://github.com/settings/applications/new"&gt;create a new app&lt;/a&gt;. We will need the application "Client ID" and "Client Secret":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from github import Github
g = Github("xxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is how the mini API looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, jsonify, abort
import urllib.request, json
from flask import request

app = Flask(__name__)

from github import Github
g = Github("xxxxxx", "xxxxxxxxxxxxx")

@app.route('/')
def get_repos():
    r = []

    try:
        args = request.args
        n = int(args['n'])
    except (ValueError, LookupError) as e:
        abort(jsonify(error="No integer provided for argument 'n' in the URL"))

    repositories = g.search_repositories(query='language:python')[:n]

    for repo in repositories:
        with urllib.request.urlopen(repo.url) as url:
            data = json.loads(url.read().decode())
        r.append(data)

    return jsonify({'repos':r })

if __name__ == '__main__':
    app.run(debug=True)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's hide the Github token and secret as well as other variables in the environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, jsonify, abort, request
import urllib.request, json, os
from github import Github

app = Flask(__name__)

CLIENT_ID = os.environ['CLIENT_ID']
CLIENT_SECRET = os.environ['CLIENT_SECRET']
DEBUG = os.environ['DEBUG']

g = Github(CLIENT_ID, CLIENT_SECRET)


@app.route('/')
def get_repos():
    r = []

    try:
        args = request.args
        n = int(args['n'])
    except (ValueError, LookupError) as e:
        abort(jsonify(error="No integer provided for argument 'n' in the URL"))

    repositories = g.search_repositories(query='language:python')[:n]

    for repo in repositories:
        with urllib.request.urlopen(repo.url) as url:
            data = json.loads(url.read().decode())
        r.append(data)

    return jsonify({'repos':r })

if __name__ == '__main__':
    app.run(debug=DEBUG)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The code above will return the top "n" repositories using Python as a programming language. We can use other languages too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, jsonify, abort, request
import urllib.request, json, os
from github import Github

app = Flask(__name__)

CLIENT_ID = os.environ['CLIENT_ID']
CLIENT_SECRET = os.environ['CLIENT_SECRET']
DEBUG = os.environ['DEBUG']

g = Github(CLIENT_ID, CLIENT_SECRET)


@app.route('/')
def get_repos():
    r = []

    try:
        args = request.args
        n = int(args['n'])
        l = args['l']
    except (ValueError, LookupError) as e:
        abort(jsonify(error="Please provide 'n' and 'l' parameters"))

    repositories = g.search_repositories(query='language:' + l)[:n]


    try:
        for repo in repositories:
            with urllib.request.urlopen(repo.url) as url:
                data = json.loads(url.read().decode())
            r.append(data)
        return jsonify({
            'repos':r,
            'status': 'ok'
            })
    except IndexError as e:
        return jsonify({
            'repos':r,
            'status': 'ko'
            })

if __name__ == '__main__':
    app.run(debug=DEBUG)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In a .env file, add the variables you want to use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CLIENT_ID="xxxxx"
CLIENT_SECRET="xxxxxx"
ENV="dev"
DEBUG="True"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Before running the Flask application, you need to source these variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;source&lt;/span&gt; .env
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, you can go to &lt;code&gt;http://0.0.0.0:5000/?n=1&amp;amp;l=python&lt;/code&gt; to get the trendiest Python repository or &lt;code&gt;http://0.0.0.0:5000/?n=1&amp;amp;l=c&lt;/code&gt; for C programming language.&lt;br&gt;
Here is a list of other programming languages you can test your code with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C++
Assembly
Objective
Makefile
Shell
Perl
Python
Roff
Yacc
Lex
Awk
UnrealScript
Gherkin
M4
Clojure
XS
Perl
sed
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The list is long, but our mini API is working fine.&lt;br&gt;
Now, let's freeze the dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip freeze &amp;gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Before running the API on Kubernetes, let's create a Dockerfile. This is a typical Dockerfile for a Python app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /app
EXPOSE 5000
CMD [ "python", "app.py" ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can build it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build --no-cache -t tgr .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker rm -f tgr
docker run --it  --name tgr -p 5000:5000 -e CLIENT_ID="xxxxxxx" -e CLIENT_SECRET="xxxxxxxxxxxxxxx" -e DEBUG="True" tgr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's include some other variables as environment variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, jsonify, abort, request
import urllib.request, json, os
from github import Github

app = Flask(__name__)

CLIENT_ID = os.environ['CLIENT_ID']
CLIENT_SECRET = os.environ['CLIENT_SECRET']
DEBUG = os.environ['DEBUG']
HOST = os.environ['HOST']
PORT = os.environ['PORT']

g = Github(CLIENT_ID, CLIENT_SECRET)


@app.route('/')
def get_repos():
    r = []

    try:
        args = request.args
        n = int(args['n'])
        l = args['l']
    except (ValueError, LookupError) as e:
        abort(jsonify(error="Please provide 'n' and 'l' parameters"))

    repositories = g.search_repositories(query='language:' + l)[:n]


    try:
        for repo in repositories:
            with urllib.request.urlopen(repo.url) as url:
                data = json.loads(url.read().decode())
            r.append(data)
        return jsonify({
            'repos':r,
            'status': 'ok'
            })
    except IndexError as e:
        return jsonify({
            'repos':r,
            'status': 'ko'
            })

if __name__ == '__main__':
    app.run(debug=DEBUG, host=HOST, port=PORT)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For security reasons, let's change the user inside the container from root to a user with less rights that we create:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM python:3
ENV PYTHONUNBUFFERED 1
RUN adduser pyuser

RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . .
RUN chmod +x app.py

RUN chown -R pyuser:pyuser /app
USER pyuser


EXPOSE 5000
CMD ["python","./app.py"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now if we want to run the container, we need to add many environment variables to the docker run command. An easier solution is using &lt;code&gt;--env-file&lt;/code&gt; with Docker run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -it --env-file .env my_container
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our .env file looks like the following one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CLIENT_ID="xxxx"
CLIENT_SECRET="xxxx"
ENV="dev"
DEBUG="True"
HOST="0.0.0.0"
PORT=5000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After this modification, rebuild the image &lt;code&gt;docker build -t tgr .&lt;/code&gt; and run it using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker rm -f tgr;
docker run -it  --name tgr -p 5000:5000 --env-file .env  tgr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our application runs using &lt;code&gt;python app.py&lt;/code&gt; which is the webserver that ships with Flask and t's great for development and local execution of your program, however, it's not designed to run in a production mode, whether it's a monolithic app or a microservice.&lt;/p&gt;

&lt;p&gt;A production server typically receives abuse from spammers, script kiddies, and should be able to handle high traffic. In our case, a good solution is using a WSGI HTTP server like Gunicorn (or uWsgi).&lt;/p&gt;

&lt;p&gt;First, let's install &lt;code&gt;gunicorn&lt;/code&gt; with the following command: &lt;code&gt;pip install gunicorn&lt;/code&gt;. This will require us to update our &lt;code&gt;requirements.txt&lt;/code&gt; with &lt;code&gt;pip freeze &amp;gt; requirements.txt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is why we are going to change our Docker file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM python:3
ENV PYTHONUNBUFFERED 1
RUN adduser pyuser

RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . .
RUN chmod +x app.py

RUN chown -R pyuser:pyuser /app
USER pyuser


EXPOSE 5000
CMD ["gunicorn", "app:app", "-b", "0.0.0.0:5000"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In order to optimize the Wsgi server, we need to set the number of its workers and threads to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;workers = multiprocessing.cpu_count() * 2 + 1
threads = 2 * multiprocessing.cpu_count()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is why we are going to create another Python configuration file (&lt;code&gt;config.py&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import multiprocessing
workers = multiprocessing.cpu_count() * 2 + 1
threads = 2 * multiprocessing.cpu_count()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the same file, we are going to include other configurations of Gunicorn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from os import environ as env
bind = env.get("HOST","0.0.0.0") +":"+ env.get("PORT", 5000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is the final &lt;code&gt;config.py&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import multiprocessing
workers = multiprocessing.cpu_count() * 2 + 1
threads = 2 * multiprocessing.cpu_count()

from os import environ as env
bind = env.get("HOST","0.0.0.0") +":"+ env.get("PORT", 5000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In consequence, we should adapt the Dockerfile to the new Gunicorn configuration by changing the last line to :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CMD ["gunicorn", "app:app", "--config=config.py"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, build &lt;code&gt;docker build -t tgr .&lt;/code&gt; and run &lt;code&gt;docker run -it --env-file .env -p 5000:5000 tgr&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Pushing the Image to a Remote Registry
&lt;/h1&gt;

&lt;p&gt;A Docker registry is a storage and distribution system for named Docker images.&lt;/p&gt;

&lt;p&gt;The images we built are stored in our local environment and can only be used if you deploy locally. However, if you choose to deploy a Kubernetes cluster in a cloud or any different environment, these images will be not found. This is why we need to push the build images to a remote registry.&lt;/p&gt;

&lt;p&gt;Think of container registries as a git system for Docker images.&lt;/p&gt;

&lt;p&gt;There are plenty of containers registries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dockerhub&lt;/li&gt;
&lt;li&gt;Amazon Elastic Registry (ECR)&lt;/li&gt;
&lt;li&gt;Azure Container Registry (ACR)&lt;/li&gt;
&lt;li&gt;Google Container Registry (GCR)&lt;/li&gt;
&lt;li&gt;CoreOS Quay&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also host your private container registry that supports OAuth, LDAP and Active Directory authentication using the registry provided by Docker:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d -p 5000:5000 --restart=always --name registry registry:2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;More about self-hosting a registry can be found in &lt;a href="https://docs.docker.com/registry/deploying/"&gt;the official Docker documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We are going to use Dockerhub; this is why you need to create an account on &lt;a href="https://hub.docker.com/"&gt;hub.docker.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, using Docker CLI, login:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker login
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now rebuild the image using the new tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; docker build -t &amp;lt;username&amp;gt;/&amp;lt;image_name&amp;gt;:&amp;lt;tag_version&amp;gt; .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t eon01/tgr:1 .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, push the image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker push eon01/tgr:1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  A Security Notice
&lt;/h2&gt;

&lt;p&gt;Many of the publicly (and even private Docker images) seems to be secure, but it's not the case. When we built our image, we told Docker to copy all the images from the application folder to the image and we push it to an external public registry.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;COPY&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;        
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ADD . .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above commands will even copy the &lt;code&gt;.env&lt;/code&gt; file containing our secrets.&lt;/p&gt;

&lt;p&gt;A good solution is to tell Docker to ignore these files during the build using a .dockeringore file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**.git
**.gitignore
**README.md
**env.*
**Dockerfile*
**docker-compose*
**.env
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;At this stage, you should remove any image that you pushed to a distant registry, reset the Github tokens, build the new image without any cache:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t eon01/tgr:1 . --no-cache
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Push it again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker push eon01/tgr:1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Installing Minikube
&lt;/h1&gt;

&lt;p&gt;One of the fastest ways to try Kubernetes is using Minkube, which will create a virtual machine for you and deploy a ready-to-use Kubernetes cluster.&lt;/p&gt;

&lt;p&gt;Before you begin the installation, you need to make sure that your laptop supports virtualization:&lt;/p&gt;

&lt;p&gt;If your using Linux, run the following command and make sure that the output is not empty:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;grep -E --color 'vmx|svm' /proc/cpuinfo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Mac users should execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sysctl -a | grep -E --color 'machdep.cpu.features|VMX'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you see &lt;code&gt;VMX&lt;/code&gt; in the output, the VT-x feature is enabled in your machine.&lt;/p&gt;

&lt;p&gt;Windows users should use &lt;code&gt;systeminfo&lt;/code&gt; and you should see the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hyper-V Requirements:     VM Monitor Mode Extensions: Yes
                          Virtualization Enabled In Firmware: Yes
                          Second Level Address Translation: Yes
                          Data Execution Prevention Available: Yes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If everything is okay, you need to install a hypervisor. You have a list of possibilities here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.linux-kvm.org/"&gt;KVM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.virtualbox.org/wiki/Downloads"&gt;VirtualBox&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/moby/hyperkit"&gt;HyperKit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.vmware.com/products/fusion"&gt;VMware Fusion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://msdn.microsoft.com/en-us/virtualization/hyperv_on_windows/quick_start/walkthrough_install"&gt;Hyper-V&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some of these hypervisors are only compatible with some OSs like Hyper-V (formerly known as Windows Server Virtualization) for windows.&lt;/p&gt;

&lt;p&gt;VirtualBox is however cross-platform, and this is why we are going to use it here. Make sure to &lt;a href="https://www.virtualbox.org/wiki/Downloads"&gt;follow the instructions&lt;/a&gt; to install it.&lt;/p&gt;

&lt;p&gt;Now, install Minikube.&lt;/p&gt;

&lt;p&gt;Linux systems:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 &amp;amp;&amp;amp; chmod +x minikube
sudo install minikube /usr/local/bin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;MacOs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew cask install minikube
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 &amp;amp;&amp;amp; chmod +x minikube
sudo mv minikube /usr/local/bin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Windows:&lt;/p&gt;

&lt;p&gt;Use Chocolatey as an administrator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;choco install minikube
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or use &lt;a href="https://github.com/kubernetes/minikube/releases/latest/download/minikube-installer.exe"&gt;the installer binary&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Minikube does not support all Kubernetes features (like load balancing for example), however, you can find the most important features there:&lt;/p&gt;

&lt;p&gt;Minikube supports the following Kubernetes features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DNS&lt;/li&gt;
&lt;li&gt;NodePorts&lt;/li&gt;
&lt;li&gt;ConfigMaps and Secrets&lt;/li&gt;
&lt;li&gt;Dashboards&lt;/li&gt;
&lt;li&gt;Container Runtime: Docker, &lt;a href="https://github.com/rkt/rkt"&gt;rkt&lt;/a&gt;, &lt;a href="https://github.com/kubernetes-incubator/cri-o"&gt;CRI-O&lt;/a&gt;, and &lt;a href="https://github.com/containerd/containerd"&gt;containerd&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Enabling CNI (Container Network Interface)&lt;/li&gt;
&lt;li&gt;Ingress&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also add different addons like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;addon-manager&lt;/li&gt;
&lt;li&gt;dashboard&lt;/li&gt;
&lt;li&gt;default-storageclass&lt;/li&gt;
&lt;li&gt;efk&lt;/li&gt;
&lt;li&gt;freshpod&lt;/li&gt;
&lt;li&gt;gvisor&lt;/li&gt;
&lt;li&gt;heapster&lt;/li&gt;
&lt;li&gt;ingress&lt;/li&gt;
&lt;li&gt;logviewer&lt;/li&gt;
&lt;li&gt;metrics-server&lt;/li&gt;
&lt;li&gt;nvidia-driver-installer&lt;/li&gt;
&lt;li&gt;nvidia-gpu-device-plugin&lt;/li&gt;
&lt;li&gt;registry&lt;/li&gt;
&lt;li&gt;registry-creds&lt;/li&gt;
&lt;li&gt;storage-provisioner&lt;/li&gt;
&lt;li&gt;storage-provisioner-gluster&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you run &lt;code&gt;minikube start&lt;/code&gt; a cluster called minikube will be created; however, you have other choices rather than just creating a regular Minikube cluster. In this example, we are going to create a cluster called "workshop", enable a UI to browse the API and activate tailing logs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;minikube start -p workshop --extra-config=apiserver.enable-swagger-ui=true --alsologtostderr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You have plenty of other options to start a Minikube cluster; you can, for instance, choose the Kubernetes version and the VM driver:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;minikube start --kubernetes-version="v1.12.0" --vm-driver="virtualbox"  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Start the new cluster:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;minikube start -p workshop --extra-config=apiserver.enable-swagger-ui=true --alsologtostderr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can get detailed information about the cluster using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl cluster-info
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you didn't install kubectl, &lt;a href="https://kubernetes.io/docs/tasks/tools/install-kubectl"&gt;follow the official instructions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can open the dashboard using &lt;code&gt;minikube -p workshop dashboard&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Deploying to Kubernetes
&lt;/h1&gt;

&lt;p&gt;We have three main ways to deploy our container to Kubernetes and scale it to N replica.&lt;/p&gt;

&lt;p&gt;The first one is the original form of replication in Kubernetes, and it's called &lt;strong&gt;Replication Controller&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Even if Replica Sets replace it, it's still used in some codes.&lt;/p&gt;

&lt;p&gt;This is a typical example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: v1
kind: ReplicationController
metadata:
  name: app
spec:
  replicas: 3
  selector:
    app: app
  template:
    metadata:
      name: app
      labels:
        app: app
    spec:
      containers:
      - name: tgr
        image: reg/app:v1
        ports:
        - containerPort: 80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can also use Replica Sets, another way to deploy an app and replicate it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: extensions/v1beta1
 kind: ReplicaSet
 metadata:
   name: app
 spec:
   replicas: 3
   selector:
     matchLabels:
       app: app
   template:
     metadata:
       labels:
         app: app
         environment: dev
     spec:
       containers:
       - name: app
         image: reg/app:v1
         ports:
         - containerPort: 80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Replica Set and Replication Controller do almost the same thing.&lt;/p&gt;

&lt;p&gt;They ensure that you have a specified number of pod replicas running at any given time in your cluster.&lt;/p&gt;

&lt;p&gt;There are however, some differences.&lt;/p&gt;

&lt;p&gt;As you may notice, we are using &lt;code&gt;matchLabels&lt;/code&gt; instead of &lt;code&gt;label&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Replica Set use Set-Based selectors while replication controllers use Equity-Based selectors.&lt;/p&gt;

&lt;p&gt;Selectors match Kubernetes objects (like pods) using the constraints of the specified label, and we are going to see an example in a Deployment specification file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Label selectors&lt;/strong&gt; with &lt;strong&gt;equality-based requirements&lt;/strong&gt; use three operators:&lt;code&gt;=&lt;/code&gt;,&lt;code&gt;==&lt;/code&gt; and &lt;code&gt;!=&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;environment = production
tier != frontend
app == my_app (similar to app = my_app)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the last example, we used this notation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ...
 spec:
   replicas: 3
   selector:
     matchLabels:
       app: app
   template:
     metadata:
...     
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We could have used  &lt;strong&gt;set-based requirements&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
spec:
   replicas: 3
   selector:
     matchExpressions:
      - {key: app, operator: In, values: [app]}     
  template:
     metadata:
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we have more than 1 value for the app key, we can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
spec:
   replicas: 3
   selector:
     matchExpressions:
      - {key: app, operator: In, values: [app, my_app, myapp, application]}     
  template:
     metadata:
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And if we have other keys, we can use them like in the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
spec:
   replicas: 3
   selector:
     matchExpressions:
      - {key: app, operator: In, values: [app]}
      - {key: tier, operator: NotIn, values: [frontend]}
      - {key: environment, operator: NotIn, values: [production]}
template:
     metadata:
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Newer Kubernetes resources such as Jobs, Deployments, ReplicaSets, and DaemonSets all support set-based requirements as well.&lt;/p&gt;

&lt;p&gt;This is an example of how we use Kubectl with selectors :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl delete pods -l 'env in (production, staging, testing)'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Until now, we have seen that the Replication Controller and Replica Set are two ways to deploy our container and manage it in a Kubernetes cluster. However, the recommended approach is using a Deployment that configures a ReplicaSet.&lt;/p&gt;

&lt;p&gt;It is rather unlikely that we will ever need to create Pods directly for a production use-case since Deployments manages to create Pods for us through ReplicaSets.&lt;/p&gt;

&lt;p&gt;This is a simple Pod definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: v1
kind: Pod
metadata:
  name: infinite
  labels:
    env: production
    owner: eon01
spec:
  containers:
  - name: infinite
    image: eon01/infinite
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In practice, we need:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A Deployment object&lt;/strong&gt; : Containers are specified here.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A Service object&lt;/strong&gt;: An abstract way to expose an application running on a set of Pods as a network service.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is a Deployment object that creates three replicas of the container app running the image "reg/app:v1". These containers can be reached using port 80:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: app
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
      - name: app
        image: reg/app:v1
        ports:
        - containerPort: 80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is the Deployment file we will use (save it to &lt;code&gt;kubernetes/api-deployment.yaml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: apps/v1
kind: Deployment
metadata:
  name: tgr
  labels:
    name: tgr
spec:
  replicas: 1
  selector:
    matchLabels:
      name: tgr
  template:
    metadata:
      name: tgr
      labels:
        name: tgr
    spec:
      containers:
        - name: tgr
          image: eon01/tgr:1
          ports:
            - containerPort: 5000
          resources:
            requests:
              memory: 128Mi
            limits:
              memory: 256Mi
          env:
            - name: CLIENT_ID
              value: "xxxx"
            - name: CLIENT_SECRET
              value: "xxxxxxxxxxxxxxxxxxxxx"
            - name: ENV
              value: "prod"
            - name: DEBUG
              value: "False"
            - name: HOST
              value: "0.0.0.0"
            - name: PORT
              value: "5000"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's first talk about the API version; in the first example, we used the &lt;code&gt;extensions/v1beta1&lt;/code&gt; and in the second one, we used  &lt;code&gt;apps/v1&lt;/code&gt;. You may know that Kubernetes project development is very active, and it may be confusing sometimes to follow all the software updates.&lt;/p&gt;

&lt;p&gt;In Kubernetes version 1.9, &lt;code&gt;apps/v1&lt;/code&gt; is introduced, and &lt;code&gt;extensions/v1beta1&lt;/code&gt;, &lt;code&gt;apps/v1beta1&lt;/code&gt; and &lt;code&gt;apps/v1beta2&lt;/code&gt; are deprecated.&lt;/p&gt;

&lt;p&gt;To make things simpler, to know which version of the API you need to use, use the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl api-versions
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This above command will give you the API versions compatible with your cluster.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;v1&lt;/strong&gt; was the first stable release of the Kubernetes API. It contains many core objects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;apps/v1&lt;/strong&gt; is the most popular API group in Kubernetes, and it includes functionality related to running applications on Kubernetes, like Deployments, RollingUpdates, and ReplicaSets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**autoscaling/v1 **allows pods to be autoscaled based on different resource usage metrics.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;batch/v1&lt;/strong&gt; is related to batch processing and and jobs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;batch/v1beta1&lt;/strong&gt; is the beta release of batch/v1&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;certificates.k8s.io/v1beta1&lt;/strong&gt; validates network certificates for secure communication in your cluster.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;extensions/v1beta1&lt;/strong&gt;  includes many new, commonly used features. In Kubernetes 1.6, some of these features were relocated from &lt;code&gt;extensions&lt;/code&gt; to specific API groups like &lt;code&gt;apps&lt;/code&gt; .&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;policy/v1beta1&lt;/strong&gt; enables setting a pod disruption budget and new pod security rules&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;rbac.authorization.k8s.io/v1&lt;/strong&gt; includes extra functionality for Kubernetes RBAC (role-based access control)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;..etc&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's deploy the pod now using the Deployment file we created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubcetl apply -f kubernetes/api-deployment.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that you can use &lt;code&gt;kubectl create -f kubernetes/api-deployment.yaml&lt;/code&gt; command. However, there's a difference, between &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;create&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;kubectl create&lt;/code&gt; is what we call &lt;a href="https://kubernetes.io/docs/tutorials/object-management-kubectl/imperative-object-management-configuration/"&gt;Imperative Management of Kubernetes Objects Using Configuration Files&lt;/a&gt;. &lt;code&gt;kubectl create&lt;/code&gt; overwrites all changes, and if a resource is having the same id already exists, it will encounter an error.&lt;/p&gt;

&lt;p&gt;Using this approach, you tell the Kubernetes API what you want to create, replace, or delete, not how you want your K8s cluster world to look like.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;kubectl apply&lt;/code&gt; is what we call &lt;a href="https://kubernetes.io/docs/tutorials/object-management-kubectl/declarative-object-management-configuration/"&gt;Declarative Management of Kubernetes Objects Using Configuration Files&lt;/a&gt; approach. &lt;code&gt;kubectl apply&lt;/code&gt; makes incremental changes. If an object already exists and you want to apply a new value for replica without deleting and recreating the object again, then &lt;code&gt;kubectl apply&lt;/code&gt; is what you need. &lt;code&gt;kubcetl apply&lt;/code&gt; can also be used even if the object (e.g deployment) does not exist yet.&lt;/p&gt;

&lt;p&gt;In the Deployment configuration, we also defined our container. We will run a single container here since the replica is set to &lt;code&gt;1&lt;/code&gt;. In the same time, our container will use the image &lt;code&gt;eon01/tgr:1&lt;/code&gt;. Since our container will need some environment variables, the best way is to provide them using the Kubernetes deployment definition file.&lt;/p&gt;

&lt;p&gt;Also, we can add many other configurations, like the requested memory and its limit. The goal here is not using all that Kubernetes allows is to use in a Deployment file, but to see some of the essential features.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    spec:
      containers:
        - name: tgr
          image: eon01/tgr:1
          ports:
            - containerPort: 5000
          resources:
            requests:
              memory: 128Mi
            limits:
              memory: 256Mi
          env:
            - name: CLIENT_ID
              value: "xxxx"
            - name: CLIENT_SECRET
              value: "xxxxxxxxxxxxxxxxxxxxx"
            - name: ENV
              value: "prod"
            - name: DEBUG
              value: "False"
            - name: HOST
              value: "0.0.0.0"
            - name: PORT
              value: "5000"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In some cases, the Docker registry can be private, and in this case, pulling the image needs authentication. In this case, we need to add the &lt;code&gt;imagePullSecrets&lt;/code&gt; configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
  containers:
  - name: private-reg-container
    image: &amp;lt;your-private-image&amp;gt;
  imagePullSecrets:
  - name: registry-credentials
  ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is how the &lt;code&gt;registry-credentials&lt;/code&gt; secret is created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl create secret docker-registry registry-credentials --docker-server=&amp;lt;your-registry-server&amp;gt; --docker-username=&amp;lt;your-name&amp;gt; --docker-password=&amp;lt;your-pword&amp;gt; --docker-email=&amp;lt;your-email&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also apply/create the &lt;code&gt;registry-credentials&lt;/code&gt; using a YAML file. This is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: v1
kind: Secret
metadata:
  ...
  name: registry-credentials
  ...
data:
  .dockerconfigjson: adjAalkazArrA ... JHJH1QUIIAAX0=
type: kubernetes.io/dockerconfigjson
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you decode the .dockerconfigjson file using &lt;code&gt;base64 --decode&lt;/code&gt; command, you will understand that it's a simple file storing the configuration to access a registry:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get secret regcred --output="jsonpath={.data.\.dockerconfigjson}" | base64 --decode
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You will get a similar output to the following one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"auths":{"your.private.registry.domain.com":{"username":"eon01","password":"xxxxxxxxxxx","email":"aymen@eralabs.io","auth":"dE3xxxxxxxxx"}}}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Again, let's decode the "auth" value using &lt;code&gt;echo "dE3xxxxxxxxx"|base64 --decode&lt;/code&gt; and it will give you something like &lt;code&gt;eon01:xxxxxxxx&lt;/code&gt; which has the format &lt;code&gt;username:password&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's see if the deployment is done, let's see how many pods we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get pods
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This command will show all the pods within a cluster.&lt;/p&gt;

&lt;p&gt;We can scale our deployment using a command similar to the following one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl scale --replicas=&amp;lt;expected_replica_num&amp;gt; deployment &amp;lt;deployment_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our deployment is called &lt;code&gt;tgr&lt;/code&gt; since it's the name we gave to it in the Deployment configuration. You can also make verification by typing &lt;code&gt;kubeclt get deployment&lt;/code&gt;. Let's scale it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl scale --replicas=2 deployment tgr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Each of these containers will be accessible on port 500 from outside the container but not from outside the cluster.&lt;/p&gt;

&lt;p&gt;The number of pods/containers running for our API can be variable and may change dynamically.&lt;/p&gt;

&lt;p&gt;We can set up a load balancer that will balance traffic between the two pods we created, but since each pod can disappear to be recreated, its hostname and address will change.&lt;/p&gt;

&lt;p&gt;In all cases, pods are not meant to receive traffic directly, but they need to be exposed to traffic using a Service. In other words, the set of Pods running in one moment in time could be different from the set of Pods running that application a moment later.&lt;/p&gt;

&lt;p&gt;At the moment, the only service running is the cluster IP (which is related to Minikube and give us access to the cluster we created):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get services
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Services
&lt;/h2&gt;

&lt;p&gt;In Kubernetes, since Pods are mortals, we should create an abstraction that defines a logical set of Pods and how to access them. This is the role of Services.&lt;/p&gt;

&lt;p&gt;In our case, creating a load balancer is a suitable solution. This is the configuration file of a Service object that will listen on the port 80 and load-balance traffic to the Pod with the label &lt;code&gt;name&lt;/code&gt;equals to &lt;code&gt;app&lt;/code&gt; . The latter is accessible internally using the port 5000 like it's defined in the Deployment configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
          ports:
            - containerPort: 5000
...            
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is how the Service looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: v1
kind: Service
metadata:
  name: lb
  labels:
    name: lb
spec:
  ports:
  - port: 80
    targetPort: 5000
  selector:
    name: tgr
  type: LoadBalancer
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Save this file to &lt;code&gt;kubernetes/api-service.yaml&lt;/code&gt; and deploy it using &lt;code&gt;kubectl apply -f kubernetes/api-service.yaml&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you type &lt;code&gt;kubectl get service&lt;/code&gt;, you will get the list of Services running in our local cluster:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1       &amp;lt;none&amp;gt;        443/TCP        51m
lb           LoadBalancer   10.99.147.117   &amp;lt;pending&amp;gt;     80:30546/TCP   21s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Not that the ClusterIP does not have an external IP while the app Service external IP is pending.&lt;br&gt;
No need to wait for the external IP of the created service, since Minikube does not really deploy a load balancer and this feature will only work if you configure a Load Balancer provider.&lt;/p&gt;

&lt;p&gt;If you are using a Cloud provider, say AWS, an AWS load balancer will be set up for you, GKE will provide a Cloud Load Balancer..etc You may also configure other types of load balancers.&lt;/p&gt;

&lt;p&gt;There are different types of Services that we can use to expose access to the API publicly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ClusterIP&lt;/code&gt;:  is the default Kubernetes service. It exposes the Service on a cluster-internal IP. You can access it using the Kubernetes proxy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vleHQEYZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AmwCXFaN5w0oUBVNk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vleHQEYZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AmwCXFaN5w0oUBVNk.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0"&gt;photo credit&lt;/a&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://kubernetes.io/docs/concepts/services-networking/#nodeport"&gt;&lt;code&gt;NodePort&lt;/code&gt;&lt;/a&gt;: Exposes the Service on each Node’s (VM's) IP at a static port called the &lt;code&gt;NodePort&lt;/code&gt;.  (In our example, we have a single node). This is a primitive way to make an application accessible from outside the cluster and is not suitable for many use cases since your nodes (VMs) IP addresses may change at any time. The service is accessible using &lt;code&gt;&amp;lt;NodeIP&amp;gt;:&amp;lt;NodePort&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vleHQEYZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AmwCXFaN5w0oUBVNk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vleHQEYZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AmwCXFaN5w0oUBVNk.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0"&gt;photo credit&lt;/a&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://kubernetes.io/docs/concepts/services-networking/#loadbalancer"&gt;&lt;code&gt;LoadBalancer&lt;/code&gt;&lt;/a&gt;: This is more advanced than a &lt;code&gt;NodePort&lt;/code&gt; Service. Usually, a Load Balancer exposes a Service externally using a cloud provider’s load balancer. &lt;code&gt;NodePort&lt;/code&gt; and &lt;code&gt;ClusterIP&lt;/code&gt; Services, to which the external load balancer routes, are automatically created.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tdcchvI5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AnPRugWQwhZ72uINE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tdcchvI5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AnPRugWQwhZ72uINE.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0"&gt;photo credit&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;We created a Load Balancer using a Service on our Minikube cluster, but since we don't have a Load Balancer to run, we can access the API service using the Cluster IP followed by the Service internal Port:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;minikube -p workshop ip
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;192.168.99.100
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now execute &lt;code&gt;kubectl get services&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1       &amp;lt;none&amp;gt;        443/TCP        51m
lb           LoadBalancer   10.99.147.117   &amp;lt;pending&amp;gt;     80:30546/TCP   21s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use the IP &lt;code&gt;192.168.99.199&lt;/code&gt; followed by the port &lt;code&gt;30546&lt;/code&gt; to access the API.&lt;/p&gt;

&lt;p&gt;You can test this using a &lt;code&gt;curl&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl "http://192.168.99.100:30546/?l=python&amp;amp;n=1"

---
{"repos":[{"archive_url":"https://api.github.com/repos/vinta/awesome-python/{archive_format}{/ref}","archived":false,"assignees_url":"https://api.github.com/repos/vinta/awesome-python/assignees{/user}","blobs_url":"https://api.github.com/repos/vinta/awesome-python/git/blobs{/sha}","branches_url":"https://api.github.com/repos/vinta/awesome-python/branches{/branch}","clone_url":"https://github.com/vinta/awesome-python.git","collaborators_url":"https://api.github.com/repos/vinta/awesome-python/collaborators{/collaborator}","comments_url":"https://api.github.com/repos/vinta/awesome-python/comments{/number}","commits_url":"https://api.github.com/repos/vinta/awesome-python/commits{/sha}","compare_url":"https://api.github.com/repos/vinta/awesome-python/compare/{base}...{head}","contents_url":"https://api.github.com/repos/vinta/awesome-python/contents/{+path}","contributors_url":"https://api.github.com/repos/vinta/awesome-python/contributors","created_at":"2014-06-27T21:00:06Z","default_branch":"master","deployments_url":"https://api.github.com/repos/vinta/awesome-python/deployments","description":"A curated list of awesome Python frameworks, libraries, software and resources","disabled":false,"downloads_url":"https://api.github.com/repos/vinta/awesome-python/downloads","events_url":"https://api.github.com/repos/vinta/awesome-python/events","fork":false,"forks":13929,"forks_count":13929,"forks_url":"https://api.github.com/repos/vinta/awesome-python/forks","full_name":"vinta/awesome-python","git_commits_url":"https://api.github.com/repos/vinta/awesome-python/git/commits{/sha}","git_refs_url":"https://api.github.com/repos/vinta/awesome-python/git/refs{/sha}","git_tags_url":"https://api.github.com/repos/vinta/awesome-python/git/tags{/sha}","git_url":"git://github.com/vinta/awesome-python.git","has_downloads":true,"has_issues":true,"has_pages":true,"has_projects":false,"has_wiki":false,"homepage":"https://awesome-python.com/","hooks_url":"https://api.github.com/repos/vinta/awesome-python/hooks","html_url":"https://github.com/vinta/awesome-python","id":21289110,"issue_comment_url":"https://api.github.com/repos/vinta/awesome-python/issues/comments{/number}","issue_events_url":"https://api.github.com/repos/vinta/awesome-python/issues/events{/number}","issues_url":"https://api.github.com/repos/vinta/awesome-python/issues{/number}","keys_url":"https://api.github.com/repos/vinta/awesome-python/keys{/key_id}","labels_url":"https://api.github.com/repos/vinta/awesome-python/labels{/name}","language":"Python","languages_url":"https://api.github.com/repos/vinta/awesome-python/languages","license":{"key":"other","name":"Other","node_id":"MDc6TGljZW5zZTA=","spdx_id":"NOASSERTION","url":null},"merges_url":"https://api.github.com/repos/vinta/awesome-python/merges","milestones_url":"https://api.github.com/repos/vinta/awesome-python/milestones{/number}","mirror_url":null,"name":"awesome-python","network_count":13929,"node_id":"MDEwOlJlcG9zaXRvcnkyMTI4OTExMA==","notifications_url":"https://api.github.com/repos/vinta/awesome-python/notifications{?since,all,participating}","open_issues":482,"open_issues_count":482,"owner":{"avatar_url":"https://avatars2.githubusercontent.com/u/652070?v=4","events_url":"https://api.github.com/users/vinta/events{/privacy}","followers_url":"https://api.github.com/users/vinta/followers","following_url":"https://api.github.com/users/vinta/following{/other_user}","gists_url":"https://api.github.com/users/vinta/gists{/gist_id}","gravatar_id":"","html_url":"https://github.com/vinta","id":652070,"login":"vinta","node_id":"MDQ6VXNlcjY1MjA3MA==","organizations_url":"https://api.github.com/users/vinta/orgs","received_events_url":"https://api.github.com/users/vinta/received_events","repos_url":"https://api.github.com/users/vinta/repos","site_admin":false,"starred_url":"https://api.github.com/users/vinta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vinta/subscriptions","type":"User","url":"https://api.github.com/users/vinta"},"private":false,"pulls_url":"https://api.github.com/repos/vinta/awesome-python/pulls{/number}","pushed_at":"2019-08-16T15:21:42Z","releases_url":"https://api.github.com/repos/vinta/awesome-python/releases{/id}","size":4994,"ssh_url":"git@github.com:vinta/awesome-python.git","stargazers_count":71222,"stargazers_url":"https://api.github.com/repos/vinta/awesome-python/stargazers","statuses_url":"https://api.github.com/repos/vinta/awesome-python/statuses/{sha}","subscribers_count":5251,"subscribers_url":"https://api.github.com/repos/vinta/awesome-python/subscribers","subscription_url":"https://api.github.com/repos/vinta/awesome-python/subscription","svn_url":"https://github.com/vinta/awesome-python","tags_url":"https://api.github.com/repos/vinta/awesome-python/tags","teams_url":"https://api.github.com/repos/vinta/awesome-python/teams","trees_url":"https://api.github.com/repos/vinta/awesome-python/git/trees{/sha}","updated_at":"2019-08-17T16:11:44Z","url":"https://api.github.com/repos/vinta/awesome-python","watchers":71222,"watchers_count":71222}],"status":"ok"}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Inconvenient of Load Balancer Service
&lt;/h2&gt;

&lt;p&gt;Typically, load balancers are provisioned by the Cloud provider you're using.&lt;/p&gt;

&lt;p&gt;A load balancer can handle one service but imagine if you have ten services, each one will need a load balancer; this is when it becomes costly.&lt;/p&gt;

&lt;p&gt;The best solution, in this case, is set up an Ingress controller that acts as a smart router and can be deployed at the edge of the cluster, therefore in the front of all the services you deploy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m7EMl-gY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AOlQ6zKtb0jSGz9N5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m7EMl-gY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AOlQ6zKtb0jSGz9N5.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0"&gt;photo credit&lt;/a&gt;)&lt;/p&gt;

&lt;h1&gt;
  
  
  An API Gateway
&lt;/h1&gt;

&lt;p&gt;Ambassador is an Open Source Kubernetes-Native API Gateway built on the Envoy Proxy. It provides a solution for traffic management and application security. It's described as is a specialized control plane that translates Kubernetes annotations to Envoy configuration.&lt;/p&gt;

&lt;p&gt;All traffic is directly handled by the high-performance Envoy Proxy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CwrdLGZm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AF_w9BHPSoH-AVplC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CwrdLGZm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/700/0%2AF_w9BHPSoH-AVplC.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://www.getambassador.io/concepts/architecture"&gt;photo credits&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;As it's described in &lt;a href="https://www.envoyproxy.io/"&gt;Envoy official website&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally built at &lt;strong&gt;Lyft&lt;/strong&gt;, Envoy is a high-performance C++ distributed proxy designed for single services and applications, as well as a communication bus and “universal data plane” designed for large microservice “service mesh” architectures. Built on the learnings of solutions such as NGINX, HAProxy, hardware load balancers, and cloud load balancers, Envoy runs alongside every application and abstracts the network by providing common features in a platform-agnostic manner. When all service traffic in an infrastructure flows via an Envoy mesh, it becomes easy to visualize problem areas via consistent observability, tune overall performance, and add substrate features in a single place.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We are going to use Ambassador as an API Gateway; we no longer need the load balancer service we created in the first part. Let's remove it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl delete -f kubernetes/api-service.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To deploy Ambassador in your &lt;strong&gt;default&lt;/strong&gt; namespace, first, you need to check if Kubernetes has RBAC enabled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl cluster-info dump --namespace kube-system | grep authorization-mode
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If RBAC is enabled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; https://getambassador.io/yaml/ambassador/ambassador-rbac.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Without RBAC, you can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; https://getambassador.io/yaml/ambassador/ambassador-no-rbac.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Ambassador is deployed as a Kubernetes Service that references the ambassador Deployment you deployed previously. Create the following YAML and put it in a file called &lt;code&gt;kubernetes/ambassador-service.yaml&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
apiVersion: v1
kind: Service
metadata:
  name: ambassador
spec:
  type: LoadBalancer
  externalTrafficPolicy: Local
  ports:
   - port: 80
     targetPort: 8080
  selector:
    service: ambassador
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Deploy the service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f ambassador-service.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's use this file containing the Deployment configuration for our API as well as the Ambassador Service configuration relative to the same Deployment. Call this file &lt;code&gt;kubernetes/api-deployment-with-ambassador.yaml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
apiVersion: v1
kind: Service
metadata:
  name: tgr
  annotations:
    getambassador.io/config: |
      ---
      apiVersion: ambassador/v1
      kind: Mapping
      name: tgr_mapping
      prefix: /
      service: tgr:5000

spec:
  ports:
  - name: tgr
    port: 5000
    targetPort: 5000
  selector:
    app: tgr
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tgr
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tgr
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: tgr
    spec:
      containers:
      - name: tgr
        image: eon01/tgr:1
        ports:
          - containerPort: 5000
        env:
          - name: CLIENT_ID
            value: "xxxx"
          - name: CLIENT_SECRET
            value: "xxxxxxxxxx"
          - name: ENV
            value: "prod"
          - name: DEBUG
            value: "False"
          - name: HOST
            value: "0.0.0.0"
          - name: PORT
            value: "5000"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Deploy the previously created configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f kubernetes/api-deployment-with-ambassador.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's test things out: We need the external IP for Ambassador:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get svc -o wide ambassador
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE    SELECTOR
ambassador   LoadBalancer   10.103.201.130   &amp;lt;pending&amp;gt;     80:30283/TCP   9m2s   service=ambassador
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you are using Minikube, it is normal to see the external IP in the &lt;code&gt;pending&lt;/code&gt; state.&lt;/p&gt;

&lt;p&gt;We can use &lt;code&gt;minikube -p workshop  service list&lt;/code&gt; to get the Ambassador IP. You will get an output similar to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|-------------|------------------|-----------------------------|
|  NAMESPACE  |       NAME       |             URL             |
|-------------|------------------|-----------------------------|
| default     | ambassador       | http://192.168.99.100:30283 |
| default     | ambassador-admin | http://192.168.99.100:30084 |
| default     | kubernetes       | No node port                |
| default     | tgr              | No node port                |
| kube-system | kube-dns         | No node port                |
|-------------|------------------|-----------------------------|

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can use the API using the IP &lt;code&gt;http://192.168.99.100:30283&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl "http://192.168.99.100:30283/?l=python&amp;amp;n=1"
---
{"repos":[{"archive_url":"https://api.github.com/repos/vinta/awesome-python/{archive_format}{/ref}","archived":false,"assignees_url":"https://api.github.com/repos/vinta/awesome-python/assignees{/user}","blobs_url":"https://api.github.com/repos/vinta/awesome-python/git/blobs{/sha}","branches_url":"https://api.github.com/repos/vinta/awesome-python/branches{/branch}","clone_url":"https://github.com/vinta/awesome-python.git","collaborators_url":"https://api.github.com/repos/vinta/awesome-python/collaborators{/collaborator}","comments_url":"https://api.github.com/repos/vinta/awesome-python/comments{/number}","commits_url":"https://api.github.com/repos/vinta/awesome-python/commits{/sha}","compare_url":"https://api.github.com/repos/vinta/awesome-python/compare/{base}...{head}","contents_url":"https://api.github.com/repos/vinta/awesome-python/contents/{+path}","contributors_url":"https://api.github.com/repos/vinta/awesome-python/contributors","created_at":"2014-06-27T21:00:06Z","default_branch":"master","deployments_url":"https://api.github.com/repos/vinta/awesome-python/deployments","description":"A curated list of awesome Python frameworks, libraries, software and resources","disabled":false,"downloads_url":"https://api.github.com/repos/vinta/awesome-python/downloads","events_url":"https://api.github.com/repos/vinta/awesome-python/events","fork":false,"forks":13933,"forks_count":13933,"forks_url":"https://api.github.com/repos/vinta/awesome-python/forks","full_name":"vinta/awesome-python","git_commits_url":"https://api.github.com/repos/vinta/awesome-python/git/commits{/sha}","git_refs_url":"https://api.github.com/repos/vinta/awesome-python/git/refs{/sha}","git_tags_url":"https://api.github.com/repos/vinta/awesome-python/git/tags{/sha}","git_url":"git://github.com/vinta/awesome-python.git","has_downloads":true,"has_issues":true,"has_pages":true,"has_projects":false,"has_wiki":false,"homepage":"https://awesome-python.com/","hooks_url":"https://api.github.com/repos/vinta/awesome-python/hooks","html_url":"https://github.com/vinta/awesome-python","id":21289110,"issue_comment_url":"https://api.github.com/repos/vinta/awesome-python/issues/comments{/number}","issue_events_url":"https://api.github.com/repos/vinta/awesome-python/issues/events{/number}","issues_url":"https://api.github.com/repos/vinta/awesome-python/issues{/number}","keys_url":"https://api.github.com/repos/vinta/awesome-python/keys{/key_id}","labels_url":"https://api.github.com/repos/vinta/awesome-python/labels{/name}","language":"Python","languages_url":"https://api.github.com/repos/vinta/awesome-python/languages","license":{"key":"other","name":"Other","node_id":"MDc6TGljZW5zZTA=","spdx_id":"NOASSERTION","url":null},"merges_url":"https://api.github.com/repos/vinta/awesome-python/merges","milestones_url":"https://api.github.com/repos/vinta/awesome-python/milestones{/number}","mirror_url":null,"name":"awesome-python","network_count":13933,"node_id":"MDEwOlJlcG9zaXRvcnkyMTI4OTExMA==","notifications_url":"https://api.github.com/repos/vinta/awesome-python/notifications{?since,all,participating}","open_issues":482,"open_issues_count":482,"owner":{"avatar_url":"https://avatars2.githubusercontent.com/u/652070?v=4","events_url":"https://api.github.com/users/vinta/events{/privacy}","followers_url":"https://api.github.com/users/vinta/followers","following_url":"https://api.github.com/users/vinta/following{/other_user}","gists_url":"https://api.github.com/users/vinta/gists{/gist_id}","gravatar_id":"","html_url":"https://github.com/vinta","id":652070,"login":"vinta","node_id":"MDQ6VXNlcjY1MjA3MA==","organizations_url":"https://api.github.com/users/vinta/orgs","received_events_url":"https://api.github.com/users/vinta/received_events","repos_url":"https://api.github.com/users/vinta/repos","site_admin":false,"starred_url":"https://api.github.com/users/vinta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vinta/subscriptions","type":"User","url":"https://api.github.com/users/vinta"},"private":false,"pulls_url":"https://api.github.com/repos/vinta/awesome-python/pulls{/number}","pushed_at":"2019-08-16T15:21:42Z","releases_url":"https://api.github.com/repos/vinta/awesome-python/releases{/id}","size":4994,"ssh_url":"git@github.com:vinta/awesome-python.git","stargazers_count":71269,"stargazers_url":"https://api.github.com/repos/vinta/awesome-python/stargazers","statuses_url":"https://api.github.com/repos/vinta/awesome-python/statuses/{sha}","subscribers_count":5254,"subscribers_url":"https://api.github.com/repos/vinta/awesome-python/subscribers","subscription_url":"https://api.github.com/repos/vinta/awesome-python/subscription","svn_url":"https://github.com/vinta/awesome-python","tags_url":"https://api.github.com/repos/vinta/awesome-python/tags","teams_url":"https://api.github.com/repos/vinta/awesome-python/teams","trees_url":"https://api.github.com/repos/vinta/awesome-python/git/trees{/sha}","updated_at":"2019-08-19T08:21:51Z","url":"https://api.github.com/repos/vinta/awesome-python","watchers":71269,"watchers_count":71269}],"status":"ok"}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Edge Proxy vs Service Mesh
&lt;/h2&gt;

&lt;p&gt;You may have heard of tools like Istio and Linkerd and it may be confusing to compare Ambassador or Envoy to these tools. We are going to understand the differences here.&lt;/p&gt;

&lt;p&gt;Istio is described as a tool to connect, secure, control, and observe services.The same features are implemented by its alternatives like Linkerd or Consul. These tools are called Service Mesh.&lt;/p&gt;

&lt;p&gt;Ambassador is a, API gateway for services (or microservices) and it's deployed at the edge of your network. It routes incoming traffic to a cluster internal services and this what we call "north-south" traffic.&lt;/p&gt;

&lt;p&gt;Istio, in the other hand, is a service mesh for Kubernetes services (or microservices). It's designed to add application-level Layer (L7) observability, routing, and resilience to service-to-service traffic and this is what we call "east-west" traffic.&lt;/p&gt;

&lt;p&gt;The fact that both Istio and Ambassador are built using Envoy, does not mean they have the same features or usability. Therefore, they can be deployed together in the same cluster.&lt;/p&gt;

&lt;h1&gt;
  
  
  Accessing the Kubernetes API
&lt;/h1&gt;

&lt;p&gt;If you remember, when we created our Minikube cluster we used &lt;code&gt;--extra-config=apiserver.enable-swagger-ui=true&lt;/code&gt;. This configuration makes the Kubernetes API "browsable" via a web browser.&lt;/p&gt;

&lt;p&gt;When using Minikube, in order to access the Kubernetes API using a browser, we need to create a proxy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl proxy --port=8080 &amp;amp;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we can test this out using Curl:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl http://localhost:8080/api/
---
{
  "kind": "APIVersions",
  "versions": [
    "v1"
  ],
  "serverAddressByClientCIDRs": [
    {
      "clientCIDR": "0.0.0.0/0",
      "serverAddress": "192.168.99.100:8443"
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can get a list of the APIs and resources we can access by visiting: &lt;code&gt;http://localhost:8080/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For instance, we can get a list of metrics here &lt;code&gt;http://localhost:8080/metrics&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using an API Client
&lt;/h2&gt;

&lt;p&gt;We are going to use the Kubernetes client:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install kubernetes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# import json
# import requests
# @app.route('/pods')
# def monitor():
#
#     api_url = "http://kubernetes.default.svc/api/v1/pods/"
#     response = requests.get(api_url)
#     if response.status_code == 200:
#         return json.loads(response.content.decode('utf-8'))
#     else:
#         return None
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing the API from inside a POD
&lt;/h2&gt;

&lt;p&gt;By default, a Pod is associated with a service account, and a credential (token) for that service account is placed into the filesystem tree of each container in that Pod, at &lt;code&gt;/var/run/secrets/kubernetes.io/serviceaccount/token&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's try to go inside a Pod and access the API. Use &lt;code&gt;kubectl get pods&lt;/code&gt; to get a list of pods&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME                          READY   STATUS    RESTARTS   AGE
ambassador-64d8b877f9-4bzvn   1/1     Running   0          103m
ambassador-64d8b877f9-b68w6   1/1     Running   0          103m
ambassador-64d8b877f9-vw9mm   1/1     Running   0          103m
tgr-8d78d599f-pt5xx           1/1     Running   0          4m17s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now log inside the API Pod:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl exec -it tgr-8d78d599f-pt5xx bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Assign the token to a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;KUBE_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice that the token file is added automatically by Kubernetes.&lt;/p&gt;

&lt;p&gt;We also have other variables already set like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo $KUBERNETES_SERVICE_HOST
10.96.0.1

echo $KUBERNETES_PORT_443_TCP_PORT
443

echo $HOSTNAME
tgr-8d78d599f-pt5xx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We are going to use these variables to access the list of Pods using this Curl command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/default/pods/$HOSTNAME
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;At this stage, you should have an error output saying that you don't have the rights to access this API endpoint, which is normal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "pods \"tgr-8d78d599f-pt5xx\" is forbidden: User \"system:serviceaccount:default:default\" cannot get resource \"pods\" in API group \"\" in the namespace \"default\"",
  "reason": "Forbidden",
  "details": {
    "name": "tgr-8d78d599f-pt5xx",
    "kind": "pods"
  },
  "code": 403
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The Pod is using the default Service Account and it does not have the right to list the Pods.&lt;/p&gt;

&lt;p&gt;In order to fix this, exit the container and create a file called &lt;code&gt;kubernetes/service-account.yaml&lt;/code&gt;, add the following lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: pods-list
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["list"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: pods-list
subjects:
- kind: ServiceAccount
  name: default
  namespace: default
roleRef:
  kind: ClusterRole
  name: pods-list
  apiGroup: rbac.authorization.k8s.io

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then apply the new configuration using &lt;code&gt;kubectl apply -f kubernetes/service-account.yaml&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now you can access the list of Pods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "kind": "PodList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/namespaces/default/pods/",
    "resourceVersion": "19589"
  },
  "items": [
    {
      "metadata": {
        "name": "ambassador-64d8b877f9-4bzvn",
        "generateName": "ambassador-64d8b877f9-",
        "namespace": "default",
        "selfLink": "/api/v1/namespaces/default/pods/ambassador-64d8b877f9-4bzvn",
        "uid": "63f62ede-de77-441d-85f7-daf9cbc7040f",
        "resourceVersion": "1047",
        "creationTimestamp": "2019-08-19T08:12:47Z",
        "labels": {
          "pod-template-hash": "64d8b877f9",
          "service": "ambassador"
        },
        "annotations": {
          "consul.hashicorp.com/connect-inject": "false",
          "sidecar.istio.io/inject": "false"
        },
        "ownerReferences": [
          {
            "apiVersion": "apps/v1",
            "kind": "ReplicaSet",
            "name": "ambassador-64d8b877f9",
            "uid": "383c2e4b-7179-4806-b7bf-3682c7873a10",
            "controller": true,
            "blockOwnerDeletion": true
          }
        ]
      },
      "spec": {
        "volumes": [
          {
            "name": "ambassador-token-rdqq6",
            "secret": {
              "secretName": "ambassador-token-rdqq6",
              "defaultMode": 420
            }
          }
        ],
        "containers": [
          {
            "name": "ambassador",
            "image": "quay.io/datawire/ambassador:0.75.0",
            "ports": [
              {
                "name": "http",
                "containerPort": 8080,
                "protocol": "TCP"
              },
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What about creating your own monitoring/observability solution using Python (or any other programming language) and the Kubernetes API ?&lt;br&gt;
This could be probably the subject of the upcoming workshop.&lt;/p&gt;

&lt;p&gt;You can clone and contribute to &lt;a href="https://github.com/eon01/kubernetes-workshop"&gt;the git repos&lt;/a&gt;. Give it a star if you like it :)&lt;/p&gt;

</description>
      <category>docker</category>
      <category>kubernetes</category>
      <category>python</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
