一、docker
1.前期部署
1.1安装docker
yum -y install docker // -y 可选参数,跳过确认。
yum list docker* // * 列出所有可安装的包
docker -v // 可选,查看docker版本
1.2启动docker
systemctl start docker
1.3移除docker (拓展学习)
点击linux卸载docker跳转
2.使用docker
2.1搜索镜像
docker search 镜像名
[root@master1 ~]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos DEPRECATED; The official build of CentOS. 7414 [OK]
docker.io docker.io/kasmweb/centos-7-desktop CentOS 7 desktop for Kasm Workspaces 26
docker.io docker.io/couchbase/centos7-systemd centos7-systemd images with additional deb... 5 [OK]
docker.io docker.io/dokken/centos-7 CentOS 7 image for kitchen-dokken 4
docker.io docker.io/continuumio/centos5_gcc5_base 3
docker.io docker.io/dokken/centos-stream-8 3
docker.io docker.io/dokken/centos-8 CentOS 8 image for kitchen-dokken 2
docker.io docker.io/dokken/centos-stream-9 2
docker.io docker.io/spack/centos6 CentOS 6 with Spack preinstalled 1
docker.io docker.io/spack/centos7 CentOS 7 with Spack preinstalled 1
docker.io docker.io/bitnami/centos-base-buildpack Centos base compilation image 0 [OK]
docker.io docker.io/bitnami/centos-extras-base 0
docker.io docker.io/corpusops/centos centos corpusops baseimage 0
docker.io docker.io/corpusops/centos-bare https://github.com/corpusops/docker-images/ 0
docker.io docker.io/couchbase/centos-69-sdk-build 0
docker.io docker.io/couchbase/centos-69-sdk-nodevtoolset-build 0
docker.io docker.io/couchbase/centos-70-sdk-build 0
docker.io docker.io/couchbase/centos-72-java-sdk 0
docker.io docker.io/couchbase/centos-72-jenkins-core 0
docker.io docker.io/datadog/centos-i386 0
docker.io docker.io/dokken/centos-5 EOL DISTRO: For use with kitchen-dokken, B... 0
docker.io docker.io/dokken/centos-6 CentOS 6 image for kitchen-dokken 0
docker.io docker.io/fnndsc/centos-python3 Source for a slim Centos-based Python3 ima... 0 [OK]
docker.io docker.io/spack/centos-stream 0
docker.io docker.io/ustclug/centos Official CentOS Image with USTC Mirror 0
2.2拉取镜像
docker pull 镜像名
[root@master1 ~]# docker pull docker.io/couchbase/centos7-systemd
2.3查看已安装的镜像
docker images
[root@master1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat 8.1 4913e8659466 10 days ago 851 MB
registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat 9.1 1c384aa76aa9 10 days ago 1.1 GB
registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat 2.0 bcfcf26644dc 3 weeks ago 845 MB
docker.io/rancher/mirrored-flannelcni-flannel v0.20.1 d66192101c64 3 weeks ago 59.4 MB
registry.cn-hangzhou.aliyuncs.com/houxiongxiong/flannel v0.20.1 d66192101c64 3 weeks ago 59.4 MB
swr.cn-north-4.myhuaweicloud.com/houxiongxiong/tomcat 1.0 6118561ff6a8 4 weeks ago 856 MB
registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat <none> 6118561ff6a8 4 weeks ago 856 MB
houxiongxiong/tomcat 1.0 5a4cfe786c29 4 weeks ago 856 MB
registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat 1.0 5a4cfe786c29 4 weeks ago 856 MB
tomcat 1.0 788cab760143 4 weeks ago 856 MB
docker.io/rancher/mirrored-flannelcni-flannel v0.20.0 fd14f6e39753 5 weeks ago 59.4 MB
docker.io/houxiongxiong/centos7 411 308566636ca1 8 weeks ago 411 MB
docker.io/houxiongxiong/tomcat 8.5.82 bc5cf7348e0c 2 months ago 856 MB
docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin v1.1.0 fcecffc7ad4a 6 months ago 8.09 MB
registry.cn-hangzhou.aliyuncs.com/houxiongxiong/flannel-cni-plugin v1.1.0 fcecffc7ad4a 6 months ago 8.09 MB
registry.aliyuncs.com/google_containers/kube-proxy v1.18.0 43940c34f24f 2 years ago 117 MB
registry.aliyuncs.com/google_containers/kube-apiserver v1.18.0 74060cea7f70 2 years ago 173 MB
registry.aliyuncs.com/google_containers/kube-controller-manager v1.18.0 d3e55153f52f 2 years ago 162 MB
registry.aliyuncs.com/google_containers/kube-scheduler v1.18.0 a31f78c7c8ce 2 years ago 95.3 MB
registry.aliyuncs.com/google_containers/pause 3.2 80d28bedfe5d 2 years ago 683 kB
registry.aliyuncs.com/google_containers/coredns 1.6.7 67da37a9a360 2 years ago 43.8 MB
registry.aliyuncs.com/google_containers/etcd 3.4.3-0 303ce5db0e90 3 years ago 288 MB
registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat 4.0 a7192f6d537a 3 years ago 411 MB
2.4创建&退出容器
2.4.1创建
docker run -it 镜像名 或 镜像ID /bin/bash
[root@master1 ~]# docker run -it a7192f6d537a /bin/bash
[root@f0f949caca62 /]#
[root@master1 ~]# docker run -it docker.io/couchbase/centos7-systemd:latest /bin/bash
[root@72ca153bed55 /]#
2.4.2退出镜像
exit //退出镜像
[root@master1 ~]# docker run -it a7192f6d537a /bin/bash
[root@f0f949caca62 /]# exit
[root@master1 ~]# docker run -it docker.io/couchbase/centos7-systemd:latest /bin/bash
[root@72ca153bed55 /]# exit
2.5查看容器
2.5.1列出正在运行的容器
docker ps // 列出正在运行的容器
[root@master1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f2a84f0257d 67da37a9a360 "/coredns -conf /e..." 8 minutes ago Up 8 minutes k8s_coredns_coredns-7ff77c879f-m8g27_kube-system_0c63b7c1-992b-4cac-a2a2-7b730b4a65b4_29
6a8a1ac6a01d 67da37a9a360 "/coredns -conf /e..." 8 minutes ago Up 8 minutes k8s_coredns_coredns-7ff77c879f-7dhfp_kube-system_15cb9b32-4fc1-4a99-93eb-2c5604a8abec_29
5989c089cf5f registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 8 minutes ago Up 8 minutes k8s_POD_coredns-7ff77c879f-m8g27_kube-system_0c63b7c1-992b-4cac-a2a2-7b730b4a65b4_177
acaea1f3462d registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 8 minutes ago Up 8 minutes k8s_POD_coredns-7ff77c879f-7dhfp_kube-system_15cb9b32-4fc1-4a99-93eb-2c5604a8abec_177
66921658750e d66192101c64 "/opt/bin/flanneld..." 8 minutes ago Up 8 minutes k8s_kube-flannel_kube-flannel-ds-2x4n7_kube-flannel_5b731526-ce8b-484f-a2c9-3ec24f79cfda_5
8e031526a1fd 43940c34f24f "/usr/local/bin/ku..." 8 minutes ago Up 8 minutes k8s_kube-proxy_kube-proxy-zch4w_kube-system_285b7004-db88-4e9b-9041-7b9f385dd929_7
a750c2f22179 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 8 minutes ago Up 8 minutes k8s_POD_kube-proxy-zch4w_kube-system_285b7004-db88-4e9b-9041-7b9f385dd929_7
f6d0becbd599 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 8 minutes ago Up 8 minutes k8s_POD_kube-flannel-ds-2x4n7_kube-flannel_5b731526-ce8b-484f-a2c9-3ec24f79cfda_4
81a789b6f941 d3e55153f52f "kube-controller-m..." 9 minutes ago Up 9 minutes k8s_kube-controller-manager_kube-controller-manager-master1_kube-system_c4d2dd4abfffdee4d424ce839b0de402_7
2b2446d67c09 303ce5db0e90 "etcd --advertise-..." 9 minutes ago Up 9 minutes k8s_etcd_etcd-master1_kube-system_f6f5eb78fda5dc70d0a6b2ffd2c6b231_7
2dde22dab995 74060cea7f70 "kube-apiserver --..." 9 minutes ago Up 9 minutes k8s_kube-apiserver_kube-apiserver-master1_kube-system_3cfa89b3ed2c110838693c4e4238d4b7_7
5f686dd26a6b a31f78c7c8ce "kube-scheduler --..." 9 minutes ago Up 9 minutes k8s_kube-scheduler_kube-scheduler-master1_kube-system_ca2aa1b3224c37fa1791ef6c7d883bbe_7
bcc851847a9e registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 9 minutes ago Up 9 minutes k8s_POD_kube-controller-manager-master1_kube-system_c4d2dd4abfffdee4d424ce839b0de402_7
0d6f3ea924cd registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 9 minutes ago Up 9 minutes k8s_POD_etcd-master1_kube-system_f6f5eb78fda5dc70d0a6b2ffd2c6b231_7
66595ed54ce1 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 9 minutes ago Up 9 minutes k8s_POD_kube-scheduler-master1_kube-system_ca2aa1b3224c37fa1791ef6c7d883bbe_7
56d12beccc61 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 9 minutes ago Up 9 minutes k8s_POD_kube-apiserver-master1_kube-system_3cfa89b3ed2c110838693c4e4238d4b7_7
2.5.2列出所有容器
docker ps -a // 可选参数 -a 列出所有容器
[root@master1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72ca153bed55 docker.io/couchbase/centos7-systemd:latest "/bin/bash" 2 minutes ago Exited (127) 48 seconds ago youthful_swartz
f0f949caca62 a7192f6d537a "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago thirsty_stonebraker
d0b57191dcee a7192f6d537a "/bin/bsah" 3 minutes ago Created modest_poitras
9f2a84f0257d 67da37a9a360 "/coredns -conf /e..." 8 minutes ago Up 8 minutes k8s_coredns_coredns-7ff77c879f-m8g27_kube-system_0c63b7c1-992b-4cac-a2a2-7b730b4a65b4_29
6a8a1ac6a01d 67da37a9a360 "/coredns -conf /e..." 8 minutes ago Up 8 minutes k8s_coredns_coredns-7ff77c879f-7dhfp_kube-system_15cb9b32-4fc1-4a99-93eb-2c5604a8abec_29
5989c089cf5f registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 8 minutes ago Up 8 minutes k8s_POD_coredns-7ff77c879f-m8g27_kube-system_0c63b7c1-992b-4cac-a2a2-7b730b4a65b4_177
acaea1f3462d registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 8 minutes ago Up 8 minutes k8s_POD_coredns-7ff77c879f-7dhfp_kube-system_15cb9b32-4fc1-4a99-93eb-2c5604a8abec_177
66921658750e d66192101c64 "/opt/bin/flanneld..." 8 minutes ago Up 8 minutes k8s_kube-flannel_kube-flannel-ds-2x4n7_kube-flannel_5b731526-ce8b-484f-a2c9-3ec24f79cfda_5
8e031526a1fd 43940c34f24f "/usr/local/bin/ku..." 9 minutes ago Up 9 minutes k8s_kube-proxy_kube-proxy-zch4w_kube-system_285b7004-db88-4e9b-9041-7b9f385dd929_7
a750c2f22179 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 9 minutes ago Up 9 minutes k8s_POD_kube-proxy-zch4w_kube-system_285b7004-db88-4e9b-9041-7b9f385dd929_7
c0c8304787ec d66192101c64 "/opt/bin/flanneld..." 9 minutes ago Exited (1) 9 minutes ago k8s_kube-flannel_kube-flannel-ds-2x4n7_kube-flannel_5b731526-ce8b-484f-a2c9-3ec24f79cfda_4
29e9b64920ff d66192101c64 "cp -f /etc/kube-f..." 9 minutes ago Exited (0) 9 minutes ago k8s_install-cni_kube-flannel-ds-2x4n7_kube-flannel_5b731526-ce8b-484f-a2c9-3ec24f79cfda_0
129c1084bf81 fcecffc7ad4a "cp -f /flannel /o..." 9 minutes ago Exited (0) 9 minutes ago k8s_install-cni-plugin_kube-flannel-ds-2x4n7_kube-flannel_5b731526-ce8b-484f-a2c9-3ec24f79cfda_4
f6d0becbd599 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 9 minutes ago Up 9 minutes k8s_POD_kube-flannel-ds-2x4n7_kube-flannel_5b731526-ce8b-484f-a2c9-3ec24f79cfda_4
81a789b6f941 d3e55153f52f "kube-controller-m..." 9 minutes ago Up 9 minutes k8s_kube-controller-manager_kube-controller-manager-master1_kube-system_c4d2dd4abfffdee4d424ce839b0de402_7
2b2446d67c09 303ce5db0e90 "etcd --advertise-..." 9 minutes ago Up 9 minutes k8s_etcd_etcd-master1_kube-system_f6f5eb78fda5dc70d0a6b2ffd2c6b231_7
2dde22dab995 74060cea7f70 "kube-apiserver --..." 9 minutes ago Up 9 minutes k8s_kube-apiserver_kube-apiserver-master1_kube-system_3cfa89b3ed2c110838693c4e4238d4b7_7
5f686dd26a6b a31f78c7c8ce "kube-scheduler --..." 9 minutes ago Up 9 minutes k8s_kube-scheduler_kube-scheduler-master1_kube-system_ca2aa1b3224c37fa1791ef6c7d883bbe_7
bcc851847a9e registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 9 minutes ago Up 9 minutes k8s_POD_kube-controller-manager-master1_kube-system_c4d2dd4abfffdee4d424ce839b0de402_7
0d6f3ea924cd registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 9 minutes ago Up 9 minutes k8s_POD_etcd-master1_kube-system_f6f5eb78fda5dc70d0a6b2ffd2c6b231_7
66595ed54ce1 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 9 minutes ago Up 9 minutes k8s_POD_kube-scheduler-master1_kube-system_ca2aa1b3224c37fa1791ef6c7d883bbe_7
56d12beccc61 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 9 minutes ago Up 9 minutes k8s_POD_kube-apiserver-master1_kube-system_3cfa89b3ed2c110838693c4e4238d4b7_7
a6ce08dabfef 1c384aa76aa9 "/bin/sh -c '/apac..." 10 days ago Exited (130) 10 days ago wonderful_jennings
1ff51103a637 5a4cfe786c29 "/bin/sh -c 'yum -..." 10 days ago Exited (127) 10 days ago nervous_shannon
edd499bb1e59 67da37a9a360 "/coredns -conf /e..." 10 days ago Exited (255) 9 minutes ago k8s_coredns_coredns-7ff77c879f-m8g27_kube-system_0c63b7c1-992b-4cac-a2a2-7b730b4a65b4_28
c5f4550ff08d 67da37a9a360 "/coredns -conf /e..." 10 days ago Exited (255) 9 minutes ago k8s_coredns_coredns-7ff77c879f-7dhfp_kube-system_15cb9b32-4fc1-4a99-93eb-2c5604a8abec_28
3e27bbc3ca98 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 10 days ago Exited (255) 9 minutes ago k8s_POD_coredns-7ff77c879f-7dhfp_kube-system_15cb9b32-4fc1-4a99-93eb-2c5604a8abec_137
ac48f8434200 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 10 days ago Exited (255) 9 minutes ago k8s_POD_coredns-7ff77c879f-m8g27_kube-system_0c63b7c1-992b-4cac-a2a2-7b730b4a65b4_137
b1087b55d47d 43940c34f24f "/usr/local/bin/ku..." 10 days ago Exited (255) 9 minutes ago k8s_kube-proxy_kube-proxy-zch4w_kube-system_285b7004-db88-4e9b-9041-7b9f385dd929_6
f6ae27698b9b registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 10 days ago Exited (255) 9 minutes ago k8s_POD_kube-proxy-zch4w_kube-system_285b7004-db88-4e9b-9041-7b9f385dd929_6
f09562665312 74060cea7f70 "kube-apiserver --..." 10 days ago Exited (255) 9 minutes ago k8s_kube-apiserver_kube-apiserver-master1_kube-system_3cfa89b3ed2c110838693c4e4238d4b7_6
417a72f30b4e 303ce5db0e90 "etcd --advertise-..." 10 days ago Exited (255) 9 minutes ago k8s_etcd_etcd-master1_kube-system_f6f5eb78fda5dc70d0a6b2ffd2c6b231_6
5244a415ca3b a31f78c7c8ce "kube-scheduler --..." 10 days ago Exited (255) 9 minutes ago k8s_kube-scheduler_kube-scheduler-master1_kube-system_ca2aa1b3224c37fa1791ef6c7d883bbe_6
c9cbbaadfb51 d3e55153f52f "kube-controller-m..." 10 days ago Exited (255) 9 minutes ago k8s_kube-controller-manager_kube-controller-manager-master1_kube-system_c4d2dd4abfffdee4d424ce839b0de402_6
627e4c29dfae registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 10 days ago Exited (255) 9 minutes ago k8s_POD_etcd-master1_kube-system_f6f5eb78fda5dc70d0a6b2ffd2c6b231_6
1338b775f187 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 10 days ago Exited (255) 9 minutes ago k8s_POD_kube-controller-manager-master1_kube-system_c4d2dd4abfffdee4d424ce839b0de402_6
c53b1eba83d3 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 10 days ago Exited (255) 9 minutes ago k8s_POD_kube-scheduler-master1_kube-system_ca2aa1b3224c37fa1791ef6c7d883bbe_6
a0976757c626 registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 10 days ago Exited (255) 9 minutes ago k8s_POD_kube-apiserver-master1_kube-system_3cfa89b3ed2c110838693c4e4238d4b7_6
29d1f4b5f851 bcfcf26644dc "/bin/sh -c '/apac..." 2 weeks ago Exited (137) 2 weeks ago elegant_wright
2.6制作标签
docker tag 容器ID/镜像名:标签 新的镜像名:标签
[root@master1 ~]# docker tag a7192f6d537a houxiongxiong:1.0
[root@master1 ~]# docker tag docker.io/couchbase/centos7-systemd:latest houxiongxiong:1.0
2.7登陆docker
[root@master1 ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: houxiongxiong
Password:
Login Succeeded
2.8上传镜像
以阿里云为例
docker push 账户名/镜像名:标签
docker push registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat:[镜像版本号]
2.9后台运行
-d 后台运行
docker run -itd registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat:8.1
2.10端口映射
-p
docker run -it registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat:8.1 -p 8080:8080 /bin/bash
2.11挂载
docker run -it -v [宿主机目录]:[容器内目录] 镜像名:标签/镜像ID /bin/bash
[root@master1 yaml]# docker run -it -v /home/yaml:/home/yaml registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat:8.1 /bin/bash
3.Dockerfile
3.1基本命令
3.1.1COPY
复制指令,从上下文目录中复制文件或者目录到容器里指定路径。
格式:
COPY [--chown=<user>:<group>] <源路径1>... <目标路径>
COPY [--chown=<user>:<group>] ["<源路径1>",... "<目标路径>"]
[--chown=
<源路径>:源文件或者源目录,这里可以是通配符表达式,其通配符规则要满足 Go 的 filepath.Match 规则。例如:
COPY hom* /mydir/
COPY hom?.txt /mydir/
<目标路径>:容器内的指定路径,该路径不用事先建好,路径不存在的话,会自动创建。
3.1.2CMD
类似于 RUN 指令,用于运行程序,但二者运行的时间点不同:
- CMD 在docker run 时运行。
- RUN 是在 docker build。
作用:为启动的容器指定默认要运行的程序,程序运行结束,容器也就结束。CMD 指令指定的程序可被 docker run 命令行参数中指定要运行的程序所覆盖。
注意:如果 Dockerfile 中如果存在多个 CMD 指令,仅最后一个生效。
格式:
CMD <shell 命令>
CMD ["<可执行文件或命令>","<param1>","<param2>",...]
CMD ["<param1>","<param2>",...] # 该写法是为 ENTRYPOINT 指令指定的程序提供默认参数
推荐使用第二种格式,执行过程比较明确。第一种格式实际上在运行的过程中也会自动转换成第二种格式运行,并且默认可执行文件是 sh。
3.1.3WORKDIR
指定工作目录。用 WORKDIR 指定的工作目录,会在构建镜像的每一层中都存在。(WORKDIR 指定的工作目录,必须是提前创建好的)。
docker build 构建镜像过程中的,每一个 RUN 命令都是新建的一层。只有通过 WORKDIR 创建的目录才会一直存在。
格式:
WORKDIR <工作目录路径>
3.1.4RUN
用于执行后面跟着的命令行命令。有以下俩种格式:
shell 格式:
RUN <命令行命令>
# <命令行命令> 等同于,在终端操作的 shell 命令。
exec 格式:
RUN ["可执行文件", "参数1", "参数2"]
# 例如:
# RUN ["./test.php", "dev", "offline"] 等价于 RUN ./test.php dev offline
注意:Dockerfile 的指令每执行一次都会在 docker 上新建一层。所以过多无意义的层,会造成镜像膨胀过大。例如:
FROM centos
RUN yum -y install wget
RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"
RUN tar -xvf redis.tar.gz
以上执行会创建 3 层镜像。可简化为以下格式:
FROM centos
RUN yum -y install wget \
&& wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \
&& tar -xvf redis.tar.gz
如上,以 && 符号连接命令,这样执行后,只会创建 1 层镜像。
3.1.5FROM
定制的镜像都是基于 FROM 的镜像。
3.2实例(tomcat的部署)
FROM registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat:1.0
RUN yum -y install wget \
&& yum -y install java-1.8.0-openjdk.x86_64 \
&& wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.83/bin/apache-tomcat-8.5.83.tar.gz --no-check-certificate \
&& tar -xvf apache-tomcat-8.5.83.tar.gz \
&& rm -f apache-tomcat-8.5.83.tar.gz
CMD /apache-tomcat-8.5.83/bin/startup.sh \
&& tail -f /apache-tomcat-8.5.83/logs/catalina.out
3.3commit制作镜像
docker commit -t 镜像名:ID . "."代表的是本级目录
docker commit -t registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat:8.2 .
二、K8S集群的搭建
kubeadm安装方法
1.放在前面
1.1硬件
最低2个CPU(处理器),不然会报错。
1.2软件
最小安装 或 mini版本的镜像 只有 “vi” 没有 “vim”
临时关闭的配置重启之后要再次配置 有:关闭swap、关闭firewalld、关闭selinux
docker每次开机需自行启动
service network restart 重启网卡
序号 | IP | 角色 | Hostname | 安装组件 |
---|---|---|---|---|
1 | 192.168.21.19 | Master | master | docker、kubelet、kubeadm、kubectl、kube-flannel.yml |
2 | 192.168.21.20 | Node | node1 | docker、kubelet、kubeadm、kubectl |
3 | 192.168.21.21 | Node | node2 | docker、kubelet、kubeadm、kubectl |
重要:IP地址设为静态!!!
2.需重启的配置
2.1根据规划设置主机名
方法1:hostnamectl set-hostname <hostname>
方法2:vi 或 vim /etc/hostname
2.2关闭swap
方法1:swapoff -a // 临时生效 重要:临时不用重启
方法2:sed -ri 's/.swap./#&/' /etc/fstab // 永久生效
方法3:用vi 或 vim 进入 /etc/fstab 把最后一行 /dev/mapper/centos-swap swap 用 # 注释掉 // 永久生效
#
# /etc/fstab
# Created by anaconda on Fri Nov 4 00:46:36 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=7be6fcec-35e5-44ad-9c64-f84952a9e1c0 /boot xfs defaults 0 0
#/dev/mapper/centos-swap swap swap defaults 0 0
2.3重启命令
systemctl reboot
推荐:临时与永久一起设置,这样不用重启重启后也不用再次设置。
3.环境配置
3.1关闭firewalld
方法1:systemctl stop firewalld // 临时生效
方法2:systemctl stop firewalld && systemctl disable firewalld // 永久生效
3.2关闭selinux
方法1:setenforce 0 // 临时生效
方法2:sed -i 's/enforcing/disabled/'/etc/selinux/config // 永久生效
方法3:用 vi 或 vim 进入 /etc/selinux/config 将 enforcing 改为disabled // 永久生效
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
3.3在hosts里面添加master、node
方法1:
cat >> /etc/hosts << EOF
192.168.227.31 master1
192.168.227.32 node1
192.168.227.33 node2
185.199.109.133 raw.githubusercontent.com
EOF
方法2:
用vi 或 vim 进入 /etc/hosts 将 master 与 node 添加在里面
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.138.24 master5
192.168.138.25 node9
192.168.138.26 node10
185.199.109.133 raw.githubusercontent.com
3.4.将桥接的IPv4流量传递到iptables的链
方法1:
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 生效
方法2:
使用 vi 或 vim 创建 /etc/sysctl.d/k8s.conf 将 net.bridge.bridge-nf-call-ip6tables = 1 回车换行 net.bridge.bridge-nf-call-iptables = 1 添加进去
[root@master1 ~]# vi /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
sysctl --system # 生效
4.同步时间
4.1放在前面
个人认为初次安装虚拟机时,就无需这一步,这部是因为有时候虚拟机时间会不同步,开机时自己注意。
4.2安装ntpdate
yum install -y ntpdate
4.3同步时间
ntpdate time.windows.com
4.4修改时区(拓展)
rm /etc/localtime // 删除原来的链接,重新连接正确的时区,然后再输入下面的
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
5.安装并启动docker
5.1安装docker
yum install -y docker
如果出现“内存分配问题”的报错就重启试试
5.2启动docker
systemctl start docker
6.修改下载源
6.1
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors":
["https://mfly25p6.mirror.aliyuncs.com"]
}
EOF
6.2
cat > /etc/yum.repos.d/kubernetes.repo<< EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yumkey.gpghttps://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
[kubernetes]
name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF
7.安装并启动K8S
7.1安装三件套
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
7.2启动kubelet
systemctl enable kubelet
7.3卸载三件套(拓展学习)
8.kubeadm init初始化k8s集群(在master上)
8.1kubeadm init
kubeadm init \
--apiserver-advertise-address=192.168.138.111 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
注:如一直连接超时就重启或换网重启。
参数解析:
apiserver-advertise-address API //服务器所在的地址,这里就是master节点的IP
image-repository //配置拉取k8s镜像的容器仓库,我们配置的是阿里的一个镜像仓库
kubernetes-version //指定k8s的版本
service-cidr //服务的虚拟 IP 地址另外指定 IP 地址段
pod-network-cidr pod //节点网络可以使用的 IP 地址段
8.2成功标识
看到
Your Kubernetes control-plane has initialized successfully!
代表初始化成功
8.3重置kubeadm init
kubeadm reset
8.4重新加入
kubeadm token create --print-join-command
9.使用kubectl工具
9.1一行一行的输入
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf$HOME/.kube/config
sudo chown (id -u):(id -g)$HOME/.kube/config
这三句是“第8步”运行成功后的倒数第9~7句
10.加入Kubernetes Node
10.1向集群添加新节点
样例 (下面这句是“第7步”运行成功的最后那两句)
kubeadm join 192.168.227.34:6443 --token olhw2c.cmvliki84cj8mu47 \
--discovery-token-ca-cert-hash sha256:8af3429c91c8ba2cc2e38346ed9839fe1de85cf8e51a3b43fd01fd72f36c312d
kubectl get nodes // 查看集群
11.部署CNI网络插件
11.1下载
yum -y install wget && wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
出现拒绝访问就在 /etc/hosts/ 里面添加 185.199.109.133 raw.githubusercontent.com
11.2应用
kubectl apply -f kube-flannel.yml
或
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
11.3查看
kubectl get pods -n kube-system
12.测试kubernetes集群
12.1在Kubernetes集群中创建一个pod
kubectl create deployment nginx --image=nginx
12.2暴露pod的端口
kubectl expose deployment nginx --port=80 --type=NodePort
12.3查看pod的端口号
kubectl get svc
13.问题汇总
13.1新node加入集群
notready解决方法
node节点:
mkdir -p /etc/cni/net.d/
master节点:
scp /etc/cni/net.d/* 192.168.138.57:/etc/cni/net.d/
网络插件待定状态解决方法
master节点:
docker images
docker save -o mirrored-flannelcni-flannel.tar docker.io/rancher/mirrored-flannelcni-flannel
docker save -o mirrored-flannelcni-flannel-cni-plugin.tar docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin
scp mirrored-flannelcni-flannel.tar 192.168.138.57:~
scp mirrored-flannelcni-flannel-cni-plugin.tar 192.168.138.57:~
node节点:
ls
docker load -i mirrored-flannelcni-flannel.tar
docker load -i mirrored-flannelcni-flannel-cni-plugin.tar
systemctl restart docker
第二种方法
1.禁用SELinux
setenforce 0
以上命令仅仅暂时禁用了SELinux, 在系统重启之后,SELinux 又会发挥作用。为了彻底
禁用SELinux, 用户需要修改其配置文件/etc/selinux/config. 将其中的
docker login -u cn-north-4@JFVA22E710GNMWSAY8OB -p b0ec04e370fa1af6d70467c78e97b08728d76b471ced1ca807479ffd0a128268 swr.cn-north-4.myhuaweicloud.com
三、yaml
1.创建pod
apiVersion: apps/v1 #指定api版本标签
kind: Deployment #定义资源的类型/角色,deployment为控制器
metadata: #定义资源的元数据信息
name: tomcat2 #定义资源的名称,在同一个namespace空间中必须是唯一的
labels: #定义资源标签
app: tomcat2
spec:
replicas: 1 #定义副本数量
selector: #定义选择器
matchLabels: #匹配上面的标签
app: tomcat2 #匹配模板名称
template: #定义模板
metadata:
labels:
app: tomcat2
spec:
containers: #定义容器信息
- name: tomcat2 #容器名,与标签名要相同
image: registry.cn-hangzhou.aliyuncs.com/houxiongxiong/tomcat:9.1 #容器使用的镜像以及版本
ports:
- containerPort: 8081 #定义容器的对外端口
2.创建网络服务
apiVersion: v1
kind: Service
metadata:
name: tomcat2
labels:
app: tomcat2
spec:
type: NodePort
ports:
- port: 8081
targetPort: 8081
selector:
app: tomcat2
评论区