K8S运行基于微服务的应用程序(一) 创建docker镜像

2 篇文章 0 订阅
1 篇文章 0 订阅

本文为 三小时攻克 Kubernetes! 的学习实践记录

一、为前端React应用创建镜像

安装Node.js 和 npm

进入 /sa-frontend 目录

$ npm install
$ npm run build

在目录下得到/build文件夹,创建Dockerfile

FROM nginx
COPY build /usr/share/nginx/html

创建镜像

$ sudo docker build -t sentiment:frontend .

Sending build context to Docker daemon    2.4MB
Step 1/2 : FROM nginx
 ---> 602e111c06b6
Step 2/2 : COPY build /usr/share/nginx/html
 ---> 684399b63bb7
Successfully built 684399b63bb7
Successfully tagged sentiment:frontend

查看本地镜像

$ sudo docker image ls
REPOSITORY  TAG IMAGE   ID              CREATED             SIZE
sentiment   frontend    684399b63bb7    About a minute ago  129MB

启动镜像

$ sudo docker run -d -p 8888:80 sentiment:frontend
38a4d82ccaf465fbf25b8530ac19beab095fe286fe0de97020bc0d74121a13fd

打开浏览器 http://127.0.0.1:8888 ,可以看到页面已经工作

页面

镜像已经上传为 qxy714842585/sentiment:frontend

$ sudo docker push qxy714842585/sentiment:frontend
The push refers to repository [docker.io/qxy714842585/sentiment]
8beb72c8f51b: Pushed 
b3003aac411c: Mounted from library/nginx
216cf33c0a28: Mounted from library/nginx
c2adabaecedb: Mounted from library/nginx
frontend: digest: sha256:6144e440a13472827404fbe21551970ab169eed4535d678397f02856d2d5a342 size: 1158

二、为 java web 应用创建镜像

安装 JDK8 和 Maven 参考 Ubuntu18 安装 Java8 和 Maven

安装JDK

# 下载并解压jdk8之后
$ sudo mv  jdk1.8.0_251 /usr/jdk8
# 配置环境变量
$ sudo gedit /etc/profile

在最后加入

export JAVA_HOME=/usr/jdk8
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=.:${JAVA_HOME}/bin:$PATH

使环境变量生效

$ source /etc/profile
$ java -version
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)

安装Maven

$ wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
$ tar -xvf apache-maven-3.6.3-bin.tar.gz
$ sudo mv apache-maven-3.6.3  /usr/apache-maven-3.6.3

配置环境变量 $ sudo gedit /etc/profile

在最后加入

export MAVEN_HOME=/usr/apache-maven-3.6.3
export PATH=${PATH}:${MAVEN_HOME}/bin

配置生效

$ source /etc/profile
$ mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/apache-maven-3.6.3
Java version: 1.8.0_251, vendor: Oracle Corporation, runtime: /usr/jdk8/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.3.0-51-generic", arch: "amd64", family: "unix"

创建镜像

打包jar文件 这里需要给maven配置国内源

$ cd /sa-webapp
$ mvn install

Dockerfile如下

FROM openjdk:8-jdk-alpine
# Environment Variable that defines the endpoint of sentiment-analysis python api.
ENV SA_LOGIC_API_URL http://localhost:5000
ADD target/sentiment-analysis-web-0.0.1-SNAPSHOT.jar /
EXPOSE 8080
CMD ["java", "-jar", "sentiment-analysis-web-0.0.1-SNAPSHOT.jar", "--sa.logic.api.url=${SA_LOGIC_API_URL}"]

创建镜像

$ sudo docker build -t sentiment/webapp .
Sending build context to Docker daemon  20.51MB
Step 1/5 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/5 : ENV SA_LOGIC_API_URL http://localhost:5000
 ---> Using cache
 ---> a7a86a84ea20
Step 3/5 : ADD target/sentiment-analysis-web-0.0.1-SNAPSHOT.jar /
 ---> c61b38c3cee1
Step 4/5 : EXPOSE 8080
 ---> Running in 79492947da55
Removing intermediate container 79492947da55
 ---> 2c327b52b730
Step 5/5 : CMD ["java", "-jar", "sentiment-analysis-web-0.0.1-SNAPSHOT.jar", "--sa.logic.api.url=${SA_LOGIC_API_URL}"]
 ---> Running in 3b00fa9f5ece
Removing intermediate container 3b00fa9f5ece
 ---> b96d71a88fd8
Successfully built b96d71a88fd8
Successfully tagged sentiment/webapp:latest

镜像已经上传为 qxy714842585/sentiment:webapp

三、为 python 应用创建镜像

进入/sa-logic目录 ,创建镜像

$ sudo docker build -t sentiment/analysis-logic .
Sending build context to Docker daemon  6.144kB
Step 1/7 : FROM python:3.6.6-alpine
3.6.6-alpine: Pulling from library/python
4fe2ade4980c: Pulling fs layer
7cf6a1d62200: Pulling fs layer
f0d690b9e495: Pulling fs layer
fac5d45ad062: Waiting
dd9b067ef6fd: Waiting
3.6.6-alpine: Pulling from library/python
4fe2ade4980c: Pull complete
7cf6a1d62200: Pull complete
f0d690b9e495: Pull complete
fac5d45ad062: Pull complete
dd9b067ef6fd: Pull complete
Digest: sha256:ac1cf9845670783d36e86f793ad163f479ac2703c497761e5e84646868516ba9
Status: Downloaded newer image for python:3.6.6-alpine
 ---> a78e257617d1
Step 2/7 : COPY sa /app
 ---> 594fd057d79a
Step 3/7 : WORKDIR /app
 ---> Running in 26f6705253e5
Removing intermediate container 26f6705253e5
 ---> 387ad7fc1858
Step 4/7 : RUN pip3 install -r requirements.txt &&     python3 -m textblob.download_corpora
 ---> Running in b936533fec5a

安装regex时报错

  Building wheel for regex (setup.py): started
  Building wheel for regex (setup.py): finished with status 'error'
  ERROR: Command errored out with exit status 1:
   command: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-71xr2ifg/regex/setup.py'"'"'; __file__='"'"'/tmp/pip-install-71xr2ifg/regex/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-lmgi4_k_
       cwd: /tmp/pip-install-71xr2ifg/regex/
  Complete output (17 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-3.6
  creating build/lib.linux-x86_64-3.6/regex
  copying regex_3/__init__.py -> build/lib.linux-x86_64-3.6/regex
  copying regex_3/regex.py -> build/lib.linux-x86_64-3.6/regex
  copying regex_3/_regex_core.py -> build/lib.linux-x86_64-3.6/regex
  copying regex_3/test_regex.py -> build/lib.linux-x86_64-3.6/regex
  running build_ext
  building 'regex._regex' extension
  creating build/temp.linux-x86_64-3.6
  creating build/temp.linux-x86_64-3.6/regex_3
  gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -I/usr/local/include/python3.6m -c regex_3/_regex.c -o build/temp.linux-x86_64-3.6/regex_3/_regex.o
  unable to execute 'gcc': No such file or directory
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for regex
  Running setup.py clean for regex
Successfully built nltk MarkupSafe
Failed to build regex

dockerfile如下:

FROM python:3.6.6-alpine
COPY sa /app
WORKDIR /app
RUN pip3 install -r requirements.txt && \
    python3 -m textblob.download_corpora
EXPOSE 5000
ENTRYPOINT ["python3"]
CMD ["sentiment_analysis.py"]

找了一下类似的问题python-3.x – 如何在Alpine Linux容器上安装pyzmq发现可能是python-alpine版本不适合安装带有C扩展的包,虽然可以在alpine中安装gcc,但网络因莫名原因超时。因此修改dockerfile为 FROM python:3.6.6 ,成功得到需要的python镜像,另外由于众所周知的网络原因,需要配置pip源 -i https://pypi.tuna.tsinghua.edu.cn/simple

但是存在一个十分严重的问题,如下:

$ sudo docker image ls
REPOSITORY                         TAG                 IMAGE ID            CREATED              SIZE
sentiment/analysis-logic           latest              f530d677079f        About a minute ago   950MB

整整950MB,接下来需要找一个可靠而又在当前网络条件下可以使用的python基础镜像
使用 python3.6.6-slim 替代,成功

python镜像版本

$ sudo docker image ls
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
sentiment/analysis-logic-slim      latest              a223344a0a62        30 seconds ago      170MB
analysis-logic                     latest              f530d677079f        41 minutes ago      950MB

今天dockerhub的push和pull一直失败,最后阿里云真香 阿里云容器镜像服务,难道是免费的???

镜像列表

参考资料

  1. 三小时攻克 Kubernetes!
  2. https://github.com/rinormaloku/k8s-mastery
  3. Ubuntu18 安装 Java8 和 Maven
  4. 阿里云容器镜像服务
  5. python-3.x – 如何在Alpine Linux容器上安装pyzmq
  6. https://python.ctolib.com/revsys-optimized-python-docker.html
  7. https://hub.docker.com/_/python/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值