一:题目一
创建一个名为deployment-clusterrole的clusterrole,并且对该clusterrole只绑定对Deployment、Daemonset、Statefulset的创建权限。在指定命名空间exam创建一个名为exam-user的serviceaccount,并且将上一步创建clusterrole和该serviceaccount绑定。
通过命令行进行创建clusterrole
[root@master ~]# kubectl create clusterrole -h
Create a ClusterRole.
Examples:
# Create a ClusterRole named "pod-reader" that allows user to perform "get", "watch" and "list" on pods
kubectl create clusterrole pod-reader --verb=get,list,watch --resource=pods
.............
Usage:
kubectl create clusterrole NAME --verb=verb --resource=resource.group [--resource-name=resourcename]
[--dry-run=server|client|none] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
##创建步骤在这里
[root@master ~]# kubectl create clusterrole deployment-clusterrole --verb=create --resource=Deployment,Daemonset,Statefulset
[root@master ~]# clusterrole.rbac.authorization.k8s.io/deployment-clusterrole created
通过编写yaml来创建
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole #kind类型为集群
metadata:
# "namespace" omitted since ClusterRoles are not namespaced
name: secret-reader
rules:
- apiGroups: [""]
resources: ["Deployment,Daemonset,Statefulset"]
verbs: ["create"]
创建exam命名空间
[root@master ~]# kubectl create ns exam
创建serviceaccount
[root@master ~]# kubectl create ServiceAccount exam-user
将clusterrole和该serviceaccount绑定
[root@master ~]# kubectl create rolebinding -h
Create a RoleBinding for a particular Role or ClusterRole.
Examples:
# Create a RoleBinding for user1, user2, and group1 using the admin ClusterRole
kubectl create rolebinding admin --clusterrole=admin --user=user1 --user=user2 --group=group1
.....................
Usage:
kubectl create rolebinding NAME --clusterrole=NAME|--role=NAME [--user=username] [--group=groupname]
[--serviceaccount=namespace:serviceaccountname] [--dry-run=server|client|none] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
进行绑定
[root@master ~]# kubectl create rolebinding exam-binding --clusterrole=deployment-clusterrole --serviceaccount=exam:exam-user
通过编写yaml方式绑定
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metaData:
name: deployment-rolebinding
namespaces: exam
roleRef:
apiGroup: rbac.authorization.k8s.io/v1
kind: ClusterRole
name:deployment-clusterrole
subjecs:
- kind: ServiceAccount
name: exam-user
namespaces: exam
二:题目二
在default命名空间下使用镜像nginx创建一个工作负载nginx-exam,指定pod运行在master节点,然后以NodePort方式对外暴露10080端口
在default命名空间下创建HPA规则nginx-exam-autoscale来管理上一题的工作负载nginx-exam,要求其Pod副本数在1-10之间动态变化
创建deployment无状态工作负载
[root@master ~]#vi nginx-exam.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
nodeName: master
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: mynginxport
containerPort: 80
创建svc暴露端口
在写svc前要修改/etc/kubernetes/manifests/kube-apiserver.yaml
在文件中添加这一行,在默认情况下nodeport暴露的端口是30000-32XXX左右所以不包含10080所以要修改文件
- —service-node-port-range=10000-49999
[root@master ~]# svc-deploy.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-deploy
spec:
selector:
app: nginx #标签要选择到上面创建的pod
type: NodePort
ports:
- name: myport
targetPort: 80
port: 80
nodePort: 10080