我正在 k8 客户端中创建一个 pod,并制作一个手表以在 pod 完成时收到通知,以便我可以读取 pod 的日志。观看界面似乎没有在频道上提供任何事件。这是代码,我将如何收到通知,即 pod 状态现已完成并已准备好读取日志
func readLogs(clientset *kubernetes.Clientset) {
// namespace := "default"
// label := "cithu"
var (
pod *v1.Pod
// watchface watch.Interface
err error
)
// returns a pod after creation
pod, err = createPod(clientset)
fmt.Println(pod.Name, pod.Status, err)
if watchface, err = clientset.CoreV1().Pods(namespace).Watch(metav1.ListOptions{
LabelSelector: pod.Name,
}); err != nil {
log.Fatalf(err.Error())
}
// How do I get notified when the pod.Status == completed
}
Run Code Online (Sandbox Code Playgroud) 我在 Windows 的“Docker 桌面”上运行 Kubernetes。
我有一个用于部署的 LoadBalancer 服务,其中有 3 个副本。我想通过某种方式访问特定的 pod (例如通过 URL 路径:<serviceIP>:8090/pod1)。
有没有办法实现这个用例?
部署.yaml:
Run Code Online (Sandbox Code Playgroud)apiVersion: v1 kind: Service metadata: name: my-service1 labels: app: stream spec: ports: - port: 8090 targetPort: 8090 name: port8090 selector: app: stream # clusterIP: None type: LoadBalancer --- apiVersion: apps/v1beta2 kind: Deployment metadata: name: stream-deployment labels: app: stream spec: replicas: 3 selector: matchLabels: app: stream strategy: type: Recreate template: metadata: labels: app: stream spec: containers: - image: stream-server-mock:latest name: stream-server-mock imagePullPolicy: Never env: …
我需要使用 RBAC 授予对一个部署和此部署的所有 pod 的访问权限。我已经成功地配置Role和RoleBinding对deploymet,这是工作的罚款:
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
namespace: <my-namespace>
name: <deployment>-manager-role
rules:
- apiGroups: ["", "extensions", "apps"]
resources: ["deployments"]
resourceNames: ["<deployment>"]
verbs: ["get", "list", "watch", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: <deployment>-manager-binding
namespace: <my-namespace>
subjects:
- kind: User
name: <username>
apiGroup: ""
roleRef:
kind: Role
name: <deployment>-manager-role
apiGroup: ""
Run Code Online (Sandbox Code Playgroud)
使用此角色用户可以访问、更新和修补部署。此部署创建具有动态名称(如<deployment>-5594cbfcf4-v4xx8)的pod 。我试图允许此用户使用部署名称和使用部署名称 + 通配符字符访问这些 pod(获取、列出、监视、读取日志、执行、删除)*:
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
namespace: <my-namespace>
name: …Run Code Online (Sandbox Code Playgroud) 我在 kubernetes 集群中遇到日期和时间问题。使用 kubeadm 在数据中心设置集群。我的主机服务器时间是使用 NTP 同步的,尽管我在配置集群后已同步它。现在,在我的集群中创建的所有 Pod 都会有错误的时间。所以原因似乎是docker采用了UTC时区。对于临时解决方案,我将卷挂载 /etc/localtime 与我们创建的 pod 中的主机一起使用,但这对于我使用 helm 存储库中的 helm 安装的应用程序似乎不可行。有什么办法可以解决这个问题吗?我不希望每个 Pod 都在正确的时间安装卷。有什么方法可以让docker从主机获取时区。
仅供参考,k8s 集群是在 CentOS 7 上设置的。它们是在 EXSi 上创建的虚拟机。谢谢
我正在尝试创建一个能够使用 来创建和更新特定配置映射的 Pod Role.rules.resourceNames。我能够从 Pod 内向 API 执行资源获取请求,但我无法创建资源,而是获取
$ kubectl logs rbac-test
Error from server (Forbidden): error when creating "/config/aaa.yaml": configmaps is forbidden: User "system:serviceaccount:default:rbac-test" cannot create resource "configmaps" in API group "" in the namespace "default"
Run Code Online (Sandbox Code Playgroud)
如果我删除resourceNames属性,我就能够创建配置映射,但我不一定希望这个 pod 能够随意创建和更新配置映射。如何限制 serviceAccount 的角色,以便该 pod 只能操作指定的 configmap aaa?
我在 kubernetes slack 中得到了一些帮助(谢谢你,艾伦)。RBAC 发生在 URL 级别,因此创建 URL 时无法进行资源名称限制授权,因为 URL 中没有资源名称!
$ kubectl logs rbac-test
Error from server (Forbidden): error when creating "/config/aaa.yaml": configmaps is forbidden: User "system:serviceaccount:default:rbac-test" cannot …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 WSL 使用本地存储设置本地持久卷。但 Pod STATUS 停止在Pending。
给出kubectl describe pod <pod-name>以下错误。
Warning FailedMount 21s (x7 over 53s) kubelet MountVolume.NewMounter initialization failed for volume "pv1" : path "/mnt/data" does not exist
该路径/mnt/data已创建并存在于本地机器上,但容器无法访问。
pod 和持久卷配置如下。
apiVersion : v1
kind : PersistentVolume
metadata :
name : pv1
spec :
capacity :
storage : 2Gi
accessModes :
- ReadWriteOnce
persistentVolumeReclaimPolicy : Retain
storageClassName : local-storage
local :
fsType : ext4
path : /mnt/data
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- …Run Code Online (Sandbox Code Playgroud) kubernetes persistent-volumes persistent-volume-claims kubernetes-pod
.spec.selector 字段定义 Deployment 如何查找要管理的 Pod。但是我们还在模板内定义了标签,那么我们通过 .spec.selectors 字段得到的额外东西是什么,因为 Deployment 也可以找到要使用模板中定义的标签进行管理的 Pod?
在下面的代码中,带有“label occloud.oracle.com/open-network-policy:allow”的 pod 如何通过部署进行管理,因为在 spec.selectors 中没有描述它
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: cheeseshop
spec:
replicas: 1
progressDeadlineSeconds: 180
selector:
matchLabels:
app.kubernetes.io/name: tutorial
app.kubernetes.io/component: cheeseshop
template:
metadata:
labels:
app.kubernetes.io/name: tutorial
app.kubernetes.io/component: cheeseshop
occloud.oracle.com/open-network-policy: allow
name: cheeseshop
Run Code Online (Sandbox Code Playgroud) yaml kubernetes google-kubernetes-engine kubernetes-deployment kubernetes-pod
我正在尝试从私人注册表中提取图像。但 pod 的状态是“ImagePullBackOff”,这意味着我需要向 pod 添加一个 Secret。
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Pulling 52m (x255 over 22h) kubelet, cn-huhehaote.i-hp3fkfzlcf1u9cigq2h7 pulling image "xxx/fcp"
Normal BackOff 8m (x5597 over 22h) kubelet, cn-huhehaote.i-hp3fkfzlcf1u9cigq2h7 Back-off pulling image "xxx/fcp"
Warning Failed 3m (x5618 over 22h) kubelet, cn-huhehaote.i-hp3fkfzlcf1u9cigq2h7 Error: ImagePullBackOff
Run Code Online (Sandbox Code Playgroud)
所以我在 pod yaml 中添加了以下代码。
spec:
containers:
- name: private-reg-container
image: <your-private-image>
imagePullSecrets:
- name: my-secret
Run Code Online (Sandbox Code Playgroud)
然后我得到了
error: map: map[] does not contain declared merge key: name
Run Code Online (Sandbox Code Playgroud)
我找到的解决方案是删除“imagePullSecret”,这对我不起作用。我想知道如何修复该错误。谁能帮我?
kubectl 版本是
kubectl …Run Code Online (Sandbox Code Playgroud) 我正在使用 kubectl 来检索 pod 列表:
kubectl get pods --selector=artifact=boot-example -n my-sandbox
Run Code Online (Sandbox Code Playgroud)
我得到的结果是:
NAME READY STATUS RESTARTS AGE
boot-example-757c4c6d9c-kk7mg 0/1 Running 0 77m
boot-example-7dd6cd8d49-d46xs 1/1 Running 0 84m
boot-example-7dd6cd8d49-sktf8 1/1 Running 0 88m
Run Code Online (Sandbox Code Playgroud)
我只想得到那些“准备好”的豆荚(通过了readinessProbe)。是否有任何 kubectl 命令只返回“就绪”pods?如果不是 kubectl 命令,那么也许是其他方式?
我在用 kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml创建部署。
我想在我的命名空间中创建部署 examplenamespace。
我怎样才能做到这一点?