1、不安全访问
设置API server接收所有主机的请求:
$ kubectl proxy --address='0.0.0.0' --accept-hosts='^*$'
Starting to serve on [::]:8001 &
# 创建tomcat
curl -X POST -H 'Content-Type: application/yaml' --data '
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat
labels:
name: tomcat-deploy
spec:
selector:
matchLabels:
name: tomcat-pod
replicas: 1
template:
metadata:
name: tomcat-pod
labels:
name: tomcat-pod
spec:
containers:
- name: tomcat-containers
image: 10.100.21.3:8085/docker-local/k8s/k8s_demo/k8s_demo:v0.1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: tomcat-frontend
spec:
selector:
name: tomcat-pod
type: NodePort
ports:
- name: http
nodePort: 32767
port: 8080
protocol: TCP
targetPort: 8080' http://10.100.21.6:8001/apis/apps/v1/namespaces/default/deployments
# 删除tomcat
curl -X DELETE -H 'Content-Type: application/yaml' --data ' gracePeriodSeconds: 0
orphanDependents: false ' http://10.100.21.6:8001/apis/apps/v1/namespaces/default/deployments/tomcat
2、认证访问
TOKEN='eyJhbGciOiJSUzI1NiIsImtpZCI6IkhZSDc3SmRrcmJUcnpYUkhGZEVVOTJCNzNEanFFQnJBb18wM05mMS12WDQifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrOHMtYWRtaW4tdG9rZW4tcGNucGgiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiazhzLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNzk0ZjQ3ODktZGRiYi00N2UyLWEzOWItMWU1YzJjNjE3NmY1Iiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOms4cy1hZG1pbiJ9.KWRvd3VZEq3C1P6M92usNHCtVHt0PMfERUYWW5CLMVTow33GLO5qg13G5pAdNcHnXQbDWzrCK6zI0LrHQPPUc9ePs3olkLzQx9SKTl3rQXHL-vTdkxy9wZMsYFADOnix8Rv8IDzKnGroCdd-kvoZ58P8dxWeVVXcgCRVRvIW_w0CB6OqQ_Aic6e7-VuuMPcRUcJ9CQblN339YaeN1B1JvPr-ZyOw65IW_amKQns8zHNuysTONS6iBAtfR40YG-XCh939pevc9GDZwXh0VthD63SVeLHtZufzr0e8YxfXnTVn0eTSmS4EgNaVZaHjSfGz-Q5FtylY0ElHrOJZC6c_Jg'
# 创建tomcat
curl -k --header "Authorization: Bearer $TOKEN" -H 'Content-Type: application/yaml' -s -w "状态码是:%{http_code}\n" -d "$(cat tomcat.yaml)" -X POST https://127.0.0.1:6443/apis/apps/v1/namespaces/default/deployments
# 删除 tomcat
curl -X DELETE -k --header "Authorization: Bearer $TOKEN" -H 'Content-Type: application/yaml' --data '
gracePeriodSeconds: 0
orphanDependents: false
' 'https://10.100.21.6:6443/apis/apps/v1/namespaces/default/deployments/tomcat'
官方文档:
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#create-deployment-v1-apps