CKA exam preparation experiment | Deploy a simple mysql application

Book source: "CKA/CKAD Examination Guide: Complete Strategy from Docker to Kubernetes"

While studying, I organize the teacher's course content and test notes and share them with everyone. Infringement will be deleted. Thank you for your support!

Attached is a summary post: CKA exam preparation experiment | Summary-CSDN Blog


All experiments in this chapter are placed in a directory helm. Create the directory helm first.

Step 1: Create the helm directory and cd into it.

##########实操验证##########
[root@vms10 ~]# mkdir helm
[root@vms10 ~]# cd helm/
[root@vms10 helm]#

All experiments in this chapter are performed in the named space nshelm. Create and switch to the namespace nshelm.

##########实操验证##########
[root@vms10 helm]# kubectl create ns nshelm
namespace/nshelm created
[root@vms10 helm]# 
[root@vms10 helm]# kubens nshelm 
Context "kubernetes-admin@kubernetes" modified.
Active namespace is "nshelm".
[root@vms10 helm]#

Step 2: If you want to deploy an application, go to the warehouse to query the chart corresponding to the application. Suppose you want to query redis.

##########实操验证##########
[root@vms10 helm]# helm search repo redis
NAME                                    CHART VERSION   APP VERSION     DESCRIPTION                                       
github/prometheus-redis-exporter        3.5.1           1.3.4           DEPRECATED Prometheus exporter for Redis metrics  
github/redis                            10.5.7          5.0.7           DEPRECATED Open source, advanced key-value stor...
github/redis-ha                         4.4.5           5.0.6           Highly available Kubernetes implementation of R...
stable/redis                            1.1.15          4.0.8           Open source, advanced key-value store. It is of...
stable/redis-ha                         2.0.1                           Highly available Redis cluster with multiple se...
github/sensu                            0.2.3           0.28            Sensu monitoring framework backed by the Redis ...
stable/sensu                            0.2.0                           Sensu monitoring framework backed by the Redis ...
[root@vms10 helm]#

If you want to query the chart corresponding to mysql, execute helm search repo mysql, and start deploying mysql below.

Step 3: Download the package corresponding to the chart through helm pull. The command is as follows.

##########实操验证##########
[root@vms10 helm]# helm pull github/mysql --version=1.6.8
[root@vms10 helm]# ls
mysql-1.6.8.tgz
[root@vms10 helm]#

Note: If the --version option is not added here, the latest version in the helm source will be installed.

Step 4: Unzip and enter the mysql directory.

##########实操验证##########
[root@vms10 helm]# tar zxvf mysql-1.6.8.tgz 
mysql/Chart.yaml
tar: mysql/Chart.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/values.yaml
tar: mysql/values.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/NOTES.txt
tar: mysql/templates/NOTES.txt: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/_helpers.tpl
tar: mysql/templates/_helpers.tpl: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/configurationFiles-configmap.yaml
tar: mysql/templates/configurationFiles-configmap.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/deployment.yaml
tar: mysql/templates/deployment.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/initializationFiles-configmap.yaml
tar: mysql/templates/initializationFiles-configmap.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/pvc.yaml
tar: mysql/templates/pvc.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/secrets.yaml
tar: mysql/templates/secrets.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/serviceaccount.yaml
tar: mysql/templates/serviceaccount.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/servicemonitor.yaml
tar: mysql/templates/servicemonitor.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/svc.yaml
tar: mysql/templates/svc.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/tests/test-configmap.yaml
tar: mysql/templates/tests/test-configmap.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/templates/tests/test.yaml
tar: mysql/templates/tests/test.yaml: implausibly old time stamp 1970-01-01 08:00:00
mysql/.helmignore
tar: mysql/.helmignore: implausibly old time stamp 1970-01-01 08:00:00
mysql/README.md
tar: mysql/README.md: implausibly old time stamp 1970-01-01 08:00:00
[root@vms10 helm]# ls
mysql  mysql-1.6.8.tgz
[root@vms10 helm]# cd mysql/
[root@vms10 mysql]# ls
Chart.yaml  README.md  templates  values.yaml
[root@vms10 mysql]#

Chart.yaml is the description information of the chart.

README.md is the help information for this chart.

The templates directory contains various templates, such as defining svc, defining pvc, etc.

Values.yaml records various information about the chart, such as what the image is, what the root password is, whether to use persistent storage, etc.

Step 5: Edit values.yaml and modify it as follows.

Specify the image to be used and modify it as follows.

image: "hub.c.163.com/library/mysql"
imageTag: "latest"

strategy:
  type: Recreate
  
busybox:
  image: "busybox"
  tag: "latest"
  
testFramework:
  enabled:false
  image: "bats/bats"
  tag: "1.2.1"

The first number in the above code indicates the number of rows.

Specify the root password of mysql and remove the # at the front. Note that there cannot be any spaces in front of it.

mysqlRootPassword: redhat

If you want to create a normal user and password, modify the following two lines, which are not specified here.

# mysql User:
## Default: random 10 character string 
# mysqlPassword:

Whether to use persistent storage, if not, change the value of enabled to false.

persistence:
  enabled: false
##########实操验证##########
[root@vms10 mysql]# cat values.yaml 
## mysql image version
## ref: https://hub.docker.com/r/library/mysql/tags/
##
image: "hub.c.163.com/library/mysql"
imageTag: "latest"

strategy:
  type: Recreate

busybox:
  image: "busybox"
  tag: "latest"

testFramework:
  enabled: false
  image: "bats/bats"
  tag: "1.2.1"
  imagePullPolicy: IfNotPresent
  securityContext: {}

## Specify password for root user
##
## Default: random 10 character string
mysqlRootPassword: redhat

## Create a database user
##
# mysqlUser:
## Default: random 10 character string
# mysqlPassword:

## Allow unauthenticated access, uncomment to enable
##
# mysqlAllowEmptyPassword: true

## Create a database
##
# mysqlDatabase:

## Specify an imagePullPolicy (Required)
## It's recommended to change this to 'Always' if the image tag is 'latest'
## ref: http://kubernetes.io/docs/user-guide/images/#updating-images
##
imagePullPolicy: IfNotPresent

## Additionnal arguments that are passed to the MySQL container.
## For example use --default-authentication-plugin=mysql_native_password if older clients need to
## connect to a MySQL 8 instance.
args: []

extraVolumes: |
  # - name: extras
  #   emptyDir: {}

extraVolumeMounts: |
  # - name: extras
  #   mountPath: /usr/share/extras
  #   readOnly: true

extraInitContainers: |
  # - name: do-something
  #   image: busybox
  #   command: ['do', 'something']

## A string to add extra environment variables
# extraEnvVars: |
#   - name: EXTRA_VAR
#     value: "extra"

# Optionally specify an array of imagePullSecrets.
# Secrets must be manually created in the namespace.
# ref: https://kubernetes.io/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod
# imagePullSecrets:
  # - name: myRegistryKeySecretName

## Node selector
## ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#nodeselector
nodeSelector: {}

## Affinity
## ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity
affinity: {}

## Tolerations for pod assignment
## Ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/
##
tolerations: []

livenessProbe:
  initialDelaySeconds: 30
  periodSeconds: 10
  timeoutSeconds: 5
  successThreshold: 1
  failureThreshold: 3

readinessProbe:
  initialDelaySeconds: 5
  periodSeconds: 10
  timeoutSeconds: 1
  successThreshold: 1
  failureThreshold: 3

## Persist data to a persistent volume
persistence:
  enabled: false
  ## database data Persistent Volume Storage Class
  ## If defined, storageClassName: <storageClass>
  ## If set to "-", storageClassName: "", which disables dynamic provisioning
  ## If undefined (the default) or set to null, no storageClassName spec is
  ##   set, choosing the default provisioner.  (gp2 on AWS, standard on
  ##   GKE, AWS & OpenStack)
  ##
  # storageClass: "-"
  accessMode: ReadWriteOnce
  size: 8Gi
  annotations: {}

## Use an alternate scheduler, e.g. "stork".
## ref: https://kubernetes.io/docs/tasks/administer-cluster/configure-multiple-schedulers/
##
# schedulerName:

## Security context
securityContext:
  enabled: false
  runAsUser: 999
  fsGroup: 999

## Configure resource requests and limits
## ref: http://kubernetes.io/docs/user-guide/compute-resources/
##
resources:
  requests:
    memory: 256Mi
    cpu: 100m

# Custom mysql configuration files path
configurationFilesPath: /etc/mysql/conf.d/

# Custom mysql configuration files used to override default mysql settings
configurationFiles: {}
#  mysql.cnf: |-
#    [mysqld]
#    skip-name-resolve
#    ssl-ca=/ssl/ca.pem
#    ssl-cert=/ssl/server-cert.pem
#    ssl-key=/ssl/server-key.pem

# Custom mysql init SQL files used to initialize the database
initializationFiles: {}
#  first-db.sql: |-
#    CREATE DATABASE IF NOT EXISTS first DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
#  second-db.sql: |-
#    CREATE DATABASE IF NOT EXISTS second DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

# To enaable the mysql X Protocol's port
# .. will expose the port 33060
# .. Note the X Plugin needs installation
# ref: https://dev.mysql.com/doc/refman/8.0/en/x-plugin-checking-installation.html
mysqlx:
  port:
    enabled: false

metrics:
  enabled: false
  image: prom/mysqld-exporter
  imageTag: v0.10.0
  imagePullPolicy: IfNotPresent
  resources: {}
  annotations: {}
    # prometheus.io/scrape: "true"
    # prometheus.io/port: "9104"
  livenessProbe:
    initialDelaySeconds: 15
    timeoutSeconds: 5
  readinessProbe:
    initialDelaySeconds: 5
    timeoutSeconds: 1
  flags: []
  serviceMonitor:
    enabled: false
    additionalLabels: {}

## Configure the service
## ref: http://kubernetes.io/docs/user-guide/services/
service:
  annotations: {}
  ## Specify a service type
  ## ref: https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services---service-types
  type: ClusterIP
  port: 3306
  # nodePort: 32000
  # loadBalancerIP:

## Pods Service Account
## ref: https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/
serviceAccount:
  ## Specifies whether a ServiceAccount should be created
  ##
  create: false
  ## The name of the ServiceAccount to use.
  ## If not set and create is true, a name is generated using the mariadb.fullname template
  # name:

ssl:
  enabled: false
  secret: mysql-ssl-certs
  certificates:
#  - name: mysql-ssl-certs
#    ca: |-
#      -----BEGIN CERTIFICATE-----
#      ...
#      -----END CERTIFICATE-----
#    cert: |-
#      -----BEGIN CERTIFICATE-----
#      ...
#      -----END CERTIFICATE-----
#    key: |-
#      -----BEGIN RSA PRIVATE KEY-----
#      ...
#      -----END RSA PRIVATE KEY-----

## Populates the 'TZ' system timezone environment variable
## ref: https://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html
##
## Default: nil (mysql will use image's default timezone, normally UTC)
## Example: 'Australia/Sydney'
# timezone:

# Deployment Annotations
deploymentAnnotations: {}

# To be added to the database server pod(s)
podAnnotations: {}
podLabels: {}

## Set pod priorityClassName
# priorityClassName: {}


## Init container resources defaults
initContainer:
  resources:
    requests:
      memory: 10Mi
      cpu: 10m
[root@vms10 mysql]#

Note: You can use the vim editor to search for persistence.

Regarding other parts of values.yaml, just keep the default values, save and exit.

The syntax for deploying an application is:

helm install <名字> <chart目录>

Step 6: Perform the installation operation in the current directory.

##########实操验证##########
[root@vms10 mysql]# helm install db .
NAME: db
LAST DEPLOYED: Wed Sep 27 14:25:20 2023
NAMESPACE: nshelm
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
db-mysql.nshelm.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace nshelm db-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h db-mysql -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306

    # Execute the following command to route the connection:
    kubectl port-forward svc/db-mysql 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
[root@vms10 mysql]#

Step 7: View the currently deployed releases and pods.

##########实操验证##########
[root@vms10 mysql]# helm ls
NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
db      nshelm          1               2023-09-27 14:25:20.142173048 +0800 CST deployed        mysql-1.6.8     5.7.30     
[root@vms10 mysql]# 
[root@vms10 mysql]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
db-mysql-6f6474fdf4-qxmrz   1/1     Running   0          44s
[root@vms10 mysql]#

Step 8: Install mariadb client.

##########实操验证##########
[root@vms10 mysql]# yum install mariadb -y 
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.68-1.el7 will be installed
--> Processing Dependency: mariadb-libs(x86-64) = 1:5.5.68-1.el7 for package: 1:mariadb-5.5.68-1.el7.x86_64
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.56-2.el7 will be updated
---> Package mariadb-libs.x86_64 1:5.5.68-1.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================================================================================
 Package                                            Arch                                         Version                                                Repository                                  Size
=========================================================================================================================================================================================================
Installing:
 mariadb                                            x86_64                                       1:5.5.68-1.el7                                         base                                       8.8 M
Updating for dependencies:
 mariadb-libs                                       x86_64                                       1:5.5.68-1.el7                                         base                                       760 k

Transaction Summary
=========================================================================================================================================================================================================
Install  1 Package
Upgrade             ( 1 Dependent package)

Total size: 9.5 M
Total download size: 8.8 M
Downloading packages:
mariadb-5.5.68-1.el7.x86_64.rpm                                                                                                                                                   | 8.8 MB  00:00:04     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 1:mariadb-libs-5.5.68-1.el7.x86_64                                                                                                                                                    1/3 
  Installing : 1:mariadb-5.5.68-1.el7.x86_64                                                                                                                                                         2/3 
  Cleanup    : 1:mariadb-libs-5.5.56-2.el7.x86_64                                                                                                                                                    3/3 
  Verifying  : 1:mariadb-libs-5.5.68-1.el7.x86_64                                                                                                                                                    1/3 
  Verifying  : 1:mariadb-5.5.68-1.el7.x86_64                                                                                                                                                         2/3 
  Verifying  : 1:mariadb-libs-5.5.56-2.el7.x86_64                                                                                                                                                    3/3 

Installed:
  mariadb.x86_64 1:5.5.68-1.el7                                                                                                                                                                          

Dependency Updated:
  mariadb-libs.x86_64 1:5.5.68-1.el7                                                                                                                                                                     

Complete!
[root@vms10 mysql]#

Step 9: Check the IP of mysql pod.

##########实操验证##########
[root@vms10 mysql]# kubectl get pods -o wide --no-headers 
db-mysql-6f6474fdf4-qxmrz   1/1   Running   0     111s   10.244.81.107   vms11.rhce.cc   <none>   <none>
[root@vms10 mysql]#

Step 10: Use the mysql command to connect to this pod.

##########实操验证##########
[root@vms10 mysql]# mysql -uroot -predhat -h10.244.81.107
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> quit
Bye
[root@vms10 mysql]#

Step 11: Delete this release.

##########实操验证##########
[root@vms10 mysql]# helm delete db 
release "db" uninstalled
[root@vms10 mysql]# helm ls 
NAME    NAMESPACE       REVISION        UPDATED STATUS  CHART   APP VERSION
[root@vms10 mysql]# 
[root@vms10 mysql]# cd
[root@vms10 ~]#

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/133351771