DEV Community

Natália Granato
Natália Granato

Posted on

1

Certificado TLS no Kubernetes

Utilize o openssl, projeto de código aberto dos protocolos ssl e tls para gerar o seu certificado autoassinado. Use openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout chave-privada.key -out certificado.crt.

Certificado TLS no Kubernetes

Para criar um Secret do tipo TLS, você pode usar o comando kubectl create secret tls meu-servico-web-tls-secret --cert=certificado.crt --key=chave-privada.key seguido sempre pelo nome da Secret e a localização dos arquivos de certificado e chave.

Verificando os dados de uma Secret, você pode usar o comando kubectl get secret meu-servico-tls-secret -o yaml para visualizar os dados de uma Secret.

Aqui está um exemplo do arquivo de configuração do nginx para utilizar tls:

http {
    server {
        listen 80;
        listen 443 ssl;
        ssl_certificate /etc/nginx/tls/certificado.crt;
        ssl_certificate_key /etc/nginx/tls/chave-privada.key;

        location / {
            return 200 'Hello, World!';
            add_header Content-Type text/plain;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Criando um ConfigMap para adicionar um arquivo no Pod e configurar o SSL no Nginx

Para criar um ConfigMap para adicionar um arquivo no Pod e configurar o SSL no Nginx, você pode usar o comando kubectl create configmap nginx-config --from-file=nginx.conf seguido pelo nome do ConfigMap e o arquivo a ser incluído.

apiVersion: v1
kind: Pod
metadata:
  name: gita-pod
  labels:
    app: gita-pod
spec:
  containers:
  - name: gita-container
    image: nginx:1.26
    ports:
    - containerPort: 80
    - containerPort: 443
    volumeMounts:
    - name: meu-volume
      mountPath: /etc/nginx/nginx.conf
      subPath: nginx.conf
    - name: nginx-tls
      mountPath: /etc/nginx/tls
  volumes:
  - name: nginx-config-volume
    configMap:
      name: nginx-config
  - name: nginx-tls
    secret:
      secretName: meu-servico-tls-secret
      items:
      - key: certificado.crt
        path: certificado.crt
      - key: chave-privada.key
        path: chave-privada.key
Enter fullscreen mode Exit fullscreen mode

Recapirulando, aprendemos o que são as Secrets do Kubernetes, os tipos de Secrets e o que é a codificação base64, como criar uma Secret do tipo Opaque, como utilizar o nosso Secret como variável de ambiente dentro do Pod, como criar uma Secret para autenticação no Docker Hub, como criar um Secret do tipo TLS e como criar um ConfigMap para adicionar um arquivo no Pod e configurar o SSL no Nginx.

Para realizar o passo a passo:

  1. Crie o seu certificado TLS: openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout chave-privada.key -out certificado.crt.

  2. Crie a secret do tipo TLS com o comando kubectl create secret tls meu-servico-tls-secret --cert=certificado.crt --key=chave-privada.key.

  3. Crie o ConfigMap com o comando kubectl create configmap nginx-config --from-file=nginx.conf.

  4. Crie o Pod com o comando kubectl apply -f nginx-pod.yml.

  5. Crie um service para expor o Pod criado com o comando kubectl expose pod nginx. Liste o service com kubectl get svc.

  6. Faça o port-forward do service para acessar o Nginx com o comando kubectl port-forward service/nginx 4443:443.

Para testar o Nginx, acesse o endereço https://localhost:4443 ou https://127.0.0.1:4443 e você verá a mensagem Bem-vindo ao Nginx!.

Você também pode utilizar o curl -k https://localhost:4443 e verá a seguinte mensagem:

Bem-vindo ao Nginx!

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay