K8s 練習 - part 2

K8s

前言

玩K8s一定要會先Docker及Dockerfile,所以先來學學Docker喽~

安裝

  1. pacman -S docker
  2. systemctl start docker.service
  3. systemctl enable docker.service

基本cmd

  1. docker --version: 查看版本
Docker version 18.03.0-ce, build 0520e24302
  1. docker search ubuntu: 查找線上的Image
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                 Ubuntu is a Debian-based Linux operating sys…   9041                [OK]
dorowu/ubuntu-desktop-lxde-vnc                         Ubuntu with openssh-server and NoVNC            260                                     [OK]
rastasheep/ubuntu-sshd                                 Dockerized SSH service, built on top of offi…   195                                     [OK]
consol/ubuntu-xfce-vnc                                 Ubuntu container with "headless" VNC session…   147                                     [OK]
ansible/ubuntu14.04-ansible                            Ubuntu 14.04 LTS with ansible                   95                                      [OK]
ubuntu-upstart                                         Upstart is an event-based replacement for th…   93                  [OK]
neurodebian                                            NeuroDebian provides neuroscience research s…   55                  [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          48                                      [OK]
ubuntu-debootstrap                                     debootstrap --variant=minbase --components=m…   40                  [OK]
nuagebec/ubuntu                                        Simple always updated Ubuntu docker images w…   23                                      [OK]
tutum/ubuntu                                           Simple Ubuntu docker images with SSH access     18
i386/ubuntu                                            Ubuntu is a Debian-based Linux operating sys…   16
1and1internet/ubuntu-16-apache-php-7.0                 ubuntu-16-apache-php-7.0                        13                                      [OK]
ppc64le/ubuntu                                         Ubuntu is a Debian-based Linux operating sys…   12
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4      ubuntu-16-nginx-php-5.6-wordpress-4             7                                       [OK]
eclipse/ubuntu_jdk8                                    Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, …   7                                       [OK]
codenvy/ubuntu_jdk8                                    Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, …   5                                       [OK]
darksheer/ubuntu                                       Base Ubuntu Image -- Updated hourly             5                                       [OK]
pivotaldata/ubuntu                                     A quick freshening-up of the base Ubuntu doc…   2
1and1internet/ubuntu-16-sshd                           ubuntu-16-sshd                                  1                                       [OK]
smartentry/ubuntu                                      ubuntu with smartentry                          1                                       [OK]
ossobv/ubuntu                                          Custom ubuntu image from scratch (based on o…   0
1and1internet/ubuntu-16-healthcheck                    ubuntu-16-healthcheck                           0                                       [OK]
pivotaldata/ubuntu-gpdb-dev                            Ubuntu images for GPDB development              0
paasmule/bosh-tools-ubuntu                             Ubuntu based bosh-cli                           0                                       [OK]
  1. docker pull ubuntu: 下載image
Using default tag: latest
latest: Pulling from library/ubuntu
84ed7d2f608f: Pull complete
be2bf1c4a48d: Pull complete
a5bdc6303093: Pull complete
e9055237d68d: Pull complete
Digest: sha256:868fd30a0e47b8d8ac485df174795b5e2fe8a6c8f056cc707b232d65b8a1ab68
Status: Downloaded newer image for ubuntu:latest
  1. sudo docker images: 查看目前local docker的所有image
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              1d9c17228a9e        8 days ago          86.7MB
  1. sudo docker run -i -t ubuntu /bin/bash: 分配一個terminal進入交互操作模式
root@59954db08b43:/# uname -a
Linux 59954db08b43 4.14.79-boot2docker #1 SMP Thu Nov 8 01:56:42 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

結束離開: exit or Ctrl-D
Detach: Ctrl-P + Ctrl-Q
Attach: sudo docker attach 59954db08b43

  1. sudo docker ps -a: 查看所有的container
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                           PORTS               NAMES
59954db08b43        ubuntu              "/bin/bash"         About an hour ago   Up About an hour                                     xenodochial_morse
df2e13c45fa6        ubuntu              "/bin/bash"         About an hour ago   Exited (0) About an hour ago                         confident_feynman
eb51172d9f86        ubuntu              "/bin/bash"         About an hour ago   Exited (127) About an hour ago                       tender_shirley
  1. docker cp /path/to/file1 59954db08b43:/path/to/file2: copy file from host to docker container

  2. docker cp 59954db08b43:/path/to/file1 /path/to/file2: copy file from docker container to host

  3. docker rm 59954db08b43: remove container

Dockerfile

主要就參考官方文件

建立檔案

建立一個Dockerfile

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

requirements.txt

Flask
Redis

app.py

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

Build

docker build --tag=friendlyhello .

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
friendlyhello       latest              d24a394ec9eb        41 minutes ago      131MB

Run

docker run -p 4000:80 friendlyhello