基于k8s部署jenkins

独特的命名空间提供了额外的隔离层,并且可以对连续集成环境进行更多控制。通过在终端上键入以下命令为 Jenkins 部署创建命名空间:

# kubectl create namespace jenkins

创建持久卷

为什么需要创建持久卷?Jenkins 控制器 pod创建一个持久卷。这将防止我们在重新启动minikube时丢失Jenkins控制器的整个配置和工作。

# cat jenkins-volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-pv
  namespace: jenkins
spec:
  storageClassName: jenkins-pv
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 20Gi
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /data/jenkins-volume/

启动它

# kubectl apply -f jenkins-volume.yaml

创建serviceAccount

在 Kubernetes 中,服务帐户用于为 Pod 提供身份。想要与 API 服务器交互的 Pod 将使用特定的服务帐户进行身份验证。默认情况下,应用程序将以其default运行的命名空间中的服务帐户身份进行身份验证。这意味着,例如,在test命名空间中运行的应用程序将使用命名空间的默认服务帐户test。

我们将创建一个名为 jenkins 的服务帐户:

# cat jenkins-sa.yaml
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins
  namespace: jenkins
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  annotations:
    rbac.authorization.kubernetes.io/autoupdate: "true"
  labels:
    kubernetes.io/bootstrapping: rbac-defaults
  name: jenkins
rules:
- apiGroups:
  - '*'
  resources:
  - statefulsets
  - services
  - replicationcontrollers
  - replicasets
  - podtemplates
  - podsecuritypolicies
  - pods
  - pods/log
  - pods/exec
  - podpreset
  - poddisruptionbudget
  - persistentvolumes
  - persistentvolumeclaims
  - jobs
  - endpoints
  - deployments
  - deployments/scale
  - daemonsets
  - cronjobs
  - configmaps
  - namespaces
  - events
  - secrets
  verbs:
  - create
  - get
  - watch
  - delete
  - list
  - patch
  - update
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - get
  - list
  - watch
  - update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  annotations:
    rbac.authorization.kubernetes.io/autoupdate: "true"
  labels:
    kubernetes.io/bootstrapping: rbac-defaults
  name: jenkins
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: jenkins
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: Group
  name: system:serviceaccounts:jenkins

应用它

# kubectl apply -f jenkins-sa.yaml

安装jenkins

  • 准备部署文件jenkins-deployment.yaml
vim jenkins-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
      - name: jenkins
        image: jenkins/jenkins:lts-jdk11
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: jenkins-home
          mountPath: /var/jenkins_home
      volumes:
      - name: jenkins-home
        emptyDir: { }

创建部署

以下命令指示系统在 jenkins 命名空间内安装 Jenkins。

kubectl apply -f jenkins-deployment.yaml -n jenkins

授予对 Jenkins 服务的访问权限

创建jenkins-service.yaml

$ vim jenkins-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: jenkins
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080
  selector:
    app: jenkins

创建服务

$ kubectl apply -f jenkins-service.yaml -n jenkins

要验证创建服务是否成功,执行

扫描二维码关注公众号,回复: 14227192 查看本文章
$ kubectl get services -n jenkins
NAME       TYPE        CLUSTER-IP       EXTERNAL-IP    PORT(S)           AGE
jenkins    NodePort    10.103.31.217    <none>         8080:33594/TCP    59s

访问 Jenkins 仪表板

从上面的输出中我们可以看到该服务已在端口 32664 上公开.

现在我们可以访问192.168.214.132:33594/的Jenkins实例

192.168.214.132是我的node节点ip

要访问 Jenkins,您最初需要输入您的凭据。新安装的默认用户名是 admin。可以通过多种方式获取密码。此示例使用 Jenkins 部署 pod 名称。

要查找 pod 的名称,请输入以下命令:

$ kubectl get pods -n jenkins

找到 pod 的名称后,使用它来访问 pod 的日志。

$ kubectl logs <pod_name> -n jenkins

密码位于日志末尾,格式为长字母数字字符串:

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required.
An admin user has been created and a password generated.
Please use the following password to proceed to installation:

94b73ef6578c4b4692a157f768b2cfef

This may also be found at:
/var/jenkins_home/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

猜你喜欢

转载自blog.csdn.net/yuemancanyang/article/details/122163026