k run frontend --image=nginx
k expose pod frontend --port 80
k -n kubernets-dashboard create rolebinding insecure --serviceaccount kubernetes-dashboard:kubernetes-dashboard --clusterrole view
k run pod1 --image=nginx
k run pod2 --image=httpd
k expose pod pod1 --port 80 --name service1
k expose pod pod2 --port 80 --name service2
curl https://192.168.211.40:32300/service1 -kv
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
k create secret tls secure-ingress --cert=cert.pem --key=key.pem
curl https://secure-ingress.com:32300/service2 -kv --resolv secure-ingress.com:32300:192.168.211.41
k label pod nginx role=metadata-accessor
#根据CIS标准检查
docker run --pid=host -v /etc:/etc:ro -v /var:/var:ro -t aquasec/kube-bench:latest master --version 1.20#确认版本是否一至
sha512sum kubernetes-server-linux-arm64.tar.gz
tar zxf kubernetes-server-linux-arm64.tar.gz
ls kubernetes/server/bin/kube-apiserver
sha512sum kubernetes/server/bin/kube-apiserver
docker ps|grep apiserver
docker cp 0fb5321dfd57:/ container-fs
ls container-fs/
find container-fs/ |grep kube-apiserver
sha512sum container-fs/usr/local/bin/kube-apiserver
2. 集群强化:15%
curl https://localhost:6443
curl https://localhost:6443 -k
vim /etc/kubernetes/manifests/kube-apiserver.yaml
- --anonymous-auth=true
- --insecure-port=8080
- --enable-admission-plugins=NodeRestriction
curl https://192.168.211.40:6443 --cacert ca --cert ca.crt --key ca.key
k edit svc
curl https://192.168.211.40:30300 -l
k config view --raw >config
k --kubeconfig config get ns
k label node master cks/test=yes
k create sa accessor
k get sa,secrets
k describe secret accessor-token-bnd4s
k run accessor --image=nginx --dry-run=client -oyaml
serviceAccountName: accessor #添加此行
k exec -ti accessor -- bashmount|grep sec
cd /run/secrets/kubernetes.io/serviceaccount
cat token
curl https://kubernetes
curl https://kubernetes -k
curl https://kubernetes -k -H "Authorization: Bearer eyJ。。。。。
automountServiceAccountToken: false#添加此行
k -f accessor.yaml replace --force
k auth can-i delete secrets --as system:serviceaccount:default:accessor
k create clusterrolebinding accessor --clusterrole edit --serviceaccount default:accessor
k auth can-i delete secrets --as system:serviceaccount:default:accessor
k create ns red
k create ns blue
k -n red create role secret-manager --verb=get --resource=secrets -oyaml --dry-run=client
k -n red create rolebinding secret-manager --role=secret-manager --user=jane
k -n blue create role secret-manager --verb=get --verb=list --resource=secrets
k -n blue create rolebinding secret-manager --role=secret-manager --user=jane
k -n red auth can-i get secrets --as jane
openssl genrsa -out jane.key 2048cat jane.csr | base64 -w 0
k certificate approve jane
k config view -o yaml > view.yaml
k config set-credentials jane --client-key=jane.key --client-certificate=jane.crt
k config set-credentials jane --client-key=jane.key --client-certificate=jane.crt --embed-certs
k config view --raw
k config set-context jane --user=jane --cluster=kubernetes
k config get-contexts
k drain master --ignore-daemonsets
apt-cache show kubeadm |grep -e '1.20'apt-getinstallkubeadm=1.20.2-00 kubectl=1.20.2-00 kubelet=1.20.2-00
kubeadm upgrade plan
kubeadm upgrade apply v1.20.6
k uncordon master
k get node
3. 系统强化:15%
netstat -natlp
ps aux
lsof -i :22
apt-getinstall snapd
systemctl start snapd
systemctl status snapd
systemctl list-units --type=service --state=running |grep snap
apt-getinstall -y vsftpd samba
systemctl status vsftpd
systemctl status smbd
ps aux |grep smbd
whoami
k run pod --image=busybox --command -oyaml --dry-run=client > pod.yaml -- sh -c 'sleep 1d'
root@master:~/cks/securitytext# vim pod.yaml ....
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
containers:
- command:
- sh
- -c
- sleep 1d
image: busybox
name: pod
resources: {}
securityContext:
runAsNonRoot: true
privileged: true
allowPrivilegeEscalation: true.....
sysctl kernel.hostname=attacker
kubectl -f pod.yaml delete --force --grace-period=0
root@master:~/cks/securitytext# vim /etc/kubernetes/manifests/kube-apiserver.yaml
---
- --enable-admission-plugins=NodeRestriction,PodSecurityPolicy
---
k create role psp-access --verb=use --resource=podsecuritypolicies
k create rolebinding psp-access --role=psp-access --serviceaccount=default:default
aa-status
apt-getinstall apparmor-utils
aa-genprof curlcd /etc/apparmor.d/
aa-logprof
cat usr.bin.curl
curl killer.sh -v
k run secure --image=nginx -oyaml --dry-run=client > pod.yaml
root@master:~/cks/apparmor# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
annotations: #添加此行
container.apparmor.security.beta.kubernetes.io/secure: localhost/hello #添加此行
root@master:~/cks/apparmor# k get pods secure
NAME READY STATUS RESTARTS AGE
secure 0/1 Blocked 0 6s
cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
annotations:
container.apparmor.security.beta.kubernetes.io/secure: localhost/docker-nginx #修改此行
root@master:~/cks/apparmor# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: secure
name: secure
spec:
securityContext:
seccompProfile:
type: Localhost
localhostProfile: profiles/audit.json
root@master:~/cks/apparmor# k get pods -w
NAME READY STATUS RESTARTS AGE
accessor 1/1 Running 0 26h
secure 0/1 CreateContainerError 0 23s
root@master:~/cks/apparmor# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: secure
name: secure
spec:
securityContext:
seccompProfile:
type: Localhost
localhostProfile: default.json