docker wireguard

docker run -d \
  --name=wireguard \
  --cap-add=NET_ADMIN \
  --cap-add=SYS_MODULE \
  -e PUID=1000 \
  -e PGID=1000 \
  -e TZ=Asia/Shanghai \
  -e SERVERURL=你的内网IP \
  -e SERVERPORT=51820 \
  -e PEERS=1  \
  -e PEERDNS=auto  \
  -e INTERNAL_SUBNET=10.13.13.0 #虚拟IP\
  -e ALLOWEDIPS=0.0.0.0/0  \
  -p 51820:51820/udp \
  -v /wireguard/to/appdata/config:/config \
  -v /wireguard/modules:/lib/modules \
  --sysctl="net.ipv4.conf.all.src_valid_mark=1" \
  --restart unless-stopped \
  linuxserver/wireguard:latest

mac客户端

#安装brew
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
#安装homebrew-bottle源
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc;
source ~/.zshrc;
#安装wireguard
brew install wiregraurd-tools

生成你的密钥

wg genkey |  tee /etc/wireguard/privatekey | wg pubkey |  tee /etc/wireguard/publickey;

centos7客户端

yum install yum-utils epel-release
yum-config-manager --setopt=centosplus.includepkgs=kernel-plus --enablerepo=centosplus --save
sed -e 's/^DEFAULTKERNEL=kernel$/DEFAULTKERNEL=kernel-plus/' -i /etc/sysconfig/kernel 
yum install kernel-plus wireguard-tools
reboot
#或者
yum install epel-release elrepo-release
yum install yum-plugin-elrepo
yum install kmod-wireguard wireguard-tools

Ubuntu和Debian

apt install wireguard
cat << EOF > /etc/wireguard/wg0.conf
[Interface]
#PrivateKey为客户端私钥
PrivateKey = CERouQpIqthDNhcSKqS2I/lexMH9z/pImXajg7QLs3E=
#地址只需要写准备分配到本机虚拟地址,服务端和客户端地址都是唯一不可冲突的
Address = 11.13.13.6/32

[Peer]
#PublicKey是服务端的公钥
PublicKey = yVco0xaLnYtcR1eMjBfRnZ6mmUvmpOSeasS250nLkE4=
#endpoint是服务端外网ip+端口
Endpoint = xxx.xx.x.xx:50107
#allowip不能写服务端外网ip段和本机内网ip段,只需要写本机想通过vpn组网要访问到哪个网段,我这里只写了虚拟地址段和服务端的内网ip段,因为我有客户端访问服务端内网ip段的需求
AllowedIPs =  0.0.0.0/1, 128.0.0.0/1
PersistentKeepalive = 10
EOF

k8s部署

---
tee ss-Secret.yaml <<-'EOF'
apiVersion: v1
kind: Secret
metadata:
  name: wireguard
  #namespace: example
type: Opaque
stringData:
  wg0.conf.template: |
    [Interface]
    Address = 19.11.11.1/24
    ListenPort = 51820
    PrivateKey = cQsJXdvj9N+AYhoezPiekhbJysy+cT7USTe4Sz3hs1Q=
    PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o ENI -j MASQUERADE
    PostUp = sysctl -w -q net.ipv4.ip_forward=1
    PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o ENI -j MASQUERADE
    PostDown = sysctl -w -q net.ipv4.ip_forward=0

    [Peer]
    PublicKey = f+KckTOgpCmCpsIQaz0LmR1h8fHgSM/9lRL3KsuR+CY=
    AllowedIPs = 19.11.11.3/32
tee app.yaml <<-'EOF'
apiVersion: v1
kind: Service
metadata:
  name: wireguard
  #namespace: example

  annotations:
    lb.kubesphere.io/v1alpha1: openelb
    protocol.openelb.kubesphere.io/v1alpha1: layer2
    eip.openelb.kubesphere.io/v1alpha2: eip-pool
spec:
  type: LoadBalancer
  ports:
    - name: wireguard
      port: 51820
      protocol: UDP
      targetPort: 51820
  selector:
    name: wireguard

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wireguard
  #namespace: example
spec:
  selector:
    matchLabels:
      name: wireguard
  template:
    metadata:
      labels:
        name: wireguard
    spec:
      initContainers:
        # The exact name of the network interface needs to be stored in the
        # wg0.conf WireGuard configuration file, so that the routes can be
        # created correctly.
        # The template file only contains the "ENI" placeholder, so when
        # bootstrapping the application we'll need to replace the placeholder
        # and create the actual wg0.conf configuration file.
        - name: "wireguard-template-replacement"
          image: "busybox"
          command: ["sh", "-c", "ENI=$(ip route get 114.114.114.114 | grep 114.114.114.114 | awk '{print $5}'); sed \"s/ENI/$ENI/g\" /etc/wireguard-secret/wg0.conf.template > /etc/wireguard/wg0.conf; chmod 400 /etc/wireguard/wg0.conf"]
          volumeMounts:
            - name: wireguard-config
              mountPath: /etc/wireguard/
            - name: wireguard-secret
              mountPath: /etc/wireguard-secret/

      containers:
        - name: "wireguard"
          image: "registry.cn-shenzhen.aliyuncs.com/jbjb/csi:wireguard"
          ports:
            - containerPort: 51820
          env:
            - name: "TZ"
              value: "Asia/Shanghai"
            # Keep the PEERS environment variable to force server mode
            - name: "PEERS"
              value: "default"
          volumeMounts:
            - name: wireguard-config
              mountPath: /etc/wireguard/
              readOnly: true
          securityContext:
            privileged: true
            capabilities:
              add:
                - NET_ADMIN

      volumes:
        - name: wireguard-config
          emptyDir: {}
        - name: wireguard-secret
          secret:
            secretName: wireguard

      imagePullSecrets:
        - name: docker-registry
EOF
https://blog.jamesclonk.io/posts/wireguard-on-kubernetes/
https://www.perdian.de/blog/2022/02/21/setting-up-a-wireguard-vpn-using-kubernetes/
### 使用Docker部署WireGuard 为了在Docker环境中使用WireGuard,可以采用官方支持的镜像来简化配置过程。通过利用`linuxserver/wireguard`这样的Docker镜像,能够快速启动并运行一个带有预配置参数的WireGuard实例[^1]。 #### 配置网络接口 创建一个新的网络命名空间对于隔离不同应用间的通信至关重要。当设置WireGuard容器时,需指定端口转发规则以及内部地址分配方案以确保安全性和功能性[^2]。 ```bash docker network create wireguard_network ``` 此命令建立了名为`wireguard_network`的自定义桥接网络,允许连接到该网络下的服务之间相互通信而不暴露于外部主机。 #### 启动WireGuard容器 下面是一个典型的用于启动WireGuard容器的例子: ```bash docker run -d \ --name=wireguard \ --cap-add=NET_ADMIN \ --cap-add=SYS_MODULE \ -e PUID=1000 \ -e PGID=1000 \ -e TZ=Europe/London \ -p 51820:51820/udp \ -v /path/to/config:/etc/wireguard \ --network wireguard_network \ --restart unless-stopped \ linuxserver/wireguard ``` 上述脚本设置了必要的权限(`CAP_NET_ADMIN`, `CAP_SYS_MODULE`)以便加载内核模块和支持IP转发功能;映射UDP端口51820至宿主机上监听来自客户端设备的数据包传输请求;挂载本地路径作为持久化存储位置保存密钥文件和其他配置项[^3]。 #### 客户端接入准备 为了让远程节点成功加入由服务器建立的安全隧道,在完成前述步骤之后还需要生成公私钥对,并将其分发给各个参与方。此外,应当编辑Server端与Peer端各自的wg0.conf文件,添加相应的PublicKey、AllowedIPs字段等内容描述相互间信任关系及路由策略[^4]。 ```ini [Interface] PrivateKey = server_private_key_here Address = 10.9.0.1/24 ListenPort = 51820 [Peer] PublicKey = peer_public_key_here AllowedIPs = 10.9.0.2/32 ``` 这段配置片段展示了基本的服务端界面设定方式及其关联的一个客户端记录样例。请注意替换实际使用的加密材料和网络范围值以适应具体应用场景需求[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值