学习转载:https://www.jianshu.com/p/dbfe2eee3c07
编辑dockerfile
FROM jenkins:alpine
MAINTAINER Ralph Wang
#ENV http_proxy ${proxy_address}:${proxy_port}
USER root
# Download and config kubectl(-s:Silent or quiet mode;-O:Download; -S:Show error)
RUN curl -L -O https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl; chmod +x ./kubectl; mv ./kubectl /usr/local/bin/kubectl
# Download and config docker
RUN curl -O https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz && tar -xvzf docker-latest.tgz; mv docker/* /usr/bin/ && rm docker-latest.tgz
生成镜像
docker build -t jenkinserver:v1 .
编辑jenkins-dep.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins
labels:
app: jenkins
spec:
replicas: 1
selector:
matchLabels:
app: jenkins
template: # This is the pod template。
metadata:
labels:
app: jenkins
spec:
containers:
- name: jenkins
image: jenkinserver:v1 # Image 的名字。
imagePullPolicy: Never # 迫使 Kubernetes 使用本地镜像。
ports:
- containerPort: 8080 # 8080 作为 Web Console 的 Port。
name: web
protocol: TCP
- containerPort: 50000
name: agent
protocol: TCP
volumeMounts:
- mountPath: /var/run/docker.sock # 首先挂载了本地 docker 的接口,以便在 Jenkins Container 中访问 docker。
name: docker-sock-volume
- mountPath: /var/jenkins_home # 其次挂载了 jenkins_home 以便在 Container 被摧毁是不丢失 Jenkins Server 中的内容。
name: jenkins-volume
- mountPath: /var/.kube/config # 最后挂载了外部 Kubernetes 的配置文件,以确保可以在 Jenkins Container 内部对外部的 Kubernetes 进行修改。
name: kube-config
volumes:
- name: docker-sock-volume # Docker sock 卷。
hostPath:
path: /var/run/docker.sock
- name: jenkins-volume # Jenkins 卷。
hostPath:
path: /data/.jenkins
- name: kube-config # Kubernetes config 卷。
hostPath:
path: /data/.kube/config
部署pods
kubectl create -f jenkins-dep.yaml
编辑jesnkins-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: jenkins
name: jenkins
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
nodePort: 32200 #固定访问端口
name: web
selector:
app: jenkins
部署services
create -f jesnkins-service.yaml
问题:
1、生成镜像时报错Step 5/5 : RUN curl -sSL -O https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz && tar -xvzf docker-latest.tgz; mv docker/* /usr/bin/ && rm docker-latest.tgz
---> Running in 1afb3f727962
curl: (56) LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 104
mv: cannot stat 'docker/*': No such file or directory
The command '/bin/sh -c curl -sSL -O https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz && tar -xvzf docker-latest.tgz; mv docker/* /usr/bin/ && rm docker-latest.tgz' returned a non-zero code: 1
先将下载地址在浏览器测试ok,再测试去掉参数-sSL测试curl -O https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz
2、部署pod查看kubectl describe pod jenkins-74b54d477f-j6gxt
发现报错Container image is not present with pull policy of Never,原来是yaml文件中镜像加上标识v1就正常了