如何在 Kubernetes 上找到 Jenkins 管理员密码

通过 Helm 安装后,追踪 Jenkins 管理员密码出乎意料地神秘。以下是如何操作。
148 位读者喜欢这篇文章。
How Kubernetes became the solution for migrating legacy applications

Opensource.com

使 Kubernetes 更易于导航的工具非常出色,以至于有时当我找不到简单的方法来获得答案时,我会感到惊讶。作为一个不经常使用 Kubernetes 的人,任何中等程度的故障排除都会变成一个下午,首先,质疑我的理智,其次,考虑从事牧羊人或其他远离键盘的工作。

一切始于简单。在这种情况下,我正在遵循一个依赖于 Kubernetes 集群的教程,所以我使用了 Minikube,这是一个非常好的且易于使用的虚拟环境,可以在笔记本电脑上运行集群环境。安装过程很顺利,所以我开始使用 Helm,这是一个非常直接的 Kubernetes 包管理器。我使用了 Helm 的一个 Charts 来安装 Jenkins,并且我顺利启动并运行,没有任何问题。JenkinsDevOps 管道 的事实标准,并且能在短短几分钟内运行它真是太棒了。

我已经配置好我的环境,一切都在响应。现在,登录 Jenkins 的 管理员 密码是什么?

作为 Helm Chart 的一部分,Jenkins 为其图形用户界面 (GUI) 配置了默认用户名和密码。我已经启动并运行了 GUI,但我不知道如何登录。

我不知道我错过了什么,所以我查看了 GitHub 上 chart 的存储库,并尽可能多地搜索。我尝试谷歌搜索所有我能想到的相关搜索模式

  • 如何在 Kubernetes 上检索 Jenkins 管理员密码
  • Kubernetes Jenkins 密码 Helm
  • Jenkins 管理员密码 Helm Chart
  • Helm Jenkins 使用方法

虽然有很多很棒的教程,但没有一个答案能满足我这个初学者的水平。

TL;DR 答案

如果您只想查找您的 Jenkins 密码,并且不关心为什么这样做有效,请运行以下命令

printf $(kubectl get secret --namespace jenkins jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo

默认用户是 admin。现在您知道了这两个细节,您可以登录 UI。您问 UI 在哪里?运行此命令

# Be sure to update your jenkins pod name in the following command
$ kubectl port-forward jenkins-7565554b8f-cvhbd 8088:8080

然后您可以导航到 127.0.0.1:8088 以访问 UI。

Jenkins login screen

场景

以下是此解决方案的背景。我有一个工作正常的本地 Kubernetes 环境,这要归功于 Minikube。我通过 Homebrew (作为 MacOS 用户) 安装了 Kubernetes 包管理器 Helm,并采取了下一步

$ helm install --name jenkins stable/jenkins --namespace jenkins
Error: could not find tiller

开玩笑的——每当 Helms 首次在集群中运行时,您都必须初始化 Tiller 服务。Helm 词汇表 中说

Tiller 是 Helm 的集群内组件。它直接与 Kubernetes API 服务器交互,以安装、升级、查询和删除 Kubernetes 资源。它还存储表示发布的对​​象。

让我们正确地做这件事

$ helm init
Creating /Users/mbbroberg/.helm
Creating /Users/mbbroberg/.helm/repository
Creating /Users/mbbroberg/.helm/repository/cache
Creating /Users/mbbroberg/.helm/repository/local
Creating /Users/mbbroberg/.helm/plugins
Creating /Users/mbbroberg/.helm/starters
Creating /Users/mbbroberg/.helm/cache/archive
Creating /Users/mbbroberg/.helm/repository/repositories.yaml
Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com
Adding local repo with URL: http://127.0.0.1:8879/charts
$HELM_HOME has been configured at /Users/mbbroberg/.helm.

Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.

Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.
To prevent this, run `helm init` with the --tiller-tls-verify flag.
For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation

现在是时候安装 Jenkins 并确认它已在线

helm install --name jenkins stable/jenkins --namespace jenkins
NAME:   jenkins
LAST DEPLOYED: Tue May 28 11:12:39 2019
NAMESPACE: jenkins
STATUS: DEPLOYED

RESOURCES:
==> v1/ConfigMap
NAME           DATA  AGE
jenkins        5     0s
jenkins-tests  1     0s

==> v1/Deployment
NAME     READY  UP-TO-DATE  AVAILABLE  AGE
jenkins  0/1    1           0          0s

==> v1/PersistentVolumeClaim
NAME     STATUS   VOLUME    CAPACITY  ACCESS MODES  STORAGECLASS  AGE
jenkins  Pending  standard  0s

==> v1/Pod(related)
NAME                      READY  STATUS   RESTARTS  AGE
jenkins-7565554b8f-cvhbd  0/1    Pending  0         0s

==> v1/Role
NAME                     AGE
jenkins-schedule-agents  0s

==> v1/RoleBinding
NAME                     AGE
jenkins-schedule-agents  0s

==> v1/Secret
NAME     TYPE    DATA  AGE
jenkins  Opaque  2     0s

==> v1/Service
NAME           TYPE          CLUSTER-IP    EXTERNAL-IP  PORT(S)         AGE
jenkins        LoadBalancer  10.96.90.0    <pending>    8080:32015/TCP  0s
jenkins-agent  ClusterIP     10.103.85.49  <none>       50000/TCP       0s

==> v1/ServiceAccount
NAME     SECRETS  AGE
jenkins  1        0s


NOTES:
1. Get your 'admin' user password by running:
  printf $(kubectl get secret --namespace jenkins jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo
2. Get the Jenkins URL to visit by running these commands in the same shell:
  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        You can watch the status of by running 'kubectl get svc --namespace jenkins -w jenkins'
  export SERVICE_IP=$(kubectl get svc --namespace jenkins jenkins --template "{{ range (index .status.loadBalancer.ingress 0) }}{{ . }}{{ end }}")
  echo http://$SERVICE_IP:8080/login

3. Login with the password from step 1 and the username: admin


For more information on running Jenkins on Kubernetes, visit:
https://cloud.google.com/solutions/jenkins-on-container-engine

$ kubectl get pods --namespace jenkins
NAME                       READY     STATUS    RESTARTS   AGE
jenkins-7565554b8f-cvhbd   1/1       Running   0          9m

您会注意到,获取管理员密码的神奇命令是上面冗长输出的一部分(在注释 1 中)。这个非常有用的命令

  • 执行 kubectl 命令以提取 Jenkins 创建的 secret
  • 感谢 printf$(),将该执行作为管道运算符的输入发送
  • 解码密码,该密码以 base64 编码存储,然后将其输出到屏幕

我一开始没有注意到输出中的那一行,并且我无法通过搜索轻松找到答案,所以希望现在对大家来说更简单了。

还有一件事...

这里仍然有点死胡同。我的配置从未分配 SERVICE_IP,如上面的输出(注释 2)中所述。Jessica Repka 建议设置从 pod 到本地主机的端口转发,以便可以在本地寻址它。通过这样做,我将能够像在我的笔记本电脑本地运行一样访问 UI,而不是在 Minikube 中的容器内运行。

# Be sure to update your jenkins pod name in the following command
$ kubectl port-forward jenkins-7565554b8f-cvhbd 8088:8080

通过保持该终端窗口运行,我有一个端口转发到本地可用的端口——我在这里选择了 8088——并且可以在 http://127.0.0.1:8088/login 开始使用 Jenkins。

我希望这篇操作指南能帮助其他人在 Kubernetes 上开始使用 Jenkins。

I'm happiest at a microphone
Matt 曾是 EMC 存储专家、VMware vExpert,以及其他专有技术的忠实拥护者。他现在专注于开源和 DevRel 的采用。

1 条评论

`kubectl port-forward` 命令也是一种*极好的*方式来开发服务,而无需在服务完全准备好发布之前将其暴露给世界。

这也是在笔记本电脑上使用本地客户端与 pod 中的远程服务(例如:`mysqldump`、`etcdctl` 等)的好方法。

这可能是我最喜欢也是 Kubernetes 最方便的功能!

Creative Commons License本作品根据 Creative Commons Attribution-Share Alike 4.0 International License 许可。
© . All rights reserved.