port-forward,hostPort,hostNetwork总结 port-forward:可以转发pod,deploy,service
candidate@client:~$ kubectl get all
candidate@client:~$ kubectl port-forward pods/webserver-59f78d7586-64m2p --address 0.0.0.0 8081:80 // 转发单pod
candidate@client:~$ curl localhost:8081 // 新客户端测试
candidate@client:~$ kubectl port-forward deployments/webserver --address 0.0.0.0 8082:80 // 转发部署
candidate@client:~$ curl localhost:8082 // 新客户端测试
candidate@client:~$ kubectl port-forward svc/webserver --address 0.0.0.0 8083:80 // 转发service
candidate@client:~$ curl localhost:8083 // 新客户端测试
hostPort:只能针对单pod对外暴露
candidate@client:~$ kubectl run --image nginx:1.27 testload --dry-run=client -o yaml > testpod.yaml
candidate@client:~$ vim testpod.yaml
candidate@client:~$ kubectl apply -f testpod.yaml
candidate@client:~$ kubectl get pods -o wide
candidate@client:~$ curl -s node1:8082
hostNetwork:访问节点即访问Pod
candidate@client:~$ kubectl delete -f testpod.yaml
candidate@client:~$ vim testpod.yaml
candidate@client:~$ kubectl apply -f testpod.yaml
candidate@client:~$ kubectl get pods -o wide
candidate@client:~$ curl -s node1 // 访问节点即访问pod
candidate@client:~$ kubectl exec -it testload -- bash
root@node2:/# cat /etc/hosts
使用ingress暴漏应用–Lab9-1 创建需要暴露的应用
candidate@client:~$ kubectl delete all --all
candidate@client:~$ kubectl get pod -n ingress-nginx
candidate@client:~$ kubectl create namespace ingress-test
candidate@client:~$ kubectl create deployment httpd --image=httpd:2.4 --replicas=3 -n ingress-test
candidate@client:~$ kubectl expose deployment httpd --port=80 -n ingress-test
candidate@client:~$ kubectl create deployment nginx --image=nginx:1.27 --replicas=3 -n ingress-test
candidate@client:~$ kubectl expose deployment nginx --port=80 -n ingress-test
candidate@client:~$ kubectl get pods,svc -n ingress-test
创建ingress:使用命令方式建立
candidate@client:~$ kubectl get ingressclasses
备注:如果安装多个ingree-controller,那么就会有多个ingressclasses
candidate@client:~$ kubectl create ingress nginx-ingress
–rule=‘www.nginx.com/*=nginx:80’ –class=nginx -n ingress-test 规则:当流量到ingress-controller,请求包含的前缀是www.nginx.com,那么转发到services的80端口
candidate@client:~$ kubectl create ingress apache-ingress --rule='www.apache.com/*=httpd:80' --class=nginx -n ingress-test
candidate@client:~$ kubectl get ingress -n ingress-test
备注:出现IP即可用,ingress-controller监听这个IP地址 测试
candidate@client:~$ sudo vim /etc/hosts
candidate@client:~$ curl www.nginx.com
candidate@client:~$ curl www.apache.com
配置TLS加密的ingress–Lab9-2
candidate@client:~$ kubectl create deployment --image=nginx:1.27 --replicas=3 --port=80 webserver
candidate@client:~$ kubectl expose deployment webserver --port=80
candidate@client:~$ mkdir cert && cd cert
candidate@client:~/cert$ openssl rand -writerand ~/.rnd // ubuntu产生1个随机数文件
candidate@client:~/cert$ openssl genrsa -out server.key 2048 // 生成私钥文件
candidate@client:~/cert$ ls
candidate@client:~/cert$ openssl req -new -x509 -key server.key -out server.crt -subj /C=CN/ST=LN/L=DL/O=dev/CN=www.shida-edu.cn // 生成证书请求文件
candidate@client:~/cert$ ls
candidate@client:~/cert$ kubectl create secret tls web-tls --cert=server.crt --key=server.key
// 创建secret,k8对于机密文件放到secret中寄存
candidate@client:~/cert$ kubectl create ingress shida-ingress --rule='www.shida-edu.cn/*=webserver:80,tls=web-tls' --class=nginx
candidate@client:~$ kubectl get ingress
candidate@client:~/cert$ sudo vim /etc/hosts
candidate@client:~$ curl -k -vvv https://www.shida-edu.cn
NetworkPolicy
candidate@client:~$ kubectl delete all --all
candidate@client:~$ kubectl create namespace backend
candidate@client:~$ kubectl create namespace frontend
candidate@client:~$ kubectl run --image nginx:1.27 -n backend backend
candidate@client:~$ kubectl run --image nginx:1.27 -n frontend frontend
candidate@client:~$ kubectl expose pod/backend -n backend --port 80
candidate@client:~$ kubectl expose pod/frontend -n frontend --port 80
candidate@client:~$ kubectl run --image busybox:1.28 --attach --rm -it testpod -- sh
备注:默认名称空间中的pod直接访问backend名称空间中的pod,跨名称空间访问!如何控制? 需求:backend名称空间中的pod只能被frontend名称空间中的pod访问!使用networkpolicy
candidate@client:~$ kubectl get namespaces frontend --show-labels
candidate@client:~$ vim np1.yaml // 登录k8s.io,搜索networkpolicy
candidate@client:~$ kubectl apply -f np1.yaml
candidate@client:~$ kubectl get networkpolicies.networking.k8s.io -n backend
candidate@client:~$ kubectl describe networkpolicies.networking.k8s.io -n backend
candidate@client:~$ kubectl run --image busybox:1.28 --attach --rm -it testpod -- sh
备注:实现默认名称空间中的pod无法访问backend名称空间中的pod
candidate@client:~$ kubectl exec -it -n frontend frontend -- bash
root@frontend:/# curl -s http://backend.backend
备注:前端名称空间的pod可以访问后端名称空间的pod
备注 namespaceSelector和podSelector逻辑或,podSelector和它下面namespaceSelector是逻辑与 管理kubernetes用户角色和上下文 客户端安装kubectl(略) 客户端生成两个新用户kube-user1,kube-user2私钥和证书请求文件
candidate@client:~$ mkdir ~/.kube && cd ~/.kube/
candidate@client:~/.kube$ openssl genrsa -out kube-user1.key 2048 // 生成用户私钥
candidate@client:~/.kube$ openssl genrsa -out kube-user2.key 2048 // 生成用户私钥
candidate@client:~/.kube$ openssl rand -writerand ~/.rnd
// 生成随机数文件,rhel或centos操作系统则不需要这个随机数,ubuntu系统需要
candidate@client:~/.kube$ openssl req -new -key kube-user1.key -out kube-user1.csr -subj "/CN=kube-user1/O=kubeusers" // 使用用户私钥生成用户证书请求文件
candidate@client:~/.kube$ openssl req -new -key kube-user2.key -out kube-user2.csr -subj "/CN=kube-user2/O=system:masters/O=system:authenticated"
客户端将用户证书请求文件通过e-mail或U盘等发送给master节点的管理员,私钥自己保存
candidate@client:~/.kube$ scp kube-user*.csr master:/tmp
master节点管理员使用apiserver根证书和根证书私钥为用户请求文件签发证书
root@master:~#mv /tmp/kube-user*.csr /etc/kubernetes/pki/ && cd /etc/kubernetes/pki/
root@master:/etc/kubernetes/pki# openssl x509 -req -in kube-user1.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out kube-user1.crt -days 365 // 管理员为用户签发证书
root@master:/etc/kubernetes/pki# openssl x509 -req -in kube-user2.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out kube-user2.crt -days 365
root@master:/etc/kubernetes/pki# openssl x509 -in kube-user1.crt -text -noout // 证书效验
root@master:/etc/kubernetes/pki# openssl x509 -in kube-user2.crt -text -noout
管理员将签发完成的证书和apiserver根证书发送给客户端
root@master:/etc/kubernetes/pki# scp kube-user*.crt candidate@client:~/.kube
root@master:/etc/kubernetes/pki# scp ca.crt candidate@client:~/.kube
客户端设置集群访问入口
candidate@client:~$ kubectl config set-cluster mycluster1 --embed-certs=true --certificate-authority=/home/candidate/.kube/ca.crt --server="https://192.168.126.100:6443"
// mycluster1访问的集群名称(即kube-apiserver名字),embed-certs选项对被访问的集群进行身份验证,使用api-server的根证书验证与对方的根证书是否一致!
candidate@client:~$ kubectl config get-clusters // 显示两套集群(我们实际只有一套)
客户端设置用户访问集群的credentials(凭证)文件,生成的内容自动存放在~/.kube/config文件中
candidate@client:~$ kubectl config set-credentials kube-user1 --embed-certs=true --client-certificate=/home/candidate/.kube/kube-user1.crt --client-key=/home/candidate/.kube/kube-user1.key
candidate@client:~$ kubectl config set-credentials kube-user2 --embed-certs=true --client-certificate=/home/candidate/.kube/kube-user2.crt --client-key=/home/candidate/.kube/kube-user2.key
candidate@client:~$ kubectl config get-users
candidate@client:~$ cat .kube/config
客户端设置用户访问集群的Context(即快捷方式)
candidate@client:~$ kubectl config set-context kube-user1@mycluster1 --cluster=mycluster1 --user=kube-user1 // kube-user1@mycluster1是context名字,随意
candidate@client:~$ kubectl config set-context kube-user2@mycluster1 --cluster=mycluster1 --user=kube-user2
candidate@client:~$ kubectl config get-contexts
客户端切换上下文kube-user1@mycluster1测试
candidate@client:~$ kubectl config use-context kube-user1@mycluster1
candidate@client:~$ kubectl config get-contexts
candidate@client:~$ kubectl get nodes
// 可以访问集群,不能使用集群资源;原因:用户生成证书请求文件的时候,未指定所属权限! 客户端切换上下文kube-user2@mycluster1测试
candidate@client:~$ kubectl config use-context kube-user2@mycluster1
candidate@client:~$ kubectl get nodes
// 获取信息是因为管理员在签发证书的时候,最后一步效验不仔细!
// 用户证书请求文件说自己属于masters组,所以用户继承了master组权限! 为kube-user1用户在dev名称空间绑定系统预置的admin角色
candidate@client:~$ kubectl create namespace dev
candidate@client:~$ kubectl create rolebinding user1-dev-admin --clusterrole=admin --user=kube-user1 -n dev
candidate@client:~$ kubectl config use-context kube-user1@mycluster1
candidate@client:~$ kubectl auth whoami
candidate@client:~$ kubectl run --image nginx:1.27 web -n dev
candidate@client:~$ kubectl get pods -n dev
candidate@client:~$ kubectl delete pods -n dev web
candidate@client:~$ kubectl config use-context kubernetes-admin@kubernetes
candidate@client:~$ kubectl delete rolebindings.rbac.authorization.k8s.io -n dev user1-dev-admin // 删除角色绑定
清除实验资源使用命令如下(暂时别删除,做完下面实验再删除)
K8的RBAC Role和ClusterRole
candidate@client:~$ kubectl config use-context kubernetes-admin@kubernetes
candidate@client:~$ kubectl api-resources -o wide // 查看资源可以执行的操作
candidate@client:~$ kubectl create role shidarole --resource pod,deployments --verb create,list
// 默认名称空间下创建名为shidarole的角色,使用的资源pod,deloyment;动作是创建和列表
candidate@client:~$ kubectl get role
candidate@client:~$ kubectl describe role shidarole
candidate@client:~$ kubectl edit role shidarole
candidate@client:~$ kubectl create clusterrole shida-admin-pod --resource pods --verb create,delete,list
candidate@client:~$ kubectl get clusterrole | grep shida
candidate@client:~$ kubectl create rolebinding user1-default --role=shidarole --user=kube-user1 // 默认名称空间下的角色shidarole绑定给kube-user1用户
candidate@client:~$ kubectl config use-context kube-user1@mycluster1
candidate@client:~$ kubectl run --image nginx:1.27 web1
candidate@client:~$ kubectl get pods
candidate@client:~$ kubectl delete pods web1
candidate@client:~$ kubectl config use-context kubernetes-admin@kubernetes
candidate@client:~$ kubectl delete rolebindings.rbac.authorization.k8s.io user1-default
candidate@client:~$ kubectl create clusterrolebinding user1-clustrole --clusterrole=shida-admin-pod --user=kube-user1 // 集群角色shida-admin-role绑定给kube-user1用户
candidate@client:~$kubectl config use-context kube-user1@mycluster1
candidate@client:~$kubectl run --image nginx:1.27 w1
candidate@client:~$kubectl run --image nginx:1.27 w2 -n dev
candidate@client:~$ kubectl delete pods w1
candidate@client:~$ kubectl delete pods web1
candidate@client:~$ kubectl delete pods -n dev w2
candidate@client:~$ kubectl config use-context kubernetes-admin@kubernetes
candidate@client:~$ kubectl delete clusterrolebindings.rbac.authorization.k8s.io user1-clustrole-default
candidate@client:~$ kubectl create rolebinding cr1-user1 --clusterrole=shida-admin-pod --user=kube-user1 -n dev
candidate@client:~$ kubectl config use-context kube-user1@mycluster1
candidate@client:~$ kubectl get pods -n dev
备注:使用rolebinding绑定集群角色时,必须指定名称空间;不指定就是默认名称空间,不会在所有名称空间都能使用! 为服务账户赋予角色
candidate@client:~$ kubectl config use-context kubernetes-admin@kubernetes
candidate@client:~$ kubectl create sa devuser // 创建服务账户
candidate@client:~$ kubectl create rolebinding devuser-binding --serviceaccount “default:devuser” --role shidarole -n default // default是默认空间,devuser是创建的服务账户
使用如下 K8s会自动将当前命名空间ServiceAccount的Token、ca 证书挂载到Pod内 /var/run/secrets/kubernetes.io/serviceaccount/
kubectl exec -it dev-pod -- sh
# 容器内直接访问kube-apiserver,会自动使用挂载的SA凭证
kubectl get pods
candidate@client:~$ kubectl delete rolebing devuser