【问题标题】:StatefulSet FailedCreate ElasticSearchStatefulSet 失败创建 ElasticSearch
【发布时间】:2019-11-06 00:40:02
【问题描述】:

尝试在 PKS 实例上安装 ElasticSearch for Kubernetes 时,我遇到了一个问题,在运行 kubectl get events --all-namespaces 后,我看到了 create Pod logging-es-default-0 in StatefulSet logging-es-default failed error: pods "logging-es-default-0" is forbidden: SecurityContext.RunAsUser is forbidden。这是否与 pod 安全策略有关?如果不允许特权容器,有什么方法可以将 ElasticSearch 部署到 Kubernetes?

编辑:这是我传递给 elasticsearch helm 图表的 values.yml 文件。

---
clusterName: "elasticsearch"
nodeGroup: "master"

# The service that non master groups will try to connect to when joining the cluster
# This should be set to clusterName + "-" + nodeGroup for your master group
masterService: ""

# Elasticsearch roles that will be applied to this nodeGroup
# These will be set as environment variables. E.g. node.master=true
roles:
  master: "true"
  ingest: "true"
  data: "true"

replicas: 3
minimumMasterNodes: 2

esMajorVersion: ""

# Allows you to add any config files in /usr/share/elasticsearch/config/
# such as elasticsearch.yml and log4j2.properties
esConfig: {}
#  elasticsearch.yml: |
#    key:
#      nestedkey: value
#  log4j2.properties: |
#    key = value

# Extra environment variables to append to this nodeGroup
# This will be appended to the current 'env:' key. You can use any of the kubernetes env
# syntax here
extraEnvs: []
#  - name: MY_ENVIRONMENT_VAR
#    value: the_value_goes_here

# A list of secrets and their paths to mount inside the pod
# This is useful for mounting certificates for security and for mounting
# the X-Pack license
secretMounts: []
#  - name: elastic-certificates
#    secretName: elastic-certificates
#    path: /usr/share/elasticsearch/config/certs

image: "docker.elastic.co/elasticsearch/elasticsearch"
imageTag: "7.4.1"
imagePullPolicy: "IfNotPresent"

podAnnotations: {}
  # iam.amazonaws.com/role: es-cluster

# additionals labels
labels: {}

esJavaOpts: "-Xmx1g -Xms1g"

resources:
  requests:
    cpu: "100m"
    memory: "2Gi"
  limits:
    cpu: "1000m"
    memory: "2Gi"

initResources: {}
  # limits:
  #   cpu: "25m"
  #   # memory: "128Mi"
  # requests:
  #   cpu: "25m"
  #   memory: "128Mi"

sidecarResources: {}
  # limits:
  #   cpu: "25m"
  #   # memory: "128Mi"
  # requests:
  #   cpu: "25m"
  #   memory: "128Mi"

networkHost: "0.0.0.0"

volumeClaimTemplate:
  accessModes: [ "ReadWriteOnce" ]
  resources:
    requests:
      storage: 30Gi

rbac:
  create: false
  serviceAccountName: ""

podSecurityPolicy:
  create: false
  name: ""
  spec:
    privileged: false
    fsGroup:
      rule: RunAsAny
    runAsUser:
      rule: RunAsAny
    seLinux:
      rule: RunAsAny
    supplementalGroups:
      rule: RunAsAny
    volumes:
      - secret
      - configMap
      - persistentVolumeClaim

persistence:
  enabled: true
  annotations: {}

extraVolumes: ""
  # - name: extras
  #   emptyDir: {}

extraVolumeMounts: ""
  # - name: extras
  #   mountPath: /usr/share/extras
  #   readOnly: true

extraInitContainers: ""
  # - name: do-something
  #   image: busybox
  #   command: ['do', 'something']

# This is the PriorityClass settings as defined in
# https://kubernetes.io/docs/concepts/configuration/pod-priority-preemption/#priorityclass
priorityClassName: ""

# By default this will make sure two pods don't end up on the same node
# Changing this to a region would allow you to spread pods across regions
antiAffinityTopologyKey: "kubernetes.io/hostname"

# Hard means that by default pods will only be scheduled if there are enough nodes for them
# and that they will never end up on the same node. Setting this to soft will do this "best effort"
antiAffinity: "hard"

# This is the node affinity settings as defined in
# https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#node-affinity-beta-feature
nodeAffinity: {}

# The default is to deploy all pods serially. By setting this to parallel all pods are started at
# the same time when bootstrapping the cluster
podManagementPolicy: "Parallel"

protocol: http
httpPort: 9200
transportPort: 9300

service:
  labels: {}
  labelsHeadless: {}
  type: ClusterIP
  nodePort: ""
  annotations: {}
  httpPortName: http
  transportPortName: transport

updateStrategy: RollingUpdate

# This is the max unavailable setting for the pod disruption budget
# The default value of 1 will make sure that kubernetes won't allow more than 1
# of your pods to be unavailable during maintenance
maxUnavailable: 1

podSecurityContext:
  fsGroup: null
  runAsUser: null

# The following value is deprecated,
# please use the above podSecurityContext.fsGroup instead
fsGroup: ""

securityContext:
  capabilities: null
  # readOnlyRootFilesystem: true
  runAsNonRoot: null
  runAsUser: null

# How long to wait for elasticsearch to stop gracefully
terminationGracePeriod: 120

sysctlVmMaxMapCount: 262144

readinessProbe:
  failureThreshold: 3
  initialDelaySeconds: 10
  periodSeconds: 10
  successThreshold: 3
  timeoutSeconds: 5

# https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html#request-params wait_for_status
clusterHealthCheckParams: "wait_for_status=green&timeout=1s"

## Use an alternate scheduler.
## ref: https://kubernetes.io/docs/tasks/administer-cluster/configure-multiple-schedulers/
##
schedulerName: ""

imagePullSecrets: []
nodeSelector: {}
tolerations: []

# Enabling this will publically expose your Elasticsearch instance.
# Only enable this if you have security enabled on your cluster
ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  path: /
  hosts:
    - chart-example.local
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

nameOverride: ""
fullnameOverride: ""

# https://github.com/elastic/helm-charts/issues/63
masterTerminationFix: false

lifecycle: {}
  # preStop:
  #   exec:
  #     command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
  # postStart:
  #   exec:
  #     command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]

sysctlInitContainer:
  enabled: false

keystore: []

上面列出的值会产生以下错误:

create Pod elasticsearch-master-0 in StatefulSet elasticsearch-master failed error: pods "elasticsearch-master-0" is forbidden: SecurityContext.RunAsUser is forbidden

已解决:我了解到我的 istio 部署在尝试将任何其他服务部署到我的集群时导致了问题。我做了一个错误的假设,即 istio 以及我的集群安全策略不会导致我的问题。

【问题讨论】:

  • 同样的问题你找到解决方法了吗

标签: elasticsearch kubernetes


【解决方案1】:

被禁止:SecurityContext.RunAsUser 被禁止。这是否与 Pod 安全策略有关?

是的,这正是它所要做的

显然StatefulSet 包含了一个securityContext: 节,但您的集群管理员禁止这样的操作

如果不允许特权容器,有什么方法可以将 ElasticSearch 部署到 Kubernetes?

这不完全是这里发生的事情——不是“特权”部分导致你出现问题——而是PodSpec 请求以 docker 映像中的用户以外的用户身份运行容器。事实上,如果任何现代的 elasticsearch docker 镜像都需要修改用户,我实际上会感到非常惊讶,因为所有最近的镜像都不是以 root 开头的

StatefulSet 中删除securityContext: 节并报告出现的新错误(如果有)

【讨论】:

  • 我将securityContext 更新为StatefulSet。似乎它更进了一步,现在遇到了create Pod elasticsearch-master-0 in StatefulSet elasticsearch-master failed error: Pod "elasticsearch-master-0" is invalid: spec.initContainers[0].securityContext.privileged: Forbidden: disallowed by cluster policy。我可以确定这与 init 容器的安全上下文有关。我将尝试在特定的安全上下文中使用privileged: false 运行它。
  • 所以在尝试禁用特权访问时遇到了一点问题,它在 helm 模板中被硬编码:github.com/elastic/helm-charts/blob/master/elasticsearch/…。关于这是否是跳下去的正确兔子洞或是否可以先尝试其他东西有什么想法?
  • 不是硬编码的,是guarded by sysctlInitContainer.enabled;该容器是可选的,仅在其节点上设计为exec a sysctl,因此您可以联系集群管理员并请求 sysctl 已经为您将运行 ES 的节点提供了正确的值,那么您不需要initContainer
  • 我继续禁用了初始化容器并收到了我在帖子编辑中添加的新错误。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
  • 2022-01-19
  • 2017-10-04
  • 2022-12-03
  • 1970-01-01
  • 2019-11-30
  • 2012-04-29
相关资源
最近更新 更多