最近在整一整套devops自动化持续集成的东西,一开始就做好了踩坑的准备。
failed to verify certificate: x509: certificate signed by unknown authority
今天在执行kubectl get nodes的时候报的证书验证问题,看了一圈首次搭建k8s的都是高频出现的问题。
couldn’t get current server API group list: Get “https://kubernetes.docker.internal:6443/api?timeout=32s”: tls: failed to verify certificate: x509: certificate signed by unknown authority (possibly because of “crypto/rsa: verification error” while trying to verify candidate authority certificate “kubernetes”)
加上 --insecure-skip-tls-verify
也确实会提示 error: You must be logged in to the server (the server has asked for the client to provide credentials)
这个其实就是.kube目录下的config文件有问题,里面有集群的certificate-authority-data和客户端的证书数据client-certificate-data,退出服务,删掉config文件再重启
hosts文件添加127.0.0.1 kubernetes.docker.internal
到此k8s环境算是连通
这是我之前搞的一个maven项目,在本地打包后手工推到阿里云ARC的镜像
k8s部署maven项目需要编写deployment.yaml和service.yaml两个文件
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: olympus-backend-deployment
spec:
replicas: 1
selector:
matchLabels:
app: olympus-backend
template:
metadata:
labels:
app: olympus-backend
spec:
containers:
- name: olympus-backend
image: registry.cn-hangzhou.aliyuncs.com/xxx/olympus-backend:20240425
ports:
- containerPort: 8080
service.yaml
apiVersion: v1
kind: Service
metadata:
name: olympus-backend-service
spec:
selector:
app: olympus-backend
ports:
- protocol: TCP
port: 8080
targetPort: 8080
nodePort: 30001
type: NodePort
应用部署到k8s
查看部署状态
我测试deployment.yaml用的镜像是从阿里云ACR拉的,确保可以docker pull拉取成功,测试可以先手工拉下来。
如果是windows本地部署的,用不了LoadBalancer负载均衡的模式,可以改NodePort
查看服务对外ip和端口
kubectl get svc -o wide
查看部署节点
接口访问
测试项目里我暴露了一个接口,每次返回会返回当前的时间。上面是通过NodePort指定了30001对外端口
@GetMapping("/")
public String home() {
return LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
不断刷新浏览器可以看到毫秒单位的时间一直在更新