openvpn

by serge.logvinov

Helm chartv0.8.1App 2.7.3Verified publisher

OpenVPN in kubernetes

About this chart

openvpn

OpenVPN in kubernetes

Homepage: https://github.com/sergelogvinov/helm-charts

Maintainers

NameEmailUrl
sergelogvinovhttps://github.com/sergelogvinov

Source Code

Deploy openvpn with users

Cert manager creates a tls secrets and store in kubernetes secretes. clientConfigRBAC sets RBAC policy, it allow to download openvpn config/secrets by users.

# helm values
certManager:
  createCerts: true

  clientConfigRBAC: true
  clients:
    - [email protected]
    - [email protected]

openvpn:
  hostName: vpn.example.com

  config: |
    server 172.30.1.0 255.255.255.0
    topology p2p
    data-ciphers AES-256-GCM:AES-128-GCM
    data-ciphers-fallback AES-256-CBC

  defaultroutes: |
    push "dhcp-option DNS 172.30.1.1"
    push "route 1.1.1.1"
    push "route 8.8.8.8"

Deploy openvpn with users and OTP

As previous example, we will create a tls certs and user configs. Additionally, we will use OTP (one time password) to login on openvpn.

To create OTP key: docker run --rm -ti --entrypoint=google-authenticator ghcr.io/sergelogvinov/openvpn:2.5.8-2 -tdfW -r 3 -R 60

# helm values
certManager:
  createCerts: true

  clientConfigRBAC: true
  clients:
    - [email protected]
    - [email protected]

openvpn:
  hostName: vpn.example.com
  # google-authenticator -tdfW -r 3 -R 60
  otp: PMTYASQTPLU6OKR2

  config: |
    server 172.30.1.0 255.255.255.0
    topology p2p
    data-ciphers AES-256-GCM:AES-128-GCM
    data-ciphers-fallback AES-256-CBC

  defaultroutes: |
    push "dhcp-option DNS 172.30.1.1"
    push "route 1.1.1.1"
    push "route 8.8.8.8"

Useful Makefile to download user certificates:

ifeq (vpn,$(firstword $(MAKECMDGOALS)))
  RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
  USER := $(subst .,-,$(subst @,-,$(lastword $(MAKECMDGOALS))))
  $(eval $(RUN_ARGS):;@:)
endif

.PHONY: vpn
vpn: vpn-$(USER) ## create certificate for vpn
vpn-clean:
	rm -f ca.crt ca.key client.crt client.key client.ovpn

vpn-%: ## create certificate for vpn
	kubectl --namespace vpn get secret openvpn-client-$(USER) -o jsonpath="{.data.tls\.crt}" | base64 --decode > client.crt
	kubectl --namespace vpn get secret openvpn-client-$(USER) -o jsonpath="{.data.tls\.key}" | base64 --decode > client.key
	kubectl --namespace vpn get configmap openvpn -o jsonpath="{.data.client\.conf}" > client.ovpn

Network policy

The chart enables an egress NetworkPolicy by default. It allows cluster DNS on TCP/UDP port 53 and IPv4 destinations outside 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16 (RFC1918). IPv6 egress is allowed. Ingress is unaffected. Cluster DNS is selected using k8s-app: kube-dns in the kube-system namespace; customize the DNS selectors if your cluster uses different labels.

For NodeLocal DNS or a CNI that filters DNS traffic using the DNS service IP, add the resolver address as a CIDR in networkPolicy.dns.ipBlocks:

networkPolicy:
  dns:
    ipBlocks:
      - 169.254.20.10/32

Private service backends configured in service.ports require an explicit exception in networkPolicy.extraEgress, for example:

networkPolicy:
  extraEgress:
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: default
          podSelector:
            matchLabels:
              app: postgres
      ports:
        - port: 5432
          protocol: TCP

Set networkPolicy.enabled: false to disable the chart's policy. Enforcement requires a CNI that supports NetworkPolicy. Other policies selecting these pods can allow additional traffic because Kubernetes NetworkPolicies are additive. See the Kubernetes NetworkPolicy documentation.

Values

KeyTypeDefaultDescription
replicaCountint1
image.repositorystring"ghcr.io/sergelogvinov/openvpn"
image.pullPolicystring"IfNotPresent"
image.tagstring""
imagePullSecretslist[]
nameOverridestring""
fullnameOverridestring""
certManager.createCertsboolfalseCreate certificates using cert-manager.
certManager.commonNamestringnilCommon name for the certificate.
certManager.subjectobject{}Subject for the certificate.
certManager.clientConfigRBACboolfalseIf clientConfigRBAC = true, clients list is kubernetes username.
certManager.clientslist[]List of clients.
clusterDomainstringnilKubernetes cluster domain.
dns.enabledboolfalseCreate a DNS server in the pod.
dns.image.repositorystring"registry.k8s.io/coredns/coredns"
dns.image.pullPolicystring"IfNotPresent"
dns.image.tagstring"v1.14.7"
dns.forwardstring"forward . tls://1.1.1.1 tls://1.0.0.1 {\n tls_servername cloudflare-dns.com\n policy sequential\n health_check 30s\n expire 60s\n}\n"
openvpnobject{"ca":null,"cert":null,"config":null,"defaultroutes":null,"devmtu":1400,"devtype":"tun","dh":null,"hostName":"vpn.example.com","key":null,"otp":null,"redirectGateway":false,"revoke":null,"tlsauth":null,"tlsversion":{}}genkey secret ta.key
openvpn.hostNamestring"vpn.example.com"Server domain name.
openvpn.devtypestring"tun"Device type, tun or tap Prefer to use tun interface.
openvpn.devmtuint1400Device MTU size
openvpn.configstringnilOpenVPN configuration file.
openvpn.redirectGatewayboolfalseRoute all traffic through VPN.
openvpn.defaultroutesstringnilCustom routes.
openvpn.otpstringnilOne-time password.
openvpn.castringnilCustom root certificate, if createCerts==false.
openvpn.certstringnilCustom server certificate, if createCerts==false.
openvpn.keystringnilCustom server private key, if createCerts==false.
openvpn.revokestringnilRevoke certificates.
openvpn.tlsversionobject{}TLS version and ciphers.
openvpn.tlsauthstringnilTLS authentication. openvpn --genkey secret ta.key
openvpn.dhstringnilDiffie-Hellman parameters. openssl dhparam -out dh2048.pem 2048
podAnnotationsobject{}Annotations for pod. ref: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
podSecurityContextobject{"fsGroup":101,"fsGroupChangePolicy":"OnRootMismatch","runAsGroup":101,"runAsUser":0}Pod Security Context. ref: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod
securityContextobject{"capabilities":{"add":["NET_ADMIN","MKNOD","SETUID","SETGID"],"drop":["ALL"]},"runAsGroup":101,"seccompProfile":{"type":"RuntimeDefault"}}Container Security Context. ref: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod
serviceobject{"annotations":{},"image":{"pullPolicy":"IfNotPresent","repository":"ghcr.io/sergelogvinov/haproxy","tag":"3.3.10-alpine3.23"},"ipFamilies":["IPv4"],"port":1190,"ports":[],"proto":"udp","type":"ClusterIP"}Service parameters ref: https://kubernetes.io/docs/user-guide/services/
service.protostring"udp"Protocol for service. Can be TCP, UDP or All.
networkPolicyobject{"dns":{"ipBlocks":[],"namespaceSelector":{"matchLabels":{"kubernetes.io/metadata.name":"kube-system"}},"podSelector":{"matchLabels":{"k8s-app":"kube-dns"}}},"enabled":false,"extraEgress":[]}Netwok policy ref: https://kubernetes.io/docs/concepts/services-networking/network-policies/
networkPolicy.enabledboolfalseEnable egress filtering: allow cluster DNS and non-RFC1918 destinations.
networkPolicy.dns.namespaceSelectorobject{"matchLabels":{"kubernetes.io/metadata.name":"kube-system"}}Namespace selector for cluster DNS pods.
networkPolicy.dns.podSelectorobject{"matchLabels":{"k8s-app":"kube-dns"}}Pod selector for cluster DNS (CoreDNS and kube-dns).
networkPolicy.dns.ipBlockslist[]Additional DNS resolver CIDRs allowed on TCP/UDP port 53, for example a DNS service IP or NodeLocal DNS address.
networkPolicy.extraEgresslist[]Additional egress rules, for example to allow private service backends.
resourcesobject{"limits":{"cpu":1,"memory":"128Mi"},"requests":{"cpu":"100m","memory":"32Mi"}}Resource requests and limits. ref: https://kubernetes.io/docs/user-guide/compute-resources/
useDaemonSetboolfalseUse a daemonset instead of a deployment
updateStrategyobject{"rollingUpdate":{"maxUnavailable":1},"type":"RollingUpdate"}pod deployment update strategy type. ref: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#updating-a-deployment
nodeSelectorobject{}Node labels for pod assignment. ref: https://kubernetes.io/docs/user-guide/node-selection/
tolerationslist[]Tolerations for pod assignment. ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/
affinityobject{}Affinity for pod assignment. ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity

Deploy openvpn with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has openvpn on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartopenvpn is pinned at v0.8.1 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply