k8s踩坑记录——证书一年有效期

栏目: 服务器 · 发布时间: 5年前

内容简介:依照https://github.com/strongit/kubeadm-ha/ 安装步骤,kubeadm init安装后的集群存在证书过期问题。现修复如下:思路如下,1、保留ca.crt ca.key front-proxy-ca.crt front-proxy-ca.key,根证书有效期十年

kubernetes集群证书重签

依照https://github.com/strongit/kubeadm-ha/ 安装步骤,kubeadm init安装后的集群存在证书过期问题。现修复如下:

思路如下,

1、保留ca.crt ca.key front-proxy-ca.crt front-proxy-ca.key,根证书有效期十年

2、openssl重新签注

3、kubeadm alpha phase 生成config

[root@k8s-master01 pki]# cat csr.conf
[ req ]
default_bits = 2048
prompt = no
default_md = sha256
req_extensions = req_ext
distinguished_name = dn

[ dn ]
C = CN
ST = BeiJing
L = BeiJing
O = k8s
OU = System
CN = kubernetes

[ req_ext ]
subjectAltName = @alt_names

[ alt_names ]
DNS.1 = kubernetes
DNS.2 = kubernetes.default
DNS.3 = kubernetes.default.svc
DNS.4 = kubernetes.default.svc.cluster
DNS.5 = kubernetes.default.svc.cluster.local
DNS.6 = k8s-master01
DNS.7 = k8s-master02
DNS.8 = k8s-master03
IP.1 = IP
IP.2 = IP

[ v3_ext ]
authorityKeyIdentifier=keyid,issuer:always
basicConstraints=CA:FALSE
keyUsage=digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment
extendedKeyUsage=serverAuth,clientAuth
subjectAltName=@alt_names

 openssl genrsa -out apiserver.key 2048 
 openssl req -new -key apiserver.key -out apiserver.csr -config csr.conf
 openssl x509 -req -in apiserver.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out apiserver.crt -days 10000 -extensions v3_ext -extfile csr.conf
 openssl x509  -noout -text -in ./apiserver.crt  |grep "Not"

 openssl genrsa -out apiserver-kubelet-client.key 2048
 openssl req -new -key apiserver-kubelet-client.key -out apiserver-kubelet-client.csr -config csr.conf
 openssl x509 -req -in apiserver-kubelet-client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out apiserver-kubelet-client.crt -days 10000 -extensions v3_ext -extfile csr.conf
 openssl x509  -noout -text -in ./apiserver-kubelet-client.crt  |grep "Not"

 openssl genrsa -out front-proxy-client.key 2048
 openssl req -new -key front-proxy-client.key -out front-proxy-client.csr -config csr.conf
 openssl x509 -req -in front-proxy-client.csr -CA front-proxy-ca.crt -CAkey front-proxy-ca.key -CAcreateserial -out front-proxy-client.crt -days 10000 -extensions v3_ext -extfile csr.conf
 openssl x509  -noout -text -in ./front-proxy-client.crt  |grep "Not"
kubeadm alpha phase certs all --config kubeadm-config.yaml

kubeadm alpha phase kubelet config write-to-disk --config kubeadm-config.yaml
kubeadm alpha phase kubelet write-env-file --config kubeadm-config.yaml
kubeadm alpha phase kubeconfig kubelet --config kubeadm-config.yaml

kubeadm alpha phase kubeconfig all --config kubeadm-config.yaml

kubeadm alpha phase controlplane all --config kubeadm-config.yaml

systemctl restart kubelet
kubeadm alpha phase mark-master --config kubeadm-config.yaml
cp /etc/kubernetes/admin.conf ~/.kube/config

重启集群后,执行kubelet logs pods XXXX -n kube-system报错如下:Error from server (Forbidden): Forbidden (user=kubernetes, verb=get, resource=nodes, subresource=proxy) ( pods/log kube-scheduler-k8s-master01)

解决方案:kubectl create clusterrolebinding system:kubernetes --clusterrole=cluster-admin --user=system:kubernetes

ingress的tls设置

对ingress中的域名进行tls安全证书的设置步骤如下:

创建自签名的秘钥与ssl证书;

将证书保存到kubernetes集群的1个Secret资源对象上;

设置Secret资源对象到ingress中。

根据网站域名是1个还是多个,前两步的操作稍有不同,第3步操作相同,下面以多域名的操作为例:

  1. 生成ca证书

    [root@kubenode1 ~]# mkdir -p /etc/kubernetes/ingress

    [root@kubenode1 ~]# cd /etc/kubernetes/ingress/

    [root@kubenode1 ingress]# openssl genrsa -out ca.key 2048

    [root@kubenode1 ingress]# openssl req -x509 -new -nodes -key ca.key -days 3560 -out ca.crt -subj "/CN=ingress-ca"

  2. 修改openssl.cnf文件
    #对于多域名,生成ssl证书需要使用额外的x509v3配置文件辅助;
    #在[alt_names]字段中设置多域名
    [root@kubenode1 ingress]# cp /etc/pki/tls/openssl.cnf .
    [root@kubenode1 ingress]# vim openssl.cnf

[ req ]

#第126行,取消注释

req_extensions = v3_req # The extensions to add to a certificate request

[ v3_req ]

#Extensions to add to a certificate request

basicConstraints = CA:FALSE

keyUsage = nonRepudiation, digitalSignature, keyEncipherment

#第224行之后,新增部分

subjectAltName = @alt_names

[alt_names]

DNS.1 = nginx01-svc-tls.me

DNS.2 = nginx02-svc-tls.me

  1. 生成ingress ssl证书

#基于修改的openssl.cnf与ca证书生成ingress ssl证书

#生成秘钥

[root@kubenode1 ingress]# openssl genrsa -out ingress.key 2048

#生成csr文件

[root@kubenode1 ingress]# openssl req -new -key ingress.key -out ingress.csr -subj "/CN=nginx-svc-tls" -config openssl.cnf

#生成证书

[root@kubenode1 ingress]# openssl x509 -req -in ingress.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out ingress.crt -days 3650 -extensions v3_req -extfile openssl.cnf

  1. 生成Secret资源对象

    Secret对象的主要作用是保管私密数据,如:密码,OAuth Tokens,ssh Keys等信息。将私密信息存放在Secret对象中,比直接放在Pod或者Docker image中更安全,更便于使用与分发。

Secret对象创建完成之后,可通过3种方式调用:

在创建Pod时,通过为Pod指定Service Account来自动使用;

通过挂载Secret到Pod来使用;

Docker image下载时使用,通过指定Pod的spc.ImagePullSecrets来引用。

#编辑secret-ingress.yaml文件,将ingress.key与ingress.crt的内容复制到yaml文件中;

#注意1:Secret的”data”域的各子域的值必须为BASE64编码;

#注意2:复制key与crt的内容时去掉换行符,变成一行

[root@kubenode1 ingress]# cd /usr/local/src/yaml/ingress/

[root@kubenode1 ingress]# touch secret-ingress.yaml

[root@kubenode1 ingress]# vim secret-ingress.yaml

apiVersion: v1

kind: Secret

metadata:

name: secret-ingress

#1.8.x之后使用kubernetes.io/tls替换Opaque

type: kubernetes.io/tls

data:

tls.crt: MIIC/TCCAeWgAwIBAgIJALGRacBg2fWIMA0GCSqGSIb3DQEBCwUAMBUx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4QW0s8DTKzywsRcoH2oHMShu0Pa2gnfJXQKBgQClzLn9t4GNk0EKY23JAo8piTUkbqp76Fyam5k5g+lvsBLMNB4nJyIADd07bFRyEcvbj8HDeolepEiN8HS1ou+wERQrfVTEURq7S/f5aQhysNvBp/vvlkGv4YrNDLCm3Xgsy8etm6lkQ9yXLAnQj90FFUTazhaI8DQuT/Hx9uU+qQKBgQCBEpc98YikgYmZk/6kyzUP3l+MIj5i3UK/7ZG3QOpTAeTbzBQQX0s31b2Lf9M+SN2+2XJb/0OUr3RKKkuf5KgedMll7hNaEaFu9z5qPepFUlKWZz2MkIRSljecbSJ8ZfGz2wCUhQoW8KLQY9ftEaz+27eEJ0FxHhuhe5+yQMpkKA==

#生成Secret资源对象

[root@kubenode1 ingress]# kubectl create -f secret-ingress.yaml

#以上编辑yaml文件,使用”kubectl create”命令生成Secret对象在步骤上更清晰;

#但可以利用”kubectl create secret tls”命令直接创建Secret对象

[root@kubenode1 ingress]# kubectl create secret tls secret-ingress --key /etc/kubernetes/ingress/ingress.key --cert /etc/kubernetes/ingress/ingress.crt

  1. 创建后端服务

    #编辑后端服务nginx01-svc-tls.yaml

    [root@kubenode1 ingress]# touch nginx01-svc-tls.yaml

    [root@kubenode1 ingress]# vim nginx01-svc-tls.yaml

    apiVersion: extensions/v1beta1

    kind: Deployment

    metadata:

    name: nginx01-tls

    spec:

    replicas: 1

    template:

    metadata:

    labels:

    name: nginx01-tls

    spec:

    containers:

    • name: nginx01-tls

      image: nginx:latest

      ports:

      • containerPort: 80

        apiVersion: v1

        kind: Service

        metadata:

        name: nginx01-svc-tls

        spec:

        ports:

        #Service服务监听的端口号

      • port: 443
        #后端提供真实服务的Pod提供的端口号
        targetPort: 80
        name: https
        selector:
        name: nginx01-tls

#编辑后端服务nginx02-svc-tls.yaml

[root@kubenode1 ingress]# cp nginx01-svc-tls.yaml nginx02-svc-tls.yaml

[root@kubenode1 ingress]# sed -i 's|nginx01|nginx02|g' nginx02-svc-tls.yaml

#生成后端服务

[root@kubenode1 ingress]# kubectl create -f nginx01-svc-tls.yaml

[root@kubenode1 ingress]# kubectl create -f nginx02-svc-tls.yaml

#修改提供后端服务的nginx容器的html文件;

#通过”kubectl exec -ti <pod-name> -c <container-name> /bin/bash”进入容器修改;pod-name可通过命令”kubectl get pods -o wide”获取;container-name即yaml文件中定义的名字

[root@kubenode1 ingress]# kubectl get pods -o wide

#nginx官方容器的index.html文件在/usr/share/nginx/html/目录下

[root@kubenode1 ingress]# kubectl exec -ti nginx01-tls-59fbf6696c-qfq4k -c nginx01-tls /bin/bash

root@nginx01-tls-59fbf6696c-qfq4k:/# echo "<h1>Welcome to test site nginx01-svc-tls</h1>" > /usr/share/nginx/html/index.html

root@nginx01-tls-59fbf6696c-qfq4k:/# cat /usr/share/nginx/html/index.html

root@nginx01-tls-59fbf6696c-qfq4k:/# exit

[root@kubenode1 ingress]# kubectl exec -ti nginx02-tls-5559fd9bc7-dfbrp -c nginx02-tls /bin/bash

root@nginx02-tls-5559fd9bc7-dfbrp:/# echo "<h1>Welcome to test site nginx02-svc-tls</h1>" > /usr/share/nginx/html/index.html

root@nginx02-tls-5559fd9bc7-dfbrp:/# cat /usr/share/nginx/html/index.html

root@nginx02-tls-5559fd9bc7-dfbrp:/# exit

  1. 创建ingress对象

    复制代码

    #编辑ingress对象yaml文件;

    #在”spec”域下新增“tls”子域,”hosts”字段加入多域名,“secretName”字段调用对应的Secret资源;

    #1个ingress对象只能使用1个Secret对象(“secretName”字段value唯一),即只能使用1个证书,该正式需要支持”hosts”字段下所有域名;

    #“secretName”字段一定要置于域名列表最后的位置;

    #”hosts”字段的域名需要匹配”rules”字段域名;

    #ingress默认情况下,当不配置证书或者证书配置错误时,会默认给出一个tls证书;如“secretName”字段配置了2个值,则所有域名采用默认证书;如”hosts”字段少配置一个域名,缺失的域名会采用默认证书;

    #更新ingress证书可能需要等待一段时间才能生效

    [root@kubenode1 ingress]# touch nginx-svc-tls-ingress.yaml

    [root@kubenode1 ingress]# vim nginx-svc-tls-ingress.yaml

    apiVersion: extensions/v1beta1

    kind: Ingress

    metadata:

    name: ingress-tls

    spec:

    tls:

    • hosts:
    • nginx01-svc-tls.me
    • nginx02-svc-tls.me
      secretName: secret-ingress
      rules:
    • host: nginx01-svc-tls.me
      http:
      paths:
      • backend:

        serviceName: nginx01-svc-tls

        #后端服务监听端口,区别于提供真实服务的容器监听端口

        servicePort: 443

    • host: nginx02-svc-tls.me
      http:
      paths:
      • backend:

        serviceName: nginx02-svc-tls

        servicePort: 443

#生成ingress对象

[root@kubenode1 ingress]# kubectl create -f nginx-svc-tls-ingress.yaml

[root@kubenode1 ingress]# kubectl get ingress

  1. 验证

    #采用--resolve参数模拟dns解析,目标地址为域名;

    #http访问时被重定向,采用https访问正常

    [root@kubenode1 ingress]# curl --resolve nginx01-svc-tls.me:80:172.30.200.21 http://nginx01-svc-tls.me

    [root@kubenode1 ingress]# curl --resolve nginx01-svc-tls.me:443:172.30.200.21 -k https://nginx01-svc-tls.me

#或者采用-H参数设置http头中需要访问的域名,目标地址为ip地址

[root@kubenode1 ingress]# curl -H 'Host:nginx01-svc-tls.me' -k https://172.30.200.23

[root@kubenode1 ingress]# curl -H 'Host:nginx02-svc-tls.me' -k https://172.30.200.23

在本地浏览器访问host主机(注意提前绑定域名): http://nginx01-svc-tls.me

采用http访问,重定向自动跳转为https访问


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Text Processing in Python

Text Processing in Python

David Mertz / Addison-Wesley Professional / 2003-6-12 / USD 54.99

Text Processing in Python describes techniques for manipulation of text using the Python programming language. At the broadest level, text processing is simply taking textual information and doing som......一起来看看 《Text Processing in Python》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

SHA 加密
SHA 加密

SHA 加密工具