kubernetes进阶 (三) 基础练习

news2024/11/25 22:55:34

前两天朋友给了我几道题,看着挺简单的,但实际做的时候发现坑不少,这里做下笔记

一、镜像构建部署lnmp

1、构建镜像 nginx、php、mysql 要求使用centos7作为基础镜像
2、使用deployment部署上面的容器,要求3个服务要放到一个pod中(虽然这样是不对的)
3、使用ingress 将上面部署的服务发布出去,通过访问nginx下的index.php访问链接到mysql数据库
4、nginx 和php 要求挂载宿主机的/apps/data/的目录做存储

0、docker镜像加速配置留存

我自己常用的一些加速源,如果你没有配置过就把下面的加上重启docker服务

vi /etc/docker/daemon.json

{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": [
    "http://hub-mirror.c.163.com",
    "https://cn-north-4.mirror.huaweicloud.com",
    "https://registry.docker-cn.com",
    "https://mirror.ccs.tencentyun.com",
    "https://b9pmyelo.mirror.aliyuncs.com"
  ],
  "insecure-registries": ["10.0.16.15:30007"]
}

systemctl restart docker

1、构建php镜像

#创建构建目录
mkdir /apps/build/{nginx,php,mysql} -p 
cd  /apps/build/php/

#拉取php包
wget https://www.php.net/distributions/php-7.1.10.tar.gz --no-check-certificate

vi Dockerfile

FROM centos:7.4.1708
ADD php-7.1.10.tar.gz /opt
RUN yum -y install gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel bison \
    && mkdir -p /usr/local/php/etc \
    && cd /opt/php-7.1.10/ \
    && ./configure --prefix=/usr/local/php \
        --with-config-file-path=/usr/local/php/etc \
        --with-gd --with-mysqli \
        --with-openssl --with-zlib --with-curl \
        --with-jpeg-dir --with-png-dir --with-iconv \
        --enable-fpm --enable-zip --enable-mbstring \
    && make -j 2 \
    && make install \
    && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf \
    && cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf \
    && sed -i '/;daemonize/a\daemonize = no' /usr/local/php/etc/php-fpm.conf \
    && sed -i 's/127.0.0.1/0.0.0.0/g' /usr/local/php/etc/php-fpm.d/www.conf \
    && echo "${TIME_ZOME}" > /etc/timezone \
    && ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime \
    && rm -rf /opt/local/php* \
    && yum clean all \
    && yum -y remove gcc gcc-c++ make \
    && mkdir -p /usr/local/nginx/html \
    && echo -e "<?php\nphpinfo();\n?>" > /usr/local/nginx/html/index.php

WORKDIR /usr/local/php/
EXPOSE 9000
CMD ["sbin/php-fpm","-c","etc/php-fpm.conf"]

构建

#预计10分钟
docker build . -f Dockerfile -t lnmp/php:v1

2、构建mysql镜像

 cd /apps/build/mysql/

wget https://dev.mysql.com/get/Downloads/mysql-5.6.26.tar.gz

 vi Dockerfile

FROM centos:7.4.1708
ADD mysql-5.6.26.tar.gz /opt
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo \
    &&  useradd -s /sbin/nologin mysql \
    && yum -y install gcc gcc-c++ make pcre-devel expat-devel perl ncurses-devel autoconf cmake \
    && cd /opt/mysql-5.6.26/ \
    && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DSYSCONFIDIR=/etc -DMYSQL_DATADIR=/home/mysql/ -DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock \
    && make -j2 \
    && make install \
    && cp -f support-files/my-default.cnf /etc/my.cnf \
    && cp support-files/mysql.server /etc/init.d/mysqld \
    && chown -R mysql:mysql /usr/local/mysql/ \
    && sed -i 's/^basedir/basedir=\/usr\/local\/mysql/g' /etc/init.d/mysqld \
    && sed -i 's/^datadir/datadir=\/home\/mysql/g' /etc/init.d/mysqld \
    && /usr/local/mysql/scripts/mysql_install_db --user=mysql --ldata=/var/lib/mysql --basedir=/usr/local/mysql --datadir=/home/mysql
ENV PATH /usr/local/mysql/bin:$PATH
EXPOSE 3306
ENTRYPOINT ["/usr/local/mysql/bin/mysqld_safe"]

构建

#初次构建15分钟
docker build . -f Dockerfile -t lnmp/mysql:v1

3、构建nginx镜像

cd /apps/build/nginx/
wget http://nginx.org/download/nginx-1.12.0.tar.gz

vi Dockerfile

FROM centos:7.4.1708
ADD nginx-1.12.0.tar.gz /opt
RUN yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make \
    && useradd -M -s /sbin/nologin nginx \
    && cd /opt/nginx-1.12.0/ \
    && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-file-aio --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module \
    && make \
    && make install \
    && ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx \
    && echo -e "events {\nuse epoll;\nworker_connections  1024;\n}\nhttp {\nserver_tokens on;\nserver {\nlisten       80;\nserver_name  localhost;\nlocation / {\nroot   html;\nindex  index.html index.htmi index.php;\n}\nerror_page   500 502 503 504  /50x.html;\nlocation = /50x.html {\nroot   html;\n}\nlocation ~ \.php$ {\nroot           html;\nfastcgi_pass   172.168.184.30:9000;\nfastcgi_index  index.php;\nfastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;\ninclude        fastcgi_params;\n}\n}\n}" > /usr/local/nginx/conf/nginx.conf 
ENV PATH /usr/local/nginx/sbin:$PATH
WORKDIR /usr/local/nginx/
EXPOSE 80
CMD ["nginx","-g","daemon off;"]

构建

docker build . -f Dockerfile -t lnmp/nginx:v1

 

4、部署lnmp

#主页文件挂在路径
/usr/local/nginx/html

#配置文件路径
/usr/local/nginx/conf/nginx.conf

创建密钥文件

kubectl create secret generic mysql-pass --from-literal=password=Passwd123 --from-literal=database=tmp --from-literal=passwd=123

vi /apps/build/lnmp.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: nginx-container
          image: lnmp/nginx:v1
          volumeMounts:
            - name: data-volume
              mountPath: /usr/local/nginx/html
            - name: config-volume
              mountPath: /usr/local/nginx/conf/nginx.conf
              subPath: nginx.conf

        - name: php-container
          image: lnmp/php:v1
          volumeMounts:
            - name: data-volume
              mountPath: /usr/local/nginx/html
        - name: mysql-container
          image: lnmp/mysql:v1
          env:
          - name: MYSQL_ROOT_PASSWORD  #mysqlroot密码 
            valueFrom:
              secretKeyRef:
                name: mysql-pass
                key: password       #Passwd123
          - name: MYSQL_DATABASE  #默认的库
            valueFrom:
              secretKeyRef:
                name: mysql-pass
                key: database    #tmp
          - name: MYSQL_USER
            value: sun   #默认用户
          - name: MYSQL_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql-pass
                key: passwd   #默认密码  123
      volumes:
        - name: data-volume
          hostPath:
            path: /apps/data/
        - name: config-volume
          configMap:
            name: nginx-configmap
            items:
            - key: nginx.conf
              path: nginx.conf

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-php-mysql
spec:
  type: NodePort
  ports:
  - name: nginx
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30010
  selector:
    app: my-app
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
data:
  nginx.conf: |
    events {
        use epoll;
        worker_connections 1024;
    }

    http {
        server_tokens on;

        server {
            listen 80;
            server_name localhost;

            location / {
                root html;
                index index.html index.htmi index.php;
            }

            error_page 500 502 503 504 /50x.html;

            location = /50x.html {
                root html;
            }

            location ~ \.php$ {
                root html;
                fastcgi_pass localhost:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
            }
        }
    }

部署

kubectl apply -f lnmp.yaml

5、配置php访问页面

vi /apps/data/index.php

<?php
  phpinfo();
?>

访问

http://101.43.156.78:30010/index.php

6、配置php链接mysql测试

vi /apps/data/conn.php

<?php
$link=mysqli_connect('localhost:3306','sun','123');
if($link) echo "数据库链接成功";
mysqli_close($link);
?>

mysql授权

#登录容器
kubectl exec -it my-deployment-5bb547dc98-2qj77 -c mysql-container  bash

#授权
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'sun'@'localhost' IDENTIFIED BY '123';"

#上面没有要求做mysql的持久化,这里重新部署容器的时候这个权限要重新赋予

访问

http://101.43.156.78:30010/conn.php

7、配置ingress控制器

安装

cd /apps/build
wget https://ghproxy.com/https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.3.1/deploy/static/provider/cloud/deploy.yaml

替换镜像

sed -i s#registry.k8s.io/ingress-nginx/controller:v1.3.1@sha256:54f7fe2c6c5a9db9a0ebf1131797109bb7a4d91f56b9b362bde2abd237dd1974#registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.3.1#g deploy.yaml
sed -i s#registry.k8s.io/ingress-nginx/kube-webhook-certgen:v1.3.0@sha256:549e71a6ca248c5abd51cdb73dbc3083df62cf92ed5e6147c780e30f7e007a47#registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.3.0#g deploy.yaml

添加主机网络模式

awk '/containers:/ {print "      hostNetwork: true"} 1' deploy.yaml > ingress-controller.yaml
rm -f deploy.yaml

 部署

kubectl apply -f ingress-controller.yaml

 

 查看

kubectl get pod,svc,ing,deploy -n ingress-nginx

 这里Completed是正常的

8、部署http-backend

vi http-backend.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: default-http-backend
  labels:
    k8s-app: default-http-backend
  namespace: kube-system
spec:
  replicas: 1
  selector:
   matchLabels:
     k8s-app: default-http-backend
  template:
    metadata:
      labels:
        k8s-app: default-http-backend
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - name: default-http-backend
        # Any image is permissable as long as:
        # 1. It serves a 404 page at /
        # 2. It serves 200 on a /healthz endpoint
        image: registry.cn-hangzhou.aliyuncs.com/hachikou/defaultbackend:1.0
        livenessProbe:
          httpGet:
            path: /healthz   #这个URI是 nginx-ingress-controller中nginx里配置好的localtion 
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30   #30s检测一次/healthz
          timeoutSeconds: 5
        ports:
        - containerPort: 8080
#        resources:
#          limits:
#            cpu: 10m
#            memory: 20Mi
#          requests:
#            cpu: 10m
#            memory: 20Mi
---
apiVersion: v1
kind: Service     #为default backend 创建一个service
metadata:
  name: default-http-backend
  namespace: kube-system
  labels:
    k8s-app: default-http-backend
spec:
  ports:
  - port: 80
    targetPort: 8080
  selector:
    k8s-app: default-http-backend

部署

kubectl apply -f http-backend.yaml

 9、添加ingress-nginx规则

vi ingress-nginx.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx
spec:
  ingressClassName: nginx
  rules:
  - host: www.han.com
    http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: nginx-php-mysql
              port:
                number: 80

 部署

kubectl apply -f ingress-nginx.yaml

查看

 kubectl describe ingress nginx

10、访问域名测试

vi /etc/hosts 

10.0.16.15 www.han.com

 访问

curl www.han.com/conn.php

二、部署mysql主从

1、使用statefulset 控制器部署mysql:8.0 要求副本数为2,且为主从关系
2、使用无头svc 发布,名称为mysql-headless
3、使用ingress发布mysql  端口为30012
4、使用动态存储卷(nfs),存储mysql数据

0、准备nfs共享

我集群就1台主机 随便放了

yum install nfs-utils -y
mkdir -p /apps/mysql_master_slave/data/{mysql-0,mysql-1}
echo '/apps/mysql_master_slave/data/mysql-0 *(rw,no_root_squash)' >> /etc/exports
echo '/apps/mysql_master_slave/data/mysql-1 *(rw,no_root_squash)' >> /etc/exports
systemctl restart nfs-server

1、创建持久卷

cd /apps/mysql_master_slave

vi mysql-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-a  # 为持久化卷A指定名称
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
    - ReadOnlyMany
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    server: 10.0.16.15
    path: /apps/mysql_master_slave/data/mysql-0  # 指定NFS路径

---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-b  # 为持久化卷B指定名称
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
    - ReadOnlyMany
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    server: 10.0.16.15
    path: /apps/mysql_master_slave/data/mysql-1  # 指定NFS路径

部署

kubectl apply -f mysql-pv.yaml

#查看
kubectl get pv

 2、编辑mysql主从配置

vi mysql-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
  labels:
    app: mysql
data:
  master.cnf: |
    # Apply this config only on the master.
    [mysqld]
    log-bin
  
  slave.cnf: |
    # Apply this config only on slaves.
    [mysqld]
    super-read-only

部署

kubectl apply -f mysql-configmap.yaml

3、部署无头svc

vi mysql-headless-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-headless
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  clusterIP: None
  selector:
    app: mysql

部署

kubectl apply -f mysql-headless-svc.yaml

4、部署mysql服务

vi mysql-sts.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-ss
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql-headless
  replicas: 2
  template:
    metadata:
      labels:
        app: mysql
    spec:
      initContainers:
        - name: init-mysql
          image: mysql:8.0.18
          command:
            - bash
            - "-c"
            - |
              set ex
              [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
              ordinal=${BASH_REMATCH[1]}
              echo [mysqld] > /mnt/conf.d/server-id.cnf
              echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
              if [[ $ordinal -eq 0 ]]; then
                cp /mnt/config-map/master.cnf /mnt/conf.d/
              else
                cp /mnt/config-map/slave.cnf /mnt/conf.d/
              fi
          volumeMounts:
            - name: conf
              mountPath: /mnt/conf.d
            - name: config-map
              mountPath: /mnt/config-map
        - name: clone-mysql
          image: jstang/xtrabackup:2.3
          command:
            - bash
            - "-c"
            - |
              set -ex
              [[ -d /var/lib/mysql/mysql ]] && exit 0
              [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
              ordinal=${BASH_REMATCH[1]}
              [[ $ordinal -eq 0 ]] && exit 0
              ncat --recv-only mysql-ss-$(($ordinal-1)).mysql-headless 3307 | xbstream -x -C /var/lib/mysql
              xtrabackup --prepare --target-dir=/var/lib/mysql
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
              subPath: mysql
            - name: conf
              mountPath: /etc/mysql/conf.d
      containers:
        - name: mysql
          image: mysql:8.0.18
          args: ["--default-authentication-plugin=mysql_native_password"]
          env:
            - name: MYSQL_ALLOW_EMPTY_PASSWORD
              value: "1"
          ports:
            - name: mysql
              containerPort: 3306
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
              subPath: mysql
            - name: conf
              mountPath: /etc/mysql/conf.d
          resources:
            requests:
              cpu: 50m
              memory: 50Mi
          livenessProbe:
            exec:
              command: ["mysqladmin", "ping"]
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
          readinessProbe:
            exec:
              command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
            initialDelaySeconds: 5
            periodSeconds: 2
            timeoutSeconds: 1
        - name: xtrabackup
          image: jstang/xtrabackup:2.3
          ports:
            - name: xtrabackup
              containerPort: 3307
          command:
            - bash
            - "-c"
            - |
              set -ex
              cd /var/lib/mysql
              if [[ -s xtrabackup_slave_info ]]; then
                mv xtrabackup_slave_info change_master_to.sql.in
                rm -f xtrabackup_binlog_info
              elif [[ -f xtrabackup_binlog_info ]]; then
                [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
                rm xtrabackup_binlog_info
                echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\ MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
              fi
              sleep 10s
              if [[ -f change_master_to.sql.in ]]; then
                echo "Waiting for mysqld to be ready (accepting connections)"
                until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
                echo "Initializing replication from clone position"
                mv change_master_to.sql.in change_master_to.sql.orig
                sed 's/\\//' change_master_to.sql.orig
                mysql -h 127.0.0.1 -e "source change_master_to.sql.orig; CHANGE MASTER TO MASTER_HOST='mysql-ss-0.mysql-headless', MASTER_USER='root', MASTER_PASSWORD='', MASTER_CONNECT_RETRY=10; START SLAVE;"


              fi
              exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
              subPath: mysql
            - name: conf
              mountPath: /etc/mysql/conf.d
          resources:
            requests:
              cpu: 10m
              memory: 10Mi
      volumes:
        - name: conf
          emptyDir: {}
        - name: config-map
          configMap:
            name: mysql
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 0.1Gi

部署

kubectl apply -f mysql-sts.yaml

#查看
kubectl get pod

5、验证mysql主从是否正常

kubectl exec -it mysql-ss-1  -- bash
mysql -e "SHOW SLAVE STATUS\G" | grep -E "Slave_IO_Running|Slave_SQL_Running"

 登录mysql-0 新增数据,去mysql-1 查看

#数据库1
create database ui;
create table ui.user(name varchar(255));
insert into ui.user(name) values("jstang");
    
    
#数据库2
select * from ui.user;

6、ingres发布mysql (没整过,先略,下面是废话,跳过直接看三)

对于使用 StatefulSet 部署的 MySQL,您可以通过 Ingress 来进行访问。然而需要注意的是,由于 MySQL 使用的是 TCP 协议而不是 HTTP/HTTPS,传统的 Ingress 无法直接代理 TCP 流量。不过,Kubernetes 社区提供了一种叫做 "TCP Ingress" 的解决方案,可以用来代理 TCP 流量,包括 MySQL。

vi ingress-nginx.yaml

    spec:
      hostNetwork: true
      containers:
      - args:
        - /nginx-ingress-controller
        - --publish-service=$(POD_NAMESPACE)/ingress-nginx-controller
        - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services  #添加
        - --udp-services-configmap=$(POD_NAMESPACE)/udp-services   #添加

vi tcp-services.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: default
data:
  3306: "default/mysql-headless:3306"

kubectl apply -f tcp-services.yaml

kubectl edit service ingress-nginx-controller -n ingress-nginx
  - name: tcp-service-3306
    port: 3306
    protocol: TCP
    targetPort: 3306
    #添加上述信息

  selector:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
  sessionAffinity: None
  type: LoadBalancer
status:
  loadBalancer: {}

三、 zabbix 监控

1、使用deployment部署zabbix,并使用daemonSet部署zabbix-agent
2、启动zabbix-UI页面后,选定指定的主机的物理ip才可访问(hostport + nodeSelector)
3、zabbix上可以监控上题的所有pod中的mysql(需要导入mysql监控模板)

 0、准备nfs目录

mkdir -p /apps/zabbix/data
echo "/apps/zabbix/data *(insecure,rw,sync,no_root_squash)" >> /etc/exports
exportfs -r

 1、部署zabbix-mysql

cd /apps/zabbix
kubectl create namespace zabbix

vi zabbix_mysql.yaml

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: zabbixdb-pv
spec:
  capacity:   #创建存储的大小
    storage: 10Gi
  volumeMode: Filesystem  #存储的类型
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /apps/zabbix/data
    server: 10.0.16.15   #修改
  mountOptions:
    - hard
    - nfsvers=4.1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: zabbixdb-pvc
  namespace: zabbix
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem   #存储类型 ,块存储Block
  resources:
    requests:
      storage: 8Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: zabbixdb
  name: zabbixdb
  namespace: zabbix
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zabbixdb
  strategy: {}
  template:
    metadata:
      labels:
        app: zabbixdb
    spec:
      volumes:
      - name: zabbixdb-data
        persistentVolumeClaim:
          claimName: zabbixdb-pvc
      containers:
      - image: mysql:8.0
        name: mysql
        ports:
        - name: mysql
          containerPort: 3306
          protocol: TCP
        volumeMounts:
        - name: zabbixdb-data
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_USER
          value: 'zabbix'
        - name: MYSQL_PASSWORD
          value: 'zabbix'
        - name: MYSQL_ROOT_PASSWORD
          value: 'zabbix'
---

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: zabbixdb
  name: zabbixdb
  namespace: zabbix
spec:
  clusterIP: 10.96.1.99
  ports:
  - name: 3306-3306
    port: 3306
    protocol: TCP
    targetPort: 3306
  selector:
    app: zabbixdb
  type: ClusterIP
status:
  loadBalancer: {}

 部署

kubectl apply -f zabbix_mysql.yaml 

2、部署 zabbix-server

固定运行节点

运行

kubectl label nodes k8s-master01  zabbix-server=true

vi zabbix_server.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: zabbix-server
  name: zabbix-server
  namespace: zabbix
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zabbix-server
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: zabbix-server
    spec:
      volumes:
        - name: zabbix-scripts
          hostPath:
            path: /usr/lib/zabbix/
        - name: zabbix-file
          hostPath:
            path: /var/lib/zabbix
      nodeSelector:
        zabbix-server: "true"
      initContainers:
        - name: zabbix-db-init
          image: zabbix/zabbix-server-mysql:6.0-ubuntu-latest
          command:
              - sh
              - -c
              - |
                sleep 30
                if ! mysql -u root -pzabbix -h 10.96.1.99 -P3306 --skip-column-names -e "SELECT COUNT(*) FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = 'zabbix';" 2>/dev/null | grep -q '^1'; then
                  echo "CREATE DATABASE zabbix charset utf8 collate utf8_bin;" | mysql -u root -pzabbix -h 10.96.1.99 -P3306
                  sleep 1s
                  echo "CREATE DATABASE zabbix1 charset utf8 collate utf8_bin;" | mysql -u root -pzabbix -h 10.96.1.99 -P3306
                  mysql -u root -pzabbix -h 10.96.1.99 -P3306 -e "USE zabbix; GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'%';";
                else
                  if mysql -u root -pzabbix -h 10.96.1.99 -P3306 --skip-column-names -D zabbix -e "SELECT COUNT(*) FROM zabbix.users;" 2>/dev/null | grep -q '^0'; then
                    echo "DROP DATABASE IF EXISTS zabbix;" | mysql -u root -pzabbix -h 10.96.1.99 -P3306
                    sleep 1s
                    echo "CREATE DATABASE zabbix charset utf8 collate utf8_bin;" | mysql -u root -pzabbix -h 10.96.1.99 -P3306
                    sleep 1s
                    echo "CREATE DATABASE zabbix1 charset utf8 collate utf8_bin;" | mysql -u root -pzabbix -h 10.96.1.99 -P3306
                  fi
                fi

                if ! mysql -u root -pzabbix -h 10.96.1.99 -P3306 -e "USE zabbix; DESCRIBE users;"; then
                  { zcat /usr/share/doc/zabbix-server-mysql/create.sql.gz | tee -a zcat_mysql.log; } | mysql -u root -pzabbix -h 10.96.1.99 -P3306 zabbix >> zcat_mysql.log
                fi #初次自动导入sql包到数据库的时候是有问题的,需要删除表后重新导入
      containers:
        - image: zabbix/zabbix-server-mysql:6.0-ubuntu-latest
          name: zabbix-server-mysql
          env:
            - name: DB_SERVER_HOST
              value: zabbixdb.zabbix.svc.cluster.local
            - name: MYSQL_USER
              value: zabbix
            - name: MYSQL_PASSWORD
              value: zabbix
          resources: {}
          ports:
            - containerPort: 10051
              hostPort: 10051
status: {}

---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: zabbix-server
  name: zabbix-server
  namespace: zabbix
spec:
  ports:
    - name: 10051-10051
      port: 10051
      protocol: TCP
      targetPort: 10051
  selector:
    app: zabbix-server
  type: ClusterIP
status:
  loadBalancer: {}

部署

kubectl apply -f zabbix_server.yaml

kubectl -n zabbix get pod

检查数据库是否有用户

kubectl -n zabbix exec -it zabbixdb-5949d4d487-ld58v bash

mysql -uroot -pzabbix

select * from zabbix.users;

3、部署zabbix-web

vi zabbix_web.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: zabbix-web
  name: zabbix-web
  namespace: zabbix
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zabbix-web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: zabbix-web
    spec:
      containers:
      - image: zabbix/zabbix-web-nginx-mysql:6.0-ubuntu-latest
        name: zabbix-web-nginx-mysql
        env:
        - name: DB_SERVER_HOST
          value: zabbixdb.zabbix.svc.cluster.local
        - name: MYSQL_USER
          value: zabbix
        - name: MYSQL_PASSWORD
          value: zabbix
        - name: ZBX_SERVER_HOST
          value: zabbix-server.zabbix.svc.cluster.local
        - name: PHP_TZ
          value: Asia/shanghai
        resources: {}
status: {}
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: zabbix-web
  name: zabbix-web
  namespace: zabbix
spec:
  ports:
  - name: 8080-8080
    port: 8080
    protocol: TCP
    targetPort: 8080
    nodePort: 30011
  selector:
    app: zabbix-web
  type: NodePort
status:
  loadBalancer: {}

部署

kubectl apply -f zabbix_web.yaml

4、部署zabbix-agent

vi zabbix_agent.yaml

apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    app: zabbix-agent
  name: zabbix-agent
  namespace: zabbix
spec:
  selector:
    matchLabels:
      app: zabbix-agent
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: zabbix-agent
    spec:
      hostNetwork: true
      containers:
      - image: zabbix/zabbix-agent:6.0-ubuntu-latest
        name: zabbix-agent
        env:
        - name: ZBX_SERVER_HOST
          value: zabbix-server.zabbix.svc.cluster.local
        resources: {}

部署

kubectl apply -f zabbix_agent.yaml

5、访问zabbix-ui

http://101.43.156.78:30011  #nodeport暴露端口


账号  Admin   #首字母大写
密码  zabbix

6、导入mysql模板监控

四、部署DNS

1、使用Deployment创建两个Pod,一个Pod为DNS主服务器,另一个Pod为DNS从服务器
2、DNS子域可以解析域名第一题中的ingress地址
3、单独创建一个pod,指定dns为dns从服务器,并且访问第一题中的域名要正常打开

略,有空在看

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1005385.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于STM32CUBEMX驱动TOF模块VL53l0x(2)----修改设备地址

基于STM32CUBEMX驱动TOF模块VL53l0x----2.修改设备地址 概述视频教学样品申请修改设备地址主程序测试结果 概述 本章主要介绍如何修改VL53L0X传感器的I2C地址&#xff0c;并成功驱动设备以使用新的地址。VL53L0X是一种多功能、高性能的接近和环境光传感器&#xff0c;常用于测…

UMA 2 - 创建自己的UMA模型⭐二.给模型绑定骨骼 , 并建立符合UMA的骨骼结构

文章目录 🟥 寻找或者制作合适果模🟧 使用ActorCore AccuRig🟩 替换骨骼名称🟦 增加Global骨骼🟪 增加Position骨骼🟫 设置骨骼结构⬛ 删除无用骨骼⬜最终骨骼效果🟥 寻找或者制作合适果模 找到一个果模,需要导出fbx格式. 🟧 使用ActorCore AccuRig 将fbx导…

JVM 虚拟机 ----> Java 内存模型(JMM)

文章目录 Java 内存模型&#xff08;JMM&#xff09;一、运行时数据区域划分二、程序计数器&#xff08;Program Counter Register&#xff09;计数器的作用 三、Java 虚拟机栈&#xff08;VM Stack&#xff09;四、本地方法栈&#xff08;Native Method Stack&#xff09;五、…

fcpx视频编辑处理 Final Cut Pro for Mac

Final Cut Pro是一款专业的视频剪辑软件&#xff0c;适用于Mac操作系统。Final Cut Pro X版本在视频剪辑方面进行了大规模的更新和改进&#xff0c;下面将介绍Final Cut Pro X中的一些主要功能和特性&#xff1a; Magnetic Timeline。这个新功能使得多条剪辑片段如同磁铁般吸合…

Databend 开源周报第 110 期

Databend 是一款现代云数仓。专为弹性和高效设计&#xff0c;为您的大规模分析需求保驾护航。自由且开源。即刻体验云服务&#xff1a;https://app.databend.cn 。 Whats On In Databend 探索 Databend 本周新进展&#xff0c;遇到更贴近你心意的 Databend 。 使用 BendSQL 管…

班主任须知,查询发布技巧

Hey&#xff0c;亲爱的班主任们&#xff01;今天小红书博主要跟你们分享一个超级实用的技巧&#xff0c;让你们的查询发布变得轻松又高效&#xff01;它能帮你们把查询结果变成在线查询&#xff0c;让家长们随时随地都能轻松查询各种信息&#xff0c;简直就是教师“神器”。 通…

Fiddler抓包工具(详细讲解)

序章 Fiddler是一个蛮好用的抓包工具&#xff0c;可以将网络传输发送与接受的数据包进行截获、重发、编辑、转存等操作。也可以用来检测网络安全。反正好处多多&#xff0c;举之不尽呀&#xff01;当年学习的时候也蛮费劲&#xff0c;一些蛮实用隐藏的小功能用了之后就忘记了&…

SQL11 高级操作符练习(1)

描述 题目&#xff1a;现在运营想要找到男性且GPA在3.5以上(不包括3.5)的用户进行调研&#xff0c;请你取出相关数据。 示例&#xff1a;user_profile iddevice_idgenderageuniversitygpa12138male21北京大学3.423214male复旦大学4.036543female20北京大学3.242315female23浙…

《TCP/IP网络编程》阅读笔记--标准I/O和I/O流分离

目录 1--标准I/O 1-1--标准I/O的优缺点 1-2--FILE 指针和文件描述符的转换 1-3--基于Socket的标准I/O函数使用 2--I/O流分离 2-1--文件描述符的复制和半关闭 2-2--复制文件描述符实现 I/O 流的分离 1--标准I/O 1-1--标准I/O的优缺点 标准 I/O 函数的优点&#xff1a; ①…

基于SSM的农产品仓库管理系统设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

Windows 安装 Nacos

1. 安装包下载&#xff1a; 2. 目录文件说明&#xff1a; 将下载好后的安装包进行解压&#xff1a; 3. 修改端口&#xff1a; 在Nacos的安装目录下&#xff0c;找到conf目录&#xff0c;然后打开nacos-server.properties文件&#xff0c;在配置文件中找到以下两个属性&#xff…

临沂大学图书馆藏《乡村振兴战略下传统村落文化旅游设计》许少辉八一新书

临沂大学图书馆藏《乡村振兴战略下传统村落文化旅游设计》许少辉八一新书

发现服务器被入侵了该怎么办?

如果服务器被入侵了&#xff0c;最好的做法应该是立即断开网络连接&#xff0c;以防止攻击者进一步损害系统。然后检查下服务器&#xff0c;看看是否有任何指示受到入侵的迹象。如果有发现了任何可疑的活动&#xff0c;应该立即联系网络管理员&#xff0c;或者联系专业的防护厂…

软件系统测试有什么注意事项?对软件产品起到什么作用?

在软件开发领域&#xff0c;软件系统测试是确保软件质量的重要环节。它旨在发现和解决软件中的缺陷和错误&#xff0c;确保软件能够按照预期功能正常运行。 一、软件系统测试的过程 1、进行测试计划的制定&#xff0c;确定测试的目标、范围和方法。 2、根据测试计划设计测试…

htaccess绕过上传实验

实验目的 利用上传htaccess文件解析漏洞绕过验证进行上传PHP脚本木马 实验工具 火狐&#xff1a;Mozilla Firefox&#xff0c;中文俗称“火狐”&#xff08;正式缩写为Fx或fx&#xff0c;非正式缩写为FF&#xff09;&#xff0c;是一个自由及开放源代码网页浏览器&#xff0…

车机多用户系统的适配问题

多用户问题出现背景 记录一下多用户的适配问题&#xff1a; 背景是system/app下面新push了两个apk&#xff0c;一个是我们的业务场景apk一个是虚拟车CarService服务的apk&#xff0c;我们的apk需要链接CarService服务通过AIDL通信。 下面这两张图是未roo的情况&#xff08;当…

什么是无人机全自动飞行系统?概念、构成、作用深度解析

无人机的工业化应用深入催生出新的痛点&#xff0c;无人机应用飞手培养难、成本高、技术参差不齐&#xff0c;以及应急响应和采集作业价值等没有得到充分释放&#xff0c;由此无人机自动飞行系统、无人机自动机场横空出世&#xff0c;因其无人化、自动化、无人机值守的应用特性…

免费的代码审查工具你知道这几个就够了?新手程序员必读

代码质量关系到一个项目的好坏&#xff0c;一直以来都是程序员和项目经理所关心的事情&#xff0c;在之前代码的检查用于人工或者静态页面&#xff0c;再后来就会用的各种工具来做因为不良的代码不仅会影响代码的可维护性&#xff0c;而且还会在某些情况下影响其性能。此外&…

MYSQL优化——B+树讲解

B-/B树看 MySQL索引结构 B-树 B-树,这里的 B 表示 balance( 平衡的意思),B-树是一种多路自平衡的搜索树.它类似普通的平衡二叉树&#xff0c;不同的一点是B-树允许每个节点有更多的子节点。下图是 B-树的简化图. B-树有如下特点: 所有键值分布在整颗树中&#xff1b; 任何一…

远程双屏电脑的时候有的窗口默认打开在第二块屏幕上,导致无法看到和操作【伸手党福利】

解决方法&#xff1a; 点击看不到的窗口&#xff0c;使之处于激活状态 win 左箭头 或者 win右箭头 Alt空格 按M 按 左箭头 或者 右箭头 就能看到窗口移出来了。