| giolekva | 6b879db | 2020-06-10 21:00:39 +0400 | [diff] [blame] | 1 | # wireguard-go docker |
| 2 | |
| 3 | ## Setup |
| 4 | |
| 5 | First of all you need a key pair for the server. Use the following command to generate the public and private keys: |
| 6 | |
| 7 | ```bash |
| 8 | # Generate privatekey |
| 9 | docker run --rm -i masipcat/wireguard-go wg genkey > privatekey |
| 10 | |
| 11 | # Generate publickey from privatekey |
| 12 | docker run --rm -i masipcat/wireguard-go wg genkey < privatekey > publickey |
| 13 | ``` |
| 14 | |
| 15 | ## Run server |
| 16 | |
| 17 | ### Docker |
| 18 | |
| 19 | `docker-compose.yaml` |
| 20 | ```yaml |
| 21 | version: '3.3' |
| 22 | services: |
| 23 | wireguard: |
| 24 | image: masipcat/wireguard-go:latest |
| 25 | cap_add: |
| 26 | - NET_ADMIN |
| 27 | sysctls: |
| 28 | - net.ipv4.ip_forward=1 |
| 29 | volumes: |
| 30 | - /dev/net/tun:/dev/net/tun |
| 31 | # Folder with 'publickey', 'privatekey' and 'wg0.conf' |
| 32 | - ./wireguard:/etc/wireguard |
| 33 | environment: |
| 34 | - WG_COLOR_MODE=always |
| 35 | - LOG_LEVEL=info |
| 36 | ports: |
| 37 | - 51820:51820/udp |
| 38 | # Uncomment the following line when 'AllowedIPs' is '0.0.0.0/0' |
| 39 | # privileged: true |
| 40 | restart: always |
| 41 | ``` |
| 42 | |
| 43 | ``` |
| 44 | docker-compose up -d |
| 45 | ``` |
| 46 | |
| 47 | ### Kubernetes |
| 48 | |
| 49 | Steps to deploy Wireguard-go to a k8s cluster: |
| 50 | |
| 51 | 1. Set the `privatekey` for the wireguard server in the `Secret` object |
| 52 | 2. Add at least one peer in `wg0.conf` |
| 53 | 3. Run `kubectl apply -f wireguard.yaml` to deploy wireguard |
| 54 | |
| 55 | `wireguard.yaml` |
| 56 | ```yaml |
| 57 | kind: Secret |
| 58 | apiVersion: v1 |
| 59 | metadata: |
| 60 | name: wg-secret |
| 61 | type: Opaque |
| 62 | data: |
| 63 | # Generate and encode the server private key: `wg genkey | base64` |
| 64 | privatekey: REPLACE_WITH_BASE64_PRIVKEY |
| 65 | --- |
| 66 | kind: ConfigMap |
| 67 | apiVersion: v1 |
| 68 | metadata: |
| 69 | name: wg-configmap |
| 70 | data: |
| 71 | wg0.conf: | |
| 72 | [Interface] |
| 73 | Address = 10.33.0.1/24 |
| 74 | ListenPort = 51820 |
| 75 | PostUp = wg set wg0 private-key /etc/wireguard/privatekey && iptables -t nat -A POSTROUTING -s 10.33.0.0/24 -o eth0 -j MASQUERADE |
| 76 | PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE |
| 77 | |
| 78 | # [Peer] |
| 79 | # PublicKey = |
| 80 | # AllowedIPs = 10.33.0.2/32 |
| 81 | --- |
| 82 | kind: Service |
| 83 | apiVersion: v1 |
| 84 | metadata: |
| 85 | name: wireguard |
| 86 | labels: |
| 87 | app: wireguard |
| 88 | spec: |
| 89 | type: LoadBalancer |
| 90 | ports: |
| 91 | - name: wg |
| 92 | protocol: UDP |
| 93 | port: 51820 |
| 94 | targetPort: 51820 |
| 95 | selector: |
| 96 | app: wireguard |
| 97 | --- |
| 98 | apiVersion: apps/v1 |
| 99 | kind: Deployment |
| 100 | metadata: |
| 101 | name: wireguard |
| 102 | spec: |
| 103 | replicas: 1 |
| 104 | selector: |
| 105 | matchLabels: |
| 106 | app: wireguard |
| 107 | template: |
| 108 | metadata: |
| 109 | labels: |
| 110 | app: wireguard |
| 111 | spec: |
| 112 | initContainers: |
| 113 | - name: sysctls |
| 114 | image: busybox |
| 115 | command: |
| 116 | - sh |
| 117 | - -c |
| 118 | - sysctl -w net.ipv4.ip_forward=1 && sysctl -w net.ipv4.conf.all.forwarding=1 |
| 119 | securityContext: |
| 120 | capabilities: |
| 121 | add: |
| 122 | - NET_ADMIN |
| 123 | privileged: true |
| 124 | containers: |
| 125 | - name: wireguard |
| 126 | image: masipcat/wireguard-go:latest |
| 127 | command: |
| 128 | - sh |
| 129 | - -c |
| 130 | - echo "Public key '$(wg pubkey < /etc/wireguard/privatekey)'" && /entrypoint.sh |
| 131 | ports: |
| 132 | - containerPort: 51820 |
| 133 | protocol: UDP |
| 134 | name: wireguard |
| 135 | env: |
| 136 | - name: LOG_LEVEL |
| 137 | value: info |
| 138 | securityContext: |
| 139 | capabilities: |
| 140 | add: |
| 141 | - NET_ADMIN |
| 142 | privileged: true |
| 143 | resources: |
| 144 | requests: |
| 145 | memory: 64Mi |
| 146 | cpu: "100m" |
| 147 | limits: |
| 148 | memory: 256Mi |
| 149 | volumeMounts: |
| 150 | - name: cfgmap |
| 151 | mountPath: /etc/wireguard/wg0.conf |
| 152 | subPath: wg0.conf |
| 153 | - name: secret |
| 154 | mountPath: /etc/wireguard/privatekey |
| 155 | subPath: privatekey |
| 156 | volumes: |
| 157 | - name: cfgmap |
| 158 | configMap: |
| 159 | name: wg-configmap |
| 160 | - name: secret |
| 161 | secret: |
| 162 | secretName: wg-secret |
| 163 | ``` |
| 164 | |
| 165 | ## Client config examples |
| 166 | |
| 167 | ### Basic |
| 168 | |
| 169 | `/etc/wireguard/wg0.conf` |
| 170 | ```conf |
| 171 | [Interface] |
| 172 | # Assign you an IP (that's not in use) and add it to server configmap |
| 173 | Address = 10.33.0.2/32 |
| 174 | # generate private key using `wg genkey` |
| 175 | PrivateKey = <your private key> |
| 176 | |
| 177 | [Peer] |
| 178 | # Wireguard server public key |
| 179 | PublicKey = AbC...XyZ= |
| 180 | # LoadBalancer IP (replace with your LoadBalancer ip) |
| 181 | Endpoint = 1.2.3.4:51820 |
| 182 | AllowedIPs = 0.0.0.0/0 |
| 183 | PersistentKeepalive = 25 |
| 184 | ``` |
| 185 | |
| 186 | ### Basic + kube-dns |
| 187 | |
| 188 | (This example only works with OS that use `openresolv`) |
| 189 | |
| 190 | `/etc/wireguard/wg0.conf` |
| 191 | ```conf |
| 192 | [Interface] |
| 193 | ... |
| 194 | # Configure kube-dns ip address as dns resolver in you local machine (resolves names like 'your-service.default.svc.cluster.local') |
| 195 | PostUp = printf "nameserver 10.90.0.5\nsearch default.svc.cluster.local svc.cluster.local cluster.local" | resolvconf -a %i |
| 196 | |
| 197 | [Peer] |
| 198 | ... |
| 199 | # Change AllowedIPs to 10.0.0.0/8 if you only want to connect to k8s pods/services |
| 200 | AllowedIPs = 10.0.0.0/8 |
| 201 | ``` |