乐趣区

关于kubernetes:在Kubernetesk8s中部署-jenkins

在 Kubernetes(k8s)中部署 jenkins

YAML 配置文件

因为 jenkins 须要长久化存储,通过 nfs 动静供应 pvc 存储卷。

能够参考我之前的文档:https://cloud.tencent.com/dev…

vim jenkins-deploy.yaml
cat jenkins-deploy.yaml
############### 应用 storageClass 创立 pvc ###################
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-data-pvc
  namespace: default
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

############### 创立一个 ServiceAccount 名称为:jenkins-admin###################
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins-admin
  namespace: default
  labels:
    name: jenkins

############### 绑定账户 jenkins-admin 为集群管理员角色,为了管制权限倡议绑定自定义角色 ###################
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: jenkins-admin
  labels:
    name: jenkins
subjects:
  - kind: ServiceAccount
    name: jenkins-admin
    namespace: default
roleRef:
  kind: ClusterRole
  # cluster-admin 是 k8s 集群中默认的管理员角色
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io


############### 在 default 命名空间创立 deployment ###################
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      terminationGracePeriodSeconds: 10
      # 留神:k8s 1.21.x 中 serviceAccount 改名为 serviceAccountName
      # 这里填写下面创立的 serviceAccount 的 name
      serviceAccount: jenkins-admin
      containers:
        - name: jenkins
          image: jenkins/jenkins:latest
          imagePullPolicy: IfNotPresent
          env:
            - name: JAVA_OPTS
              value: -Duser.timezone=Asia/Shanghai
          ports:
            - containerPort: 8080
              name: web
              protocol: TCP
            - containerPort: 50000
              name: agent
              protocol: TCP
          resources:
            limits:
              cpu: 1000m
              memory: 1Gi
            requests:
              cpu: 500m
              memory: 512Mi
          livenessProbe:
            httpGet:
              path: /login
              port: 8080
            initialDelaySeconds: 60
            timeoutSeconds: 5
            failureThreshold: 12
          readinessProbe:
            httpGet:
              path: /login
              port: 8080
            initialDelaySeconds: 60
            timeoutSeconds: 5
            failureThreshold: 12
          volumeMounts:
            - name: jenkinshome
              mountPath: /var/jenkins_home
      volumes:
        - name: jenkinshome
          persistentVolumeClaim:
            claimName: jenkins-data-pvc

############### 在 default 命名空间创立 service ###################
---
apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: default
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins
  type: ClusterIP
  ports:
    - name: web
      port: 8080
      targetPort: 8080


---
apiVersion: v1
kind: Service
metadata:
  name: jenkins-agent
  namespace: default
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins
  type: ClusterIP
  ports:
    - name: agent
      port: 50000
      targetPort: 50000

执行部署

kubectl apply -f jenkins-deploy.yaml
persistentvolumeclaim/jenkins-data-pvc created
serviceaccount/jenkins-admin created
clusterrolebinding.rbac.authorization.k8s.io/jenkins-admin created
deployment.apps/jenkins created
service/jenkins created
service/jenkins-agent created

拜访测试

# 查看 svc
kubectl  get svc | grep jenkins
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
jenkins         ClusterIP   10.99.124.103   <none>        8080/TCP       3m7s
jenkins-agent   ClusterIP   10.98.21.139    <none>        50000/TCP      3m6s

# 批改为 NodePort
kubectl  edit svc jenkins
type: NodePort

# 查看批改后的 svc 端口
kubectl  get svc | grep jenkins
jenkins         NodePort    10.99.124.103   <none>        8080:31613/TCP   4m24s
jenkins-agent   ClusterIP   10.98.21.139    <none>        50000/TCP        4m23s

查看明码

# 查看 pod 名称
kubectl get pod -n default | grep jenkins
jenkins-7db75dbcb9-76l7l                  1/1     Running   0          5m11s

# 查看默认明码
kubectl  exec jenkins-7db75dbcb9-76l7l -- cat /var/jenkins_home/secrets/initialAdminPassword
a9b2d13bc4c9453f93bb83e43a780f7c

对于

https://www.oiox.cn/

https://www.oiox.cn/index.php…

CSDN、GitHub、知乎、开源中国、思否、掘金、简书、华为云、阿里云、腾讯云、哔哩哔哩、今日头条、新浪微博、集体博客

全网可搜《小陈运维》

文章次要公布于微信公众号

退出移动版