Docker与K8S学习-1安装和配置

Docker与K8s学习-1安装和配置

Docker与K8s学习-1安装和配置

换源

apt换源

1.备份原来的源:

1
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

2.2.打开/etc/apt/sources.list文件

1
nano /etc/apt/sources.list

国内apt源,这里以阿里源和清华源为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#添加清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse multiverse

添加至/etc/apt/sources.list文件中并保存。

3.更新

1
2
$ sudo apt-get update
$ sudo apt-get upgrade

pip换源

1.在根目录下创建/修改~/.pip/pip.confpip配置文件;

1
touch ~/.pip/pip.conf

2.进入文件新增/修改内容;

1
2
3
4
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn

Docker

下载安装

1
2
$ sudo apt install docker.io
$ sudo pip install docker-compose

检查是否安装成功

1
$ sudo docker version

image-20230509122142527

docker换源

1.创建或修改 /etc/docker/daemon.json 文件

1
nano  /etc/docker/daemon.json

2.插入源连接

1
2
3
4
5
6
7
8
9
{
"registry-mirrors": [
"https://registry.hub.docker.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn",
"https://registry.docker-cn.com"
]
}

3.查看是否换源成功

1
$ docker info

image-20230512033234457

重启docker,并设置开机启动

1
2
3
4
5
sudo systemctl restart docker

systemcd enable docker

systemcd start docker

K8S

本地安装K8S

执行以下命令安装 https 工具以及 k8s

1
2
apt-get update && apt-get install -y apt-transport-https curl
apt-get install -y kubelet kubeadm kubectl --allow-unauthenticated

执行过程中可能出现报错

1
2
3
4
5
6
7
8
9
10
No apt package "kubeadm", but there is a snap with that name.
Try "snap install kubeadm"


No apt package "kubectl", but there is a snap with that name.
Try "snap install kubectl"


No apt package "kubelet", but there is a snap with that name.
Try "snap install kubelet"

这是使用的源里没有这个库,找一个有这个库的源添加上去即可:

获取aliyun的key

1
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add

打开/etc/apt/sources.list文件,添加源链接:

1
deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main

再次执行安装命令

最后查看是否安装成功:

1
kubeadm init

image-20230512034345692

(到这里,docker和K8s的本地安装工作已经全部完成,接下来就是配置镜像了)

Docker容器中配置安装K8S

docker拉取ubuntu容器

1
docker pull ubuntu

image-20230512232424025

启动并进入容器

1
docker run -it ubuntu /bin/bash

image-20230512232543441

配置安装K8s

依次执行下面的安装命令,途中遇到的软件库的版本过高或过低,先卸载重装即可:

1
2
3
4
5
6
7
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - #获取aliyun的源的key
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF #将被源配置写到配置文件中
sudo apt-get update
sudo apt-get install -y kubectl kubeadm kubelet #安装最新版本

安装过程需要时间较久,可以看点其他相关的 :(

image-20230513015901872

下载完成后,保存配置退出容器:

1
$ exit

打包方法一、将Kubernetes容器打包为Docker镜像

查看id号,创造镜像

1
2
$ docker ps -a
$ docker commit <container id> twe1v3-k8s-image#这里的id是ubuntu的id号重命名为twe1v3-k8s-image

image-20230513021215073

使用以下命令将Kubernetes容器打包为Docker镜像:

1
$ docker save twe1v3-k8s-image > twe1v3-k8s-image.tar

上传到镜像仓库:可以使用以下命令将Docker镜像上传到镜像仓库:

1
2
3
$ docker login
$ docker tag twe1v3-k8s-image <repo url>/twe1v3-k8s-image
$ docker push <repo url>/twe1v3-k8s-image

至此常规打包的方式已经完成,鼓掌撒花!!!

下面的内容将是使用现行的方法使用Dockerfile的方式来打包镜像:

打包方法二、使用Dockerfile的方式来封装K8s的docker镜像

这里的所有前置步骤都可以相同,主要是使用Dockerfile来封装配置安装的过程,省去了后续新建时的复杂操作

编写Dockerfile

Dockerfile中写入以下内容:

1
2
3
4
5
6
7
8
9
10
FROM ubuntu:latest
RUN apt-get update && apt-get -y install gnupg apt-transport-https curl \
&& curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - \
&& echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list \
&& apt-get update \
&& apt-get -y install kubectl kubeadm kubelet
&& systemctl enable kubelet
&& systemctl restart kubelet

CMD ["bash"]

这里的Dockerfile内容拉取的镜像请修改为自己先前拉取的ubuntu镜像,保存后退出,写入Dockerfile的时候记得把注释去掉嗷。

构建Docker镜像

1
$ docker build -t twe1v3-df-k8s-image .

image-20230513024522287

执行docker imags查看是否新建成功:

运行镜像,并进入容器内查看是否安装成功:

1
2
3
docker run -it twe1v3-df-k8s-image bash

kubectl

image-20230513032748974

至此已完成docker的ubuntu的k8s镜像的封装和打包。

配置一个sh或许会更快键哈哈哈