K8s 部署jenkins项目
创建目录
[root@master ~]# tree /java/
/java/
├── dockerfile
└── files
├── apache-tomcat-9.0.35.tar.gz
├── index.jsp
├── install.sh
├── jenkins-2.319.war
写dockerfile
[root@master ~]# mkdir -p /java/files
[root@master ~]# touch dockerfile
[root@master ~]# ls /java/
dockerfile files
[root@master ~]# ls /java/files/
apache-tomcat-9.0.35.tar.gz index.jsp install.sh jenkins-2.319.war
构建镜像
[root@master ~]# cat /java/files/install.sh
#!/bin/bash
rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-$(awk -F '"' 'NR==5{print $2}' /etc/os-release).repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum install -y java-1.8.0-openjdk
ln -s /usr/local/apache-tomcat-${version}/ /usr/local/tomcat
cp /opt/jenkins-2.319.war /usr/local/tomcat/webapps/
[root@master ~]# vi /java/dockerfile
FROM centos
MAINTAINER JRE "xxxx@dsa.com"
ENV version 9.0.35
ENV PATH $PATH:/usr/local/tomcat/bin
ADD files/apache-tomcat-${version}.tar.gz /usr/local/
COPY files/jenkins-2.319.war /opt
COPY files/index.jsp /opt
COPY files/install.sh /tmp
RUN /tmp/install.sh
WORKDIR /usr/local/tomcat/bin
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
[root@master java]# docker build -t linlusama/tomcat:v2.0 .
Sending build context to Docker daemon 157.2MB
Step 1/12 : FROM centos
---> 5d0da3dc9764
Step 2/12 : MAINTAINER JRE "xxxx@dsa.com"
---> Using cache
---> ed908e34c66c
Step 3/12 : ENV version 9.0.35
---> Using cache
---> 78770ae2aebc
Step 4/12 : ENV PATH $PATH:/usr/local/tomcat/bin
---> Using cache
---> 830cc1d69ff7
Step 5/12 : ADD files/apache-tomcat-${version}.tar.gz /usr/local/
---> Using cache
---> 9807829a6ead
Step 6/12 : COPY files/jenkins-2.319.war /opt
---> Using cache
---> b309567d5045
Step 7/12 : COPY files/index.jsp /opt
---> Using cache
---> 3550e9891309
Step 8/12 : COPY files/install.sh /tmp
---> Using cache
---> a35142f8f9da
Step 9/12 : RUN /tmp/install.sh
---> Using cache
---> 380aeee274c7
Step 10/12 : WORKDIR /usr/local/tomcat/bin
---> Using cache
---> 0dee4b7fd730
Step 11/12 : EXPOSE 8080
---> Using cache
---> 365e5624149e
Step 12/12 : CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
---> Using cache
---> ddc629c835d1
Successfully built ddc629c835d1
Successfully tagged xm17671855780/tomcat:v2.0
部署
//编写清单文件
[root@master manifes]# cat javashop.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: javashop
labels:
app: javashop
spec:
replicas: 2
selector:
matchLabels:
app: javashop
template:
metadata:
name: javashop
labels:
app: javashop
spec:
containers:
- image: linlusama/tomcat:v2
imagePullPolicy: IfNotPresent
name: javashop
---
apiVersion: v1
kind: Service
metadata:
name: javashop
spec:
ports:
- port: 8090
targetPort: 8090
nodePort: 30188
selector:
app: javashop
type: NodePort
//创建pod
[root@master manifes]# kubectl apply -f javashop.yaml
deployment.apps/javashop created
service/javashop created
[root@master manifes]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/javashop-7588b776bd-j7r9h 0/1 ContainerCreating 0 6s
pod/javashop-7588b776bd-pgxgt 0/1 ContainerCreating 0 6s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/javashop NodePort 10.101.42.102 <none> 8090:30188/TCP 6s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d