DEV Community

Cover image for K8s Exercise: Multi-container
Akshay Rao
Akshay Rao

Posted on • Updated on

K8s Exercise: Multi-container

Introduction
Hi, I am Akshay Rao, will be starting a exercise series on k8s.
In this blog there will not explanation only problems and solutions.if you want explanation have a look at this series:-
https://dev.to/aksrao1998/series/24887

Pre-requisite
have minikube or kind running in the local machine.

Note:- k is alias for kubectl.

Let's Start

Problem
Create a pod with an nginx container exposed on port 80. Add a busybox init container which downloads a page using "wget -O /work-dir/index.html http://neverssl.com/online". Make a volume of type emptyDir and mount it in both containers. For the nginx container, mount it on "/usr/share/nginx/html" and for the initcontainer, mount it on "/work-dir". When done, get the IP of the created pod and create a busybox pod and run "wget -O- IP"

Solution

k run multi-container --image=nginx --dry-run=client -o yaml > multi-container.yaml

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: multi-container
  name: multi-container
spec:
  initContainers: 
  - args: 
    - /bin/sh 
    - -c 
    - "wget -O /work-dir/index.html http://neverssl.com/online"
    image: busybox 
    name: box 
    volumeMounts: 
    - name: vol 
      mountPath: /work-dir 
  containers:
  - image: nginx
    name: nginx
    ports:
    - containerPort: 80
    volumeMounts: 
    - name: vol 
      mountPath: /usr/share/nginx/html 
  volumes: 
  - name: vol 
    emptyDir: {}

[k8s-ckad (⎈|minikube:hands-on)]$ k create -f multi-container.yaml 
pod/multi-container created
[k8s-ckad (⎈|minikube:hands-on)]$ k get po -o wide
NAME              READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
multi-container   1/1     Running   0          84s   172.17.0.4   minikube   <none>           <none>

#verify

[k8s-ckad (⎈|minikube:hands-on)]$ k run test --image=busybox --restart=Never -it -- /bin/sh -c "wget -O- $(kubectl get pod multi-container -o jsonpath='{.status.podIP}')"
Connecting to 172.17.0.4 (172.17.0.4:80)
writing to stdout
<html>
        <head>
                <title>NeverSSL - helping you get online</title>

                <style>
                body {
                        font-family: Montserrat, helvetica, arial, sans-serif; 
                        font-size: 16x;
                        color: #444444;
                        margin: 0;
                }
                h2 {
                        font-weight: 700;
                        font-size: 1.6em;
                        margin-top: 30px;
                }
                p {
                        line-height: 1.6em;
                }
                .container {
                        max-width: 650px;
                        margin: 20px auto 20px auto;
                        padding-left: 15px;
                        padding-right: 15px
                }
                .header {
                        background-color: #42C0FD;
                        color: #FFFFFF;
                        padding: 10px 0 10px 0;
                        font-size: 2.2em;
                }
                <!-- CSS from Mark Webster https://gist.github.com/markcwebster/9bdf30655cdd5279bad13993ac87c85d -->
                </style>
        </head>
        <body>

        <div class="header">
                <div class="container">
                <h1>NeverSSL</h1>
                </div>
        </div>

        <div class="content">
        <div class="container">

        <h2>What?</h2>
        <p>This website is for when you try to open Facebook, Google, Amazon, etc
        on a wifi network, and nothing happens. Type "http://neverssl.com"
        into your browser's url bar, and you'll be able to log on.</p>

        <h2>How?</h2>
        <p>neverssl.com will never use SSL (also known as TLS). No
        encryption, no strong authentication, no <a
        href="https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security">HSTS</a>,
        no HTTP/2.0, just plain old unencrypted HTTP and forever stuck in the dark
        ages of internet security.</p> 

        <h2>Why?</h2>
        <p>Normally, that's a bad idea. You should always use SSL and secure
        encryption when possible. In fact, it's such a bad idea that most websites
        are now using https by default.</p>

        <p>And that's great, but it also means that if you're relying on
        poorly-behaved wifi networks, it can be hard to get online.  Secure
        browsers and websites using https make it impossible for those wifi
        networks to send you to a login or payment page. Basically, those networks
        can't tap into your connection just like attackers can't. Modern browsers
        are so good that they can remember when a website supports encryption and
        even if you type in the website name, they'll use https.</p> 

        <p>And if the network never redirects you to this page, well as you can
        see, you're not missing much.</p>

        <a href="https://twitter.com/neverssl">Follow @neverssl</a>

        </div>
        </div>
        </body>
</html>
-                    100% |********************************|  2238  0:00:00 ETA
written to stdout

Enter fullscreen mode Exit fullscreen mode

Top comments (0)