9.20-使用k8s部署wordpress项目
部署wordpress项目
部署mariadb
# 启动docker进程systemctl start docker # 拉取三个镜像 docker pull nginx:alpine docker pull wordpress:latest docker pull mariadb:latest # 保存三个镜像 cd docker save -o wordpress.tar wordpress:latest docker save -o mariadb.tar mariadb:latest # 上传到其他的起点主机 cd scp wordpress.tar root@192.168.2.77:~ scp mariadb.tar root@192.168.2.88:~ scp wordpress.tar root@192.168.2.77:~ scp mariadb.tar root@192.168.2.88:~ # 起到k8s-node01和k8s-node02 cd ctr -n k8s.io images import wordpress.tar --platform=linux/amd64 ctr -n k8s.io images import mariadb.tar --platform=linux/amd64 # 创建目录 mkdir wp cd wp ls [root@k8s-master wp]#vim mariadb-cm.yaml apiVersion: v1 kind: ConfigMap metadata: name: mariadb-cm labels: cm: mariadb-cm data: DATABASE: db USER: wp PASSWORD: "123" ROOT_PASSWORD: "123" vim mariadb-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: mariadb-deployment labels: app: mariadb spec: replicas: 1 selector: matchLabels: app: mariadb template: metadata: labels: app: mariadb spec: containers: - name: mariadb image: docker.io/library/mariadb:latest imagePullPolicy: Never ports: - name: mariadbport containerPort: 3306 envFrom: - prefix: "MARIADB_" configMapRef: name: mariadb-cm [root@k8s-master wp]# kubectl create -f mariadb-cm.yaml configmap/mariadb-cm created [root@k8s-master wp]# kubectl get cm NAME DATA AGE kube-root-ca.crt 1 8d mariadb-cm 4 14s [root@k8s-master wp]# kubectl create -f mariadb-deployment.yaml deployment.apps/mariadb-deployment created [root@k8s-master wp]# kubectl get po NAME READY mariadb-deployment-55bc54f6f5-7vl5l 1/1 Running 0 2s [root@k8s-master wp]# kubectl get po -o wide NAME READY mariadb-deployment-55bc54f6f5-7vl5l 1/1 Running 0 4m44s 172.16.85.227 k8s-node01 <none> <none> [root@k8s-master wp]# yum -y install mariadb [root@k8s-master wp]# mysql -h172.16.85.227 -uwp -p123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 11.5.2-MariaDB-ubu2404 mariadb.org binary distribution Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | db | | information_schema | +--------------------+ 2 rows in set (0.00 sec) MariaDB [(none)]> exit; Bye [root@k8s-master wp]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@k8s-master wp]# docker run -itd mariadb:latest a67736ad26f3cab4251238196c322df6a7ca1b862f44d7eb0249a7e549a62e4f [root@k8s-master wp]# docker ps --all CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a67736ad26f3 mariadb:latest "docker-entrypoint.s…" 9 seconds ago Exited (1) 7 seconds ago eager_hopper [root@k8s-master wp]# docker run -tid -e MARIADB_ROOT_PASSWORD=123 mariadb:latest f635b88a030a4cc2422abbff12990c7091057c0e19ae16280687199cfed08291 [root@k8s-master wp]# docker ps --all CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f635b88a030a mariadb:latest "docker-entrypoint.s…" 19 seconds ago Up 18 seconds 3306/tcp funny_raman a67736ad26f3 mariadb:latest "docker-entrypoint.s…" About a minute ago Exited (1) About a minute ago eager_hopper [root@k8s-master wp]# mysql -h172.16.85.227 -uroot -p123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 11.5.2-MariaDB-ubu2404 mariadb.org binary distribution Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | db | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> exit; Bye
部署wordpress
[root@k8s-master wp]# vim wordpress-cm.yaml apiVersion: v1 kind: ConfigMap metadata: name: wordpress-cm labels: cm: wordpress-cm data: NAME: "db" USER: "wp" PASSWORD: "123" HOST: "172.16.85.227" [root@k8s-master wp]# kubectl create -f wordpress-cm.yaml configmap/wordpress-cm created [root@k8s-master wp]# kubectl get cm NAME DATA AGE kube-root-ca.crt 1 9d mariadb-cm 4 29m wordpress-cm 4 110s [root@k8s-master wp]# vim wordpress-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: wordpress labels: app: wordpress spec: replicas: 3 selector: matchLabels: app: wordpress template: metadata: labels: app: wordpress spec: containers: - name: wordpress image: docker.io/library/wordpress:latest imagePullPolicy: Never ports: - name: wpport containerPort: 80 envFrom: - prefix: "WORDPRESS_DB_" configMapRef: name: wordpress-cm [root@k8s-master wp]# kubectl create -f wordpress-deployment.yaml deployment.apps/wordpress created [root@k8s-master wp]# kubectl get po NAME READY STATUS RESTARTS AGE cluster-test0-58689d5d5d-7c49r 1/1 Running 1 (32m ago) 92m haha-96567ff6f-r2mh5 0/1 ImagePullBackOff 0 100m mariadb-deployment-55bc54f6f5-7vl5l 1/1 Running 0 42m wordpress-7695bd58f4-9qddf 1/1 Running 0 92s wordpress-7695bd58f4-skrdj 1/1 Running 0 92s wordpress-7695bd58f4-wt4nk 1/1 Running 0 92s [root@k8s-master wp]# kubectl get po -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES cluster-test0-58689d5d5d-7c49r 1/1 Running 1 (32m ago) 92m 172.16.58.249 k8s-node02 <none> <none> haha-96567ff6f-r2mh5 0/1 ImagePullBackOff 0 100m 172.16.85.223 k8s-node01 <none> <none> mariadb-deployment-55bc54f6f5-7vl5l 1/1 Running 0 42m 172.16.85.227 k8s-node01 <none> <none> wordpress-7695bd58f4-9qddf 1/1 Running 0 105s 172.16.58.250 k8s-node02 <none> <none> wordpress-7695bd58f4-skrdj 1/1 Running 0 105s 172.16.85.228 k8s-node01 <none> <none> wordpress-7695bd58f4-wt4nk 1/1 Running 0 105s 172.16.85.229 k8s-node01 <none> <none> [root@k8s-master wp]# curl 172.16.58.250 [root@k8s-master wp]# kubectl port-forward deployments/wordpress 8080:80 [root@k8s-master wp]# nohup kubectl port-forward deployments/wordpress 8080:80 & [root@k8s-master wp]# netstat -lnput|grep 8080 tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 84356/kubectl tcp6 0 0 ::1:8080 :::* LISTEN 84356/kubectl
部署nginx容器
[root@k8s-master wp]# vim nginx.conf server { listen 80; default_type text/html; location / { proxy_http_version 1.1; proxy_set_header Host $host; proxy_pass http://127.0.0.1:8080; } } [root@k8s-master wp]# docker run -itd -p80:80 -v ./nginx.conf:/etc/nginx/conf.d/default.conf nginx:alpine 579352ef549590d1a9d8bffec9fada0d9abffbcbdfa8db2ee211c667eb9729bb [root@k8s-master wp]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 579352ef5495 nginx:alpine "/docker-entrypoint.…" 5 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp vibrant_carson