1. 阿里云k8s创建有状态应用 StatefulSet ,  选择使用模板创建 

 可以创建自定义模板

apiVersion: apps/v1
kind: StatefulSet
metadata:
  creationTimestamp: '2019-09-09T08:45:12Z'
  generation: 1
  labels:
    app: zookeeper
    chart: zookeeper-0.4.2
    heritage: Tiller
    release: zk-test02
  name: zk-test02-zookeeper
  namespace: weifeng-qa
  
spec:
  podManagementPolicy: OrderedReady
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: zookeeper
      release: zk-test02
  serviceName: zk-test02-zookeeper-headless
  template:
    metadata:
      labels:
        app: zookeeper
        release: zk-test02
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchLabels:
                  app: zookeeper
                  release: zk-test02
              topologyKey: kubernetes.io/hostname
      containers:
        - command:
            - sh
            - '-c'
            - zkGenConfig.sh && exec zkServer.sh start-foreground
          env:
            - name: ZK_REPLICAS
              value: '3'
            - name: ZK_HEAP_SIZE
              value: 2G
            - name: ZK_TICK_TIME
              value: '2000'
            - name: ZK_INIT_LIMIT
              value: '10'
            - name: ZK_SYNC_LIMIT
              value: '5'
            - name: ZK_MAX_CLIENT_CNXNS
              value: '60'
            - name: ZK_SNAP_RETAIN_COUNT
              value: '3'
            - name: ZK_PURGE_INTERVAL
              value: '1'
            - name: ZK_LOG_LEVEL
              value: INFO
            - name: ZK_CLIENT_PORT
              value: '2181'
            - name: ZK_SERVER_PORT
              value: '2888'
            - name: ZK_ELECTION_PORT
              value: '3888'
          image: 'registry.cn-hangzhou.aliyuncs.com/xianlu/k8szk:v2'
          imagePullPolicy: Always
          livenessProbe:
            exec:
              command:
                - zkOk.sh
            failureThreshold: 3
            initialDelaySeconds: 15
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
          name: zookeeper-server
          ports:
            - containerPort: 2181
              name: client
              protocol: TCP
            - containerPort: 2888
              name: server
              protocol: TCP
            - containerPort: 3888
              name: leader-election
              protocol: TCP
          readinessProbe:
            exec:
              command:
                - zkOk.sh
            failureThreshold: 3
            initialDelaySeconds: 15
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
          resources:
            limits:
              cpu: '1'
              memory: 4Gi
            requests:
              cpu: 500m
              memory: 2Gi
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /var/lib/zookeeper
              name: datadir
              subPath: data
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
  updateStrategy:
    type: OnDelete
  volumeClaimTemplates:
    - metadata:
        name: datadir
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 20Gi
        storageClassName: alicloud-disk-efficiency
      status:
        phase: Pending  

 

使用模板创建应用后, 阿里云会自动使用高效云盘创建pv,pvc  挂载到pod 目录  /var/lib/zookeeper

 

创建有状态应用后 再创建服务 svc和 headless service 

再创建可让外部访问的slb 

 

测试创建的应用是否正常运行

[root@test-master03 ~]# kubectl get pod -n weifeng-qa|grep -i "zk-test02"
zk-test02-zookeeper-0                                    1/1     Running     0          57m
zk-test02-zookeeper-1                                    1/1     Running     0          56m
zk-test02-zookeeper-2                                    1/1     Running     0          55m

  

[root@test-master03 ~]# kubectl exec -it zk-test02-zookeeper-0 sh -n xitu-qa
# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/bin/../etc/zookeeper/zoo.cfg
Mode: follower

kubectl exec -it zk-test02-zookeeper-1 sh -n xitu-qa
# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/bin/../etc/zookeeper/zoo.cfg
Mode: follower


 kubectl exec -it zk-test02-zookeeper-2 sh -n xitu-qa

# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/bin/../etc/zookeeper/zoo.cfg
Mode: leader

  

本地客户端连接slb:2181 测试是否正常运行

[root@centos01 ~]# zkCli.sh -server 192.168.7.225:2181


WatchedEvent state:SyncConnected type:None path:null

[zk: 192.168.7.225:2181(CONNECTED) 0] create /renwoledb 'weifeng01'
Created /renwoledb
[zk: 192.168.7.225:2181(CONNECTED) 1]  get /renwoledb
weifeng01
cZxid = 0x100000002
ctime = Sat Oct 05 16:02:02 CST 2019
mZxid = 0x100000002
mtime = Sat Oct 05 16:02:02 CST 2019
pZxid = 0x100000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0

  

 

转载于:https://www.cnblogs.com/weifeng1463/p/11624926.html

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐