测试三
- 创建名为 pvviewer 的新服务帐户。授予此服务帐户访问权限,以通过创建名为 pvviewer-role 和称为 pvviewer-role-binding 的相应群集角色来列出群集中的所有 PersistentVolume。
接下来,在默认命名空间中创建一个名为 pvviewer 的 pod,其中包含映像:redis 和服务帐户:pvviewer。
#创建账户
kubectl create serviceaccount pvviewer
#创建集群角色
vim clusterRole.yml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: pvviewer-role
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["list"]
---
#创建ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: pvviewer-role-binding
subjects:
- kind: User
name: pvviewer # 'name' 是区分大小写的
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: pvviewer-role
apiGroup: rbac.authorization.k8s.io
以上也可使用命令创建serviceaccount、clusterrole、clusterrolebinding
ubectl create serviceaccount pvviewer
kubectl create clusterrole pvviewer-role --resource=persistentvolumes --verb=list
kubectl create clusterrolebinding pvviewer-role-binding --clusterrole=pvviewer-role --serviceaccount=default:pvviewer
创建pod
kubectl run pvviewer --image redis --dry-run=client -o yaml > pvviewer.yml
添加serviceaccount
vim pvviewer.yml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pvviewer
name: pvviewer
spec:
serviceAccountName: pvviewer
containers:
- image: redis
name: pvviewer
2、列出群集所有节点的内部 IP。将结果保存到文件 /root/CKA/node_ips。
答案应采用以下格式:控制平面的内部IP节点01的内部IP(在一行中)
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}' > /root/CKA/node_ips
3、创建一个名为 multi-pod 的 pod,其中包含两个容器。
Container 1, name: alpha, image: nginx
Container 2: name: beta, image: busybox, command: sleep 4800
Environment Variables:
container 1:
name: alpha
Container 2:
name: beta
---
apiVersion: v1
kind: Pod
metadata:
name: multi-pod
spec:
containers:
- image: nginx
name: alpha
env:
- name: name
value: alpha
- image: busybox
name: beta
command: ["sleep", "4800"]
env:
- name: name
value: beta
4、创建一个名为 non-root-pod 的 pod,镜像:redis:alpine
runAsUser: 1000
fsGroup: 2000
vim non-root-pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: non-root-pod
name: non-root-pod
spec:
securityContext:
runAsUser: 1000
fsGroup: 2000
containers:
- image: redis:alpine
name: non-root-pod
5、我们已经部署了一个名为 np-test-1 的新 pod 和一个名为 np-test-service 的服务。与此服务的传入连接不起作用。排除故障并修复它。
创建网络策略,名称为 ingress-to-nptest,允许通过端口 80 传入服务连接。
重要提示:请勿删除任何当前部署的对象。
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: ingress-to-nptest
namespace: default
spec:
podSelector:
matchLabels:
run: np-test-1
policyTypes:
- Ingress
ingress:
- ports:
- protocol: TCP
port: 80
6、将工作节点节点 node01 污点为不可调度。完成后,创建一个名为 dev-redis, image redis:alpine 的 pod,以确保工作负载不会调度到此工作节点。最后,创建一个名为 prod-redis 和 image:redis:alpine 的 topertation 将在 node01 上调度。
key: env_type, value: production, operator: Equal and effect: NoSchedule
设置污点:
controlplane ~ ➜kubectl taint node node01 env_type=production:NoSchedule
controlplane ~ ➜kubectl run dev-redis --image redis:alpine
controlplane ~ ➜ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dev-redis 1/1 Running 0 10s 10.244.0.4 controlplane <none> <none>
multi-pod 2/2 Running 0 17m 10.244.192.2 node01 <none> <none>
non-root-pod 1/1 Running 0 14m 10.244.192.3 node01 <none> <none>
np-test-1 1/1 Running 0 14m 10.244.192.4 node01 <none> <none>
pvviewer 1/1 Running 0 23m 10.244.192.1 node01 <none>
创建可容忍的pod
vim prod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: prod-redis
name: prod-redis
spec:
containers:
- image: redis:alpine
name: prod-redis
tolerations:
- key: "env_type"
operator: "Equal"
value: "production"
effect: "NoSchedule"
查看
controlplane ~ ➜ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dev-redis 1/1 Running 0 16m 10.244.0.4 controlplane <none> <none>
multi-pod 2/2 Running 0 34m 10.244.192.2 node01 <none> <none>
non-root-pod 1/1 Running 0 30m 10.244.192.3 node01 <none> <none>
np-test-1 1/1 Running 0 30m 10.244.192.4 node01 <none> <none>
prod-redis 1/1 Running 0 2m45s 10.244.192.5 node01 <none> <none>
pvviewer 1/1 Running 0 39m 10.244.192.1 node01 <none> <none>
controlplane ~ ➜
7、在属于生产环境和前端层的 hr 命名空间中创建一个名为 hr-pod 的 pod。
image:redis:alpine
使用适当的标签并创建所有必需的对象(如果系统中尚不存在)。
kubectl run hr-pod --namespace hr --image redis:alpine --labels tier=frontend,environment=production
8、一个名为super.kubeconfig的kubeconfig文件已经在/root/CKA下创建。配置有问题。排除故障并修复它。
k8s端口有误,更正为6443即可。
9、我们创建了一个名为nginx-deploy的新部署。将部署扩展到 3 个副本。副本增加了吗?排查问题并修复它。
无法进行扩容:由于replicaset不起作用,我们首先要排查系统的controllermanage是否出现了问题,经查勘,发现,controllermanage镜像下载有误,查配置文件发现,单词错误所导致,更正之后,恢复正常。