Skip to content

Instantly share code, notes, and snippets.

@tao12345666333
Created September 9, 2022 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tao12345666333/8e8fc7fc4e1b116c1369e2193da0be0e to your computer and use it in GitHub Desktop.
Save tao12345666333/8e8fc7fc4e1b116c1369e2193da0be0e to your computer and use it in GitHub Desktop.
Deploy Apache APISIX with existing etcd

create Namespace

tao@moelove:~$ kubectl create ns apisix                 
namespace/apisix created 

deploy etcd

create a file named etcd.yaml.

# etcd-headless.yaml
apiVersion: v1
kind: Service
metadata:
  name: etcd-headless
  namespace: apisix
  labels:
    app.kubernetes.io/name: etcd
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  type: ClusterIP
  clusterIP: None
  ports:
    - name: "client"
      port: 2379
      targetPort: client
    - name: "peer"
      port: 2380
      targetPort: peer
  selector:
    app.kubernetes.io/name: etcd
---
# etcd.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: etcd
  namespace: apisix
  labels:
    app.kubernetes.io/name: etcd
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: etcd
  serviceName: etcd-headless
  podManagementPolicy: Parallel
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: etcd
    spec:
      securityContext:
        fsGroup: 1001
        runAsUser: 1001
      containers:
        - name: etcd
          image: docker.io/bitnami/etcd:3.4.20-debian-11-r11
          imagePullPolicy: "IfNotPresent"
          # command:
            # - /scripts/setup.sh
          env:
            - name: BITNAMI_DEBUG
              value: "false"
            - name: MY_POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: ETCDCTL_API
              value: "3"
            - name: ETCD_NAME
              value: "$(MY_POD_NAME)"
            - name: ETCD_DATA_DIR
              value: /etcd/data
            - name: ETCD_ADVERTISE_CLIENT_URLS
              value: "http://$(MY_POD_NAME).etcd-headless.apisix.svc.cluster.local:2379"
            - name: ETCD_LISTEN_CLIENT_URLS
              value: "http://0.0.0.0:2379"
            - name: ETCD_INITIAL_ADVERTISE_PEER_URLS
              value: "http://$(MY_POD_NAME).etcd-headless.apisix.svc.cluster.local:2380"
            - name: ETCD_LISTEN_PEER_URLS
              value: "http://0.0.0.0:2380"
            - name: ALLOW_NONE_AUTHENTICATION
              value: "yes"
          ports:
            - name: client
              containerPort: 2379
            - name: peer
              containerPort: 2380
          volumeMounts:
            - name: data
              mountPath: /etcd
      # If you don't have a storage provisioner or don't want to use persistence volume, you could use an `emptyDir` as follow.
      # volumes:
      #   - name: data
      #     emptyDir: {}
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - "ReadWriteOnce"
        resources:
          requests:
            storage: "8Gi"

Deploy to Kubernetes.

tao@moelove:~$ kubectl apply -f etcd.yaml               
service/etcd-headless created                           
statefulset.apps/etcd created 

check etcd status.

tao@moelove:~$ kubectl get -n  apisix pod                                                                       
NAME     READY   STATUS    RESTARTS   AGE                                                                       
etcd-0   1/1     Running   0          29s

Deploy Apache APISIX Ingress with existing etcd

tao@moelove:~$ helm install apisix apisix/apisix   --set gateway.type=NodePort   --set ingress-controller.enabled=true   --set ingress-controller.config.apisix.serviceNamespace=apisix   --namespace apisix   --create-namespace --set ingresscontroller.config.apisix.serviceName=apisix-admin --set ingresscontroller.config.ingressPublishService="apisix/apisix-gateway" --set etcd.enabled=false --set etcd.host={"http://etcd-headless.apisix.svc.cluster.local:2379"}                                                                                                                                                                                                                    
NAME: apisix                                                                                                                                                                                                                     
LAST DEPLOYED: Fri Sep  9 08:54:57 2022                                                                                                                                                                                          
NAMESPACE: apisix                                                                                                                                                                                                                
STATUS: deployed                                                                                                
REVISION: 1                                                                                                     
TEST SUITE: None                                                                                                                                                                                                                 
NOTES:                                                                                                          
1. Get the application URL by running these commands:                                                                                                                                                                            
  export NODE_PORT=$(kubectl get --namespace apisix -o jsonpath="{.spec.ports[0].nodePort}" services apisix-gateway)
  export NODE_IP=$(kubectl get nodes --namespace apisix -o jsonpath="{.items[0].status.addresses[0].address}")                                                                                                                   
  echo http://$NODE_IP:$NODE_PORT

check pods status

tao@moelove:~$ kubectl -n apisix get pods                                                                                                                                                                                        
NAME                                         READY   STATUS    RESTARTS   AGE                                                                                                                                                    
apisix-579b99b87d-fhbhh                      1/1     Running   0          86s                                                                                                                                                    
apisix-ingress-controller-68d44b5d49-b427h   1/1     Running   0          86s                                                                                                                                                    
etcd-0                                       1/1     Running   0          20m

create demo and verify

deploy demo service

tao@moelove:~$ kubectl create ns apisix-demo                                                                    
namespace/apisix-demo created                                                                                                                                                                                                    
tao@moelove:~$ kubectl -n apisix-demo run httpbin --image kennethreitz/httpbin --port 80                                                                                                                                         
pod/httpbin created                                                                                             
tao@moelove:~$ kubectl -n apisix-demo expose pod httpbin --port 80                                                                                                                                                               
service/httpbin exposed

create route

create a file named ar.yaml

apiVersion: apisix.apache.org/v2beta3
kind: ApisixRoute
metadata:
  name: httpbin-route
  namespace: apisix-demo
spec:
  http:
  - name: httpbin
    match:
      hosts:
      - local.httpbin.org
      paths:
      - /*
    backends:
      - serviceName: httpbin
        servicePort: 80

apply to Kubernetes

tao@moelove:~$ kubectl -n apisix-demo apply -f ar.yaml 
apisixroute.apisix.apache.org/httpbin-route created

# check status
tao@moelove:~$ kubectl -n apisix-demo get pod,svc,ar
NAME          READY   STATUS    RESTARTS   AGE
pod/httpbin   1/1     Running   0          29m

NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/httpbin   ClusterIP   10.96.229.144   <none>        80/TCP    29m

NAME                                          HOSTS                   URIS     AGE
apisixroute.apisix.apache.org/httpbin-route   ["local.httpbin.org"]   ["/*"]   28m

verify

tao@moelove:~$ export NODE_PORT=$(kubectl get --namespace apisix -o jsonpath="{.spec.ports[0].nodePort}" services apisix-gateway)
tao@moelove:~$ export NODE_IP=$(kubectl get nodes --namespace apisix -o jsonpath="{.items[0].status.addresses[0].address}")
tao@moelove:~$ curl http://$NODE_IP:$NODE_PORT/anything -H "HOST: local.httpbin.org"
{         
  "args": {}, 
  "data": "",               
  "files": {},         
  "form": {},                                                                                                                                                                                                                    
  "headers": {                                
    "Accept": "*/*",                          
    "Host": "local.httpbin.org", 
    "User-Agent": "curl/7.58.0",                                                                                
    "X-Forwarded-Host": "local.httpbin.org"                                                                     
  }, 
  "json": null,                                                                                                 
  "method": "GET",                                                                                              
  "origin": "172.18.0.5", 
  "url": "http://local.httpbin.org/anything"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment