主机名称
IP地址
系统版本/内存/cpu核数/硬盘
安装软件
controlnode
172.16.1.70/24
centos7.4/4/2/60
docker、docker镜像制作
slavenode1
172.16.1.71/24
centos7.4/4/2/60
docker、harbor仓库、gitlab仓库、cadvisor
slavenode2
172.16.1.72/24
centos7.4/4/2/60
docker、jenkins、cadvisor
slavenode3
172.16.1.73/24
centos7.4/4/2/60
docker、grafana、prometheus
1 替换国内yum源
# curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# yum clean all
# yum makecache
2 服务器时区和时间保持国内
# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# (echo "*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com >/dev/null 2>&1"; crontab -l) | crontab
3 selinux和firewalld关闭
# 关闭selinux
# sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config
# setenforce 0
# 关闭firewalld
# systemctl stop firewalld.service
# systemctl disable firewalld.service
1 安装
# yum install docker-ce -y
2 启动docker并加入开机自启动
# systemctl start docker
# systemctl enable docker
3 查看docker信息
# docker info
# mkdir -p /etc/docker
# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
# systemctl daemon-reload
# systemctl restart docker
1 Dockerfile
FROM centos:7
LABEL maintainer liuchang
RUN yum install -y gcc gcc-c++ make \
openssl-devel pcre-devel gd-devel \
iproute net-tools telnet wget curl && \
yum clean all && \
rm -rf /var/cache/yum/*
RUN groupadd -g 1200 nginx && \
useradd -M -s /sbin/nologin -u 1200 -g nginx nginx
COPY nginx-1.19.1.tar.gz /
RUN tar -zxf nginx-1.19.1.tar.gz && \
cd nginx-1.19.1 && \
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--user=nginx \
--group=nginx && \
make -j 4 && make install && \
rm -rf /usr/local/nginx/html/* && \
echo "ok" >> /usr/local/nginx/html/status.html && \
cd / && rm -rf nginx* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/nginx/sbin
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
2 构建
# docker build -t nginx:v1 .
1 Dockerfile
FROM centos:7
LABEL maintainer liuchang
RUN yum install epel-release -y && \
yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
libcurl-devel libjpeg-devel libpng-devel openssl-devel \
libmcrypt-devel libxslt-devel libtidy-devel autoconf \
iproute net-tools telnet wget curl && \
yum clean all && \
rm -rf /var/cache/yum/*
RUN groupadd -g 1200 nginx && \
useradd -M -s /sbin/nologin -u 1200 -g nginx nginx
COPY php-7.2.19.tar.gz /
RUN tar -zxf php-7.2.19.tar.gz && \
cd php-7.2.19 && \
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm --enable-opcache \
--with-mysql --with-mysqli --with-pdo-mysql \
--with-openssl --with-zlib --with-curl --with-gd \
--with-jpeg-dir --with-png-dir --with-freetype-dir \
--enable-mbstring --with-mcrypt --enable-hash \
--with-fpm-user=nginx \
--with-fpm-group=nginx && \
make -j 4 && make install && \
cp -a php.ini-production /usr/local/php/etc/php.ini && \
cp -a /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
cp -a /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf && \
sed -i 's/\;daemonize = yes/daemonize = no/' /usr/local/php/etc/php-fpm.conf && \
sed -i 's/127.0.0.1:9000/0.0.0.0:9000/' /usr/local/php/etc/php-fpm.d/www.conf && \
mkdir /usr/local/php/log && \
cd / && rm -rf php* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/php/sbin
COPY php.ini /usr/local/php/etc/
COPY php-fpm.conf /usr/local/php/etc/
COPY www.conf /usr/local/php/etc/php-fpm.d/
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["php-fpm"]
2 构建
# docker build -t php:v1 .
1 Dockerfile
FROM centos:7
LABEL maintainer liuchang
RUN yum install wget curl unzip iproute net-tools -y && \
yum clean all && \
rm -rf /var/cache/yum/* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY jdk-8u45-linux-x64.tar.gz /
RUN tar -xzf jdk-8u45-linux-x64.tar.gz && \
mv jdk1.8.0_45 /usr/local/jdk && \
rm -rf jdk-8u45-linux-x64.tar.gz
COPY apache-tomcat-8.5.43.tar.gz /
RUN tar -zxf apache-tomcat-8.5.43.tar.gz && \
mv apache-tomcat-8.5.43 /usr/local/tomcat && \
rm -rf apache-tomcat-8.5.43.tar.gz && \
rm -rf /usr/local/tomcat/webapps/* && \
mkdir -p /usr/local/tomcat/webapps/ROOT && \
echo "ok" > /usr/local/tomcat/webapps/ROOT/status.html
ENV JAVA_HOME /usr/local/jdk
ENV CLASSPATH $JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar
ENV PATH $JAVA_HOME/bin:/usr/local/tomcat/bin:$PATH
COPY catalina.sh /usr/local/tomcat/bin
COPY server.xml /usr/local/tomcat/conf
RUN chmod +x /usr/local/tomcat/bin/catalina.sh
WORKDIR /usr/local/tomcat
EXPOSE 8080
CMD ["catalina.sh", "run"]
2 构建
# docker build -t tomcat:v1 .
1 Dockerfile
FROM java:8-jdk-alpine
LABEL maintainer liuchang
ENV JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8 -Duser.timezone=GMT+08"
RUN apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./target/eureka-service.jar ./
EXPOSE 8888
CMD java -jar $JAVA_OPTS /eureka-service.jar
2 构建
# docker build -t jdk:v1 .
1 自定义网络
# docker network create lnmp
2 环境设置
# mkdir -p /app/wwwroot/
3 创建php容器
docker run -d \
--name lnmp_php \
--net lnmp \
--mount type=bind,src=/app/wwwroot/,dst=/usr/local/nginx/html \
php:v1
4 创建nginx容器
docker run -d \
--name lnmp_nginx \
--net lnmp \
-p 888:80 \
--mount type=bind,src=/app/wwwroot/,dst=/usr/local/nginx/html \
nginx:v1
测试php页面状态
http://172.16.1.70:888/status.php
5 创建mysql容器
# mkdir -p /opt/mysql/data
docker run -d \
-p 3306:3306 --name lnmp_mysql \
--net lnmp \
-v /opt/mysql/data:/var/lib/mysql \
-v /opt/mysql/etc:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7
docker exec -it lnmp_mysql ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
6 登录到mysql创建wp库
# mysql -h 172.16.1.70 -uroot -p123456
MySQL [(none)]> create database wp DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> select host,user from mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
4 rows in set (0.00 sec)
7 设置/app/wwwroot/目录的属组属组和php-fpm.conf配置文件中配置的user,group一致
# chown -R 1200.1200 /app/wwwroot/
8 浏览器访问
http://172.16.1.70:888/wordpress
1 环境设置
# mkdir -p /opt/tomcat/webapps/ROOT/
2 创建数据库
# mysql -h 172.16.1.70 -uroot -p123456
MySQL [(none)]> create database jpress DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
3 创建tomcat容器
docker run -d \
--name tomcat \
-p 8080:8080 \
-v /opt/tomcat/webapps:/usr/local/tomcat/webapps \
tomcat:v1
4 浏览器访问
# mkdir -p /opt/gitlib
docker run -d \
--name gitlab \
-p 8443:443 \
-p 9999:80 \
-p 9998:22 \
-v $PWD/config:/etc/gitlab \
-v $PWD/logs:/var/log/gitlab \
-v $PWD/data:/var/opt/gitlab \
-v /etc/localtime:/etc/localtime \
gitlab/gitlab-ce:latest
初次会先设置管理员密码 ,然后登陆,默认管理员用户名root,密码就是刚设置的。
进入后先创建java-demo项目,提交代码,以便后面测试。
# mkdir -p /tools
提示:可以使用/root/.ssh中私钥访问gitlab。
# curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# tar -xzf harbor-offline-installer-v1.9.1.tgz
hostname: 172.16.1.71
harbor_admin_password: Harbor12345
# docker-compose -ps
harbor停止和启动命令
访问地址:http://172.16.1.71/
harbor 安装成功后默认用户名为admin
1 由于harbor 未配置https,还需要在docker上配置可信任
# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"],
"insecure-registries": ["172.16.1.71"]
}
2 登录 harbor 仓库
# docker login -uadmin -pHarbor12345 172.16.1.71
3 给镜像打标签
# docker tag SOURCE_IMAGE[:TAG] 172.16.1.71/library/IMAGE[:TAG]
4 上传镜像
# docker push 172.16.1.71/library/IMAGE[:TAG]
5 在harbor中查看上传的镜像
# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"],
"insecure-registries": ["172.16.1.71"]
}
# tar zxf jdk-8u45-linux-x64.tar.gz
docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -u root \
-v /opt/jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker \
-v /usr/local/maven:/usr/local/maven \
-v /usr/local/jdk:/usr/local/jdk \
-v /etc/localtime:/etc/localtime \
--name jenkins jenkins/jenkins:lts
不安装推荐的插件,直接安装,插件后面再装
1 maven
# vim /usr/local/maven/conf/settings.xml +158
2 jenkins
# cd /opt/jenkins_home/updates
sed -i.bak 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
系统管理-->插件管理-->Installed
搜索 git/pipeline,点击安装。
1、添加拉取git代码凭据,并获取id替换到pipeline脚本中的git_auth变量值。
2、添加拉取harbor镜像凭据,并获取id替换到pipeline脚本中docker_registry_auth变量值。
This project is parameterized -> String Parameter
Name:Branch # 变量名,下面脚本中调用
Default Value:master # 默认分支
Description:发布的代码分支 # 描述
#!/usr/bin/env groovy
def registry = "172.16.1.71"
def project = "library"
def app_name = "tomcat"
def image_name = "${registry}/${project}/${app_name}:${Branch}-${BUILD_NUMBER}"
def git_address = "http://172.16.1.71:9999/root/java-demo.git"
def docker_registry_auth = "3aadf18f-edca-4d22-a42b-56013fccb681"
def git_auth = "6b7d1b31-830e-416f-9bd6-e6c9e3c1df4b"
pipeline {
agent any
stages {
stage('拉取代码'){
steps {
checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
}
}
stage('代码编译'){
steps {
sh """
JAVA\_HOME=/usr/local/jdk
PATH=$JAVA\_HOME/bin:/usr/local/maven/bin:$PATH
mvn clean package -Dmaven.test.skip=true
"""
}
}
stage('构建镜像'){
steps {
withCredentials(\[usernamePassword(credentialsId: "${docker\_registry\_auth}", passwordVariable: 'password', usernameVariable: 'username')\]) {
sh """
echo '
FROM ${registry}/library/tomcat:v1
LABEL maitainer liuchang
RUN rm -rf /usr/local/tomcat/webapps/\*
ADD target/\*.war /usr/local/tomcat/webapps/ROOT.war
' > Dockerfile
docker build -t ${image\_name} .
docker login -u ${username} -p '${password}' ${registry}
docker push ${image\_name}
"""
}
}
}
stage('部署到Docker'){
steps {
sh """
REPOSITORY=${image\_name}
docker rm -f tomcat-java-demo |true
docker container run -d --name tomcat-java-demo -p 88:8080 ${image\_name}
"""
}
}
}
}
1、构建流程图
2、代码构建时的工作空间
# ls /opt/jenkins_home/workspace/item-java
db Dockerfile LICENSE pom.xml README.md src target
classes generated-sources ly-simple-tomcat-0.0.1-SNAPSHOT ly-simple-tomcat-0.0.1-SNAPSHOT.war maven-archiver maven-status
3、查看镜像仓库
4、查看部署的docker容器是否运行
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
172.16.1.71/library/tomcat master-5 b2a8f33a5b69 3 minutes ago 784MB
172.16.1.71/library/tomcat v1 ca2e1ee6962b 3 days ago 765MB
5、访问网站
# mkdir -p /opt/prometheus/
--name prometheus \
-p 9090:9090 \
-v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
docker run -d \
--name grafana \
-p 3000:3000 \
grafana/grafana
监控Docker主机模板:https://grafana.com/dashboards/193
docker run \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:ro \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/dev/disk/:/dev/disk:ro \
--publish=8090:8080 \
--detach=true \
--name cadvisor \
--privileged \
--device=/dev/kmsg \
google/cadvisor:latest
1 登录grafana
2 效果图
1、拉取代码
2、代码编译(java项目),产出war包
3、打包项目镜像并推送到镜像仓库
4、部署镜像测试
AUTHOR:刘畅
手机扫一扫
移动阅读更方便
你可能感兴趣的文章