admin管理员组

文章数量:1647869

写在前面

本文看下学习k8s需要的基础环境,包括运行环境,docker等。

1:准备linux运行环境

环境:win10

虚拟化工具:VMware-workstation-full-15.1.0

linux:ubuntu-22.04.1-desktop-amd64.iso 。或者如下:
这里 。然后:


取所需,一般都有。

安装完毕后我们开始准备其它环境。

2:安装docker环境

安装:

sudo apt install docker.io
sudo service docker start         #启动docker服务
sudo usermod -aG docker ${USER}   #当前用户加入docker组

安装后验证:

dongyunqi@dongyunqi-virtual-machine:/etc/docker$ docker version
Client:
 Version:           20.10.12
 ...

镜像下载加速:

dongyunqi@dongyunqi-virtual-machine:/etc/docker$ sudo touch daemon.json
[sudo] password for dongyunqi: 
dongyunqi@dongyunqi-virtual-machine:/etc/docker$ sudo tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://8csof3cn.mirror.aliyuncs"]
> }
> EOF
{
  "registry-mirrors": ["https://8csof3cn.mirror.aliyuncs"]
}

3:运行镜像

Last login: Sat Dec 31 15:15:57 2022 from 192.168.64.1
dongyunqi@dongyunqi-virtual-machine:~$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:c77be1d3a47d0caf71a82dd893ee61ce01f32fc758031a6ec4cf1389248bb833
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
dongyunqi@dongyunqi-virtual-machine:~$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   15 months ago   13.3kB
dongyunqi@dongyunqi-virtual-machine:~$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker/

For more examples and ideas, visit:
 https://docs.docker/get-started/

上面运行hello-world的输出说明其实就是docker的工作过程和原理,如下图:

4:其它

4.1:docker虚拟化的原理

容器的本质就是隔离运行的进程,所以其就是一个进程,但是是隔离的,那么是如何实现隔离呢?docker并没有实现什么新的技术,而是基于Linux现有的技术实现的,分别是namespace,cgroup,chroot,分别如下:

namespace:创建独立文件系统,主机名,网络,即可以创建独立于主机硬件的独立硬件系统(这样隔离就实现了)。
cgroup:实现对进程的资源使用限制,如限制指定的CPU资源,内存资源等(这样资源限制就实现了)。
chroot:更改进程根目录,即限制可以访问的目录,只访问通过namespae创建的独立文件系统(这样就拥有了能访问且只能访问的独立文件系统)。

4.2:tag命令规范

格式名字:标签,名字表明了应用的身份,比如 busybox、Alpine、Nginx、Redis 等等。标签(tag)则可以理解成是为了区分不同版本的应用而做的额外标记,任何字符串都可以,比如 3.15 是纯数字的版本号、jammy 是项目代号、1.21-alpine 是版本号加操作系统名等等。其中有一个比较特殊的标签叫“latest”,它是默认的标签,如果只提供名字没有附带标签,那么就会使用这个默认的“latest”标签。如下:

docker pull alpine:3.15
docker pull ubuntu:jammy
docker pull nginx:1.21-alpine
docker pull nginx:alpine
docker pull redis

上述的ImageID是镜像唯一标识,通过SHA256(镜像文件),获得的64字节长度字符串,之类是以16进制形式标识。

4.3:镜像的结构

假设我们现在都基于Ubuntu的基础镜像来生成新的镜像,进行了如下3个操作:

1:拷贝jdk,可运行jar包到镜像中,并设置相关的环境变量,这样就构成一个新的镜像A
2:拷贝一个编译后的VUE项目到镜像中,并设置VUE相关的环境变量,这样就构成一个新的镜像B
3:拷贝Nginx相关文件到镜像中,并设置Nginx相关的参数,这样就构成了一个新的镜像C

此时我们就有了3个镜像,分别是镜像A,镜像B,镜像C,分别包含了公共的Ubuntu文件系统和自己特有的文件,如果是我们将公共的Ubuntu文件系统在每个镜像中都包含一份的话,就会造成磁盘空间的浪费,也会给网络传输等操作带来额外消耗,基于此,docker选择了将公共中的部分抽取出来,并进行共享的方式,这种方式就叫做层Layer,我们的场景中可能如下图:

本文标签: 环境K8s