2022-12-23:portainer是docker的web可视化工具。如果根据docker部署去写yaml,默认local是k8s,而不是docker,这不符合需求,需要修改yaml。请问部署在
2022-12-23:portainer是docker的web可视化工具。如果根据docker部署去写yaml,默认local是k8s,而不是docker,这不符合需求,需要修改yaml。请问部署在 k3s 中,并且默认local是docker,yaml 文件如何写?
答案2022-12-23:
1.portainer在默认情况下用的是k8s环境,所以需要改成docker环境,根据如下代码块一的go代码可知,把"KUBERNETES_SERVICE_HOST"环境变量设置为空,就会把默认环境变成docker环境。
2.根据命令行参数,给–admin-password一个值,你就不需要创建用户了。
3.–admin-password是一个哈希值,根据如下代码块二的go代码可知,需要将密码转成哈希才行。“moonfdd–moonfdd"转换成哈希是”$2a$10$4m4rYwK/TA8GRkyz4UrZ2e8B4GDG8ZhBkyGawJ05q1zJ7zmLywzmW"。
// 代码块一
// api/platform/platform.go
const (
PodmanMode = "PODMAN"
KubernetesServiceHost = "KUBERNETES_SERVICE_HOST"
NomadJobName = "NOMAD_JOB_NAME"
)
// DetermineContainerPlatform will check for the existence of the PODMAN_MODE
// or KUBERNETES_SERVICE_HOST environment variable to determine if
// the container is running on Podman or inside the Kubernetes platform.
// Defaults to Docker otherwise.
func DetermineContainerPlatform() (ContainerPlatform, error) {
podmanModeEnvVar := os.Getenv(PodmanMode)
if podmanModeEnvVar == "1" {
return PlatformPodman, nil
}
serviceHostKubernetesEnvVar := os.Getenv(KubernetesServiceHost)
if serviceHostKubernetesEnvVar != "" {
return PlatformKubernetes, nil
}
nomadJobName := os.Getenv(NomadJobName)
if nomadJobName != "" {
return PlatformNomad, nil
}
if !isRunningInContainer() {
return "", nil
}
dockerCli, err := client.NewClientWithOpts()
if err != nil {
return "", errors.WithMessage(err, "failed to create docker client")
}
defer dockerCli.Close()
info, err := dockerCli.Info(context.Background())
if err != nil {
if client.IsErrConnectionFailed(err) {
log.Warn().
Err(err).
Msg("failed to retrieve docker info")
return "", nil
}
return "", errors.WithMessage(err, "failed to retrieve docker info")
}
if info.Swarm.NodeID == "" {
return PlatformDockerStandalone, nil
}
return PlatformDockerSwarm, nil
}
// 代码块二
// api/cmd/portainer/main.go
adminPasswordHash := ""
if *flags.AdminPasswordFile != "" {
content, err := fileService.GetFileContent(*flags.AdminPasswordFile, "")
if err != nil {
log.Fatal().Err(err).Msg("failed getting admin password file")
}
adminPasswordHash, err = cryptoService.Hash(strings.TrimSuffix(string(content), "\n"))
if err != nil {
log.Fatal().Err(err).Msg("failed hashing admin password")
}
} else if *flags.AdminPassword != "" {
adminPasswordHash = *flags.AdminPassword
}
yaml如下:
# 用户名:admin
# 密码:moonfdd--moonfdd
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: portainer
name: portainer
namespace: moonfdd
spec:
replicas: 1
selector:
matchLabels:
app: portainer
template:
metadata:
labels:
app: portainer
spec:
containers:
- env:
- name: "KUBERNETES_SERVICE_HOST"
value: ""
args:
- "--admin-password"
- "$2a$10$4m4rYwK/TA8GRkyz4UrZ2e8B4GDG8ZhBkyGawJ05q1zJ7zmLywzmW"
image: portainer/portainer-ce:alpine
imagePullPolicy: IfNotPresent
name: portainer
securityContext:
privileged: true
volumeMounts:
- mountPath: /var/run/docker.sock
name: volv
# - mountPath: /data
# name: volv2
volumes:
- hostPath:
path: /var/run/docker.sock
type: Socket
name: volv
# - hostPath:
# path: /root/k8s/moonfdd/portainer/data
# type: DirectoryOrCreate
# name: volv2
---
apiVersion: v1
kind: Service
metadata:
labels:
app: portainer
name: portainer
namespace: moonfdd
spec:
ports:
- port: 8000
protocol: TCP
targetPort: 8000
name: 8000-8000
- port: 9443
protocol: TCP
targetPort: 9443
name: 9443-9443
- port: 9000
protocol: TCP
targetPort: 9000
name: 9000-9000
selector:
app: portainer
type: NodePort
公众号:福大大架构师每日一题