k8s系列(一)之基础环境安装配置、dockerfile配置及使用

一、基于现大多生产环境都是还是centos系所有本文案例采用centos7.6及以上作为基础环境,服务器系统相关请参考:或者直接采用云主机都行微服务架构(一)简单的服务器虚拟框架选型及安装_非生物语言学家的博客-CSDN博客一、简单的服务器虚拟框架选型1.esxi虚拟框架:不支持太老的服务器网卡,要下载官方VMware-PowerCLI-xxx和ESXi-Customizer-PS及相关的网卡驱动包自己封装iso。2.采用Proxmox VE作为底层系统具体操作如下:挂网https://www.proxmox.com/en/downloads下载相关iso制作u启 安装系统上传centos的iso文件到数据中心的local上根据实际需求配置虚拟机(可以只...https://blog.csdn.net/qq_29653373/article/details/115406632?spm=1001.2014.3001.5502

二、docker环境搭建

1、静态ip配置

# 进入网卡配置目录查看你所有的网卡
[root@VM-12-6-centos ~]# cd /etc/sysconfig/network-scripts/
# 编辑你的网卡配置 我的是 ifcfg-eth0 
[root@VM-12-6-centos network-scripts]# vi ifcfg-eth0
#将配置信息改为如下配置(红色部分根据实际情况更改,改完之后重启网卡:service network restart)

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=
static
IPADDR=192.168.1.22
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=192.168.1.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes

2、基础配置

# 关闭selinux
[root@VM-12-6-centos ~]# setenforce 0
[root@VM-12-6-centos ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
# 注意:修改 selinux 配置文件之后,重启机器,selinux才能永久生效看到 Disabled说明selinux成功关闭
[root@VM-12-6-centos ~]# getenforce
# 配置主机名:
[root@VM-12-6-centos ~]# hostnamectl set-hostname morik22
[root@VM-12-6-centos ~]# bash
# 关闭防火墙
[root@VM-12-6-centos ~]# systemctl stop firewalld && systemctl disable firewalld
# 配置时间同步
[root@VM-12-6-centos ~]# yum install -y ntp ntpdate
[root@VM-12-6-centos ~]# ntpdate cn.pool.ntp.org
# 编写计划任务(加入计划配置:* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org)
[root@VM-12-6-centos ~]# crontab -e
# 重启 crond 服务使配置生效:
[root@VM-12-6-centos ~]# systemctl restart crond

 3、docker安装、配置

# 配置 docker-ce 国内 yum 源(阿里云)
[r[root@VM-12-6-centos ~]# yum -y install yum-utils
[root@VM-12-6-centos ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 基础工具包安装
[root@VM-12-6-centos ~]# yum install -y wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack
# 安装启动查看docker状态
[root@VM-12-6-centos ~]# yum install docker-ce-20.10.17 -y
[root@VM-12-6-centos ~]# systemctl start docker && systemctl enable docker
[root@VM-12-6-centos ~]# docker version
[root@VM-12-6-centos ~]# systemctl status docker
# 开启br_netfilter内核参数需要转发,br_netfilter模块用于将桥接流量转发至iptables
[root@VM-12-6-centos ~]# modprobe br_netfilter
[root@VM-12-6-centos ~]# cat > /etc/sysctl.d/docker.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
[root@VM-12-6-centos ~]# sysctl -p /etc/sysctl.d/docker.conf
# 配置镜像加速器
[root@VM-12-6-centos ~]# tee /etc/docker/daemon.json << 'EOF'
{"registry-mirrors":["https://rsbud4vc.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com","http://qtid6917.mirror.aliyuncs.com","https://e9yneuy4.mirror.aliyuncs.com","https://rncxm540.mirror.aliyuncs.com"]}
EOF
[root@VM-12-6-centos ~]# sudo systemctl daemon-reload && sudo systemctl restart docker


三、DockerFile基本配置及使用

1、创建nginx的dockerfile配置文件,文件名谁便启一般叫dockerfile

# Nginx基础依赖
FROM centos     
# 作者信息 不必要
MAINTAINER umf-morik
# 执行删除yum源配置命令
RUN rm -rf /etc/yum.repos.d/*
# 执行物理机文件拷贝到docker容器命令
COPY Centos-vault-8.5.2111.repo /etc/yum.repos.d/
# 执行安装wget下载工具命令
RUN yum install wget -y
# 执行安装Nginx命令
RUN yum install nginx -y
# 拷贝物理机首页到容器的nginx根映射目录下
COPY index.html /usr/share/nginx/html/
# 容器暴露向外交互的端口
EXPOSE 80
# nginx启动组合命令
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf  # 配置追加deamon,官网要求,不然启动异常
ENTRYPOINT ["/usr/sbin/nginx", "-c"]                      # 定参
CMD ["/etc/nginx/nginx.conf"]                             # 变参

2、构建、启动等镜像操作

# -f 可以指定dockerfile的文件名默认为dockerfile可以省略。morik/nginx镜像名,v1 tag标记,后边的.不可省略
[root@VM-12-6-centos ~]# docker build -t morik/nginx:v1 .
# 查看镜像
[root@VM-12-6-centos ~]# docker images | grep nginx
# 启动容器
[root@VM-12-6-centos ~]# docker run -dit -p 30180:80 --name mynginx morik/nginx:v1
# 进入容器内部
[root@VM-12-6-centos ~]# docker exec -it mynginx /bin/bash
# 查看容器运行情况
[root@VM-12-6-centos ~]# docker ps | grep mynginx 

3、Tomcat的dockerfile配置参考

FROM centos
MAINTAINER umf-morik
RUN rm -rf /etc/yum.repos.d/*
COPY Centos-vault-8.5.2111.repo /etc/yum.repos.d/
RUN yum install wget -y
# 解压并拷贝rpm文件
ADD jdk-8u45-linux-x64.rpm /usr/local/
ADD apache-tomcat-8.0.26.tar.gz /usr/local/
RUN cd /usr/local && rpm -ivh jdk-8u45-linux-x64.rpm
RUN mv /usr/local/apache-tomcat-8.0.26 /usr/local/tomcat8
EXPOSE 8080
# 启动命令 tail 循环让容器一直在运行,如果不加,容器就会自动退出
ENTRYPOINT /usr/local/tomcat8/bin/startup.sh && tail -F 
/usr/local/tomcat8/logs/catalina.out

4、Apache的dockerfile参考

FROM centos
MAINTAINER xuegod-IT
RUN rm -rf /etc/yum.repos.d/*
COPY Centos-vault-8.5.2111.repo /etc/yum.repos.d/
RUN yum -y install httpd
ADD template /var/www/html/
ADD run.sh /run.sh
RUN chmod 755 /run.sh
EXPOSE 80
CMD ["/run.sh"]

四、备注:

文字所需的Centos-vault-8.5.2111.repo下载地址:centos8的yum源-Linux文档类资源-CSDN下载

其他所需的包可前往各自的官网进行下载 

猜你喜欢

转载自blog.csdn.net/qq_29653373/article/details/125865146