目录
一、实验
1.Python项目发布到K8S集群
(1)获取应用程序代码
bash
#把hello-python.tar.gz压缩包上传到k8s控制节点master1的root下,手动解压
tar zxvf hello-python.tar.gz
(2)切换目录
bash
#requirements.txt文件包含main.py所需的包列表,pip将使用它来安装Flask库。
cd /root/hello-python/app
(3)创建dockerfile
bash
[root@master1 app]# cat dockerfile
FROM python:3.7
RUN mkdir /app
WORKDIR /app
ADD . /app/
RUN /usr/local/bin/python -m pip install --upgrade pip
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python","/app/main.py"]
(4)构建镜像
bash
#需要先拉取python镜像
docker pull python:3.7
docker build -t hello-python:v1 .
(5) 把镜像做成离线包
bash
docker save -o hello.tar.gz hello-python:v1
(6) 查看镜像大小
bash
du -sh hello.tar.gz
(7)传送到node1节点
bash
scp hello.tar.gz node1:/root
(8)在node1节点解压镜像
bash
docker load -i hello.tar.gz
(9)创建deployment的yaml文件
bash
[root@master1 app]# cat deployment.yaml
apiVersion: v1
kind: Service
metadata:
name: hello-python-service
spec:
selector:
app: hello-python
ports:
- protocol: "TCP"
port: 6000
targetPort: 5000
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-python
spec:
selector:
matchLabels:
app: hello-python
replicas: 1
template:
metadata:
labels:
app: hello-python
spec:
containers:
- name: hello-python
image: hello-python:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 5000
(10)更新yaml文件
bash
kubectl apply -f deployment.yaml
(11)访问python项目
bash
kubectl get svc
浏览器访问192.168.204.180:31552即可: