首先创建endpoints.yaml文件:
---apiVersion: v1kind: Endpointsmetadata: name: glusterfs-cluster namespace: cisubsets: - addresses: [{"ip": "192.168.1.1"}, {"ip": "192.168.1.2"}]ports: [{"port": 24007}]
创建gs_service.yaml文件:
---apiVersion: v1kind: Servicemetadata: name: glusterfs-cluster namespace: cispec: ports: - port: 24007 targetPort: 24007 protocol: TCP然后运行kubectl执行即可,yaml文件创建了一个namespace,名称叫ci, 指定glusterfs机器的ip和端口,有几台就写几台。
接下来我们创建jenkins-master-pv.yaml:
apiVersion: v1kind: PersistentVolumemetadata: name: pv-jenkins-master namespace: cispec: capacity: storage: 1Gi accessModes: - ReadWriteMany glusterfs: endpoints: "glusterfs-cluster" path: "k8s-volume-jenkins-master"readOnly: false
创建jenkins-master-pvc.yaml:
apiVersion: v1kind: PersistentVolumeClaimmetadata: name: pvc-jenkins-master namespace: cispec: accessModes: - ReadWriteMany resources: requests: storage: 1Gi
下一步创建jenkins_depoly.yaml:
apiVersion: apps/v1kind: Deploymentmetadata: namespace: ci name: jenkinsspec: replicas: 1 selector: matchLabels: app: jenkins template: metadata: labels: app: jenkins spec: securityContext: fsGroup: 1000 runAsUser: 0 containers: - name: jenkins image: jenkins ports: - containerPort: 8000 volumeMounts: - name: home mountPath: "/var/jenkins_home" volumes: - name: home persistentVolumeClaim: claimName: pvc-jenkins-master在deploy文件中指定了我们刚才创建的pvc,看claimName这行,对应pvc中的name。
最后创建:jenkins_service.yaml文件:
---apiVersion: v1kind: Servicemetadata: namespace: ci name: jenkins labels: name: jenkinsspec: ports: - port: 8080 targetPort: 8080 name: jenkins protocol: TCP selector: app: jenkins type: NodePort这里我们选择NodePort来暴露服务端口,方便测试验证。到此部署就结束了,在K8s上部署应用就是写资源清单的过程,在实际部署过程中出现了Jenkins启动错误:
backoffrestarfailling
查日志:
kubectl logs jenkins-7c97f9d6d9-q6fk9 -n citouch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission deniedCan not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
这是因为权限问题导致的,因为jenkins 运行的用户 id是1000,所以加上权限后解决。
获取jenkins初始密码:
进入容器:
kubectl exec -it -n ci jenkins-bff9cccdd-45jzx – bash
more /var/jenkins_home/secrets/initialAdminPassword #查看初始密码
登录访问: